Skip to contentSkip to conversation

More pylibfdt updates

PATCH In review Rob Herring 2 participants 9 messages Nov 12, 2021 2 messages not in the archive

lore
Share & tools
lore
#0 lore raw

cover More pylibfdt updates

Another batch of pylibfdt updates. This series updates the README for installing, adds metadata for packaging, and reworks the tests to be able to run standalone using python test programs (pytest and tox).

A test package is uploaded here: https://test.pypi.org/project/libfdt/

Rob

Rob Herring (6):
README: Update pylibfdt install instructions
pylibfdt: Add packaging metadata
pylibfdt_tests: Don't depend on built dtbs
tests: rename pylibfdt_tests.py to test_pylibfdt.py
pylibfdt: Include tests and test data in packaging
pylibfdt: Add tox support

 MANIFEST.in                                   |  7 ++++++
 README                                        | 24 ++++++++++++-------
 setup.py                                      | 17 +++++++++++++
 tests/run_tests.sh                            |  3 +--
 tests/{pylibfdt_tests.py => test_pylibfdt.py} | 22 ++++++++++-------
 tox.ini                                       |  6 +++++
 6 files changed, 60 insertions(+), 19 deletions(-)
 rename tests/{pylibfdt_tests.py => test_pylibfdt.py} (97%)
 create mode 100644 tox.ini
Signature · 1 line
2.32.0
#2 lore raw

PATCH 1/6 README: Update pylibfdt install instructions

In reply to a message not in the archive

Now that pip is supported for installs, update the install instructions to use it. Using pip over setup.py is generally recommended and simpler.

Also, drop 'SETUP_PREFIX' as it doesn't exist anywhere.

---
 README | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
README +13 -7
index d9bf850b9dc2..5f5655df6898 100644
@@ -48,18 +48,24 @@ If you add new features, please check code coverage:
     # Open 'htmlcov/index.html' in your browser
 
 
-To install the library via the normal setup.py method, use:
+The library can be installed with pip from a local source tree:
 
-    ./pylibfdt/setup.py install [--prefix=/path/to/install_dir]
+    pip install . [--user|--prefix=/path/to/install_dir]
 
-If --prefix is not provided, the default prefix is used, typically '/usr'
-or '/usr/local'. See Python's distutils documentation for details. You can
-also install via the Makefile if you like, but the above is more common.
+Or directly from a remote git repo:
+
+    pip install git+git://git.kernel.org/pub/scm/utils/dtc/dtc.git@main
+
+The install depends on libfdt shared library being installed on the host system
+first. Generally, using --user or --prefix is not necessary and pip will use the
+default location for the Python installation which varies if the user is root or
+not.
+
+You can also install everything via make if you like, but pip is recommended.
 
 To install both libfdt and pylibfdt you can use:
 
-    make install [SETUP_PREFIX=/path/to/install_dir] \
-            [PREFIX=/path/to/install_dir]
+    make install [PREFIX=/path/to/install_dir]
 
 To disable building the python library, even if swig and Python are available,
 use:
Signature · 1 line
2.32.0
#3 lore raw

PATCH 2/6 pylibfdt: Add packaging metadata

In reply to a message not in the archive

PyPI expects to have various package metadata including long description, license, and classifiers. Add them.

---
A test package is here: https://test.pypi.org/project/libfdt/

---
 MANIFEST.in |  3 +++
 setup.py    | 17 +++++++++++++++++
 2 files changed, 20 insertions(+)
MANIFEST.in +3 -0
index 9e6c4ac4610c..ff8f5d637d46 100644
@@ -1,6 +1,9 @@
 # SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
 
 global-exclude *
