Release of libvirt-python-1.2.1
[libvirt-python/ericb.git] / setup.py
blobe0c0297e8daeaba0097dece8ad21443d59a49935
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 datetime
15 import os
16 import os.path
17 import re
18 import time
20 MIN_LIBVIRT = "0.9.11"
21 MIN_LIBVIRT_LXC = "1.0.2"
23 # Hack to stop 'pip install' failing with error
24 # about missing 'build' dir.
25 if not os.path.exists("build"):
26 os.mkdir("build")
28 pkgcfg = distutils.spawn.find_executable("pkg-config")
30 if pkgcfg is None:
31 raise Exception("pkg-config binary is required to compile libvirt-python")
33 spawn([pkgcfg,
34 "--print-errors",
35 "--atleast-version=%s" % MIN_LIBVIRT,
36 "libvirt"])
38 have_libvirt_lxc=True
39 try:
40 spawn([pkgcfg,
41 "--atleast-version=%s" % MIN_LIBVIRT_LXC,
42 "libvirt"])
43 except DistutilsExecError:
44 have_libvirt_lxc=False
46 def get_pkgconfig_data(args, mod, required=True):
47 """Run pkg-config to and return content associated with it"""
48 f = os.popen("%s %s %s" % (pkgcfg, " ".join(args), mod))
50 line = f.readline()
51 if line is not None:
52 line = line.strip()
54 if line is None or line == "":
55 if required:
56 raise Exception("Cannot determine '%s' from libvirt pkg-config file" % " ".join(args))
57 else:
58 return ""
60 return line
62 def get_api_xml_files():
63 """Check with pkg-config that libvirt is present and extract
64 the API XML file paths we need from it"""
66 libvirt_api = get_pkgconfig_data(["--variable", "libvirt_api"], "libvirt")
68 offset = libvirt_api.index("-api.xml")
69 libvirt_qemu_api = libvirt_api[0:offset] + "-qemu-api.xml"
71 offset = libvirt_api.index("-api.xml")
72 libvirt_lxc_api = libvirt_api[0:offset] + "-lxc-api.xml"
74 return (libvirt_api, libvirt_qemu_api, libvirt_lxc_api)
76 ldflags = get_pkgconfig_data(["--libs-only-L"], "libvirt", False)
77 cflags = get_pkgconfig_data(["--cflags"], "libvirt", False)
79 c_modules = []
80 py_modules = []
82 module = Extension('libvirtmod',
83 sources = ['libvirt-override.c', 'build/libvirt.c', 'typewrappers.c', 'libvirt-utils.c'],
84 libraries = [ "virt" ],
85 include_dirs = [ "." ])
86 if cflags != "":
87 module.extra_compile_args.append(cflags)
88 if ldflags != "":
89 module.extra_link_args.append(ldflags)
91 c_modules.append(module)
92 py_modules.append("libvirt")
94 moduleqemu = Extension('libvirtmod_qemu',
95 sources = ['libvirt-qemu-override.c', 'build/libvirt-qemu.c', 'typewrappers.c', 'libvirt-utils.c'],
96 libraries = [ "virt-qemu" ],
97 include_dirs = [ "." ])
98 if cflags != "":
99 moduleqemu.extra_compile_args.append(cflags)
100 if ldflags != "":
101 moduleqemu.extra_link_args.append(ldflags)
103 c_modules.append(moduleqemu)
104 py_modules.append("libvirt_qemu")
106 if have_libvirt_lxc:
107 modulelxc = Extension('libvirtmod_lxc',
108 sources = ['libvirt-lxc-override.c', 'build/libvirt-lxc.c', 'typewrappers.c', 'libvirt-utils.c'],
109 libraries = [ "virt-lxc" ],
110 include_dirs = [ "." ])
111 if cflags != "":
112 modulelxc.extra_compile_args.append(cflags)
113 if ldflags != "":
114 modulelxc.extra_link_args.append(ldflags)
116 c_modules.append(modulelxc)
117 py_modules.append("libvirt_lxc")
120 class my_build(build):
122 def run(self):
123 apis = get_api_xml_files()
125 self.spawn([sys.executable, "generator.py", "libvirt", apis[0]])
126 self.spawn([sys.executable, "generator.py", "libvirt-qemu", apis[1]])
127 if have_libvirt_lxc:
128 self.spawn([sys.executable, "generator.py", "libvirt-lxc", apis[2]])
130 build.run(self)
132 class my_sdist(sdist):
133 user_options = sdist.user_options
135 description = "Update libvirt-python.spec; build sdist-tarball."
137 def initialize_options(self):
138 self.snapshot = None
139 sdist.initialize_options(self)
141 def finalize_options(self):
142 if self.snapshot is not None:
143 self.snapshot = 1
144 sdist.finalize_options(self)
146 def gen_rpm_spec(self):
147 f1 = open('libvirt-python.spec.in', 'r')
148 f2 = open('libvirt-python.spec', 'w')
149 for line in f1:
150 f2.write(line
151 .replace('@PY_VERSION@', self.distribution.get_version())
152 .replace('@C_VERSION@', MIN_LIBVIRT))
153 f1.close()
154 f2.close()
156 def gen_authors(self):
157 f = os.popen("git log --pretty=format:'%aN <%aE>'")
158 authors = []
159 for line in f:
160 authors.append(" " + line.strip())
162 authors.sort(key=str.lower)
164 f1 = open('AUTHORS.in', 'r')
165 f2 = open('AUTHORS', 'w')
166 for line in f1:
167 f2.write(line.replace('@AUTHORS@', "\n".join(authors)))
168 f1.close()
169 f2.close()
172 def gen_changelog(self):
173 f1 = os.popen("git log '--pretty=format:%H:%ct %an <%ae>%n%n%s%n%b%n'")
174 f2 = open("ChangeLog", 'w')
176 for line in f1:
177 m = re.match(r'([a-f0-9]+):(\d+)\s(.*)', line)
178 if m:
179 t = time.gmtime(int(m.group(2)))
180 f2.write("%04d-%02d-%02d %s\n" % (t.tm_year, t.tm_mon, t.tm_mday, m.group(3)))
181 else:
182 if re.match(r'Signed-off-by', line):
183 continue
184 f2.write(" " + line.strip() + "\n")
186 f1.close()
187 f2.close()
190 def run(self):
191 if not os.path.exists("build"):
192 os.mkdir("build")
194 if os.path.exists(".git"):
195 try:
196 self.gen_rpm_spec()
197 self.gen_authors()
198 self.gen_changelog()
200 sdist.run(self)
202 finally:
203 files = ["libvirt-python.spec",
204 "AUTHORS",
205 "ChangeLog"]
206 for f in files:
207 if os.path.exists(f):
208 os.unlink(f)
209 else:
210 sdist.run(self)
212 class my_rpm(Command):
213 user_options = []
215 description = "Build src and noarch rpms."
217 def initialize_options(self):
218 pass
220 def finalize_options(self):
221 pass
223 def run(self):
225 Run sdist, then 'rpmbuild' the tar.gz
228 self.run_command('sdist')
229 os.system('rpmbuild -ta --clean dist/libvirt-python-%s.tar.gz' %
230 self.distribution.get_version())
232 class my_test(Command):
233 user_options = [
234 ('build-base=', 'b',
235 "base directory for build library"),
236 ('build-platlib=', None,
237 "build directory for platform-specific distributions"),
238 ('plat-name=', 'p',
239 "platform name to build for, if supported "
240 "(default: %s)" % get_platform()),
243 description = "Run test suite."
245 def initialize_options(self):
246 self.build_base = 'build'
247 self.build_platlib = None
248 self.plat_name = None
250 def finalize_options(self):
251 if self.plat_name is None:
252 self.plat_name = get_platform()
254 plat_specifier = ".%s-%s" % (self.plat_name, sys.version[0:3])
256 if hasattr(sys, 'gettotalrefcount'):
257 plat_specifier += '-pydebug'
259 if self.build_platlib is None:
260 self.build_platlib = os.path.join(self.build_base,
261 'lib' + plat_specifier)
263 def run(self):
265 Run test suite
268 apis = get_api_xml_files()
270 self.spawn([sys.executable, "sanitytest.py", self.build_platlib, apis[0]])
273 class my_clean(clean):
274 def run(self):
275 clean.run(self)
277 if os.path.exists("build"):
278 remove_tree("build")
280 setup(name = 'libvirt-python',
281 version = '1.2.1',
282 url = 'http://www.libvirt.org',
283 maintainer = 'Libvirt Maintainers',
284 maintainer_email = 'libvir-list@redhat.com',
285 description = 'The libvirt virtualization API',
286 ext_modules = c_modules,
287 py_modules = py_modules,
288 package_dir = {
289 '': 'build'
291 cmdclass = {
292 'build': my_build,
293 'clean': my_clean,
294 'sdist': my_sdist,
295 'rpm': my_rpm,
296 'test': my_test