portable-python 2.0.2__py3-none-any.whl → 2.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- portable_python/__init__.py +106 -98
- portable_python/cli.py +7 -11
- portable_python/config.py +22 -36
- portable_python/cpython.py +58 -12
- portable_python/external/__init__.py +2 -1
- portable_python/external/xcpython.py +43 -31
- portable_python/external/xtkinter.py +3 -5
- portable_python/inspector.py +51 -62
- portable_python/tracking.py +45 -43
- portable_python/versions.py +70 -61
- {portable_python-2.0.2.dist-info → portable_python-2.2.0.dist-info}/METADATA +3 -3
- portable_python-2.2.0.dist-info/RECORD +18 -0
- {portable_python-2.0.2.dist-info → portable_python-2.2.0.dist-info}/WHEEL +1 -1
- portable_python-2.0.2.dist-info/RECORD +0 -18
- {portable_python-2.0.2.dist-info → portable_python-2.2.0.dist-info}/entry_points.txt +0 -0
- {portable_python-2.0.2.dist-info → portable_python-2.2.0.dist-info}/licenses/LICENSE +0 -0
- {portable_python-2.0.2.dist-info → portable_python-2.2.0.dist-info}/top_level.txt +0 -0
portable_python/__init__.py
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
Designed to be used via portable-python CLI.
|
|
3
3
|
Can be used programmatically too, example usage:
|
|
4
4
|
|
|
5
|
-
from portable_python import BuildSetup
|
|
5
|
+
from portable_python import BuildSetup, required_python_spec
|
|
6
6
|
|
|
7
|
-
setup = BuildSetup("cpython:3.9.6")
|
|
7
|
+
setup = BuildSetup(required_python_spec("cpython:3.9.6"))
|
|
8
8
|
setup.compile()
|
|
9
9
|
"""
|
|
10
10
|
|
|
@@ -15,12 +15,13 @@ import multiprocessing
|
|
|
15
15
|
import os
|
|
16
16
|
import pathlib
|
|
17
17
|
import re
|
|
18
|
+
import shlex
|
|
19
|
+
from collections.abc import Iterable
|
|
18
20
|
from string import Template
|
|
19
|
-
from typing import ClassVar
|
|
20
21
|
|
|
21
22
|
import runez
|
|
22
23
|
from runez.http import RestClient
|
|
23
|
-
from runez.pyenv import PythonSpec
|
|
24
|
+
from runez.pyenv import PythonSpec, Version
|
|
24
25
|
from runez.render import Header, PrettyTable
|
|
25
26
|
|
|
26
27
|
from portable_python.versions import PPG
|
|
@@ -33,7 +34,7 @@ def is_binary_file(path):
|
|
|
33
34
|
return RX_BINARY.match(path.name)
|
|
34
35
|
|
|
35
36
|
|
|
36
|
-
def patch_folder(folder, regex, replacement, ignore=None):
|
|
37
|
+
def patch_folder(folder, regex, replacement, ignore=None, dryrun: runez.DryrunSpec = runez.UNSET):
|
|
37
38
|
"""Replace all occurrences of 'old_text' by 'new_text' in all files in 'folder'
|
|
38
39
|
|
|
39
40
|
Parameters
|
|
@@ -46,33 +47,53 @@ def patch_folder(folder, regex, replacement, ignore=None):
|
|
|
46
47
|
Replacement text
|
|
47
48
|
ignore : re.Pattern | None
|
|
48
49
|
Regex stating what to ignore
|
|
50
|
+
dryrun
|
|
51
|
+
Optionally override current dryrun setting
|
|
49
52
|
"""
|
|
50
53
|
for path in runez.ls_dir(folder):
|
|
51
54
|
if not path.is_symlink() and (not ignore or not ignore.match(path.name)):
|
|
52
55
|
if path.is_dir():
|
|
53
|
-
patch_folder(path, regex, replacement, ignore=ignore)
|
|
56
|
+
patch_folder(path, regex, replacement, ignore=ignore, dryrun=dryrun)
|
|
54
57
|
|
|
55
58
|
elif not is_binary_file(path):
|
|
56
|
-
patch_file(path, regex, replacement)
|
|
59
|
+
patch_file(path, regex, replacement, dryrun=dryrun)
|
|
57
60
|
|
|
58
61
|
|
|
59
|
-
def patch_file(path, regex, replacement):
|
|
62
|
+
def patch_file(path, regex, replacement, dryrun: runez.DryrunSpec = runez.UNSET):
|
|
63
|
+
"""Replace `regex` with `replacement` in file `path`, if it occurs there"""
|
|
64
|
+
if not os.path.exists(path):
|
|
65
|
+
if not runez.log.resolved_dryrun(dryrun): # Sources to patch are not downloaded in dryrun mode
|
|
66
|
+
LOG.warning("Can't patch %s: file not found", runez.short(path))
|
|
67
|
+
|
|
68
|
+
return
|
|
69
|
+
|
|
70
|
+
overview = f"{regex} -> {runez.short(replacement)} in {runez.short(path)}"
|
|
60
71
|
try:
|
|
61
72
|
with open(path) as fh:
|
|
62
73
|
text = fh.read()
|
|
63
74
|
|
|
64
75
|
new_text = re.sub(regex, replacement, text, flags=re.MULTILINE)
|
|
65
|
-
if text != new_text:
|
|
76
|
+
if text != new_text and not runez.log.hdry(f"apply patch: {overview}", dryrun=dryrun):
|
|
66
77
|
with open(path, "w") as fh:
|
|
67
78
|
fh.write(new_text)
|
|
68
79
|
|
|
69
|
-
LOG.info("Patched
|
|
80
|
+
LOG.info("Patched %s", overview)
|
|
70
81
|
|
|
71
82
|
except Exception as e:
|
|
72
83
|
with open(path, errors="ignore") as fh:
|
|
73
84
|
text = fh.read()
|
|
74
85
|
if re.search(regex, text):
|
|
75
|
-
LOG.warning("Can't patch
|
|
86
|
+
LOG.warning("Can't patch %s: %s", overview, e)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def correct_permissions(path: pathlib.Path, expected: int, dryrun: runez.DryrunSpec = runez.UNSET):
|
|
90
|
+
"""Ensure that `path` has `expected` permissions"""
|
|
91
|
+
current = path.stat().st_mode & 0o777
|
|
92
|
+
if current != expected:
|
|
93
|
+
overview = f"permissions for {runez.short(path)} to {oct(expected)} (was {oct(current)})"
|
|
94
|
+
if not runez.log.hdry(f"correct {overview}", dryrun=dryrun):
|
|
95
|
+
LOG.info("Corrected %s", overview)
|
|
96
|
+
path.chmod(expected)
|
|
76
97
|
|
|
77
98
|
|
|
78
99
|
class FolderMask:
|
|
@@ -147,9 +168,6 @@ class BuildContext:
|
|
|
147
168
|
|
|
148
169
|
return v
|
|
149
170
|
|
|
150
|
-
def __repr__(self):
|
|
151
|
-
return self.isolate_usr_local or "none"
|
|
152
|
-
|
|
153
171
|
def __enter__(self):
|
|
154
172
|
runez.Anchored.add(self.setup.folders.base_folder)
|
|
155
173
|
if self.isolate_usr_local == "mount-shadow":
|
|
@@ -166,7 +184,7 @@ class BuildContext:
|
|
|
166
184
|
self.masked_folders.append(mask)
|
|
167
185
|
mask.mount()
|
|
168
186
|
|
|
169
|
-
except BaseException: #
|
|
187
|
+
except BaseException: # Ensure cleanup if any folder couldn't be masked
|
|
170
188
|
self.cleanup()
|
|
171
189
|
raise
|
|
172
190
|
|
|
@@ -189,6 +207,18 @@ class BuildContext:
|
|
|
189
207
|
runez.Anchored.pop(self.setup.folders.base_folder)
|
|
190
208
|
|
|
191
209
|
|
|
210
|
+
def required_python_spec(text: str | None) -> PythonSpec:
|
|
211
|
+
"""Python to build, with a full version (aborts with a clear message otherwise), default: latest cpython"""
|
|
212
|
+
spec = PythonSpec.from_object(PPG.cpython.latest if not text or text == "latest" else text)
|
|
213
|
+
if not spec:
|
|
214
|
+
runez.abort("Invalid python spec: %s" % runez.red(text))
|
|
215
|
+
|
|
216
|
+
if spec.version.given_components_count < 3:
|
|
217
|
+
runez.abort("Please provide full desired version: %s is not good enough" % runez.red(spec))
|
|
218
|
+
|
|
219
|
+
return spec
|
|
220
|
+
|
|
221
|
+
|
|
192
222
|
class BuildSetup:
|
|
193
223
|
"""
|
|
194
224
|
Drives the compilation, external modules first, then the target python itself.
|
|
@@ -198,33 +228,22 @@ class BuildSetup:
|
|
|
198
228
|
# Internal, used to ensure files under {logs}/ folder sort alphabetically in the same order they were compiled
|
|
199
229
|
log_counter = 0
|
|
200
230
|
|
|
201
|
-
def __init__(self, python_spec
|
|
231
|
+
def __init__(self, python_spec: PythonSpec, modules: str | None = None, prefix: str | None = None):
|
|
202
232
|
"""
|
|
203
233
|
Parameters
|
|
204
234
|
----------
|
|
205
|
-
python_spec
|
|
206
|
-
Python to build (family and version)
|
|
207
|
-
modules
|
|
235
|
+
python_spec
|
|
236
|
+
Python to build (family and full version), see `required_python_spec()`
|
|
237
|
+
modules
|
|
208
238
|
Modules to build (default: from config)
|
|
209
|
-
prefix
|
|
239
|
+
prefix
|
|
210
240
|
--prefix to use
|
|
211
241
|
"""
|
|
212
|
-
if not python_spec or python_spec == "latest":
|
|
213
|
-
python_spec = PPG.cpython.latest
|
|
214
|
-
|
|
215
|
-
if not isinstance(python_spec, PythonSpec):
|
|
216
|
-
ps = PythonSpec.from_object(python_spec)
|
|
217
|
-
runez.abort_if(not ps, "Invalid python spec: %s" % runez.red(python_spec))
|
|
218
|
-
python_spec = ps
|
|
219
|
-
|
|
220
|
-
runez.abort_if(not python_spec.version or not python_spec.version.is_valid, "Invalid python spec: %s" % runez.red(python_spec))
|
|
221
|
-
if len(python_spec.version.given_components) < 3:
|
|
222
|
-
runez.abort("Please provide full desired version: %s is not good enough" % runez.red(python_spec))
|
|
223
|
-
|
|
224
242
|
self.python_spec = python_spec
|
|
225
243
|
self.folders = PPG.get_folders(
|
|
226
244
|
base=os.getcwd(), family=python_spec.family, version=python_spec.version, abi_suffix=python_spec.abi_suffix
|
|
227
245
|
)
|
|
246
|
+
self.build_context: BuildContext | None = None # Set during compile()
|
|
228
247
|
self.desired_modules = modules
|
|
229
248
|
prefix = self.folders.formatted(prefix)
|
|
230
249
|
self.prefix = prefix
|
|
@@ -244,7 +263,7 @@ class BuildSetup:
|
|
|
244
263
|
)
|
|
245
264
|
|
|
246
265
|
builder = PPG.family(python_spec.family).get_builder()
|
|
247
|
-
self.python_builder = builder(self)
|
|
266
|
+
self.python_builder = builder(self)
|
|
248
267
|
|
|
249
268
|
def __repr__(self):
|
|
250
269
|
return str(self.folders)
|
|
@@ -301,41 +320,35 @@ class BuildSetup:
|
|
|
301
320
|
class ModuleCollection:
|
|
302
321
|
"""Models a collection of sub-modules, with auto-detection and reporting as to what is active and why"""
|
|
303
322
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
def __init__(self, parent_module: "ModuleBuilder", desired=None):
|
|
309
|
-
self.selected = []
|
|
310
|
-
self.auto_selected = {}
|
|
311
|
-
self.candidates = []
|
|
323
|
+
def __init__(self, parent_module: "ModuleBuilder", desired: str | None = None):
|
|
324
|
+
self.selected: list[ModuleBuilder] = []
|
|
325
|
+
self.auto_selected: dict[str, str] = {}
|
|
326
|
+
self.candidates: list[ModuleBuilder] = []
|
|
312
327
|
self.desired = desired
|
|
313
|
-
self.module_by_name
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
self.candidates.append(module)
|
|
319
|
-
self.module_by_name[module.m_name] = module
|
|
328
|
+
self.module_by_name: dict[str, ModuleBuilder] = {}
|
|
329
|
+
for module_class in parent_module.candidate_modules():
|
|
330
|
+
module = module_class(parent_module)
|
|
331
|
+
self.candidates.append(module)
|
|
332
|
+
self.module_by_name[module.m_name] = module
|
|
320
333
|
|
|
321
334
|
if desired == "all":
|
|
322
335
|
self.selected = self.candidates
|
|
323
336
|
return
|
|
324
337
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
unknown = [x for x in
|
|
338
|
+
names = [] if desired == "none" else runez.flattened(desired, split=True)
|
|
339
|
+
names = runez.flattened(names, split=",")
|
|
340
|
+
unknown = [x for x in names if x not in self.module_by_name]
|
|
328
341
|
if unknown:
|
|
329
342
|
runez.abort("Unknown modules: %s" % runez.joined(unknown, delimiter=", ", stringify=runez.red))
|
|
330
343
|
|
|
331
344
|
for candidate in self.candidates:
|
|
332
|
-
if candidate.m_name not in
|
|
345
|
+
if candidate.m_name not in names:
|
|
333
346
|
reason = candidate.auto_select_reason()
|
|
334
347
|
if reason:
|
|
335
348
|
self.auto_selected[candidate.m_name] = reason
|
|
336
349
|
|
|
337
|
-
|
|
338
|
-
self.selected = [self.module_by_name[x] for x in
|
|
350
|
+
names.extend(self.auto_selected.keys())
|
|
351
|
+
self.selected = [self.module_by_name[x] for x in names]
|
|
339
352
|
|
|
340
353
|
def __repr__(self):
|
|
341
354
|
return "selected: %s (%s)" % (self.desired, runez.plural(self.selected, "module"))
|
|
@@ -402,13 +415,13 @@ class LinkerOutcome(enum.Enum):
|
|
|
402
415
|
class ModuleBuilder:
|
|
403
416
|
"""Common behavior for all external (typically C) modules to be compiled"""
|
|
404
417
|
|
|
405
|
-
m_build_cwd: str = None # Optional: relative (to unpacked source) folder where to run configure/make from
|
|
406
|
-
m_debian = None
|
|
407
|
-
m_include: str = None # Optional: subfolder to automatically list in CPATH when this module is active
|
|
408
|
-
m_telltale:
|
|
418
|
+
m_build_cwd: str | None = None # Optional: relative (to unpacked source) folder where to run configure/make from
|
|
419
|
+
m_debian: str | None = None # Optional: debian package, prefixed with "!": required, "+": required if selected, "-": prevents static
|
|
420
|
+
m_include: str | None = None # Optional: subfolder to automatically list in CPATH when this module is active
|
|
421
|
+
m_telltale: str = "" # Optional: file(s) that, if present, indicate this module is installed (space separated)
|
|
409
422
|
|
|
410
423
|
setup: BuildSetup
|
|
411
|
-
parent_module: "ModuleBuilder" = None
|
|
424
|
+
parent_module: "ModuleBuilder | None" = None
|
|
412
425
|
_log_handler = None
|
|
413
426
|
|
|
414
427
|
def __init__(self, parent_module):
|
|
@@ -434,13 +447,14 @@ class ModuleBuilder:
|
|
|
434
447
|
return "%s:%s" % (self.m_name, self.version)
|
|
435
448
|
|
|
436
449
|
@classmethod
|
|
437
|
-
def candidate_modules(cls) ->
|
|
450
|
+
def candidate_modules(cls) -> Iterable[type["ModuleBuilder"]]:
|
|
438
451
|
"""All possible candidate external modules for this builder"""
|
|
452
|
+
return []
|
|
439
453
|
|
|
440
454
|
def selected_modules(self):
|
|
441
455
|
return ModuleCollection(self, desired="all")
|
|
442
456
|
|
|
443
|
-
def auto_select_reason(self):
|
|
457
|
+
def auto_select_reason(self) -> str | None:
|
|
444
458
|
"""
|
|
445
459
|
If this module must be selected (build can't succeed without), descendant should return short explanation why
|
|
446
460
|
"""
|
|
@@ -480,11 +494,10 @@ class ModuleBuilder:
|
|
|
480
494
|
return "no %s" % self.m_telltale
|
|
481
495
|
|
|
482
496
|
def _find_telltale(self):
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
return telltales
|
|
497
|
+
if not self.m_telltale:
|
|
498
|
+
return runez.UNSET # Sub-modules don't have a telltale
|
|
486
499
|
|
|
487
|
-
return PPG.find_telltale(
|
|
500
|
+
return PPG.find_telltale(self.m_telltale)
|
|
488
501
|
|
|
489
502
|
def active_module(self, name):
|
|
490
503
|
return self.modules.active_module(name)
|
|
@@ -493,8 +506,9 @@ class ModuleBuilder:
|
|
|
493
506
|
"""Is module with name either selected or usable as a shared lib, as determined via its telltale"""
|
|
494
507
|
return self.modules.is_usable_module(name)
|
|
495
508
|
|
|
496
|
-
def cfg_version(self, default):
|
|
497
|
-
|
|
509
|
+
def cfg_version(self, default: str) -> Version:
|
|
510
|
+
# str(): a version in yaml config may have been parsed as a float (eg: `gdbm-version: 1.26`)
|
|
511
|
+
return Version(str(PPG.config.get_value("%s-version" % self.m_name) or default))
|
|
498
512
|
|
|
499
513
|
def cfg_http_headers(self):
|
|
500
514
|
if config_http_headers := PPG.config.get_value("%s-http-headers" % self.m_name):
|
|
@@ -514,16 +528,19 @@ class ModuleBuilder:
|
|
|
514
528
|
def cfg_src_suffix(self):
|
|
515
529
|
return PPG.config.get_value("%s-src-suffix" % self.m_name)
|
|
516
530
|
|
|
517
|
-
def cfg_configure(self, deps_lib_dir, deps_lib64_dir):
|
|
531
|
+
def cfg_configure(self, deps_lib_dir, deps_lib64_dir) -> list[str] | None:
|
|
532
|
+
"""Configure args from config (`$lib_dir` and `$lib64_dir` substituted), shell-style split into separate args"""
|
|
518
533
|
if configure := PPG.config.get_value("%s-configure" % self.m_name):
|
|
519
|
-
|
|
520
|
-
return
|
|
534
|
+
configure = Template(configure).substitute(lib_dir=deps_lib_dir, lib64_dir=deps_lib64_dir)
|
|
535
|
+
return shlex.split(configure)
|
|
536
|
+
|
|
537
|
+
return None
|
|
521
538
|
|
|
522
539
|
def cfg_patches(self):
|
|
523
540
|
return PPG.config.get_value("%s-patches" % self.m_name)
|
|
524
541
|
|
|
525
542
|
@property
|
|
526
|
-
def url(self):
|
|
543
|
+
def url(self) -> str:
|
|
527
544
|
"""Url of source tarball, if any"""
|
|
528
545
|
return ""
|
|
529
546
|
|
|
@@ -538,9 +555,12 @@ class ModuleBuilder:
|
|
|
538
555
|
return self.cfg_src_suffix()
|
|
539
556
|
|
|
540
557
|
@property
|
|
541
|
-
def version(self):
|
|
542
|
-
"""Version to use"""
|
|
543
|
-
|
|
558
|
+
def version(self) -> Version:
|
|
559
|
+
"""Version to use, sub-modules (such as tcl/tk/tix) share the version of their parent module by default"""
|
|
560
|
+
if self.parent_module is None:
|
|
561
|
+
return self.setup.python_spec.version # Top of the module tree: the python being built
|
|
562
|
+
|
|
563
|
+
return self.parent_module.version
|
|
544
564
|
|
|
545
565
|
@property
|
|
546
566
|
def deps(self):
|
|
@@ -607,7 +627,7 @@ class ModuleBuilder:
|
|
|
607
627
|
cmd = runez.flattened(*program, prefix, *args)
|
|
608
628
|
return self._do_run(*cmd)
|
|
609
629
|
|
|
610
|
-
def run_make(self, *args, program="make", cpu_count=None):
|
|
630
|
+
def run_make(self, *args, program: str = "make", cpu_count: int | None = None):
|
|
611
631
|
cmd = program.split()
|
|
612
632
|
if cpu_count is None:
|
|
613
633
|
available = multiprocessing.cpu_count()
|
|
@@ -729,11 +749,7 @@ class ModuleBuilder:
|
|
|
729
749
|
def _apply_patches(self):
|
|
730
750
|
if patches := self.cfg_patches():
|
|
731
751
|
for patch in patches:
|
|
732
|
-
|
|
733
|
-
print(f"Would apply patch: {patch}")
|
|
734
|
-
else:
|
|
735
|
-
print(f"Applying patch: {patch}")
|
|
736
|
-
patch_file(patch["file"], patch["regex"], patch["replacement"])
|
|
752
|
+
patch_file(patch["file"], patch["regex"], patch["replacement"])
|
|
737
753
|
|
|
738
754
|
def _get_env_vars(self):
|
|
739
755
|
"""Yield all found env vars, first found wins"""
|
|
@@ -782,7 +798,7 @@ class ModuleBuilder:
|
|
|
782
798
|
|
|
783
799
|
|
|
784
800
|
class PythonBuilder(ModuleBuilder):
|
|
785
|
-
_bin_python: pathlib.Path = None
|
|
801
|
+
_bin_python: pathlib.Path | None = None
|
|
786
802
|
|
|
787
803
|
def __init__(self, parent_module):
|
|
788
804
|
super().__init__(parent_module)
|
|
@@ -799,24 +815,20 @@ class PythonBuilder(ModuleBuilder):
|
|
|
799
815
|
return ModuleCollection(self, desired=desired)
|
|
800
816
|
|
|
801
817
|
@property
|
|
802
|
-
def bin_python(self):
|
|
803
|
-
"""
|
|
804
|
-
Returns
|
|
805
|
-
-------
|
|
806
|
-
pathlib.Path | None
|
|
807
|
-
Path to freshly compiled bin/python, real file (not symlink)
|
|
808
|
-
"""
|
|
818
|
+
def bin_python(self) -> pathlib.Path:
|
|
819
|
+
"""Path to freshly compiled bin/python, real file (not symlink)"""
|
|
809
820
|
if self._bin_python is None:
|
|
810
|
-
|
|
821
|
+
bin_python = PPG.config.find_main_file(self.bin_folder / "python", self.version)
|
|
822
|
+
if not bin_python:
|
|
823
|
+
runez.abort(f"Can't find bin/python in {self.bin_folder}")
|
|
811
824
|
|
|
812
|
-
|
|
825
|
+
self._bin_python = bin_python
|
|
813
826
|
|
|
814
|
-
|
|
815
|
-
def version(self):
|
|
816
|
-
return self.setup.python_spec.version
|
|
827
|
+
return self._bin_python
|
|
817
828
|
|
|
818
829
|
def xenv_LDFLAGS(self):
|
|
819
830
|
"""Python builder does not reuse the common setting"""
|
|
831
|
+
yield from ()
|
|
820
832
|
|
|
821
833
|
def run_python(self, *args):
|
|
822
834
|
"""Run python command, using the freshly compiled python binary"""
|
|
@@ -834,8 +846,4 @@ class PythonBuilder(ModuleBuilder):
|
|
|
834
846
|
for lib_dir in self.deps_lib_dirs:
|
|
835
847
|
for path in runez.ls_dir(lib_dir):
|
|
836
848
|
if not path.name.endswith(".la"):
|
|
837
|
-
|
|
838
|
-
current = path.stat().st_mode & 0o777
|
|
839
|
-
if current != expected:
|
|
840
|
-
LOG.info("Corrected permissions for %s (was %s)", runez.short(path), oct(current))
|
|
841
|
-
path.chmod(expected)
|
|
849
|
+
correct_permissions(path, 0o755 if path.is_dir() else 0o644)
|
portable_python/cli.py
CHANGED
|
@@ -5,7 +5,7 @@ import runez
|
|
|
5
5
|
from runez.pyenv import PythonSpec
|
|
6
6
|
from runez.render import PrettyTable
|
|
7
7
|
|
|
8
|
-
from portable_python import BuildSetup, PPG
|
|
8
|
+
from portable_python import BuildSetup, PPG, required_python_spec
|
|
9
9
|
from portable_python.inspector import LibAutoCorrect, PythonInspector
|
|
10
10
|
|
|
11
11
|
LOG = logging.getLogger(__name__)
|
|
@@ -41,7 +41,7 @@ def main(config, quiet, target):
|
|
|
41
41
|
@click.argument("python_spec")
|
|
42
42
|
def build(modules, prefix, python_spec):
|
|
43
43
|
"""Build a portable python binary"""
|
|
44
|
-
setup = BuildSetup(python_spec, modules=modules, prefix=prefix)
|
|
44
|
+
setup = BuildSetup(required_python_spec(python_spec), modules=modules, prefix=prefix)
|
|
45
45
|
setup.compile()
|
|
46
46
|
|
|
47
47
|
|
|
@@ -50,7 +50,7 @@ def build(modules, prefix, python_spec):
|
|
|
50
50
|
@click.argument("python_spec", required=False)
|
|
51
51
|
def build_report(modules, python_spec):
|
|
52
52
|
"""Show status of buildable modules, which will be auto-compiled"""
|
|
53
|
-
setup = BuildSetup(python_spec, modules=modules)
|
|
53
|
+
setup = BuildSetup(required_python_spec(python_spec), modules=modules)
|
|
54
54
|
print(runez.bold(setup.python_spec))
|
|
55
55
|
report = setup.python_builder.modules.report()
|
|
56
56
|
print(report)
|
|
@@ -90,10 +90,7 @@ def inspect(modules, verbose, prefix, skip_so, path):
|
|
|
90
90
|
@click.argument("family", default="cpython")
|
|
91
91
|
def list_cmd(json, family):
|
|
92
92
|
"""List latest versions"""
|
|
93
|
-
fam = PPG.family(family
|
|
94
|
-
if not fam:
|
|
95
|
-
runez.abort("Python family '%s' is not yet supported" % runez.red(family))
|
|
96
|
-
|
|
93
|
+
fam = PPG.family(family)
|
|
97
94
|
if json:
|
|
98
95
|
print(runez.represented_json(fam.available_versions))
|
|
99
96
|
return
|
|
@@ -116,6 +113,7 @@ def _find_recompress_source(folders, path):
|
|
|
116
113
|
candidates = [folders.base_folder, folders.build_folder, folders.dist, folders.destdir]
|
|
117
114
|
for candidate in candidates:
|
|
118
115
|
if candidate:
|
|
116
|
+
# TODO: `runez.to_path()` can be simplified away when all `folders.` attributes become of type Path
|
|
119
117
|
candidate = runez.to_path(candidate) / path
|
|
120
118
|
if candidate.exists():
|
|
121
119
|
return candidate.absolute()
|
|
@@ -162,8 +160,8 @@ def recompress(path, ext):
|
|
|
162
160
|
Mildly useful for comparing sizes from different compressions
|
|
163
161
|
""" # noqa: D301
|
|
164
162
|
extension = runez.SYS_INFO.platform_id.canonical_compress_extension(ext)
|
|
165
|
-
pspec = PythonSpec.from_text(path)
|
|
166
|
-
folders = PPG.get_folders(base=".", family=pspec
|
|
163
|
+
pspec = PythonSpec.from_text(path) # Best effort: a build folder like `3.9.7` yields a spec, archive names don't
|
|
164
|
+
folders = PPG.get_folders(base=".", family=pspec.family if pspec else "cpython", version=pspec.version if pspec else None)
|
|
167
165
|
with runez.Anchored(folders.base_folder):
|
|
168
166
|
actual_path = _find_recompress_source(folders, path)
|
|
169
167
|
if not actual_path:
|
|
@@ -205,6 +203,4 @@ def lib_auto_correct(commit, prefix, path):
|
|
|
205
203
|
|
|
206
204
|
|
|
207
205
|
if __name__ == "__main__":
|
|
208
|
-
from portable_python.cli import main
|
|
209
|
-
|
|
210
206
|
main()
|
portable_python/config.py
CHANGED
|
@@ -90,8 +90,8 @@ class Config:
|
|
|
90
90
|
|
|
91
91
|
self.target = target
|
|
92
92
|
self.default = ConfigSource("default config", self.parsed_yaml(DEFAULT_CONFIG, "default config"))
|
|
93
|
-
self._sources
|
|
94
|
-
self.by_path = {}
|
|
93
|
+
self._sources: list[ConfigSource] = []
|
|
94
|
+
self.by_path: dict[str, ConfigSource] = {}
|
|
95
95
|
for path in self.paths:
|
|
96
96
|
self.load(path)
|
|
97
97
|
|
|
@@ -228,9 +228,9 @@ class Config:
|
|
|
228
228
|
matcher = FileMatcher(spec)
|
|
229
229
|
LOG.info("Applying clean-up spec: %s", matcher)
|
|
230
230
|
cleaned = []
|
|
231
|
-
for
|
|
231
|
+
for root, dirnames, filenames in os.walk(module.install_folder):
|
|
232
232
|
removed = []
|
|
233
|
-
dirpath =
|
|
233
|
+
dirpath = pathlib.Path(root)
|
|
234
234
|
for name in dirnames:
|
|
235
235
|
full_path = dirpath / name
|
|
236
236
|
if matcher.is_match(full_path):
|
|
@@ -270,7 +270,7 @@ class Config:
|
|
|
270
270
|
def real_path(path: pathlib.Path):
|
|
271
271
|
if path and path.exists():
|
|
272
272
|
if path.is_symlink():
|
|
273
|
-
path =
|
|
273
|
+
path = path.resolve()
|
|
274
274
|
|
|
275
275
|
return path
|
|
276
276
|
|
|
@@ -325,8 +325,7 @@ class Config:
|
|
|
325
325
|
front = True
|
|
326
326
|
path = path[1:]
|
|
327
327
|
|
|
328
|
-
path = runez.resolved_path(path, base=base)
|
|
329
|
-
path = runez.to_path(path)
|
|
328
|
+
path = pathlib.Path(runez.resolved_path(path, base=base))
|
|
330
329
|
if path.exists():
|
|
331
330
|
with open(path) as fh:
|
|
332
331
|
data = self.parsed_yaml(fh, path)
|
|
@@ -368,22 +367,14 @@ class ConfigSource:
|
|
|
368
367
|
str | int | float | bool | dict | list | None
|
|
369
368
|
Associated value, if any
|
|
370
369
|
"""
|
|
371
|
-
|
|
370
|
+
value = self.data
|
|
371
|
+
for k in key if isinstance(key, tuple) else (key,):
|
|
372
|
+
if not isinstance(value, dict):
|
|
373
|
+
return None
|
|
372
374
|
|
|
373
|
-
|
|
374
|
-
if not key or not isinstance(data, dict):
|
|
375
|
-
return None
|
|
375
|
+
value = value.get(k)
|
|
376
376
|
|
|
377
|
-
|
|
378
|
-
if len(key) > 1:
|
|
379
|
-
value = self._deep_get(data, key[0])
|
|
380
|
-
return self._deep_get(value, key[1:])
|
|
381
|
-
|
|
382
|
-
key = key[0]
|
|
383
|
-
|
|
384
|
-
value = data.get(key)
|
|
385
|
-
if value is not None:
|
|
386
|
-
return value
|
|
377
|
+
return value
|
|
387
378
|
|
|
388
379
|
|
|
389
380
|
class FileMatcher:
|
|
@@ -395,42 +386,37 @@ class FileMatcher:
|
|
|
395
386
|
def __repr__(self):
|
|
396
387
|
return runez.joined(self.matches)
|
|
397
388
|
|
|
398
|
-
def is_match(self, path: pathlib.Path):
|
|
399
|
-
for m in self.matches
|
|
400
|
-
if m.is_match(path):
|
|
401
|
-
return path
|
|
389
|
+
def is_match(self, path: pathlib.Path) -> bool:
|
|
390
|
+
return any(m.is_match(path) for m in self.matches)
|
|
402
391
|
|
|
403
392
|
|
|
404
393
|
class SingleFileMatch:
|
|
405
|
-
_on_folder = False
|
|
406
|
-
_rx_basename = None
|
|
407
|
-
_rx_path = None
|
|
408
|
-
|
|
409
394
|
def __init__(self, spec: str):
|
|
410
395
|
self.spec = spec
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
396
|
+
self._on_folder = spec.endswith("/")
|
|
397
|
+
spec = spec.removesuffix("/")
|
|
398
|
+
self._rx_path: re.Pattern[str] | None = None
|
|
415
399
|
if "/" in spec:
|
|
416
400
|
# lib/*/config-{python_mm}-\w+/
|
|
417
401
|
path = ".*/%s$" % os.path.dirname(spec).replace("*", ".*").strip("/")
|
|
418
402
|
spec = os.path.basename(spec)
|
|
419
403
|
self._rx_path = re.compile(path)
|
|
420
404
|
|
|
421
|
-
self.
|
|
405
|
+
self._basename_glob = spec
|
|
422
406
|
|
|
423
407
|
def __repr__(self):
|
|
424
408
|
return self.spec
|
|
425
409
|
|
|
426
|
-
def is_match(self, path: pathlib.Path):
|
|
410
|
+
def is_match(self, path: pathlib.Path) -> bool:
|
|
427
411
|
if self._on_folder == path.is_dir():
|
|
428
412
|
if self._rx_path:
|
|
429
413
|
m = self._rx_path.match(str(path.parent))
|
|
430
414
|
if not m:
|
|
431
415
|
return False
|
|
432
416
|
|
|
433
|
-
return fnmatch.fnmatch(path.name, self.
|
|
417
|
+
return fnmatch.fnmatch(path.name, self._basename_glob)
|
|
418
|
+
|
|
419
|
+
return False
|
|
434
420
|
|
|
435
421
|
|
|
436
422
|
def _find_file_duplicates(seen, folder):
|