3 from distutils
.core
import setup
, Extension
, Command
4 from distutils
.command
.build
import build
5 from distutils
.command
.clean
import clean
6 from distutils
.command
.sdist
import sdist
7 from distutils
.dir_util
import remove_tree
8 from distutils
.util
import get_platform
9 from distutils
.spawn
import spawn
10 from distutils
.errors
import DistutilsExecError
19 MIN_LIBVIRT
= "0.9.11"
20 MIN_LIBVIRT_LXC
= "1.0.2"
22 # Hack to stop 'pip install' failing with error
23 # about missing 'build' dir.
24 if not os
.path
.exists("build"):
28 def get_pkgcfg(do_fail
=True):
31 _pkgcfg
= distutils
.spawn
.find_executable("pkg-config")
32 if _pkgcfg
is None and do_fail
:
33 raise Exception("pkg-config binary is required to compile libvirt-python")
36 def check_minimum_libvirt_version():
39 "--atleast-version=%s" % MIN_LIBVIRT
,
42 def have_libvirt_lxc():
45 "--atleast-version=%s" % MIN_LIBVIRT_LXC
,
48 except DistutilsExecError
:
51 def get_pkgconfig_data(args
, mod
, required
=True):
52 """Run pkg-config to and return content associated with it"""
53 f
= os
.popen("%s %s %s" % (get_pkgcfg(), " ".join(args
), mod
))
59 if line
is None or line
== "":
61 raise Exception("Cannot determine '%s' from libvirt pkg-config file" % " ".join(args
))
67 def get_api_xml_files():
68 """Check with pkg-config that libvirt is present and extract
69 the API XML file paths we need from it"""
71 libvirt_api
= get_pkgconfig_data(["--variable", "libvirt_api"], "libvirt")
73 offset
= libvirt_api
.index("-api.xml")
74 libvirt_qemu_api
= libvirt_api
[0:offset
] + "-qemu-api.xml"
76 offset
= libvirt_api
.index("-api.xml")
77 libvirt_lxc_api
= libvirt_api
[0:offset
] + "-lxc-api.xml"
79 return (libvirt_api
, libvirt_qemu_api
, libvirt_lxc_api
)
81 def get_module_lists():
83 Determine which modules we are actually building, and all their
86 if get_pkgcfg(do_fail
=False) is None:
91 ldflags
= get_pkgconfig_data(["--libs-only-L"], "libvirt", False)
92 cflags
= get_pkgconfig_data(["--cflags"], "libvirt", False)
94 module
= Extension('libvirtmod',
95 sources
= ['libvirt-override.c', 'build/libvirt.c', 'typewrappers.c', 'libvirt-utils.c'],
96 libraries
= [ "virt" ],
97 include_dirs
= [ "." ])
99 module
.extra_compile_args
.append(cflags
)
101 module
.extra_link_args
.append(ldflags
)
103 c_modules
.append(module
)
104 py_modules
.append("libvirt")
106 moduleqemu
= Extension('libvirtmod_qemu',
107 sources
= ['libvirt-qemu-override.c', 'build/libvirt-qemu.c', 'typewrappers.c', 'libvirt-utils.c'],
108 libraries
= [ "virt-qemu" ],
109 include_dirs
= [ "." ])
111 moduleqemu
.extra_compile_args
.append(cflags
)
113 moduleqemu
.extra_link_args
.append(ldflags
)
115 c_modules
.append(moduleqemu
)
116 py_modules
.append("libvirt_qemu")
118 if have_libvirt_lxc():
119 modulelxc
= Extension('libvirtmod_lxc',
120 sources
= ['libvirt-lxc-override.c', 'build/libvirt-lxc.c', 'typewrappers.c', 'libvirt-utils.c'],
121 libraries
= [ "virt-lxc" ],
122 include_dirs
= [ "." ])
124 modulelxc
.extra_compile_args
.append(cflags
)
126 modulelxc
.extra_link_args
.append(ldflags
)
128 c_modules
.append(modulelxc
)
129 py_modules
.append("libvirt_lxc")
131 return c_modules
, py_modules
138 class my_build(build
):
141 check_minimum_libvirt_version()
142 apis
= get_api_xml_files()
144 self
.spawn([sys
.executable
, "generator.py", "libvirt", apis
[0]])
145 self
.spawn([sys
.executable
, "generator.py", "libvirt-qemu", apis
[1]])
146 if have_libvirt_lxc():
147 self
.spawn([sys
.executable
, "generator.py", "libvirt-lxc", apis
[2]])
151 class my_sdist(sdist
):
152 user_options
= sdist
.user_options
154 description
= "Update libvirt-python.spec; build sdist-tarball."
156 def initialize_options(self
):
158 sdist
.initialize_options(self
)
160 def finalize_options(self
):
161 if self
.snapshot
is not None:
163 sdist
.finalize_options(self
)
165 def gen_rpm_spec(self
):
166 f1
= open('libvirt-python.spec.in', 'r')
167 f2
= open('libvirt-python.spec', 'w')
170 .replace('@PY_VERSION@', self
.distribution
.get_version())
171 .replace('@C_VERSION@', MIN_LIBVIRT
))
175 def gen_authors(self
):
176 f
= os
.popen("git log --pretty=format:'%aN <%aE>'")
179 line
= " " + line
.strip()
180 if line
not in authors
:
183 authors
.sort(key
=str.lower
)
185 f1
= open('AUTHORS.in', 'r')
186 f2
= open('AUTHORS', 'w')
188 f2
.write(line
.replace('@AUTHORS@', "\n".join(authors
)))
193 def gen_changelog(self
):
194 f1
= os
.popen("git log '--pretty=format:%H:%ct %an <%ae>%n%n%s%n%b%n'")
195 f2
= open("ChangeLog", 'w')
198 m
= re
.match(r
'([a-f0-9]+):(\d+)\s(.*)', line
)
200 t
= time
.gmtime(int(m
.group(2)))
201 f2
.write("%04d-%02d-%02d %s\n" % (t
.tm_year
, t
.tm_mon
, t
.tm_mday
, m
.group(3)))
203 if re
.match(r
'Signed-off-by', line
):
205 f2
.write(" " + line
.strip() + "\n")
212 if not os
.path
.exists("build"):
215 if os
.path
.exists(".git"):
224 files
= ["libvirt-python.spec",
228 if os
.path
.exists(f
):
233 class my_rpm(Command
):
236 description
= "Build src and noarch rpms."
238 def initialize_options(self
):
241 def finalize_options(self
):
246 Run sdist, then 'rpmbuild' the tar.gz
249 self
.run_command('sdist')
250 self
.spawn(["/usr/bin/rpmbuild", "-ta", "--clean",
251 "dist/libvirt-python-%s.tar.gz" % self
.distribution
.get_version()])
253 class my_test(Command
):
256 "base directory for build library"),
257 ('build-platlib=', None,
258 "build directory for platform-specific distributions"),
260 "platform name to build for, if supported "
261 "(default: %s)" % get_platform()),
264 description
= "Run test suite."
266 def initialize_options(self
):
267 self
.build_base
= 'build'
268 self
.build_platlib
= None
269 self
.plat_name
= None
271 def finalize_options(self
):
272 if self
.plat_name
is None:
273 self
.plat_name
= get_platform()
275 plat_specifier
= ".%s-%s" % (self
.plat_name
, sys
.version
[0:3])
277 if hasattr(sys
, 'gettotalrefcount'):
278 plat_specifier
+= '-pydebug'
280 if self
.build_platlib
is None:
281 self
.build_platlib
= os
.path
.join(self
.build_base
,
282 'lib' + plat_specifier
)
289 apis
= get_api_xml_files()
291 if "PYTHONPATH" in os
.environ
:
292 os
.environ
["PYTHONPATH"] = self
.build_platlib
+ ":" + os
.environ
["PYTHONPATH"]
294 os
.environ
["PYTHONPATH"] = self
.build_platlib
295 self
.spawn([sys
.executable
, "sanitytest.py", self
.build_platlib
, apis
[0]])
296 self
.spawn([sys
.executable
, "/usr/bin/nosetests"])
299 class my_clean(clean
):
303 if os
.path
.exists("build"):
311 _c_modules
, _py_modules
= get_module_lists()
313 setup(name
= 'libvirt-python',
315 url
= 'http://www.libvirt.org',
316 maintainer
= 'Libvirt Maintainers',
317 maintainer_email
= 'libvir-list@redhat.com',
318 description
= 'The libvirt virtualization API',
319 ext_modules
= _c_modules
,
320 py_modules
= _py_modules
,
332 "Development Status :: 5 - Production/Stable",
333 "Intended Audience :: Developers",
334 "License :: OSI Approved :: GNU Lesser General Public License v2 or later (LGPLv2+)",
335 "Programming Language :: Python",
336 "Programming Language :: Python :: 2",
337 "Programming Language :: Python :: 3",