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
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"):
28 pkgcfg
= distutils
.spawn
.find_executable("pkg-config")
31 raise Exception("pkg-config binary is required to compile libvirt-python")
35 "--atleast-version=%s" % MIN_LIBVIRT
,
41 "--atleast-version=%s" % MIN_LIBVIRT_LXC
,
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
))
54 if line
is None or line
== "":
56 raise Exception("Cannot determine '%s' from libvirt pkg-config file" % " ".join(args
))
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)
82 module
= Extension('libvirtmod',
83 sources
= ['libvirt-override.c', 'build/libvirt.c', 'typewrappers.c', 'libvirt-utils.c'],
84 libraries
= [ "virt" ],
85 include_dirs
= [ "." ])
87 module
.extra_compile_args
.append(cflags
)
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
= [ "." ])
99 moduleqemu
.extra_compile_args
.append(cflags
)
101 moduleqemu
.extra_link_args
.append(ldflags
)
103 c_modules
.append(moduleqemu
)
104 py_modules
.append("libvirt_qemu")
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
= [ "." ])
112 modulelxc
.extra_compile_args
.append(cflags
)
114 modulelxc
.extra_link_args
.append(ldflags
)
116 c_modules
.append(modulelxc
)
117 py_modules
.append("libvirt_lxc")
120 class my_build(build
):
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]])
128 self
.spawn([sys
.executable
, "generator.py", "libvirt-lxc", apis
[2]])
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
):
139 sdist
.initialize_options(self
)
141 def finalize_options(self
):
142 if self
.snapshot
is not None:
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')
151 .replace('@PY_VERSION@', self
.distribution
.get_version())
152 .replace('@C_VERSION@', MIN_LIBVIRT
))
156 def gen_authors(self
):
157 f
= os
.popen("git log --pretty=format:'%aN <%aE>'")
160 authors
.append(" " + line
.strip())
162 authors
.sort(key
=str.lower
)
164 f1
= open('AUTHORS.in', 'r')
165 f2
= open('AUTHORS', 'w')
167 f2
.write(line
.replace('@AUTHORS@', "\n".join(authors
)))
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')
177 m
= re
.match(r
'([a-f0-9]+):(\d+)\s(.*)', line
)
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)))
182 if re
.match(r
'Signed-off-by', line
):
184 f2
.write(" " + line
.strip() + "\n")
191 if not os
.path
.exists("build"):
194 if os
.path
.exists(".git"):
203 files
= ["libvirt-python.spec",
207 if os
.path
.exists(f
):
212 class my_rpm(Command
):
215 description
= "Build src and noarch rpms."
217 def initialize_options(self
):
220 def finalize_options(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
):
235 "base directory for build library"),
236 ('build-platlib=', None,
237 "build directory for platform-specific distributions"),
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
)
268 apis
= get_api_xml_files()
270 self
.spawn([sys
.executable
, "sanitytest.py", self
.build_platlib
, apis
[0]])
273 class my_clean(clean
):
277 if os
.path
.exists("build"):
280 setup(name
= 'libvirt-python',
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
,