Repository navigation
Expand file tree
/
Copy pathfixup.py
More file actions
89 lines (75 loc) · 3.04 KB
/
Copy pathfixup.py
File metadata and controls
89 lines (75 loc) · 3.04 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
"""Set the prefix of an unpacked build made by `relocatable.py gather`.
The build was configured with a placeholder prefix. This script replaces the
placeholder with the tree's actual location in the text files that contain
it. Binary files are never modified.
Run it once, after unpacking the tree where it is going to live, with any
Python 3.12 or newer other than the one in the tree:
python3 fixup.py PATH/TO/TREE
It fails if no text file in the tree has the placeholder, which is the case
for a tree it has already been run on.
"""
# /// script
# requires-python = ">=3.12"
# ///
import argparse
import os
import pathlib
import sys
PLACEHOLDER = "/the/knights/who/say/ni".ljust(255, "i")
def fixup(tree: pathlib.Path) -> tuple[list[pathlib.Path], list[pathlib.Path]]:
"""Replace the placeholder prefix in the text files of *tree*.
Return the text files that were rewritten and the binary files that
still contain the placeholder.
"""
tree = tree.resolve(strict=True)
if unsafe := set(os.fspath(tree)) & set("'\"\\\n"):
# The prefix ends up inside Python string literals in _sysconfigdata.
raise ValueError(f"{tree} contains {''.join(sorted(unsafe))!r}")
placeholder = os.fsencode(PLACEHOLDER)
prefix = os.fsencode(tree)
text = {}
binary = []
for root, _, filenames in tree.walk():
for filename in filenames:
path = root / filename
if path.is_symlink():
continue
contents = path.read_bytes()
if placeholder not in contents:
continue
if b"\0" in contents:
binary.append(path)
else:
text[path] = contents
if not text:
raise ValueError(
f"no text file in {tree} has the placeholder prefix; "
"it has already been fixed up or wasn't built by relocatable.py"
)
for path, contents in text.items():
path.write_bytes(contents.replace(placeholder, prefix))
return sorted(text), sorted(binary)
def main(args: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description=__doc__.partition("\n")[0])
parser.add_argument("tree", type=pathlib.Path, help="the unpacked installation")
namespace = parser.parse_args(args)
try:
text, binary = fixup(namespace.tree)
except (OSError, ValueError) as exc:
print(f"error: {exc}", file=sys.stderr)
return 1
tree = namespace.tree.resolve()
print(f"Set the prefix to {tree} in {len(text)} text files:")
for path in text:
print(f" {path.relative_to(tree)}")
if binary:
print(f"Left {len(binary)} binary files that contain the placeholder alone:")
# A .pyc file records where its source was when it was compiled.
if pyc := sum(path.suffix == ".pyc" for path in binary):
print(f" {pyc} .pyc files")
for path in binary:
if path.suffix != ".pyc":
print(f" {path.relative_to(tree)}")
return 0
if __name__ == "__main__":
sys.exit(main())