Peter Wu
2014-Aug-09 15:33 UTC
[Libguestfs] [hivex] [PATCH 0/3] python: export version and hive types
Hi, Here are another set of patches that attempt to make the Python interface more useful. First there is a patch to export the package version over the binary interface. The second patch is a prerequisite for the third and moves the hivex.py source to a separate directory. This may need changes to your distro packaging. The Debian packaging from Ubuntu 14.04 did however not require such changes as it does not depend on the exact location of the .py files. The third patch depends on the second one and adds a new module `hivex.hive_types` to avoid duplicating the `REG_*` constants everywhere. Tested against the following combinations: - Python 3.4.1 (Arch Linux x86_64, ./configure from git tree) - Python 2.7.6 + 3.4.0 (Ubuntu 14.04 x86_64, using Debian packaging) - Python 2.6.5 (Ubuntu 10.04 i686, ./configure from dist tarball) These patches are based on the following two patches which have not yet been merged into master (they are however independent of the functionality introduced therein): - Add type checking, support integers as value - generator: Fix mixed tabs/spaces If you prefer a git repo, have a look at the following for accumulated patches https://github.com/Lekensteyn/hivex/compare/master...develop Peter Wu (3): python: expose package version python: move module to separate directory python: export hive_types constants generator/generator.ml | 27 ++++++++++++++++++++++++++- python/Makefile.am | 19 ++++++++++++++++--- 2 files changed, 42 insertions(+), 4 deletions(-) -- 2.0.2
Exposing the module version through the `__version__` property conforms to PEP-396. The recommended versioning scheme is specified in PEP-386[1]. [1]: https://www.python.org/dev/peps/pep-0386/ --- generator/generator.ml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/generator/generator.ml b/generator/generator.ml index 40c1981..f6c34e9 100755 --- a/generator/generator.ml +++ b/generator/generator.ml @@ -3310,6 +3310,10 @@ moduleinit (void) m = Py_InitModule ((char *) \"libhivexmod\", methods); #endif + if (m) { + PyModule_AddStringConstant (m, \"__version__\", PACKAGE_VERSION); + } + return m; /* m might be NULL if module init failed */ } @@ -3345,6 +3349,8 @@ Read the hivex(3) man page to find out how to use the API. import libhivexmod +__version__ = libhivexmod.__version__ + class Hivex(object): \"\"\"Instances of this class are hivex API handles.\"\"\" -- 2.0.2
Peter Wu
2014-Aug-09 15:33 UTC
[Libguestfs] [PATCH 2/3] python: move module to separate directory
This allows submodules to be added under the 'hivex' namespace. A new `pythonmoddir` variable was added for separating libhivexmod.so and the Python hivex module. --- generator/generator.ml | 3 ++- python/Makefile.am | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/generator/generator.ml b/generator/generator.ml index f6c34e9..466efcb 100755 --- a/generator/generator.ml +++ b/generator/generator.ml @@ -3854,7 +3854,8 @@ Run it from the top source directory using the command output_to "perl/lib/Win/Hivex.pm" generate_perl_pm; output_to "perl/Hivex.xs" generate_perl_xs; - output_to "python/hivex.py" generate_python_py; + (try Unix.mkdir "python/hivex" 0o755 with Unix_error _ -> ()); + output_to "python/hivex/__init__.py" generate_python_py; output_to "python/hivex-py.c" generate_python_c; output_to "ruby/ext/hivex/_hivex.c" generate_ruby_c; diff --git a/python/Makefile.am b/python/Makefile.am index 93a70c4..6bb8ade 100644 --- a/python/Makefile.am +++ b/python/Makefile.am @@ -20,15 +20,17 @@ builddir ?= $(top_builddir)/python EXTRA_DIST = \ run-python-tests \ - hivex.py \ + hivex/__init__.py \ hivex-py.c \ t/*.py if HAVE_PYTHON pythondir = $(PYTHON_INSTALLDIR) +pythonmoddir = $(PYTHON_INSTALLDIR)/hivex -python_DATA = hivex.py +pythonmod_DATA = \ + hivex/__init__.py python_LTLIBRARIES = libhivexmod.la @@ -42,6 +44,14 @@ TESTS_ENVIRONMENT = ../run TESTS = run-python-tests -CLEANFILES = hivex.pyc +# hivex.py got moved to hivex/__init.py +CLEANFILES = \ + hivex.py \ + hivex.pyc \ + hivex/__init__.pyc + +# For Python 3 +clean-local: + -rm -rf __pycache__/ hivex/__pycache__/ endif -- 2.0.2
Peter Wu
2014-Aug-09 15:33 UTC
[Libguestfs] [PATCH 3/3] python: export hive_types constants
Instead of using magic numbers or copying the numbers all over place, make the hive type constants available so you can do: import hivex from hivex.hive_types import * h = hivex.Hivex ("../images/minimal", write = True) new_value = { "key": "abc", "t": REG_BINARY, "value": b"xyz" } h.node_set_value (h.root (), new_value) --- generator/generator.ml | 18 ++++++++++++++++++ python/Makefile.am | 3 +++ 2 files changed, 21 insertions(+) diff --git a/generator/generator.ml b/generator/generator.ml index 466efcb..349f1d1 100755 --- a/generator/generator.ml +++ b/generator/generator.ml @@ -3410,6 +3410,23 @@ class Hivex(object): ) ) functions +and generate_python_hive_types_py () + generate_header HashStyle LGPLv2plus; + + pr "\ +\"\"\"Define integer constants for hive type + +The names correspond with the hive_type enum type of the C API, but without +'hive_t_' prefix. +\"\"\" + +"; + List.iter ( + fun (t, _, new_style, description) -> + pr "# %s\n" description; + pr "REG_%s = %d\n" new_style t + ) hive_types; + and generate_ruby_c () generate_header CStyle LGPLv2plus; @@ -3856,6 +3873,7 @@ Run it from the top source directory using the command (try Unix.mkdir "python/hivex" 0o755 with Unix_error _ -> ()); output_to "python/hivex/__init__.py" generate_python_py; + output_to "python/hivex/hive_types.py" generate_python_hive_types_py; output_to "python/hivex-py.c" generate_python_c; output_to "ruby/ext/hivex/_hivex.c" generate_ruby_c; diff --git a/python/Makefile.am b/python/Makefile.am index 6bb8ade..1117ab9 100644 --- a/python/Makefile.am +++ b/python/Makefile.am @@ -21,6 +21,7 @@ builddir ?= $(top_builddir)/python EXTRA_DIST = \ run-python-tests \ hivex/__init__.py \ + hivex/hive_types.py \ hivex-py.c \ t/*.py @@ -30,6 +31,7 @@ pythondir = $(PYTHON_INSTALLDIR) pythonmoddir = $(PYTHON_INSTALLDIR)/hivex pythonmod_DATA = \ + hivex/hive_types.py \ hivex/__init__.py python_LTLIBRARIES = libhivexmod.la @@ -48,6 +50,7 @@ TESTS = run-python-tests CLEANFILES = \ hivex.py \ hivex.pyc \ + hivex/hive_types.pyc \ hivex/__init__.pyc # For Python 3 -- 2.0.2
Richard W.M. Jones
2014-Aug-12 09:53 UTC
Re: [Libguestfs] [hivex] [PATCH 0/3] python: export version and hive types
Thanks -- I've applied all 3 of these patches and will push them very shortly. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-builder quickly builds VMs from scratch http://libguestfs.org/virt-builder.1.html