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 authors
.append(" " + line
.strip())
181 authors
.sort(key
=str.lower
)
183 f1
= open('AUTHORS.in', 'r')
184 f2
= open('AUTHORS', 'w')
186 f2
.write(line
.replace('@AUTHORS@', "\n".join(authors
)))
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')
196 m
= re
.match(r
'([a-f0-9]+):(\d+)\s(.*)', line
)
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)))
201 if re
.match(r
'Signed-off-by', line
):
203 f2
.write(" " + line
.strip() + "\n")
210 if not os
.path
.exists("build"):
213 if os
.path
.exists(".git"):
222 files
= ["libvirt-python.spec",
226 if os
.path
.exists(f
):
231 class my_rpm(Command
):
234 description
= "Build src and noarch rpms."
236 def initialize_options(self
):
239 def finalize_options(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
):
254 "base directory for build library"),
255 ('build-platlib=', None,
256 "build directory for platform-specific distributions"),
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
)
287 apis
= get_api_xml_files()
289 if "PYTHONPATH" in os
.environ
:
290 os
.environ
["PYTHONPATH"] = self
.build_platlib
+ ":" + os
.environ
["PYTHONPATH"]
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
):
301 if os
.path
.exists("build"):
309 _c_modules
, _py_modules
= get_module_lists()
311 setup(name
= 'libvirt-python',
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
,
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",