Introduce Python bindings for libfdt
v9 cover Introduce Python bindings for libfdt
At present libfdt consists of only a C implementation. Many scripts are written using Python so it useful to have Python bindings for libfdt. Apparently this has never been attempted before, or if so I cannot find a reference.
This series starts the process of adding this support, with just a bare-bones set of methods.
The v8 series provides binding that can be used like this:
fdt = libfdt.Fdt(open(fname).read())
node = fdt.path_offset('/subnode@1')
print fdt.get_prop(node, 'compatible')
subnode = fdt.first_subnode(node, quiet=[libfdt.NOTFOUND]) while subnode > 0:
print fdt.get_name(subnode)
subnode = fdt.next_subnode(subnode, quiet=[libfdt.NOTFOUND])
This version includes a simple class for properties.
Changes in v9:
- Drop PYLIBFDT_CLEANFILES
- Add _libfdt.so to PYLIBFDT_cleanfiles
- Support 'make clean' properly with pylibfdt
- Move run_tests.sh updates to this patch
- Only run the pylibfdt tests automatically if we can build pylibfdt
- Update commit message to reflect swig/Python.h detection
- Update Makefile shell command to support dash shell
Changes in v8:
- Fix up setup.py name and author
- Move QUITE_ALL into a single assignment
- Drop fdt32_to_cpu()
- Adjust fdt_get_property_by_offset() to rely on the typemap more
- Move pylibfdt_copy_value(() functionality into a typemap
- Add an fdt parameter to the [const] void * typemaps
- Create an output typemap for struct fdt_property *
- Drop the arg4 TODO comment
- Avoid using hard-coded offsets (ROOT_PROPS)
- Use types.ModuleType instead of type(str)
- Compare exception number instead of using a regex
- Avoid using hard-coded size
- Drop testEndian()
- Move the changes to run_tests.sh to a later commit
- Add comments to testPropertyOffsetExceptions()
- Drop all the namelen functions from the Python bindings
- Only build pylibfdt if we have swig and Python.h
Changes in v7:
- Add QUIET_ALL to silence all exceptions
- Add a test for QUIET_ALL
Changes in v6:
- Use a tuple instead of list for the default quiet parameter
- Use a tuple instead of list for QUIET_NOTFOUND
- Use 'list' instead of 'tuple' for the comment in check_err_null()
- Return a bytearray from getprop()
- Adjust the Property constructor to accept the name and value
- Use uint8_t for pylibfdt_copy_value
- Adjust tests to avoid checking a hard-coded offset
- Use 0 instead of self.fdt.path_offset('/')
- Adjust the totalsize() test to compare against the file size
- Adjust test result processing to avoid using awk
- Update example to print the node value as an integer
- Update example to print the bytestring as well as the string
Changes in v5:
- Use a 'quiet' parameter instead of quiet versions of functions
- Add a Property object to hold a property's name and value
- Drop the data() and string() functions which are not needed now
- Rename pylibfdt_copy_data() tp pylibfdt_copy_value()
- Change order of libfdt.h inclusion to avoid #ifdef around libfdt macros
- Drop fdt_offset_ptr() and fdt_getprop_namelen() from the swig interface
- Use $(SWIG) to call swig from the Makefile
- Review function comments
- Adjust tests to match new swig bindings
- Use an interactive session to demonstrate pylibfdt
- Mention that more work remains
- Update commit message
- Drop #ifdef around fdt_get_header() macros
- Fix 'possible' typo
Changes in v4:
- Make the library less pythonic to avoid a shaky illusion
- Drop classes for Node and Prop, along with associated methods
- Include libfdt.h instead of repeating it
- Add support for fdt_getprop()
- Bring in all libfdt functions (but Python support is missing for many)
- Add full comments for Python methods
- Drop tests that are no-longer applicable
- Add a get for getprop()
- Add new patch to adjust libfdt.h to work with swig
Changes in v3:
- Make the library more pythonic
- Add classes for Node and Prop along with methods
- Add an exception class
- Use Python to generate exeptions instead of SWIG
- Add some more tests
Changes in v2:
- Add exceptions when functions return an error
- Correct Python naming to following PEP8
- Use a class to encapsulate the various methods
- Include fdt.h instead of redefining struct fdt_property
- Use bytearray to avoid the SWIG warning 454
- Add comments
- Update tests for new pylibfdt
- Add a few more tests to increase coverage
- Add details on how to obtain full help and code coverage
Simon Glass (5):
Add an initial Python library for libfdt Add tests for pylibfdt
Mention pylibfdt in the documentation Adjust libfdt.h to work with swig
Build pylibfdt as part of the normal build process
Makefile | 35 +++- README | 47 +++++ libfdt/libfdt.h | 21 ++- pylibfdt/.gitignore | 3 + pylibfdt/Makefile.pylibfdt | 17 ++ pylibfdt/libfdt.swig | 433 +++++++++++++++++++++++++++++++++++++++++++++ pylibfdt/setup.py | 34 ++++ tests/pylibfdt_tests.py | 288 ++++++++++++++++++++++++++++++ tests/run_tests.sh | 19 ++ 9 files changed, 894 insertions(+), 3 deletions(-) create mode 100644 pylibfdt/.gitignore create mode 100644 pylibfdt/Makefile.pylibfdt create mode 100644 pylibfdt/libfdt.swig create mode 100644 pylibfdt/setup.py create mode 100644 tests/pylibfdt_tests.py
Signature · 1 line
2.12.0.rc1.440.g5b76565f74-goog
A message that is not in the archive (sent off-list or not yet received): 20170304235229.5343-1-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org
PATCH v9 1/5 Add an initial Python library for libfdt
Add Python bindings for a bare-bones set of libfdt functions. These allow navigating the tree and reading node names and properties.
---
Changes in v9:
- Drop PYLIBFDT_CLEANFILES
- Add _libfdt.so to PYLIBFDT_cleanfiles
Changes in v8:
- Fix up setup.py name and author
- Move QUITE_ALL into a single assignment
- Drop fdt32_to_cpu()
- Adjust fdt_get_property_by_offset() to rely on the typemap more
- Move pylibfdt_copy_value(() functionality into a typemap
- Add an fdt parameter to the [const] void * typemaps
- Create an output typemap for struct fdt_property *
- Drop the arg4 TODO comment
Changes in v7:
- Add QUIET_ALL to silence all exceptions
Changes in v6:
- Use a tuple instead of list for the default quiet parameter
- Use a tuple instead of list for QUIET_NOTFOUND
- Use 'list' instead of 'tuple' for the comment in check_err_null()
- Return a bytearray from getprop()
- Adjust the Property constructor to accept the name and value
- Use uint8_t for pylibfdt_copy_value
Changes in v5:
- Use a 'quiet' parameter instead of quiet versions of functions
- Add a Property object to hold a property's name and value
- Drop the data() and string() functions which are not needed now
- Rename pylibfdt_copy_data() tp pylibfdt_copy_value()
- Change order of libfdt.h inclusion to avoid #ifdef around libfdt macros
- Drop fdt_offset_ptr() and fdt_getprop_namelen() from the swig interface
- Use $(SWIG) to call swig from the Makefile
- Review function comments
Changes in v4:
- Make the library less pythonic to avoid a shaky illusion
- Drop classes for Node and Prop, along with associated methods
- Include libfdt.h instead of repeating it
- Add support for fdt_getprop()
- Bring in all libfdt functions (but Python support is missing for many)
- Add full comments for Python methods
Changes in v3:
- Make the library more pythonic
- Add classes for Node and Prop along with methods
- Add an exception class
- Use Python to generate exeptions instead of SWIG
Changes in v2:
- Add exceptions when functions return an error
- Correct Python naming to following PEP8
- Use a class to encapsulate the various methods
- Include fdt.h instead of redefining struct fdt_property
- Use bytearray to avoid the SWIG warning 454
- Add comments
Makefile | 1 + pylibfdt/.gitignore | 3 + pylibfdt/Makefile.pylibfdt | 17 ++ pylibfdt/libfdt.swig | 433 +++++++++++++++++++++++++++++++++++++++++++++ pylibfdt/setup.py | 34 ++++ 5 files changed, 488 insertions(+) create mode 100644 pylibfdt/.gitignore create mode 100644 pylibfdt/Makefile.pylibfdt create mode 100644 pylibfdt/libfdt.swig create mode 100644 pylibfdt/setup.py
Makefile +1 -0
@@ -22,6 +22,7 @@ CFLAGS = -g -Os -fPIC -Werror $(WARNINGS) BISON = bison LEX = flex +SWIG = swig INSTALL = /usr/bin/install DESTDIR =
pylibfdt/.gitignore new file +3 -0
@@ -0,0 +1,3 @@ +libfdt.py +libfdt.pyc +libfdt_wrap.c
pylibfdt/Makefile.pylibfdt new file +17 -0
@@ -0,0 +1,17 @@ +# Makefile.pylibfdt +# + +PYLIBFDT_srcs = $(addprefix $(LIBFDT_srcdir)/,$(LIBFDT_SRCS)) +WRAP = $(PYLIBFDT_objdir)/libfdt_wrap.c +PYMODULE = $(PYLIBFDT_objdir)/_libfdt.so + +$(PYMODULE): $(PYLIBFDT_srcs) $(WRAP) + @$(VECHO) PYMOD $@ + python $(PYLIBFDT_objdir)/setup.py "$(CPPFLAGS)" $^ + mv _libfdt.so $(PYMODULE) + +$(WRAP): $(PYLIBFDT_srcdir)/libfdt.swig + @$(VECHO) SWIG $@ + $(SWIG) -python -o $@ $< + +PYLIBFDT_cleanfiles = libfdt_wrap.c libfdt.py libfdt.pyc _libfdt.so
pylibfdt/libfdt.swig new file +433 -0
433 lines not on this page · raw message
pylibfdt/setup.py new file +34 -0
@@ -0,0 +1,34 @@ +#!/usr/bin/env python + +""" +setup.py file for SWIG libfdt +""" + +from distutils.core import setup, Extension +import os +import sys + +progname = sys.argv[0] +cflags = sys.argv[1] +files = sys.argv[2:] + +if cflags: + cflags = [flag for flag in cflags.split(' ') if flag] +else: + cflags = None + +libfdt_module = Extension( + '_libfdt', + sources = files, + extra_compile_args = cflags +) + +sys.argv = [progname, '--quiet', 'build_ext', '--inplace'] + +setup (name = 'libfdt', + version = '0.1', + author = "Simon Glass <[email protected]>", + description = """Python binding for libfdt""", + ext_modules = [libfdt_module], + py_modules = ["libfdt"], + )
Signature · 1 line
2.12.0.rc1.440.g5b76565f74-goog
PATCH v9 2/5 Add tests for pylibfdt
In reply to a message not in the archiveAdd a set of tests to cover the functionality in pylibfdt.
---
Changes in v9: None
Changes in v8:
- Avoid using hard-coded offsets (ROOT_PROPS)
- Use types.ModuleType instead of type(str)
- Compare exception number instead of using a regex
- Avoid using hard-coded size
- Drop testEndian()
- Move the changes to run_tests.sh to a later commit
- Add comments to testPropertyOffsetExceptions()
Changes in v7:
- Add a test for QUIET_ALL
Changes in v6:
- Adjust tests to avoid checking a hard-coded offset
- Use 0 instead of self.fdt.path_offset('/')
- Adjust the totalsize() test to compare against the file size
- Adjust test result processing to avoid using awk
Changes in v5:
- Adjust tests to match new swig bindings
Changes in v4:
- Drop tests that are no-longer applicable
- Add a get for getprop()
Changes in v3:
- Add some more tests
Changes in v2:
- Update tests for new pylibfdt
- Add a few more tests to increase coverage
tests/pylibfdt_tests.py | 288 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 288 insertions(+) create mode 100644 tests/pylibfdt_tests.py
tests/pylibfdt_tests.py new file +288 -0
@@ -0,0 +1,288 @@ +# pylibfdt - Tests for Flat Device Tree manipulation in Python +# Copyright (C) 2017 Google, Inc. +# Written by Simon Glass <[email protected]> +# +# libfdt is dual licensed: you can use it either under the terms of +# the GPL, or the BSD license, at your option. +# +# a) This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 2 of the +# License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public +# License along with this library; if not, write to the Free +# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, +# MA 02110-1301 USA +# +# Alternatively, +# +# b) Redistribution and use in source and binary forms, with or +# without modification, are permitted provided that the following +# conditions are met: +# +# 1. Redistributions of source code must retain the above +# copyright notice, this list of conditions and the following +# disclaimer. +# 2. Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, +# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, +# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +import sys +import types +import unittest + +sys.path.append('../pylibfdt') +import libfdt +from libfdt import FdtException, QUIET_NOTFOUND, QUIET_ALL + +def get_err(err_code): + """Convert an error code into an error message + + Args: + err_code: Error code value (FDT_ERR_...) + + Returns: + String error code + """ + return 'pylibfdt error %d: %s' % (-err_code, libfdt.fdt_strerror(-err_code)) + +def _ReadFdt(fname): + """Read a device tree file into an Fdt object, ready for use + + Args: + fname: Filename to read from + + Returns: + Fdt bytearray suitable for passing to libfdt functions + """ + return libfdt.Fdt(open(fname).read()) + +class PyLibfdtTests(unittest.TestCase): + """Test class for pylibfdt + + Properties: + fdt: Device tree file used for testing + """ + + def setUp(self): + """Read in the device tree we use for testing""" + self.fdt = _ReadFdt('test_tree1.dtb') + + def GetPropList(self, node_path): + """Read a list of properties from a node + + Args: + node_path: Full path to node, e.g. '/subnode@1/subsubnode' + + Returns: + List of property names for that node, e.g. ['compatible', 'reg'] + """ + prop_list = [] + node = self.fdt.path_offset(node_path) + poffset = self.fdt.first_property_offset(node, QUIET_NOTFOUND) + while poffset > 0: + prop = self.fdt.get_property_by_offset(poffset) + prop_list.append(prop.name) + poffset = self.fdt.next_property_offset(poffset, QUIET_NOTFOUND) + return prop_list + + def testImport(self): + """Check that we can import the library correctly""" + self.assertEquals(type(libfdt), types.ModuleType) + + def testBadFdt(self): + """Check that a filename provided accidentally is not accepted""" + with self.assertRaises(FdtException) as e: + fdt = libfdt.Fdt('a string') + self.assertEquals(e.exception.err, -libfdt.BADMAGIC) + + def testPathOffset(self): + """Check that we can find the offset of a node""" + self.assertEquals(self.fdt.path_offset('/'), 0) + self.assertTrue(self.fdt.path_offset('/subnode@1') > 0) + with self.assertRaises(FdtException) as e: + self.fdt.path_offset('/wibble') + self.assertEquals(e.exception.err, -libfdt.NOTFOUND) + self.assertEquals(self.fdt.path_offset('/wibble', QUIET_NOTFOUND), + -libfdt.NOTFOUND) + + def testPropertyOffset(self): + """Walk through all the properties in the root node""" + offset = self.fdt.first_property_offset(0) + self.assertTrue(offset > 0) + for i in range(5): + next_offset = self.fdt.next_property_offset(offset) + self.assertTrue(next_offset > offset) + offset = next_offset + self.assertEquals(self.fdt.next_property_offset(offset, QUIET_NOTFOUND), + -libfdt.NOTFOUND) + + def testPropertyOffsetExceptions(self): + """Check that exceptions are raised as expected""" + with self.assertRaises(FdtException) as e: + self.fdt.first_property_offset(107) + self.assertEquals(e.exception.err, -libfdt.BADOFFSET) + + # Quieten the NOTFOUND exception and check that a BADOFFSET + # exception is still raised. + with self.assertRaises(FdtException) as e: + self.fdt.first_property_offset(107, QUIET_NOTFOUND) + self.assertEquals(e.exception.err, -libfdt.BADOFFSET) + with self.assertRaises(FdtException) as e: + self.fdt.next_property_offset(107, QUIET_NOTFOUND) + self.assertEquals(e.exception.err, -libfdt.BADOFFSET) + + # Check that NOTFOUND can be quietened. + node = self.fdt.path_offset('/subnode@1/ss1') + self.assertEquals(self.fdt.first_property_offset(node, QUIET_NOTFOUND), + -libfdt.NOTFOUND) + with self.assertRaises(FdtException) as e: + self.fdt.first_property_offset(node) + self.assertEquals(e.exception.err, -libfdt.NOTFOUND) + + def testGetName(self): + """Check that we can get the name of a node""" + self.assertEquals(self.fdt.get_name(0), '') + node = self.fdt.path_offset('/subnode@1/subsubnode') + self.assertEquals(self.fdt.get_name(node), 'subsubnode') + + with self.assertRaises(FdtException) as e: + self.fdt.get_name(-2) + self.assertEquals(e.exception.err, -libfdt.BADOFFSET) + + def testGetPropertyByOffset(self): + """Check that we can read the name and contents of a property""" + root = 0 + poffset = self.fdt.first_property_offset(root) + prop = self.fdt.get_property_by_offset(poffset) + self.assertEquals(prop.name, 'compatible') + self.assertEquals(prop.value, 'test_tree1\0') + + with self.assertRaises(FdtException) as e: + self.fdt.get_property_by_offset(-2) + self.assertEquals(e.exception.err, -libfdt.BADOFFSET) + self.assertEquals( + -libfdt.BADOFFSET, + self.fdt.get_property_by_offset(-2, [libfdt.BADOFFSET])) + + def testGetProp(self): + """Check that we can read the contents of a property by name""" + root = self.fdt.path_offset('/') + value = self.fdt.getprop(root, "compatible") + self.assertEquals(value, 'test_tree1\0') + self.assertEquals(-libfdt.NOTFOUND, self.fdt.getprop(root, 'missing', + QUIET_NOTFOUND)) + + with self.assertRaises(FdtException) as e: + self.fdt.getprop(root, 'missing') + self.assertEquals(e.exception.err, -libfdt.NOTFOUND) + + node = self.fdt.path_offset('/subnode@1/subsubnode') + value = self.fdt.getprop(node, "compatible") + self.assertEquals(value, 'subsubnode1\0subsubnode\0') + + def testStrError(self): + """Check that we can get an error string""" + self.assertEquals(libfdt.strerror(-libfdt.NOTFOUND), + 'FDT_ERR_NOTFOUND') + + def testFirstNextSubnodeOffset(self): + """Check that we can walk through subnodes""" + node_list = [] + node = self.fdt.first_subnode(0, QUIET_NOTFOUND) + while node >= 0: + node_list.append(self.fdt.get_name(node)) + node = self.fdt.next_subnode(node, QUIET_NOTFOUND) + self.assertEquals(node_list, ['subnode@1', 'subnode@2']) + + def testFirstNextSubnodeOffsetExceptions(self): + """Check except handling for first/next subnode functions""" + node = self.fdt.path_offset('/subnode@1/subsubnode', QUIET_NOTFOUND) + self.assertEquals(self.fdt.first_subnode(node, QUIET_NOTFOUND), + -libfdt.NOTFOUND) + with self.assertRaises(FdtException) as e: + self.fdt.first_subnode(node) + self.assertEquals(e.exception.err, -libfdt.NOTFOUND) + + node = self.fdt.path_offset('/subnode@1/ss1', QUIET_NOTFOUND) + self.assertEquals(self.fdt.next_subnode(node, QUIET_NOTFOUND), + -libfdt.NOTFOUND) + with self.assertRaises(FdtException) as e: + self.fdt.next_subnode(node) + self.assertEquals(e.exception.err, -libfdt.NOTFOUND) + + def testDeleteProperty(self): + """Test that we can delete a property""" + node_name = '/subnode@1' + self.assertEquals(self.GetPropList(node_name), + ['compatible', 'reg', 'prop-int']) + node = self.fdt.path_offset('/%s' % node_name) + self.assertEquals(self.fdt.delprop(node, 'reg'), 0) + self.assertEquals(self.GetPropList(node_name), + ['compatible', 'prop-int']) + + def testHeader(self): + """Test that we can access the header values""" + self.assertEquals(self.fdt.totalsize(), len(self.fdt._fdt)) + self.assertEquals(self.fdt.off_dt_struct(), 88) + + def testPack(self): + """Test that we can pack the tree after deleting something""" + orig_size = self.fdt.totalsize() + node = self.fdt.path_offset('/subnode@2', QUIET_NOTFOUND) + self.assertEquals(self.fdt.delprop(node, 'prop-int'), 0) + self.assertEquals(orig_size, self.fdt.totalsize()) + self.assertEquals(self.fdt.pack(), 0) + self.assertTrue(self.fdt.totalsize() < orig_size) + + def testBadPropertyOffset(self): + """Test that bad property offsets are detected""" + with self.assertRaises(FdtException) as e: + self.fdt.get_property_by_offset(13) + self.assertEquals(e.exception.err, -libfdt.BADOFFSET) + with self.assertRaises(FdtException) as e: + self.fdt.first_property_offset(3) + self.assertEquals(e.exception.err, -libfdt.BADOFFSET) + with self.assertRaises(FdtException) as e: + self.fdt.next_property_offset(3) + self.assertEquals(e.exception.err, -libfdt.BADOFFSET) + + def testBadPathOffset(self): + """Test that bad path names are detected""" + with self.assertRaisesRegexp(FdtException, get_err(libfdt.BADPATH)): + self.fdt.path_offset('not-present') + + def testQuietAll(self): + """Check that exceptions can be masked by QUIET_ALL""" + self.assertEquals(-libfdt.NOTFOUND, + self.fdt.path_offset('/missing', QUIET_ALL)) + self.assertEquals(-libfdt.BADOFFSET, + self.fdt.get_property_by_offset(13, QUIET_ALL)) + self.assertEquals(-libfdt.BADPATH, + self.fdt.path_offset('missing', QUIET_ALL)) + + +if __name__ == "__main__": + unittest.main()
Signature · 1 line
2.12.0.rc1.440.g5b76565f74-goog
A message that is not in the archive (sent off-list or not yet received): 20170304235229.5343-3-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org
Re: [PATCH v9 2/5] Add tests for pylibfdt
Quoted 3 lines · Simon Glass
Add a set of tests to cover the functionality in pylibfdt.
Signed-off-by Simon Glass <[email protected]>
Ok, the tests seem to pass now and show up in the test summary.
However, they still give no output at all, which means if one of them starts failing, there's no obvious way to see what's failing.
Quoted 333 lines
---
Changes in v9: None
Changes in v8:
- Avoid using hard-coded offsets (ROOT_PROPS)
- Use types.ModuleType instead of type(str)
- Compare exception number instead of using a regex
- Avoid using hard-coded size
- DroptestEndian()
- Move the changes torun_tests.shto a later commit
- Add comments totestPropertyOffsetExceptions()Changes in v7:
- Add a test forQUIET_ALLChanges in v6:
- Adjust tests to avoid checking a hard-coded offset
- Use 0 instead ofself.fdt.path_offset('/')
- Adjust thetotalsize()test to compare against the file size
- Adjust test result processing to avoid using awkChanges in v5:
- Adjust tests to match new swig bindingsChanges in v4:
- Drop tests that are no-longer applicable
- Add a get forgetprop()Changes in v3:
- Add some more testsChanges in v2:
- Update tests for new pylibfdt
- Add a few more tests to increase coveragetests/pylibfdt_tests.py | 288 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 288 insertions(+) create mode 100644 tests/pylibfdt_tests.pytests/pylibfdt_tests.py new file +288
-0@@ -0,0 +1,288 @@ +# pylibfdt - Tests for Flat Device Tree manipulation in Python +# Copyright (C) 2017 Google, Inc. +# Written by Simon Glass <[email protected]> +# +# libfdt is dual licensed: you can use it either under the terms of +# the GPL, or the BSD license, at your option. +# +# a) This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 2 of the +# License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public +# License along with this library; if not, write to the Free +# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, +# MA 02110-1301 USA +# +# Alternatively, +# +# b) Redistribution and use in source and binary forms, with or +# without modification, are permitted provided that the following +# conditions are met: +# +# 1. Redistributions of source code must retain the above +# copyright notice, this list of conditions and the following +# disclaimer. +# 2. Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, +# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, +# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +import sys +import types +import unittest + +sys.path.append('../pylibfdt') +import libfdt +from libfdt import FdtException, QUIET_NOTFOUND, QUIET_ALL + +def get_err(err_code): + """Convert an error code into an error message + + Args: + err_code: Error code value (FDT_ERR_...) + + Returns: + String error code + """ + return 'pylibfdt error %d: %s' % (-err_code, libfdt.fdt_strerror(-err_code)) + +def _ReadFdt(fname): + """Read a device tree file into an Fdt object, ready for use + + Args: + fname: Filename to read from + + Returns: + Fdt bytearray suitable for passing to libfdt functions + """ + return libfdt.Fdt(open(fname).read()) + +class PyLibfdtTests(unittest.TestCase): + """Test class for pylibfdt + + Properties: + fdt: Device tree file used for testing + """ + + def setUp(self): + """Read in the device tree we use for testing""" + self.fdt = _ReadFdt('test_tree1.dtb') + + def GetPropList(self, node_path): + """Read a list of properties from a node + + Args: + node_path: Full path to node, e.g. '/subnode@1/subsubnode' + + Returns: + List of property names for that node, e.g. ['compatible', 'reg'] + """ + prop_list = [] + node = self.fdt.path_offset(node_path) + poffset = self.fdt.first_property_offset(node, QUIET_NOTFOUND) + while poffset > 0: + prop = self.fdt.get_property_by_offset(poffset) + prop_list.append(prop.name) + poffset = self.fdt.next_property_offset(poffset, QUIET_NOTFOUND) + return prop_list + + def testImport(self): + """Check that we can import the library correctly""" + self.assertEquals(type(libfdt), types.ModuleType) + + def testBadFdt(self): + """Check that a filename provided accidentally is not accepted""" + with self.assertRaises(FdtException) as e: + fdt = libfdt.Fdt('a string') + self.assertEquals(e.exception.err, -libfdt.BADMAGIC) + + def testPathOffset(self): + """Check that we can find the offset of a node""" + self.assertEquals(self.fdt.path_offset('/'), 0) + self.assertTrue(self.fdt.path_offset('/subnode@1') > 0) + with self.assertRaises(FdtException) as e: + self.fdt.path_offset('/wibble') + self.assertEquals(e.exception.err, -libfdt.NOTFOUND) + self.assertEquals(self.fdt.path_offset('/wibble', QUIET_NOTFOUND), + -libfdt.NOTFOUND) + + def testPropertyOffset(self): + """Walk through all the properties in the root node""" + offset = self.fdt.first_property_offset(0) + self.assertTrue(offset > 0) + for i in range(5): + next_offset = self.fdt.next_property_offset(offset) + self.assertTrue(next_offset > offset) + offset = next_offset + self.assertEquals(self.fdt.next_property_offset(offset, QUIET_NOTFOUND), + -libfdt.NOTFOUND) + + def testPropertyOffsetExceptions(self): + """Check that exceptions are raised as expected""" + with self.assertRaises(FdtException) as e: + self.fdt.first_property_offset(107) + self.assertEquals(e.exception.err, -libfdt.BADOFFSET) + + # Quieten the NOTFOUND exception and check that a BADOFFSET + # exception is still raised. + with self.assertRaises(FdtException) as e: + self.fdt.first_property_offset(107, QUIET_NOTFOUND) + self.assertEquals(e.exception.err, -libfdt.BADOFFSET) + with self.assertRaises(FdtException) as e: + self.fdt.next_property_offset(107, QUIET_NOTFOUND) + self.assertEquals(e.exception.err, -libfdt.BADOFFSET) + + # Check that NOTFOUND can be quietened. + node = self.fdt.path_offset('/subnode@1/ss1') + self.assertEquals(self.fdt.first_property_offset(node, QUIET_NOTFOUND), + -libfdt.NOTFOUND) + with self.assertRaises(FdtException) as e: + self.fdt.first_property_offset(node) + self.assertEquals(e.exception.err, -libfdt.NOTFOUND) + + def testGetName(self): + """Check that we can get the name of a node""" + self.assertEquals(self.fdt.get_name(0), '') + node = self.fdt.path_offset('/subnode@1/subsubnode') + self.assertEquals(self.fdt.get_name(node), 'subsubnode') + + with self.assertRaises(FdtException) as e: + self.fdt.get_name(-2) + self.assertEquals(e.exception.err, -libfdt.BADOFFSET) + + def testGetPropertyByOffset(self): + """Check that we can read the name and contents of a property""" + root = 0 + poffset = self.fdt.first_property_offset(root) + prop = self.fdt.get_property_by_offset(poffset) + self.assertEquals(prop.name, 'compatible') + self.assertEquals(prop.value, 'test_tree1\0') + + with self.assertRaises(FdtException) as e: + self.fdt.get_property_by_offset(-2) + self.assertEquals(e.exception.err, -libfdt.BADOFFSET) + self.assertEquals( + -libfdt.BADOFFSET, + self.fdt.get_property_by_offset(-2, [libfdt.BADOFFSET])) + + def testGetProp(self): + """Check that we can read the contents of a property by name""" + root = self.fdt.path_offset('/') + value = self.fdt.getprop(root, "compatible") + self.assertEquals(value, 'test_tree1\0') + self.assertEquals(-libfdt.NOTFOUND, self.fdt.getprop(root, 'missing', + QUIET_NOTFOUND)) + + with self.assertRaises(FdtException) as e: + self.fdt.getprop(root, 'missing') + self.assertEquals(e.exception.err, -libfdt.NOTFOUND) + + node = self.fdt.path_offset('/subnode@1/subsubnode') + value = self.fdt.getprop(node, "compatible") + self.assertEquals(value, 'subsubnode1\0subsubnode\0') + + def testStrError(self): + """Check that we can get an error string""" + self.assertEquals(libfdt.strerror(-libfdt.NOTFOUND), + 'FDT_ERR_NOTFOUND') + + def testFirstNextSubnodeOffset(self): + """Check that we can walk through subnodes""" + node_list = [] + node = self.fdt.first_subnode(0, QUIET_NOTFOUND) + while node >= 0: + node_list.append(self.fdt.get_name(node)) + node = self.fdt.next_subnode(node, QUIET_NOTFOUND) + self.assertEquals(node_list, ['subnode@1', 'subnode@2']) + + def testFirstNextSubnodeOffsetExceptions(self): + """Check except handling for first/next subnode functions""" + node = self.fdt.path_offset('/subnode@1/subsubnode', QUIET_NOTFOUND) + self.assertEquals(self.fdt.first_subnode(node, QUIET_NOTFOUND), + -libfdt.NOTFOUND) + with self.assertRaises(FdtException) as e: + self.fdt.first_subnode(node) + self.assertEquals(e.exception.err, -libfdt.NOTFOUND) + + node = self.fdt.path_offset('/subnode@1/ss1', QUIET_NOTFOUND) + self.assertEquals(self.fdt.next_subnode(node, QUIET_NOTFOUND), + -libfdt.NOTFOUND) + with self.assertRaises(FdtException) as e: + self.fdt.next_subnode(node) + self.assertEquals(e.exception.err, -libfdt.NOTFOUND) + + def testDeleteProperty(self): + """Test that we can delete a property""" + node_name = '/subnode@1' + self.assertEquals(self.GetPropList(node_name), + ['compatible', 'reg', 'prop-int']) + node = self.fdt.path_offset('/%s' % node_name) + self.assertEquals(self.fdt.delprop(node, 'reg'), 0) + self.assertEquals(self.GetPropList(node_name), + ['compatible', 'prop-int']) + + def testHeader(self): + """Test that we can access the header values""" + self.assertEquals(self.fdt.totalsize(), len(self.fdt._fdt)) + self.assertEquals(self.fdt.off_dt_struct(), 88) + + def testPack(self): + """Test that we can pack the tree after deleting something""" + orig_size = self.fdt.totalsize() + node = self.fdt.path_offset('/subnode@2', QUIET_NOTFOUND) + self.assertEquals(self.fdt.delprop(node, 'prop-int'), 0) + self.assertEquals(orig_size, self.fdt.totalsize()) + self.assertEquals(self.fdt.pack(), 0) + self.assertTrue(self.fdt.totalsize() < orig_size) + + def testBadPropertyOffset(self): + """Test that bad property offsets are detected""" + with self.assertRaises(FdtException) as e: + self.fdt.get_property_by_offset(13) + self.assertEquals(e.exception.err, -libfdt.BADOFFSET) + with self.assertRaises(FdtException) as e: + self.fdt.first_property_offset(3) + self.assertEquals(e.exception.err, -libfdt.BADOFFSET) + with self.assertRaises(FdtException) as e: + self.fdt.next_property_offset(3) + self.assertEquals(e.exception.err, -libfdt.BADOFFSET) + + def testBadPathOffset(self): + """Test that bad path names are detected""" + with self.assertRaisesRegexp(FdtException, get_err(libfdt.BADPATH)): + self.fdt.path_offset('not-present') + + def testQuietAll(self): + """Check that exceptions can be masked by QUIET_ALL""" + self.assertEquals(-libfdt.NOTFOUND, + self.fdt.path_offset('/missing', QUIET_ALL)) + self.assertEquals(-libfdt.BADOFFSET, + self.fdt.get_property_by_offset(13, QUIET_ALL)) + self.assertEquals(-libfdt.BADPATH, + self.fdt.path_offset('missing', QUIET_ALL)) + + +if __name__ == "__main__": + unittest.main()
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
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH v9 2/5] Add tests for pylibfdt
Hi David,
Quoted 7 lines · David Gibson
Quoted 3 lines · Simon Glass
Add a set of tests to cover the functionality in pylibfdt.
Signed-off-by Simon Glass <[email protected]>Ok, the tests seem to pass now and show up in the test summary.
nit: that is no different from the previous series - my change in v9 was to move the test-running code to the final patch, so that if someone applied only the first two patches it would not run tests that could not pass.
Quoted 3 lines
However, they still give no output at all, which means if one of them starts failing, there's no obvious way to see what's failing.
I've sent a v10 of that final patch which shows the test output in that case.
Regards,
Simon
PATCH v9 3/5 Mention pylibfdt in the documentation
In reply to a message not in the archiveAdd a note about pylibfdt in the README.
---
Changes in v9: None
Changes in v8: None
Changes in v7: None
Changes in v6:
- Update example to print the node value as an integer
- Update example to print the bytestring as well as the string
Changes in v5:
- Use an interactive session to demonstrate pylibfdt
- Mention that more work remains
Changes in v4: None
Changes in v3: None
Changes in v2:
- Add details on how to obtain full help and code coverage
README | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+)
README +47 -0
@@ -7,6 +7,53 @@ DTC and LIBFDT are maintained by: David Gibson <[email protected]> Jon Loeliger <jdl-CYoMK+44s/[email protected]> + +Python library +-------------- + +A Python library is also available. To build this you will need to install +swig and Python development files. On Debian distributions: + + sudo apt-get install swig python-dev + +The library provides an Fdt class which you can use like this: + +$ PYTHONPATH=../pylibfdt python +>>> import libfdt +>>> fdt = libfdt.Fdt(open('test_tree1.dtb').read()) +>>> node = fdt.path_offset('/subnode@1') +>>> print node +124 +>>> prop_offset = fdt.first_property_offset(node) +>>> prop = fdt.get_property_by_offset(prop_offset) +>>> print '%s=%r' % (prop.name, prop.value) +compatible=bytearray(b'subnode1\x00') +>>> print '%s=%s' % (prop.name, prop.value) +compatible=subnode1 +>>> node2 = fdt.path_offset('/') +>>> print fdt.getprop(node2, 'compatible') +test_tree1 + +You will find tests in tests/pylibfdt_tests.py showing how to use each +method. Help is available using the Python help command, e.g.: + + $ cd pylibfdt + $ python -c "import libfdt; help(libfdt)" + +If you add new features, please check code coverage: + + $ sudo apt-get install python-pip python-pytest + $ sudo pip install coverage + $ cd tests + $ coverage run pylibfdt_tests.py + $ coverage html + # Open 'htmlcov/index.html' in your browser + + +More work remains to support all of libfdt, including access to numeric +values. + + Mailing list ------------ The following list is for discussion about dtc and libfdt implementation
Signature · 1 line
2.12.0.rc1.440.g5b76565f74-goog
PATCH v9 4/5 Adjust libfdt.h to work with swig
In reply to a message not in the archiveThere are a few places where libfdt.h cannot be used as is with swig:
- macros like fdt_totalsize() have to be defined as C declarations
- fdt_offset_ptr() and fdt_getprop_namelen() need special treatment due to a TODO in the wrapper for fdt_getprop(). However they are not useful to Python so can be removed
Add #ifdefs to work around these problem.
---
Changes in v9: None
Changes in v8:
- Drop all the namelen functions from the Python bindings
Changes in v7: None
Changes in v6: None
Changes in v5:
- Update commit message
- Drop #ifdef around fdt_get_header() macros
Changes in v4:
- Add new patch to adjust libfdt.h to work with swig
Changes in v3: None
Changes in v2: None
libfdt/libfdt.h | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)
libfdt/libfdt.h +20 -1
@@ -143,7 +143,9 @@/* Low-level functions (you probably don't need these) */ /**********************************************************************/ +#ifndef SWIG /* This function is not useful in Python */ const void *fdt_offset_ptr (const void *fdt,int offset,unsigned int checklen); +#endif static inline void *fdt_offset_ptr_w (void *fdt,int offset,int checklen) {return (void *)(uintptr_t )fdt_offset_ptr (fdt, offset, checklen);
@@ -210,7 +212,6 @@ int fdt_next_subnode(const void *fdt, int offset);/**********************************************************************/ /* General functions */ /**********************************************************************/ -#define fdt_get_header (fdt, field) \ (fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))#define fdt_magic (fdt) (fdt_get_header(fdt, magic))
@@ -354,8 +355,10 @@ int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);* useful for finding subnodes based on a portion of a larger string, * such as a full path. */ +#ifndef SWIG /* Not available in Python */ int fdt_subnode_offset_namelen (const void *fdt,int parentoffset,const char *name,int namelen); +#endif /** * fdt_subnode_offset - find a subnode of a given node * @fdt: pointer to the device tree blob
@@ -391,7 +394,9 @@ int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);* Identical to fdt_path_offset(), but only consider the first namelen * characters of path as the path name. */ +#ifndef SWIG /* Not available in Python */ int fdt_path_offset_namelen (const void *fdt,const char *path,int namelen); +#endif /** * fdt_path_offset - find a tree node by its full path
@@ -550,10 +555,12 @@ const struct fdt_property *fdt_get_property_by_offset(const void *fdt,* Identical to fdt_get_property(), but only examine the first namelen * characters of name for matching the property name. */ +#ifndef SWIG /* Not available in Python */ const struct fdt_property *fdt_get_property_namelen (const void *fdt,int nodeoffset,const char *name,int namelen,int *lenp); +#endif /** * fdt_get_property - find a given property in a given node
@@ -624,8 +631,10 @@ static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,* -FDT_ERR_BADSTRUCTURE, * -FDT_ERR_TRUNCATED, standard meanings */ +#ifndef SWIG /* This function is not useful in Python */ const void *fdt_getprop_by_offset (const void *fdt,int offset,const char **namep,int *lenp); +#endif /** * fdt_getprop_namelen - get property value based on substring
@@ -638,6 +647,7 @@ const void *fdt_getprop_by_offset(const void *fdt, int offset,* Identical to fdt_getprop(), but only examine the first namelen * characters of name for matching the property name. */ +#ifndef SWIG /* Not available in Python */ const void *fdt_getprop_namelen (const void *fdt,int nodeoffset,const char *name,int namelen,int *lenp); static inline void *fdt_getprop_namelen_w(void *fdt, int nodeoffset,
@@ -647,6 +657,7 @@ static inline void *fdt_getprop_namelen_w(void *fdt, int nodeoffset,return (void *)(uintptr_t )fdt_getprop_namelen (fdt, nodeoffset, name, namelen, lenp); } +#endif /** * fdt_getprop - retrieve the value of a given property
@@ -707,8 +718,10 @@ uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);* Identical to fdt_get_alias(), but only examine the first namelen * characters of name for matching the alias name. */ +#ifndef SWIG /* Not available in Python */ const char *fdt_get_alias_namelen (const void *fdt,const char *name,int namelen); +#endif /** * fdt_get_alias - retrieve the path referenced by a given alias
@@ -1106,10 +1119,12 @@ int fdt_size_cells(const void *fdt, int nodeoffset);* of the name. It is useful when you want to manipulate only one value of * an array and you have a string that doesn't end with \0. */ +#ifndef SWIG /* Not available in Python */ int fdt_setprop_inplace_namelen_partial (void *fdt,int nodeoffset,const char *name,int namelen,uint32_t idx,const void *val,int len); +#endif /** * fdt_setprop_inplace - change a property's value, but not its size
@@ -1139,8 +1154,10 @@ int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,* -FDT_ERR_BADSTRUCTURE, * -FDT_ERR_TRUNCATED, standard meanings */ +#ifndef SWIG /* Not available in Python */ int fdt_setprop_inplace (void *fdt,int nodeoffset,const char *name,const void *val,int len); +#endif /** * fdt_setprop_inplace_u32 - change the value of a 32-bit integer property
@@ -1734,8 +1751,10 @@ int fdt_delprop(void *fdt, int nodeoffset, const char *name);* creating subnodes based on a portion of a larger string, such as a * full path. */ +#ifndef SWIG /* Not available in Python */ int fdt_add_subnode_namelen (void *fdt,int parentoffset,const char *name,int namelen); +#endif /** * fdt_add_subnode - creates a new node
Signature · 1 line
2.12.0.rc1.440.g5b76565f74-goog
PATCH v9 5/5 Build pylibfdt as part of the normal build process
In reply to a message not in the archiveIf swig and the Python are available, build pylibfdt automatically. Adjust the tests to run Python tests too in this case.
---
Changes in v9:
- Support 'make clean' properly with pylibfdt
- Move run_tests.sh updates to this patch
- Only run the pylibfdt tests automatically if we can build pylibfdt
- Update commit message to reflect swig/Python.h detection
- Update Makefile shell command to support dash shell
Changes in v8:
- Only build pylibfdt if we have swig and Python.h
Changes in v7: None
Changes in v6: None
Changes in v5:
- Fix 'possible' typo
Changes in v4: None
Changes in v3: None
Changes in v2: None
Makefile | 34 ++++++++++++++++++++++++++++++++-- tests/run_tests.sh | 19 +++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-)
Makefile +32 -2
@@ -116,7 +116,21 @@ BIN += fdtput SCRIPTS = dtdiff -all: $(BIN) libfdt +# We need both Python and swig to build pylibfdt. +.PHONY: maybe_pylibfdt +maybe_pylibfdt: FORCE + if pkg-config --cflags python >/dev/null 2>&1; then \ + if which swig >/dev/null; then \ + can_build=yes; \ + fi; \ + fi; \ + if [ "$$can_build" = "yes" ]; then \ + $(MAKE) pylibfdt; \ + else \ + echo "Please install python-dev and swig to build pylibfdt"; \ + fi + +all: $(BIN) libfdt maybe_pylibfdt ifneq ($(DEPTARGETS),)
@@ -203,6 +217,22 @@ dist: cat ../dtc-$(dtc_version).tar | \ gzip -9 > ../dtc-$(dtc_version).tar.gz + +# +# Rules for pylibfdt +# +PYLIBFDT_srcdir = pylibfdt +PYLIBFDT_objdir = pylibfdt + +include $(PYLIBFDT_srcdir)/Makefile.pylibfdt + +.PHONY: pylibfdt +pylibfdt: $(PYLIBFDT_objdir)/_libfdt.so + +pylibfdt_clean: + @$(VECHO) CLEAN "(pylibfdt)" + rm -f $(addprefix $(PYLIBFDT_objdir)/,$(PYLIBFDT_cleanfiles)) + # # Release signing and uploading # This is for maintainer convenience, don't try this at home.
@@ -244,7 +274,7 @@ include tests/Makefile.tests STD_CLEANFILES = *~ *.o *.$(SHAREDLIB_EXT) *.d *.a *.i *.s core a.out vgcore.* \ *.tab.[ch] *.lex.c *.output -clean: libfdt_clean tests_clean +clean: libfdt_clean pylibfdt_clean tests_clean @$(VECHO) CLEAN rm -f $(STD_CLEANFILES) rm -f $(VERSION_FILE)
tests/run_tests.sh +19 -0
@@ -769,6 +769,17 @@ fdtdump_tests () { run_fdtdump_test fdtdump.dts } +pylibfdt_tests () { + TMP=/tmp/tests.stderr.$$ + python pylibfdt_tests.py 2> ${TMP} + result=$(head -1 ${TMP}) + + # Extract the test results and add them to our totals + tot_fail=$((tot_fail + $(echo $result | tr -d '\n.' | wc -c))) + tot_pass=$((tot_pass + $(echo $result | tr -d '\nF' | wc -c))) + tot_tests=$((tot_tests + $(echo $result | tr -d '\n' | wc -c))) +} + while getopts "vt:me" ARG ; do case $ARG in "v")
@@ -788,6 +799,11 @@ done if [ -z "$TESTSETS" ]; then TESTSETS="libfdt utilfdt dtc dtbs_equal fdtget fdtput fdtdump" + + # Test pylibfdt if the libfdt Python module is available. + if [ -f ../pylibfdt/_libfdt.so ]; then + TESTSETS="$TESTSETS pylibfdt" + fi fi # Make sure we don't have stale blobs lying around
@@ -816,6 +832,9 @@ for set in $TESTSETS; do "fdtdump") fdtdump_tests ;; + "pylibfdt") + pylibfdt_tests + ;; esac done
Signature · 1 line
2.12.0.rc1.440.g5b76565f74-goog
Hi David,
Quoted 11 lines · Simon Glass
At present libfdt consists of only a C implementation. Many scripts are written using Python so it useful to have Python bindings for libfdt. Apparently this has never been attempted before, or if so I cannot find a reference.
This series starts the process of adding this support, with just a bare-bones set of methods.
The v8 series provides binding that can be used like this:
Quoted 7 lines
fdt = libfdt.Fdt(open(fname).read())
node =fdt.path_offset('/subnode@1')fdt.get_prop(node, 'compatible')
subnode =fdt.first_subnode(node, quiet=[libfdt.NOTFOUND])while subnode > 0:fdt.get_name(subnode)
subnode =fdt.next_subnode(subnode, quiet=[libfdt.NOTFOUND])
Just checking in to see how this looks now? I sent one v10 patch but the rest is as is. Let me know if you'd like me to send the whole thing again.
Regards,
Simon
A message that is not in the archive (sent off-list or not yet received): CAPnjgZ1pdbNMCdGbSZQ9aE=rGR0XZyJ-e9Fqsj2v0ThEuk6SiQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Quoted 22 lines · Simon Glass
Hi David,
Quoted 11 lines · Simon Glass
At present libfdt consists of only a C implementation. Many scripts are written using Python so it useful to have Python bindings for libfdt. Apparently this has never been attempted before, or if so I cannot find a reference.
This series starts the process of adding this support, with just a bare-bones set of methods.
The v8 series provides binding that can be used like this:
Quoted 7 lines
fdt = libfdt.Fdt(open(fname).read())
node =fdt.path_offset('/subnode@1')fdt.get_prop(node, 'compatible')
subnode =fdt.first_subnode(node, quiet=[libfdt.NOTFOUND])while subnode > 0:fdt.get_name(subnode)
subnode =fdt.next_subnode(subnode, quiet=[libfdt.NOTFOUND])
Quoted 3 lines
Just checking in to see how this looks now? I sent one v10 patch but the rest is as is. Let me know if you'd like me to send the whole thing again.
Sorry, my day job has been kind of hectic lately.
So, for one thing, yes, I'd like a repost of the whole set - I didn't keep the last spin ready to hand.
But I'm also still not thrilled with the behaviour in the v10 patch you posted. It prints information _only_ if there are failures, which makes it a drag to check, and confusing since the tests show up in the final numbers but have no other visibility.
I think this can go one of two ways:
- Give the Python tests close to the existing testcases. I realize that's awkward since they use python unittest not the existing home-rolled framework, but do what you can.
or
- Don't integrate the python tests at all. Have make check run the existing testsuite, then the python test suite (if python is available). This should produce *some* output for both parts in all cases (so you know it has run), even if it's just a minimal summary for the unittest part. Don't include the python tests in the totals for the other part.
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
A message that is not in the archive (sent off-list or not yet received): [email protected]
Hi David,
Quoted 42 lines · David Gibson
Quoted 22 lines · Simon Glass
Hi David,
Quoted 11 lines · Simon Glass
At present libfdt consists of only a C implementation. Many scripts are written using Python so it useful to have Python bindings for libfdt. Apparently this has never been attempted before, or if so I cannot find a reference.
This series starts the process of adding this support, with just a bare-bones set of methods.
The v8 series provides binding that can be used like this:
Quoted 7 lines
fdt = libfdt.Fdt(open(fname).read())
node =fdt.path_offset('/subnode@1')fdt.get_prop(node, 'compatible')
subnode =fdt.first_subnode(node, quiet=[libfdt.NOTFOUND])while subnode > 0:fdt.get_name(subnode)
subnode =fdt.next_subnode(subnode, quiet=[libfdt.NOTFOUND])Quoted 3 lines
Just checking in to see how this looks now? I sent one v10 patch but the rest is as is. Let me know if you'd like me to send the whole thing again.
Sorry, my day job has been kind of hectic lately.
So, for one thing, yes, I'd like a repost of the whole set - I didn't keep the last spin ready to hand.
But I'm also still not thrilled with the behaviour in the v10 patch you posted. It prints information _only_ if there are failures, which makes it a drag to check, and confusing since the tests show up in the final numbers but have no other visibility.
I think this can go one of two ways:
- Give the Python tests close to the existing testcases. I realize that's awkward since they use python unittest not the existing home-rolled framework, but do what you can.
Quoted 7 lines
or
- Don't integrate the python tests at all. Have make check run the existing testsuite, then the python test suite (if python is available). This should produce *some* output for both parts in all cases (so you know it has run), even if it's just a minimal summary for the unittest part. Don't include the python tests in the totals for the other part.
It's pretty easy to just always print the output. I'll send that as v11 and see what you think.
Regards.
Simon