Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathsetup.py
More file actions
Latest commit
executable file
·305 lines (242 loc) · 10 KB
/
Copy pathsetup.py
File metadata and controls
executable file
·305 lines (242 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python3
importsys
importre
importshutil
importsubprocess
importsysconfig
importtime
frompathlibimportPath
fromsetuptoolsimportsetup, Extension
fromsetuptools.command.build_extimportbuild_ext
fromsetuptools.command.build_pyimportbuild_py
fromsetuptools.command.sdistimportsdist
defgetVersion():
withopen("VERSION") asf:
returnf.read().strip()
defcheck_pkgcfg():
try:
proc=subprocess.run(["pkg-config", "--version"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
ifproc.returncode!=0:
print("pkg-config binary does not appear to be functional")
sys.exit(1)
exceptFileNotFoundError:
print("pkg-config binary is required to compile libvirt-python")
sys.exit(1)
defcheck_minimum_libvirt_version():
subprocess.check_call(["pkg-config",
"--print-errors",
f"--atleast-version={MIN_LIBVIRT}",
"libvirt"])
defhave_libvirt_lxc():
proc=subprocess.run(["pkg-config",
f"--atleast-version={MIN_LIBVIRT_LXC}",
"libvirt"])
ifproc.returncode==0:
returnTrue
returnFalse
defget_pkgconfig_data(args, mod, required=True):
"""Run pkg-config to and return content associated with it"""
cmd= ["pkg-config"] +args+ [mod]
output=subprocess.check_output(cmd, universal_newlines=True)
forlineinoutput.splitlines():
ifline=="":
ifrequired:
args_str=" ".join(args)
raiseException(f"Cannot determine '{args_str}' from "
"libvirt pkg-config file")
line=""
returnline.strip()
defget_api_xml_files():
"""Check with pkg-config that libvirt is present and extract
the API XML file paths we need from it"""
libvirt_api=get_pkgconfig_data(["--variable", "libvirt_api"], "libvirt")
offset=libvirt_api.index("-api.xml")
libvirt_qemu_api=libvirt_api[0:offset] +"-qemu-api.xml"
offset=libvirt_api.index("-api.xml")
libvirt_lxc_api=libvirt_api[0:offset] +"-lxc-api.xml"
return (libvirt_api, libvirt_qemu_api, libvirt_lxc_api)
defget_module_lists():
"""
Determine which modules we are actually building, and all their
required config
"""
c_modules= []
py_modules= []
ldflags=get_pkgconfig_data(["--libs-only-L"], "libvirt", False).split()
cflags=get_pkgconfig_data(["--cflags"], "libvirt", False).split()
cflags+= ["-Ibuild"]
# The limited API / stable ABI (abi3) is not supported in free-threaded
# Python versions: https://docs.python.org/3/c-api/stable.html#c.Py_LIMITED_API
# So the only way to build free-threaded Python modules is to drop abi3 support
# and use the legacy API instead.
#
# Platform | Python | C flags added | Result
# -------------+---------------------+-------------------------------------+-----------------------
# Linux | python3.13/3.14 | Py_LIMITED_API=0x03060000 | Stable ABI (abi3)
# Windows | python3.13/3.14 | Py_LIMITED_API=0x03060000 | Stable ABI (abi3)
# Linux | python3.13t/3.14t | none | Version-specific API
# Windows | python3.13t/3.14t | Py_GIL_DISABLED=1 | Version-specific API
# CPython 3.15+ will be compatible with abi3t ABI (from PEP-0803).
ifsysconfig.get_config_var("Py_GIL_DISABLED"):
ifsys.platform=="win32":
cflags+= ["-DPy_GIL_DISABLED=1"]
else:
cflags+= ["-Wp,-DPy_LIMITED_API=0x03060000"]
module=Extension("libvirtmod",
sources=[
"libvirt-override.c",
"build/libvirt.c",
"typewrappers.c",
"libvirt-utils.c"
],
libraries=["virt"],
include_dirs=["."])
module.extra_compile_args.extend(cflags)
module.extra_link_args.extend(ldflags)
c_modules.append(module)
py_modules.append("libvirt")
moduleqemu=Extension("libvirtmod_qemu",
sources=[
"libvirt-qemu-override.c",
"build/libvirt-qemu.c",
"typewrappers.c",
"libvirt-utils.c"
],
libraries=["virt-qemu", "virt"],
include_dirs=["."])
moduleqemu.extra_compile_args.extend(cflags)
moduleqemu.extra_link_args.extend(ldflags)
c_modules.append(moduleqemu)
py_modules.append("libvirt_qemu")
ifhave_libvirt_lxc():
modulelxc=Extension("libvirtmod_lxc",
sources=[
"libvirt-lxc-override.c",
"build/libvirt-lxc.c",
"typewrappers.c",
"libvirt-utils.c"
],
libraries=["virt-lxc", "virt"],
include_dirs=["."])
modulelxc.extra_compile_args.extend(cflags)
modulelxc.extra_link_args.extend(ldflags)
c_modules.append(modulelxc)
py_modules.append("libvirt_lxc")
py_modules.append("libvirtaio")
returnc_modules, py_modules
###################
# Custom commands #
###################
classmy_build_ext(build_ext):
defrun(self):
check_minimum_libvirt_version()
apis=get_api_xml_files()
subprocess.check_call([sys.executable, "generator.py", "libvirt", apis[0], "c"])
subprocess.check_call([sys.executable, "generator.py", "libvirt-qemu", apis[1], "c"])
ifhave_libvirt_lxc():
subprocess.check_call([sys.executable, "generator.py", "libvirt-lxc", apis[2], "c"])
build_ext.run(self)
classmy_build_py(build_py):
defrun(self):
check_minimum_libvirt_version()
apis=get_api_xml_files()
subprocess.check_call([sys.executable, "generator.py", "libvirt", apis[0], "py"])
subprocess.check_call([sys.executable, "generator.py", "libvirt-qemu", apis[1], "py"])
ifhave_libvirt_lxc():
subprocess.check_call([sys.executable, "generator.py", "libvirt-lxc", apis[2], "py"])
shutil.copy("libvirtaio.py", "build")
build_py.run(self)
classmy_sdist(sdist):
user_options=sdist.user_options
description="Update libvirt-python.spec; build sdist-tarball."
definitialize_options(self):
self.snapshot=None
sdist.initialize_options(self)
deffinalize_options(self):
ifself.snapshotisnotNone:
self.snapshot=1
sdist.finalize_options(self)
@staticmethod
def_gen_from_in(file_in, file_out, replacements):
withopen(file_in) asf_in, open(file_out, "w") asf_out:
forlineinf_in:
forpattern, replaceinreplacements.items():
line=line.replace(pattern, replace)
f_out.write(line)
defgen_rpm_spec(self):
full_name=self.distribution.get_fullname()
dist_name=full_name[:full_name.rfind("-")]
returnself._gen_from_in("libvirt-python.spec.in",
"libvirt-python.spec",
{"@VERSION@": getVersion(),
"@DIST_NAME@": dist_name})
defgen_authors(self):
cmd= ["git", "log", "--pretty=format:%aN <%aE>"]
output=subprocess.check_output(cmd, universal_newlines=True)
git_authors= {line.strip() forlineinoutput.splitlines()}
authors=sorted(git_authors, key=str.lower)
authors= [" "+authorforauthorinauthors]
self._gen_from_in("AUTHORS.in",
"AUTHORS",
{"@AUTHORS@": "\n".join(authors)})
defgen_changelog(self):
cmd= ["git", "log", "--pretty=format:%H:%ct %an <%ae>%n%n%s%n%b%n"]
withopen("ChangeLog", "w") asf_out:
output=subprocess.check_output(cmd, universal_newlines=True)
forlineinoutput.splitlines():
m=re.match(r"([a-f0-9]+):(\d+)\s(.*)", line)
ifm:
t=time.gmtime(int(m.group(2)))
fmt="{: 04d}-{: 02d}-{: 02d} {}\n"
f_out.write(fmt.format(t.tm_year, t.tm_mon, t.tm_mday, m.group(3)))
else:
ifre.match(r"Signed-off-by", line):
continue
f_out.write(" "+line.strip() +"\n")
defrun(self):
ifPath(".git").exists():
try:
self.gen_rpm_spec()
self.gen_authors()
self.gen_changelog()
sdist.run(self)
finally:
files= [
"libvirt-python.spec",
"AUTHORS",
"ChangeLog"
]
forfinfiles:
try:
Path(f).unlink()
exceptFileNotFoundError:
pass
else:
sdist.run(self)
##################
# Invoke setup() #
##################
ifsys.version_info< (3, 6):
print("libvirt-python requires Python >= 3.6 to build")
sys.exit(1)
MIN_LIBVIRT="0.9.11"
MIN_LIBVIRT_LXC="1.0.2"
# Hack to stop "pip install" failing with error
# about missing "build" dir.
Path("build").mkdir(exist_ok=True)
check_pkgcfg()
_c_modules, _py_modules=get_module_lists()
setup(
ext_modules=_c_modules,
py_modules=_py_modules,
package_dir={
'': 'build'
},
cmdclass={
"build_ext": my_build_ext,
"build_py": my_build_py,
"sdist": my_sdist,
},
)