[libvirt] [PATCH python 0/4] setup.py: Don't unconditionally run pkg-config

This series reorganizes setup.py to not unconditionally call pkg-config. This will allow 'setup.py clean' and 'setup.py --help' to work correctly even if pkg-config or libvirt-devel aren't installed. https://bugzilla.redhat.com/show_bug.cgi?id=1074170 Cole Robinson (4): setup.py: Remove unused import setup.py: Move module list building to its own function setup.py: Make have_libvirt_lxc a function setup.py: Allow running --help or clean without pkg-config setup.py | 144 +++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 85 insertions(+), 59 deletions(-) -- 1.8.5.3

--- setup.py | 1 - 1 file changed, 1 deletion(-) diff --git a/setup.py b/setup.py index cfbbe2c..554d10d 100755 --- a/setup.py +++ b/setup.py @@ -11,7 +11,6 @@ from distutils.errors import DistutilsExecError import distutils import sys -import datetime import os import os.path import re -- 1.8.5.3

Makes it a bit more clear what all that code is used for, rather than intermixing it with function definitions. Besides the comment additions, this is a no-op and just reindents the block, into a function. --- setup.py | 97 ++++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 57 insertions(+), 40 deletions(-) diff --git a/setup.py b/setup.py index 554d10d..a8fdeb4 100755 --- a/setup.py +++ b/setup.py @@ -72,49 +72,59 @@ def get_api_xml_files(): return (libvirt_api, libvirt_qemu_api, libvirt_lxc_api) -ldflags = get_pkgconfig_data(["--libs-only-L"], "libvirt", False) -cflags = get_pkgconfig_data(["--cflags"], "libvirt", False) - -c_modules = [] -py_modules = [] - -module = Extension('libvirtmod', - sources = ['libvirt-override.c', 'build/libvirt.c', 'typewrappers.c', 'libvirt-utils.c'], - libraries = [ "virt" ], - include_dirs = [ "." ]) -if cflags != "": - module.extra_compile_args.append(cflags) -if ldflags != "": - module.extra_link_args.append(ldflags) - -c_modules.append(module) -py_modules.append("libvirt") - -moduleqemu = Extension('libvirtmod_qemu', - sources = ['libvirt-qemu-override.c', 'build/libvirt-qemu.c', 'typewrappers.c', 'libvirt-utils.c'], - libraries = [ "virt-qemu" ], +def get_module_lists(): + """ + Determine which modules we are actually building, and all their + required config + """ + c_modules = [] + py_modules = [] + ldflags = get_pkgconfig_data(["--libs-only-L"], "libvirt", False) + cflags = get_pkgconfig_data(["--cflags"], "libvirt", False) + + module = Extension('libvirtmod', + sources = ['libvirt-override.c', 'build/libvirt.c', 'typewrappers.c', 'libvirt-utils.c'], + libraries = [ "virt" ], include_dirs = [ "." ]) -if cflags != "": - moduleqemu.extra_compile_args.append(cflags) -if ldflags != "": - moduleqemu.extra_link_args.append(ldflags) - -c_modules.append(moduleqemu) -py_modules.append("libvirt_qemu") - -if have_libvirt_lxc: - modulelxc = Extension('libvirtmod_lxc', - sources = ['libvirt-lxc-override.c', 'build/libvirt-lxc.c', 'typewrappers.c', 'libvirt-utils.c'], - libraries = [ "virt-lxc" ], - include_dirs = [ "." ]) if cflags != "": - modulelxc.extra_compile_args.append(cflags) + module.extra_compile_args.append(cflags) if ldflags != "": - modulelxc.extra_link_args.append(ldflags) + module.extra_link_args.append(ldflags) - c_modules.append(modulelxc) - py_modules.append("libvirt_lxc") + c_modules.append(module) + py_modules.append("libvirt") + moduleqemu = Extension('libvirtmod_qemu', + sources = ['libvirt-qemu-override.c', 'build/libvirt-qemu.c', 'typewrappers.c', 'libvirt-utils.c'], + libraries = [ "virt-qemu" ], + include_dirs = [ "." ]) + if cflags != "": + moduleqemu.extra_compile_args.append(cflags) + if ldflags != "": + moduleqemu.extra_link_args.append(ldflags) + + c_modules.append(moduleqemu) + py_modules.append("libvirt_qemu") + + if have_libvirt_lxc: + modulelxc = Extension('libvirtmod_lxc', + sources = ['libvirt-lxc-override.c', 'build/libvirt-lxc.c', 'typewrappers.c', 'libvirt-utils.c'], + libraries = [ "virt-lxc" ], + include_dirs = [ "." ]) + if cflags != "": + modulelxc.extra_compile_args.append(cflags) + if ldflags != "": + modulelxc.extra_link_args.append(ldflags) + + c_modules.append(modulelxc) + py_modules.append("libvirt_lxc") + + return c_modules, py_modules + + +################### +# Custom commands # +################### class my_build(build): @@ -281,14 +291,21 @@ class my_clean(clean): if os.path.exists("build"): remove_tree("build") + +################## +# Invoke setup() # +################## + +_c_modules, _py_modules = get_module_lists() + setup(name = 'libvirt-python', version = '1.2.3', url = 'http://www.libvirt.org', maintainer = 'Libvirt Maintainers', maintainer_email = 'libvir-list@redhat.com', description = 'The libvirt virtualization API', - ext_modules = c_modules, - py_modules = py_modules, + ext_modules = _c_modules, + py_modules = _py_modules, package_dir = { '': 'build' }, -- 1.8.5.3

This means we call it twice for a build operation, but I don't think that's a big deal. --- setup.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/setup.py b/setup.py index a8fdeb4..f137b22 100755 --- a/setup.py +++ b/setup.py @@ -34,13 +34,14 @@ spawn([pkgcfg, "--atleast-version=%s" % MIN_LIBVIRT, "libvirt"]) -have_libvirt_lxc=True -try: - spawn([pkgcfg, - "--atleast-version=%s" % MIN_LIBVIRT_LXC, - "libvirt"]) -except DistutilsExecError: - have_libvirt_lxc=False +def have_libvirt_lxc(): + try: + spawn([pkgcfg, + "--atleast-version=%s" % MIN_LIBVIRT_LXC, + "libvirt"]) + return True + except DistutilsExecError: + return False def get_pkgconfig_data(args, mod, required=True): """Run pkg-config to and return content associated with it""" @@ -106,7 +107,7 @@ def get_module_lists(): c_modules.append(moduleqemu) py_modules.append("libvirt_qemu") - if have_libvirt_lxc: + if have_libvirt_lxc(): modulelxc = Extension('libvirtmod_lxc', sources = ['libvirt-lxc-override.c', 'build/libvirt-lxc.c', 'typewrappers.c', 'libvirt-utils.c'], libraries = [ "virt-lxc" ], @@ -133,7 +134,7 @@ class my_build(build): self.spawn([sys.executable, "generator.py", "libvirt", apis[0]]) self.spawn([sys.executable, "generator.py", "libvirt-qemu", apis[1]]) - if have_libvirt_lxc: + if have_libvirt_lxc(): self.spawn([sys.executable, "generator.py", "libvirt-lxc", apis[2]]) build.run(self) -- 1.8.5.3

If pkg-config isn't installed, or a too old libvirt, we can't even do 'python setup.py --help' without throwing an exception. Have the pkg-config checks and validation only throw an exception if being called from the 'build' step. https://bugzilla.redhat.com/show_bug.cgi?id=1074170 --- setup.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/setup.py b/setup.py index f137b22..592c30b 100755 --- a/setup.py +++ b/setup.py @@ -24,19 +24,24 @@ MIN_LIBVIRT_LXC = "1.0.2" if not os.path.exists("build"): os.mkdir("build") -pkgcfg = distutils.spawn.find_executable("pkg-config") - -if pkgcfg is None: - raise Exception("pkg-config binary is required to compile libvirt-python") - -spawn([pkgcfg, - "--print-errors", - "--atleast-version=%s" % MIN_LIBVIRT, - "libvirt"]) +_pkgcfg = -1 +def get_pkgcfg(do_fail=True): + global _pkgcfg + if _pkgcfg == -1: + _pkgcfg = distutils.spawn.find_executable("pkg-config") + if _pkgcfg is None and do_fail: + raise Exception("pkg-config binary is required to compile libvirt-python") + return _pkgcfg + +def check_minimum_libvirt_version(): + spawn([get_pkgcfg(), + "--print-errors", + "--atleast-version=%s" % MIN_LIBVIRT, + "libvirt"]) def have_libvirt_lxc(): try: - spawn([pkgcfg, + spawn([get_pkgcfg(), "--atleast-version=%s" % MIN_LIBVIRT_LXC, "libvirt"]) return True @@ -45,7 +50,7 @@ def have_libvirt_lxc(): def get_pkgconfig_data(args, mod, required=True): """Run pkg-config to and return content associated with it""" - f = os.popen("%s %s %s" % (pkgcfg, " ".join(args), mod)) + f = os.popen("%s %s %s" % (get_pkgcfg(), " ".join(args), mod)) line = f.readline() if line is not None: @@ -78,6 +83,9 @@ def get_module_lists(): Determine which modules we are actually building, and all their required config """ + if get_pkgcfg(do_fail=False) is None: + return [], [] + c_modules = [] py_modules = [] ldflags = get_pkgconfig_data(["--libs-only-L"], "libvirt", False) @@ -130,6 +138,7 @@ def get_module_lists(): class my_build(build): def run(self): + check_minimum_libvirt_version() apis = get_api_xml_files() self.spawn([sys.executable, "generator.py", "libvirt", apis[0]]) -- 1.8.5.3

On 03/25/2014 11:39 AM, Cole Robinson wrote:
This series reorganizes setup.py to not unconditionally call pkg-config. This will allow 'setup.py clean' and 'setup.py --help' to work correctly even if pkg-config or libvirt-devel aren't installed.
ACK series.
Cole Robinson (4): setup.py: Remove unused import setup.py: Move module list building to its own function setup.py: Make have_libvirt_lxc a function setup.py: Allow running --help or clean without pkg-config
setup.py | 144 +++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 85 insertions(+), 59 deletions(-)
-- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org

On 03/25/2014 04:54 PM, Eric Blake wrote:
On 03/25/2014 11:39 AM, Cole Robinson wrote:
This series reorganizes setup.py to not unconditionally call pkg-config. This will allow 'setup.py clean' and 'setup.py --help' to work correctly even if pkg-config or libvirt-devel aren't installed.
ACK series.
Thanks, pushed now. - Cole
participants (2)
-
Cole Robinson
-
Eric Blake