libvirt-python: add classifiers to setup.py
[libvirt-python/ericb.git] / setup.py
bloba8e9e862925a4928bc07d1fac140bf6f19267428
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 authors.append(" " + line.strip())
181 authors.sort(key=str.lower)
183 f1 = open('AUTHORS.in', 'r')
184 f2 = open('AUTHORS', 'w')
185 for line in f1:
186 f2.write(line.replace('@AUTHORS@', "\n".join(authors)))
187 f1.close()
188 f2.close()
191 def gen_changelog(self):
192 f1 = os.popen("git log '--pretty=format:%H:%ct %an <%ae>%n%n%s%n%b%n'")
193 f2 = open("ChangeLog", 'w')
195 for line in f1:
196 m = re.match(r'([a-f0-9]+):(\d+)\s(.*)', line)
197 if m:
198 t = time.gmtime(int(m.group(2)))
199 f2.write("%04d-%02d-%02d %s\n" % (t.tm_year, t.tm_mon, t.tm_mday, m.group(3)))
200 else:
201 if re.match(r'Signed-off-by', line):
202 continue
203 f2.write(" " + line.strip() + "\n")
205 f1.close()
206 f2.close()
209 def run(self):
210 if not os.path.exists("build"):
211 os.mkdir("build")
213 if os.path.exists(".git"):
214 try:
215 self.gen_rpm_spec()
216 self.gen_authors()
217 self.gen_changelog()
219 sdist.run(self)
221 finally:
222 files = ["libvirt-python.spec",
223 "AUTHORS",
224 "ChangeLog"]
225 for f in files:
226 if os.path.exists(f):
227 os.unlink(f)
228 else:
229 sdist.run(self)
231 class my_rpm(Command):
232 user_options = []
234 description = "Build src and noarch rpms."
236 def initialize_options(self):
237 pass
239 def finalize_options(self):
240 pass
242 def run(self):
244 Run sdist, then 'rpmbuild' the tar.gz
247 self.run_command('sdist')
248 self.spawn(["/usr/bin/rpmbuild", "-ta", "--clean",
249 "dist/libvirt-python-%s.tar.gz" % self.distribution.get_version()])
251 class my_test(Command):
252 user_options = [
253 ('build-base=', 'b',
254 "base directory for build library"),
255 ('build-platlib=', None,
256 "build directory for platform-specific distributions"),
257 ('plat-name=', 'p',
258 "platform name to build for, if supported "
259 "(default: %s)" % get_platform()),
262 description = "Run test suite."
264 def initialize_options(self):
265 self.build_base = 'build'
266 self.build_platlib = None
267 self.plat_name = None
269 def finalize_options(self):
270 if self.plat_name is None:
271 self.plat_name = get_platform()
273 plat_specifier = ".%s-%s" % (self.plat_name, sys.version[0:3])
275 if hasattr(sys, 'gettotalrefcount'):
276 plat_specifier += '-pydebug'
278 if self.build_platlib is None:
279 self.build_platlib = os.path.join(self.build_base,
280 'lib' + plat_specifier)
282 def run(self):
284 Run test suite
287 apis = get_api_xml_files()
289 if "PYTHONPATH" in os.environ:
290 os.environ["PYTHONPATH"] = self.build_platlib + ":" + os.environ["PYTHONPATH"]
291 else:
292 os.environ["PYTHONPATH"] = self.build_platlib
293 self.spawn([sys.executable, "sanitytest.py", self.build_platlib, apis[0]])
294 self.spawn([sys.executable, "/usr/bin/nosetests"])
297 class my_clean(clean):
298 def run(self):
299 clean.run(self)
301 if os.path.exists("build"):
302 remove_tree("build")
305 ##################
306 # Invoke setup() #
307 ##################
309 _c_modules, _py_modules = get_module_lists()
311 setup(name = 'libvirt-python',
312 version = '1.2.15',
313 url = 'http://www.libvirt.org',
314 maintainer = 'Libvirt Maintainers',
315 maintainer_email = 'libvir-list@redhat.com',
316 description = 'The libvirt virtualization API',
317 ext_modules = _c_modules,
318 py_modules = _py_modules,
319 package_dir = {
320 '': 'build'
322 cmdclass = {
323 'build': my_build,
324 'clean': my_clean,
325 'sdist': my_sdist,
326 'rpm': my_rpm,
327 'test': my_test
329 classifiers = [
330 "Development Status :: 5 - Production/Stable",
331 "Intended Audience :: Developers",
332 "License :: OSI Approved :: GNU Lesser General Public License v2 or later (LGPLv2+)",
333 "Programming Language :: Python",
334 "Programming Language :: Python :: 2",
335 "Programming Language :: Python :: 3",