-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinprovider_apt.py
More file actions
executable file
·360 lines (326 loc) · 13.1 KB
/
Copy pathbinprovider_apt.py
File metadata and controls
executable file
·360 lines (326 loc) · 13.1 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/env python
__package__ = "abxpkg"
import fcntl
import sys
import time
from contextlib import contextmanager
from pathlib import Path
from pydantic import TypeAdapter, model_validator
from typing import Self
from .base_types import BinProviderName, PATHStr, BinName, InstallArgs
from .semver import SemVer
from .binprovider import BinProvider, EnvProvider, ShallowBinary, remap_kwargs
from .logging import format_subprocess_output
_LAST_UPDATE_CHECK = None
UPDATE_CHECK_INTERVAL = 60 * 60 * 24 # 1 day
APT_LOCK_PATH = Path("/tmp/abxpkg-apt.lock")
class AptProvider(BinProvider):
name: BinProviderName = "apt"
_log_emoji = "🐧"
INSTALLER_BIN: BinName = "apt-get"
PATH: PATHStr = "" # Starts empty; setup_PATH() discovers package runtime bin dirs via dpkg and replaces PATH with those dirs.
euid: int | None = (
0 # Import-time default that forces every apt subprocess through the root/sudo execution path.
)
@model_validator(mode="after")
def add_package_aliases(self) -> Self:
self.overrides["gem"] = {
**self.overrides.get("gem", {}),
"install_args": ["ruby"],
}
return self
@contextmanager
def apt_lock(self):
APT_LOCK_PATH.parent.mkdir(parents=True, exist_ok=True)
with APT_LOCK_PATH.open("w") as lock_file:
fcntl.flock(lock_file, fcntl.LOCK_EX)
try:
yield
finally:
fcntl.flock(lock_file, fcntl.LOCK_UN)
def setup_PATH(self, no_cache: bool = False) -> None:
"""Populate PATH on first use from dpkg-discovered package runtime bin dirs, not from apt-get itself."""
if sys.platform != "linux":
# Apt has no runtime PATH contribution on non-Linux hosts. Returning
# here keeps fallback provider lists cheap: merely considering apt
# must not ask other providers to locate or install apt-get.
self.PATH = ""
return
# Rebuild PATH on first use, when the caller forces no_cache, or when
# PATH is still empty — the last case covers the "INSTALLER_BINARY was
# resolved out-of-band (hook preflight etc.), so _INSTALLER_BINARY is
# non-None but self.PATH was never populated" race.
if (
no_cache
or not self.PATH
or self._INSTALLER_BINARY is None
or self._INSTALLER_BINARY.loaded_abspath is None
):
dpkg_binary = EnvProvider().load("dpkg")
apt_binary = None
try:
apt_binary = self.INSTALLER_BINARY(no_cache=no_cache)
except Exception:
apt_binary = None
dpkg_abspath = (
dpkg_binary.loaded_abspath
if dpkg_binary and dpkg_binary.loaded_abspath
else None
)
apt_abspath = (
apt_binary.loaded_abspath
if apt_binary and apt_binary.loaded_abspath
else None
)
if not dpkg_abspath or not apt_abspath:
self.PATH = ""
else:
# Seed self.PATH with apt-get's bin_dir before calling
# self.exec(dpkg -L bash). self.exec's build_exec_env
# re-enters self.setup_PATH; without a non-empty PATH,
# the ``not self.PATH`` guard at the top of this method
# would fire on every recursive entry and infinitely
# loop. The bin_dir is correct as a baseline value —
# the dpkg-discovered runtime bin dirs get prepended
# onto it just below.
self.PATH = TypeAdapter(PATHStr).validate_python(
str(apt_abspath.parent),
)
PATH = self.PATH
dpkg_install_dirs = (
self.exec(
bin_name=dpkg_abspath,
cmd=["-L", "bash"],
quiet=True,
should_log_command=False,
)
.stdout.strip()
.split("\n")
)
dpkg_bin_dirs = [
path for path in dpkg_install_dirs if path.endswith("/bin")
]
for bin_dir in dpkg_bin_dirs:
if str(bin_dir) not in PATH:
PATH = ":".join([str(bin_dir), *PATH.split(":")])
self.PATH = TypeAdapter(PATHStr).validate_python(PATH)
super().setup_PATH(no_cache=no_cache)
@staticmethod
def _detect_distro_codename() -> tuple[str, str]:
"""Return (distro_id, codename) parsed from /etc/os-release with apt fallbacks."""
os_release: dict[str, str] = {}
try:
with open("/etc/os-release", encoding="utf-8") as fh:
for raw in fh:
line = raw.strip()
if not line or "=" not in line or line.startswith("#"):
continue
key, _, value = line.partition("=")
os_release[key] = value.strip().strip('"').strip("'")
except OSError:
pass
distro_id = (os_release.get("ID") or "").lower()
codename = (
os_release.get("VERSION_CODENAME")
or os_release.get("UBUNTU_CODENAME")
or ""
).lower()
if distro_id in ("ubuntu", "debian"):
return distro_id, codename or (
"noble" if distro_id == "ubuntu" else "stable"
)
# apt is debian-derived; fall back to ubuntu LTS for derivatives.
return "ubuntu", codename or "noble"
def default_docs_url_handler(
self,
bin_name: BinName,
**context,
) -> str | None:
package = self._docs_url_package_name(bin_name)
if not package:
return None
distro, codename = self._detect_distro_codename()
host = "packages.debian.org" if distro == "debian" else "packages.ubuntu.com"
return f"https://{host}/{codename}/{package}"
@remap_kwargs({"packages": "install_args"})
def default_install_handler(
self,
bin_name: BinName,
install_args: InstallArgs | None = None,
postinstall_scripts: bool | None = None,
min_release_age: float | None = None,
min_version: SemVer | None = None,
no_cache: bool = False,
timeout: int | None = None,
) -> str:
global _LAST_UPDATE_CHECK
install_args = install_args or self.get_install_args(bin_name)
installer_bin = self.INSTALLER_BINARY(no_cache=no_cache).loaded_abspath
dpkg_binary = EnvProvider().load("dpkg")
dpkg_abspath = (
dpkg_binary.loaded_abspath
if dpkg_binary and dpkg_binary.loaded_abspath
else None
)
assert installer_bin
if not dpkg_abspath:
raise Exception(
f"{self.__class__.__name__}.INSTALLER_BIN is not available on this host: {self.INSTALLER_BIN}",
)
# print(f'[*] {self.__class__.__name__}: Installing {bin_name}: {self.INSTALLER_BIN} install {install_args}')
with self.apt_lock():
if (
not _LAST_UPDATE_CHECK
or (time.time() - _LAST_UPDATE_CHECK) > UPDATE_CHECK_INTERVAL
):
# only update if we haven't checked in the last day
self.exec(
bin_name=installer_bin,
cmd=["update", "-qq"],
timeout=timeout,
)
_LAST_UPDATE_CHECK = time.time()
proc = self.exec(
bin_name=installer_bin,
cmd=["install", "-y", "-qq", "--no-install-recommends", *install_args],
timeout=timeout,
)
if proc.returncode != 0:
self._raise_proc_error("install", install_args, proc)
return (
format_subprocess_output(proc.stdout, proc.stderr)
or f"Installed {install_args} successfully."
)
@remap_kwargs({"packages": "install_args"})
def default_update_handler(
self,
bin_name: BinName,
install_args: InstallArgs | None = None,
postinstall_scripts: bool | None = None,
min_release_age: float | None = None,
min_version: SemVer | None = None,
no_cache: bool = False,
timeout: int | None = None,
) -> str:
global _LAST_UPDATE_CHECK
install_args = install_args or self.get_install_args(bin_name)
installer_bin = self.INSTALLER_BINARY(no_cache=no_cache).loaded_abspath
dpkg_binary = EnvProvider().load("dpkg")
dpkg_abspath = (
dpkg_binary.loaded_abspath
if dpkg_binary and dpkg_binary.loaded_abspath
else None
)
assert installer_bin
if not dpkg_abspath:
raise Exception(
f"{self.__class__.__name__}.INSTALLER_BIN is not available on this host: {self.INSTALLER_BIN}",
)
with self.apt_lock():
if (
not _LAST_UPDATE_CHECK
or (time.time() - _LAST_UPDATE_CHECK) > UPDATE_CHECK_INTERVAL
):
self.exec(
bin_name=installer_bin,
cmd=["update", "-qq"],
timeout=timeout,
)
_LAST_UPDATE_CHECK = time.time()
proc = self.exec(
bin_name=installer_bin,
cmd=[
"install",
"--only-upgrade",
"-y",
"-qq",
"--no-install-recommends",
*install_args,
],
timeout=timeout,
)
if proc.returncode != 0:
self._raise_proc_error("update", install_args, proc)
return (
format_subprocess_output(proc.stdout, proc.stderr)
or f"Updated {install_args} successfully."
)
def default_search_handler(
self,
bin_name: BinName,
min_version: SemVer | None = None,
min_release_age: float | None = None,
timeout: int | None = None,
**context,
) -> list[ShallowBinary]:
"""Search apt's package index for packages whose name matches bin_name (substring)."""
from .binary import Binary
# ``apt-cache search --names-only`` returns lines like ``<name> - <description>``.
# Routing through ``self.exec`` lets apt's setup_PATH/INSTALLER_BINARY
# auto-recover from a missing/broken apt-get on the ambient PATH
# (e.g. CI runners where the linuxbrew copy is unusable). The
# deadlock filter in ``BinProvider.INSTALLER_BINARY`` keeps it
# safe under restrictive ``--binproviders`` configs.
self.INSTALLER_BINARY(no_cache=bool(context.get("no_cache", False)))
proc = self.exec(
bin_name="apt-cache",
cmd=["search", "--names-only", str(bin_name)],
quiet=True,
timeout=timeout,
)
results: list[ShallowBinary] = []
for line in proc.stdout.splitlines():
pkg_name, _, description = line.partition(" - ")
pkg_name = pkg_name.strip()
if not pkg_name or str(bin_name) not in pkg_name:
continue
results.append(
Binary(
name=pkg_name,
description=description.strip(),
binproviders=[self],
overrides={self.name: {"install_args": [pkg_name]}},
),
)
return results
@remap_kwargs({"packages": "install_args"})
def default_uninstall_handler(
self,
bin_name: BinName,
install_args: InstallArgs | None = None,
postinstall_scripts: bool | None = None,
min_release_age: float | None = None,
min_version: SemVer | None = None,
no_cache: bool = False,
timeout: int | None = None,
) -> bool:
install_args = install_args or self.get_install_args(bin_name)
installer_bin = self.INSTALLER_BINARY(no_cache=no_cache).loaded_abspath
dpkg_binary = EnvProvider().load("dpkg")
dpkg_abspath = (
dpkg_binary.loaded_abspath
if dpkg_binary and dpkg_binary.loaded_abspath
else None
)
assert installer_bin
if not dpkg_abspath:
raise Exception(
f"{self.__class__.__name__}.INSTALLER_BIN is not available on this host: {self.INSTALLER_BIN}",
)
with self.apt_lock():
proc = self.exec(
bin_name=installer_bin,
cmd=["remove", "-y", "-qq", *install_args],
timeout=timeout,
)
if proc.returncode != 0:
self._raise_proc_error("uninstall", install_args, proc)
return True
if __name__ == "__main__":
result = apt = AptProvider()
func = None
if len(sys.argv) > 1:
result = func = getattr(apt, sys.argv[1]) # e.g. install
if len(sys.argv) > 2 and callable(func):
result = func(sys.argv[2]) # e.g. install ffmpeg
print(result)