forked from robotpy/roborio-wheels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhl_mod.py
executable file
·79 lines (61 loc) · 2.12 KB
/
whl_mod.py
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
#!/usr/bin/env python3
#
# Modifies a wheel to have different installation requirements
#
import argparse
import os.path
import sys
import subprocess
import toml
import tempfile
import typing
def add_requirements_to_wheel(
wheel: str, project: str, version: str, reqs: typing.List[str]
):
whldir = os.path.dirname(wheel)
if whldir == "":
whldir = "."
with tempfile.TemporaryDirectory() as tmpdir:
# unpack the wheel
args = [sys.executable, "-m", "wheel", "unpack", "-d", tmpdir, wheel]
subprocess.check_call(args)
unpacked_root = os.path.join(tmpdir, f"{project}-{version}")
# Find and modify the metadata record
metadata_path = os.path.join(
unpacked_root, f"{project}-{version}.dist-info", "METADATA"
)
with open(metadata_path, "r+") as fp:
lines = fp.readlines()
# Find the first empty line and insert our extra requirements there
i = 0
for i, line in enumerate(lines):
if line.strip() == "":
break
for req in reversed(reqs):
lines.insert(i, f"Requires-Dist: {req}\n")
print("-" * 72)
for line in lines:
print(line.strip())
print("-" * 72)
fp.seek(0)
fp.writelines(lines)
fp.truncate()
# pack the wheel back up
args = [sys.executable, "-m", "wheel", "pack", unpacked_root, "-d", whldir]
subprocess.check_call(args)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("wheel")
parser.add_argument("--config", default="packages.toml")
args = parser.parse_args()
with open(args.config) as fp:
cfg = toml.load(fp)
project, _ = os.path.basename(args.wheel).split("-", 1)
try:
pkgdata = cfg["packages"][project]
version = pkgdata["version"]
except KeyError:
parser.error(f"{project} not found in {args.config}")
reqs = pkgdata.get("install_requirements")
if reqs:
add_requirements_to_wheel(args.wheel, project, version, reqs)