9 from pathlib
import Path
10 from setuptools
import setup
, Extension
11 from setuptools
.command
.build_ext
import build_ext
12 from setuptools
.command
.build_py
import build_py
13 from setuptools
.command
.sdist
import sdist
17 with
open("VERSION") as f
:
18 return f
.read().strip()
23 proc
= subprocess
.run(["pkg-config", "--version"],
24 stdout
=subprocess
.DEVNULL
,
25 stderr
=subprocess
.DEVNULL
)
26 if proc
.returncode
!= 0:
27 print("pkg-config binary does not appear to be functional")
29 except FileNotFoundError
:
30 print("pkg-config binary is required to compile libvirt-python")
34 def check_minimum_libvirt_version():
35 subprocess
.check_call(["pkg-config",
37 f
"--atleast-version={MIN_LIBVIRT}",
41 def have_libvirt_lxc():
42 proc
= subprocess
.run(["pkg-config",
43 f
"--atleast-version={MIN_LIBVIRT_LXC}",
45 if proc
.returncode
== 0:
50 def get_pkgconfig_data(args
, mod
, required
=True):
51 """Run pkg-config to and return content associated with it"""
53 cmd
= ["pkg-config"] + args
+ [mod
]
54 output
= subprocess
.check_output(cmd
, universal_newlines
=True)
55 for line
in output
.splitlines():
58 args_str
= " ".join(args
)
59 raise Exception(f
"Cannot determine '{args_str}' from "
60 "libvirt pkg-config file")
65 def get_api_xml_files():
66 """Check with pkg-config that libvirt is present and extract
67 the API XML file paths we need from it"""
69 libvirt_api
= get_pkgconfig_data(["--variable", "libvirt_api"], "libvirt")
71 offset
= libvirt_api
.index("-api.xml")
72 libvirt_qemu_api
= libvirt_api
[0:offset
] + "-qemu-api.xml"
74 offset
= libvirt_api
.index("-api.xml")
75 libvirt_lxc_api
= libvirt_api
[0:offset
] + "-lxc-api.xml"
77 return (libvirt_api
, libvirt_qemu_api
, libvirt_lxc_api
)
80 def get_module_lists():
82 Determine which modules we are actually building, and all their
87 ldflags
= get_pkgconfig_data(["--libs-only-L"], "libvirt", False).split()
88 cflags
= get_pkgconfig_data(["--cflags"], "libvirt", False).split()
91 cflags
+= ["-Wp,-DPy_LIMITED_API=0x03060000"]
93 module
= Extension("libvirtmod",
102 module
.extra_compile_args
.extend(cflags
)
103 module
.extra_link_args
.extend(ldflags
)
105 c_modules
.append(module
)
106 py_modules
.append("libvirt")
108 moduleqemu
= Extension("libvirtmod_qemu",
110 "libvirt-qemu-override.c",
111 "build/libvirt-qemu.c",
115 libraries
=["virt-qemu", "virt"],
117 moduleqemu
.extra_compile_args
.extend(cflags
)
118 moduleqemu
.extra_link_args
.extend(ldflags
)
120 c_modules
.append(moduleqemu
)
121 py_modules
.append("libvirt_qemu")
123 if have_libvirt_lxc():
124 modulelxc
= Extension("libvirtmod_lxc",
126 "libvirt-lxc-override.c",
127 "build/libvirt-lxc.c",
131 libraries
=["virt-lxc", "virt"],
133 modulelxc
.extra_compile_args
.extend(cflags
)
134 modulelxc
.extra_link_args
.extend(ldflags
)
136 c_modules
.append(modulelxc
)
137 py_modules
.append("libvirt_lxc")
139 py_modules
.append("libvirtaio")
141 return c_modules
, py_modules
148 class my_build_ext(build_ext
):
151 check_minimum_libvirt_version()
152 apis
= get_api_xml_files()
154 subprocess
.check_call([sys
.executable
, "generator.py", "libvirt", apis
[0], "c"])
155 subprocess
.check_call([sys
.executable
, "generator.py", "libvirt-qemu", apis
[1], "c"])
156 if have_libvirt_lxc():
157 subprocess
.check_call([sys
.executable
, "generator.py", "libvirt-lxc", apis
[2], "c"])
162 class my_build_py(build_py
):
165 check_minimum_libvirt_version()
166 apis
= get_api_xml_files()
168 subprocess
.check_call([sys
.executable
, "generator.py", "libvirt", apis
[0], "py"])
169 subprocess
.check_call([sys
.executable
, "generator.py", "libvirt-qemu", apis
[1], "py"])
170 if have_libvirt_lxc():
171 subprocess
.check_call([sys
.executable
, "generator.py", "libvirt-lxc", apis
[2], "py"])
172 shutil
.copy("libvirtaio.py", "build")
177 class my_sdist(sdist
):
178 user_options
= sdist
.user_options
180 description
= "Update libvirt-python.spec; build sdist-tarball."
182 def initialize_options(self
):
184 sdist
.initialize_options(self
)
186 def finalize_options(self
):
187 if self
.snapshot
is not None:
189 sdist
.finalize_options(self
)
192 def _gen_from_in(file_in
, file_out
, replace_pattern
, replace
):
193 with
open(file_in
) as f_in
, open(file_out
, "w") as f_out
:
195 f_out
.write(line
.replace(replace_pattern
, replace
))
197 def gen_rpm_spec(self
):
198 return self
._gen
_from
_in
("libvirt-python.spec.in",
199 "libvirt-python.spec",
203 def gen_authors(self
):
205 cmd
= ["git", "log", "--pretty=format:%aN <%aE>"]
206 output
= subprocess
.check_output(cmd
, universal_newlines
=True)
207 git_authors
= {line
.strip() for line
in output
.splitlines()}
208 authors
= sorted(git_authors
, key
=str.lower
)
209 authors
= [" " + author
for author
in authors
]
210 self
._gen
_from
_in
("AUTHORS.in",
215 def gen_changelog(self
):
216 cmd
= ["git", "log", "--pretty=format:%H:%ct %an <%ae>%n%n%s%n%b%n"]
217 with
open("ChangeLog", "w") as f_out
:
218 output
= subprocess
.check_output(cmd
, universal_newlines
=True)
219 for line
in output
.splitlines():
220 m
= re
.match(r
"([a-f0-9]+):(\d+)\s(.*)", line
)
222 t
= time
.gmtime(int(m
.group(2)))
223 fmt
= "{: 04d}-{: 02d}-{: 02d} {}\n"
224 f_out
.write(fmt
.format(t
.tm_year
, t
.tm_mon
, t
.tm_mday
, m
.group(3)))
226 if re
.match(r
"Signed-off-by", line
):
228 f_out
.write(" " + line
.strip() + "\n")
231 if Path(".git").exists():
241 "libvirt-python.spec",
248 except FileNotFoundError
:
258 if sys
.version_info
< (3, 6):
259 print("libvirt-python requires Python >= 3.6 to build")
262 MIN_LIBVIRT
= "0.9.11"
263 MIN_LIBVIRT_LXC
= "1.0.2"
265 # Hack to stop "pip install" failing with error
266 # about missing "build" dir.
267 Path("build").mkdir(exist_ok
=True)
271 _c_modules
, _py_modules
= get_module_lists()
274 ext_modules
=_c_modules
,
275 py_modules
=_py_modules
,
280 "build_ext": my_build_ext
,
281 "build_py": my_build_py
,