+include README
+include GPL
+include BSD-2-Clause
 include setup.py
 include pylibfdt/libfdt.i
 include pylibfdt/*.py
setup.py +17 -0
index 0a0daf1c201d..00444e64337b 100755
@@ -17,6 +17,9 @@ import sys
 
 srcdir = os.path.dirname(__file__)
 
+with open("README", "r") as fh:
+    long_description = fh.read()
+
 def get_top_builddir():
     if '--top-builddir' in sys.argv:
         index = sys.argv.index('--top-builddir')
@@ -49,4 +52,18 @@ setup(
     ext_modules=[libfdt_module],
     package_dir={'': os.path.join(srcdir, 'pylibfdt')},
     py_modules=['libfdt'],
+
+    long_description=long_description,
+    long_description_content_type="text/plain",
+    url="https://git.kernel.org/pub/scm/utils/dtc/dtc.git",
+    license="BSD",
+    license_files=["GPL", "BSD-2-Clause"],
+
+    classifiers=[
+        "Programming Language :: Python :: 3",
+        "License :: OSI Approved :: BSD License",
+        "License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)",
+        "Operating System :: OS Independent",
+    ],
+
 )
Signature · 1 line
2.32.0
#4 lore raw

PATCH 3/6 pylibfdt_tests: Don't depend on built dtbs

In reply to a message not in the archive

The pylibfdt tests depend on the rest of the tests to build dtbs, but are otherwise independent. Modify the test to build dtbs itself so the python tests can run standalone.

This also fixes an intermittent problem with the DT strings section size varying depending on how the dtb was built. The test assumed the dtb was built with '-H both' option which is pretty much impossible to tell from run_tests.sh.

---
 tests/pylibfdt_tests.py | 22 ++++++++++++++--------
 tests/run_tests.sh      |  1 -
 2 files changed, 14 insertions(+), 9 deletions(-)
tests/pylibfdt_tests.py +14 -8
index 64b5bd1258f8..1025f192207a 100644
@@ -4,10 +4,14 @@
 # Written by Simon Glass <[email protected]>
 #
 
+import os
 import struct
 import sys
 import types
 import unittest
+import subprocess
+
+testsrc = os.path.dirname(__file__)
 
 sys.path.insert(0, '../pylibfdt')
 import libfdt
@@ -64,8 +68,9 @@ def _ReadFdt(fname):
     Returns:
         Fdt bytearray suitable for passing to libfdt functions
     """
-    with open(fname, mode='rb') as f:
-        return libfdt.Fdt(f.read())
+
+    dtb = subprocess.run(['dtc', '-O', 'dtb', fname], stdout=subprocess.PIPE)
+    return libfdt.Fdt(dtb.stdout)
 
 class PyLibfdtBasicTests(unittest.TestCase):
     """Test class for basic pylibfdt access functions
@@ -76,9 +81,9 @@ class PyLibfdtBasicTests(unittest.TestCase):
 
     def setUp(self):
         """Read in the device tree we use for testing"""
-        self.fdt = _ReadFdt('test_tree1.dtb')
-        self.fdt2 = _ReadFdt('test_props.dtb')
-        self.fdt3 = _ReadFdt('aliases.dtb')
+        self.fdt = _ReadFdt(os.path.join(testsrc, 'test_tree1.dts'))
+        self.fdt2 = _ReadFdt(os.path.join(testsrc, 'test_props.dts'))
+        self.fdt3 = _ReadFdt(os.path.join(testsrc, 'aliases.dts'))
 
     def GetPropList(self, node_path):
         """Read a list of properties from a node
@@ -291,7 +296,7 @@ class PyLibfdtBasicTests(unittest.TestCase):
         self.assertEqual(self.fdt.version(), 17)
         self.assertEqual(self.fdt.last_comp_version(), 16)
         self.assertEqual(self.fdt.boot_cpuid_phys(), 0)
-        self.assertEqual(self.fdt.size_dt_strings(), 105)
+        self.assertEqual(self.fdt.size_dt_strings(), 97)
         self.assertEqual(self.fdt.size_dt_struct(), 564)
 
     def testPack(self):
@@ -575,8 +580,9 @@ class PyLibfdtRoTests(unittest.TestCase):
 
     def setUp(self):
         """Read in the device tree we use for testing"""
-        with open('test_tree1.dtb', mode='rb') as f:
-            self.fdt = libfdt.FdtRo(f.read())
+        dtb = subprocess.run(['dtc', '-O', 'dtb', os.path.join(testsrc, 'test_tree1.dts')],
+                    stdout=subprocess.PIPE)
+        self.fdt = libfdt.FdtRo(dtb.stdout)
 
     def testAccess(self):
         """Basic sanity check for the FdtRo class"""
tests/run_tests.sh +0 -1
index d100d5aaa21f..140ac03e721d 100755
@@ -1012,7 +1012,6 @@ fdtoverlay_tests() {
 }
 
 pylibfdt_tests () {
-    run_dtc_test -I dts -O dtb -o test_props.dtb "$SRCDIR/test_props.dts"
     TMP=/tmp/tests.stderr.$$
     $PYTHON "$SRCDIR/pylibfdt_tests.py" -v 2> $TMP
 
Signature · 1 line
2.32.0
#7 lore raw

PATCH 4/6 tests: rename pylibfdt_tests.py to test_pylibfdt.py

In reply to a message not in the archive

This follows the normal Python test_*.py naming convention. With this change, pytest can automatically find and run the tests.

---
 README                                        | 4 ++--
 tests/run_tests.sh                            | 2 +-
 tests/{pylibfdt_tests.py => test_pylibfdt.py} | 0
 3 files changed, 3 insertions(+), 3 deletions(-)
 rename tests/{pylibfdt_tests.py => test_pylibfdt.py} (100%)
README +2 -2
index 5f5655df6898..380c79759ce9 100644
@@ -32,7 +32,7 @@ compatible=subnode1
 >>> print(fdt.getprop(node2, 'compatible').as_str())
 test_tree1
 
-You will find tests in tests/pylibfdt_tests.py showing how to use each
+You will find tests in tests/test_pylibfdt.py showing how to use each
 method. Help is available using the Python help command, e.g.:
 
     $ cd pylibfdt
@@ -43,7 +43,7 @@ If you add new features, please check code coverage:
     $ sudo apt-get install python3-coverage
     $ cd tests
     # It's just 'coverage' on most other distributions
-    $ python3-coverage run pylibfdt_tests.py
+    $ python3-coverage run test_pylibfdt.py
     $ python3-coverage html
     # Open 'htmlcov/index.html' in your browser
 
tests/run_tests.sh +1 -1
index 140ac03e721d..7c768052179d 100755
@@ -1013,7 +1013,7 @@ fdtoverlay_tests() {
 
 pylibfdt_tests () {
     TMP=/tmp/tests.stderr.$$
-    $PYTHON "$SRCDIR/pylibfdt_tests.py" -v 2> $TMP
+    $PYTHON "$SRCDIR/test_pylibfdt.py" -v 2> $TMP
 
     # Use the 'ok' message meaning the test passed, 'ERROR' meaning it failed
     # and the summary line for total tests (e.g. 'Ran 17 tests in 0.002s').
tests/test_pylibfdt.py renamed from tests/pylibfdt_tests.py +0 -0
similarity index 100%
rename from tests/pylibfdt_tests.py
rename to tests/test_pylibfdt.py
Signature · 1 line
2.32.0
#8 lore raw

PATCH 5/6 pylibfdt: Include tests and test data in packaging

In reply to a message not in the archive

Packaging the tests is necessary for tox to work.

---
 MANIFEST.in | 4 ++++
 1 file changed, 4 insertions(+)
MANIFEST.in +4 -0
index ff8f5d637d46..d9fb71b77a65 100644
@@ -10,3 +10,7 @@ include pylibfdt/*.py
 include libfdt/libfdt.h
 include libfdt/fdt.h
 include libfdt/libfdt_env.h
+include tests/test_pylibfdt.py
+include tests/test_tree1.dts
+include tests/test_props.dts
+include tests/aliases.dts
Signature · 1 line
2.32.0
#9 lore raw

PATCH 6/6 pylibfdt: Add tox support

In reply to a message not in the archive

'tox' allows running python tests under multiple environments.

Note that installing python versions is outside the scope of tox.

---
 tox.ini | 6 ++++++
 1 file changed, 6 insertions(+)
 create mode 100644 tox.ini
tox.ini new file +6 -0
new file mode 100644
index 000000000000..7925d1e2f390
@@ -0,0 +1,6 @@
+[tox]
+envlist = py36,py37,py38,py39,py310
+
+[testenv]
+deps = pytest
+commands = pytest
Signature · 1 line
2.32.0
#6 lore raw

Re: [PATCH 3/6] pylibfdt_tests: Don't depend on built dtbs

In reply to a message not in the archive
Quoted 10 lines · Rob Herring

The pylibfdt tests depend on the rest of the tests to build dtbs, but are otherwise independent. Modify the test to build dtbs itself so the python tests can run standalone.

This also fixes an intermittent problem with the DT strings section size varying depending on how the dtb was built. The test assumed the dtb was built with '-H both' option which is pretty much impossible to tell from run_tests.sh.

This breaks "make check" for me. I think it's relying on there being an already built dtc in the path, rather than specifically running the dtc built from the source tree.

======================================================================
ERROR: testAccess (__main__.PyLibfdtRoTests)
Basic sanity check for the FdtRo class
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/dwg/src/dtc/tests/./pylibfdt_tests.py", line 583, in setUp

    dtb = subprocess.run(['dtc', '-O', 'dtb', os.path.join(testsrc, 'test_tree1.dts')],

File "/usr/lib64/python3.10/subprocess.py", line 501, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/lib64/python3.10/subprocess.py", line 966, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib64/python3.10/subprocess.py", line 1842, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'dtc'

----------------------------------------------------------------------

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#10 lore raw
In reply to a message not in the archive
Quoted 6 lines · Rob Herring

Another batch of pylibfdt updates. This series updates the README for installing, adds metadata for packaging, and reworks the tests to be able to run standalone using python test programs (pytest and tox).

A test package is uploaded here:
https://test.pypi.org/project/libfdt/

1..2 applied. I have some questions about 3.

Quoted 21 lines

Rob

Rob Herring (6):
README: Update pylibfdt install instructions
pylibfdt: Add packaging metadata
pylibfdt_tests: Don't depend on built dtbs
tests: rename pylibfdt_tests.py to test_pylibfdt.py
pylibfdt: Include tests and test data in packaging
pylibfdt: Add tox support

 MANIFEST.in                                   |  7 ++++++
 README                                        | 24 ++++++++++++-------
 setup.py                                      | 17 +++++++++++++
 tests/run_tests.sh                            |  3 +--
 tests/{pylibfdt_tests.py => test_pylibfdt.py} | 22 ++++++++++-------
 tox.ini                                       |  6 +++++
 6 files changed, 60 insertions(+), 19 deletions(-)
 rename tests/{pylibfdt_tests.py => test_pylibfdt.py} (97%)
 create mode 100644 tox.ini
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson