Release of libvirt-python-1.2.20
[libvirt-python/ericb.git] / setup.py
blob0afcaa49219b3da16095ac197feced158a15880b
1 #!/usr/bin/python
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
11 import distutils
13 import sys
14 import os
15 import os.path
16 import re
17 import time
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"):
25 os.mkdir("build")
27 _pkgcfg = -1
28 def get_pkgcfg(do_fail=True):
29 global _pkgcfg
30 if _pkgcfg == -1:
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")
34 return _pkgcfg
36 def check_minimum_libvirt_version():
37 spawn([get_pkgcfg(),
38 "--print-errors",
39 "--atleast-version=%s" % MIN_LIBVIRT,
40 "libvirt"])
42 def have_libvirt_lxc():
43 try:
44 spawn([get_pkgcfg(),
45 "--atleast-version=%s" % MIN_LIBVIRT_LXC,
46 "libvirt"])
47 return True
48 except DistutilsExecError:
49 return False
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))
55 line = f.readline()
56 if line is not None:
57 line = line.strip()
59 if line is None or line == "":
60 if required:
61 raise Exception("Cannot determine '%s' from libvirt pkg-config file" % " ".join(args))
62 else:
63 return ""
65 return line
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():
82 """
83 Determine which modules we are actually building, and all their
84 required config
85 """
86 if get_pkgcfg(do_fail=False) is None:
87 return [], []
89 c_modules = []
90 py_modules = []
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 = [ "." ])
98 if cflags != "":
99 module.extra_compile_args.append(cflags)
100 if ldflags != "":
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 = [ "." ])
110 if cflags != "":
111 moduleqemu.extra_compile_args.append(cflags)
112 if ldflags != "":
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 = [ "." ])
123 if cflags != "":
124 modulelxc.extra_compile_args.append(cflags)
125 if ldflags != "":
126 modulelxc.extra_link_args.append(ldflags)
128 c_modules.append(modulelxc)
129 py_modules.append("libvirt_lxc")
131 return c_modules, py_modules
134 ###################
135 # Custom commands #
136 ###################
138 class my_build(build):
140 def run(self):
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]])
149 build.run(self)
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):
157 self.snapshot = None
158 sdist.initialize_options(self)
160 def finalize_options(self):
161 if self.snapshot is not None:
162 self.snapshot = 1
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')
168 for line in f1:
169 f2.write(line
170 .replace('@PY_VERSION@', self.distribution.get_version())
171 .replace('@C_VERSION@', MIN_LIBVIRT))
172 f1.close()
173 f2.close()
175 def gen_authors(self):
176 f = os.popen("git log --pretty=format:'%aN <%aE>'")
177 authors = []
178 for line in f:
179 line = " " + line.strip()
180 if line not in authors:
181 authors.append(line)
183 authors.sort(key=str.lower)
185 f1 = open('AUTHORS.in', 'r')
186 f2 = open('AUTHORS', 'w')
187 for line in f1:
188 f2.write(line.replace('@AUTHORS@', "\n".join(authors)))
189 f1.close()
190 f2.close()
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')
197 for line in f1:
198 m = re.match(r'([a-f0-9]+):(\d+)\s(.*)', line)
199 if m:
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)))
202 else:
203 if re.match(r'Signed-off-by', line):
204 continue
205 f2.write(" " + line.strip() + "\n")
207 f1.close()
208 f2.close()
211 def run(self):
212 if not os.path.exists("build"):
213 os.mkdir("build")
215 if os.path.exists(".git"):
216 try:
217 self.gen_rpm_spec()
218 self.gen_authors()
219 self.gen_changelog()
221 sdist.run(self)
223 finally:
224 files = ["libvirt-python.spec",
225 "AUTHORS",
226 "ChangeLog"]
227 for f in files:
228 if os.path.exists(f):
229 os.unlink(f)
230 else:
231 sdist.run(self)
233 class my_rpm(Command):
234 user_options = []
236 description = "Build src and noarch rpms."
238 def initialize_options(self):
239 pass
241 def finalize_options(self):
242 pass
244 def run(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):
254 user_options = [
255 ('build-base=', 'b',
256 "base directory for build library"),
257 ('build-platlib=', None,
258 "build directory for platform-specific distributions"),
259 ('plat-name=', 'p',
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)
284 def run(self):
286 Run test suite
289 apis = get_api_xml_files()
291 if "PYTHONPATH" in os.environ:
292 os.environ["PYTHONPATH"] = self.build_platlib + ":" + os.environ["PYTHONPATH"]
293 else:
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):
300 def run(self):
301 clean.run(self)
303 if os.path.exists("build"):
304 remove_tree("build")
307 ##################
308 # Invoke setup() #
309 ##################
311 _c_modules, _py_modules = get_module_lists()
313 setup(name = 'libvirt-python',
314 version = '1.2.20',
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,
321 package_dir = {
322 '': 'build'
324 cmdclass = {
325 'build': my_build,
326 'clean': my_clean,
327 'sdist': my_sdist,
328 'rpm': my_rpm,
329 'test': my_test
331 classifiers = [
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",