Skip to content

Commit

Permalink
Merge pull request #16 from robotpy/reduce-wheels
Browse files Browse the repository at this point in the history
Reduce wheels
  • Loading branch information
virtuald authored Jan 12, 2024
2 parents cda49dd + 62f9fee commit 03e72c6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 15 deletions.
7 changes: 7 additions & 0 deletions packages.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "3.0.8"

[packages.debugpy]
version = "1.8.0"
mod_version = "1.8.0+r2"

# NumPy cross compilation seems to be broken > 1.26?
# - https://github.com/mesonbuild/meson-python/issues/321
Expand Down Expand Up @@ -33,6 +34,7 @@ version = "1.8.0"

[packages.numpy]
version = "1.25.2"
mod_version = "1.25.2+r2"
build_pip_requirements = ["Cython==0.29.34,<3.0", "setuptools==68.2.2", "wheel==0.41.2"]
cross_pip_requirements = ["robotpy-openblas-dev"]
install_requirements = ["robotpy-openblas"]
Expand All @@ -51,12 +53,16 @@ NPY_DISABLE_SVML = "1"

[packages.pydevd]
version = "2.10.0"
mod_version = "2.10.0+r2"
strip_fail_ok = true

[packages.pyinstrument]
version = "4.6.0"
mod_version = "4.6.0+r2"

[packages."PyYAML"]
version = "6.0.1"
mod_version = "6.0.1+r2"
build_pip_requirements = [
"Cython<3.0",
]
Expand All @@ -67,3 +73,4 @@ build_pip_requirements = [

[packages."zope.interface"]
version = "6.1"
mod_version = "6.1+r2"
4 changes: 3 additions & 1 deletion should_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ def get_index_url() -> str:
cfg = tomllib.load(fp)

try:
version = cfg["packages"][args.project]["version"]
version = cfg["packages"][args.project].get("mod_version")
if version is None:
version = cfg["packages"][args.project]["version"]
except KeyError:
parser.error(f"{args.project} not found in {args.config}")

Expand Down
66 changes: 52 additions & 14 deletions whl_mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ def get_strip_exe():


def add_requirements_to_wheel(
wheel: str, project: str, version: str, reqs: typing.List[str]
wheel: str,
project: str,
version: str,
out_version: str,
strip_fail_ok: bool,
reqs: typing.List[str],
):
whldir = os.path.dirname(wheel)
if whldir == "":
Expand All @@ -39,20 +44,28 @@ def add_requirements_to_wheel(
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"
)
dist_info_path = os.path.join(unpacked_root, f"{project}-{version}.dist-info")
metadata_path = os.path.join(dist_info_path, "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
# Insert additional requirements?
if reqs:
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")

for req in reversed(reqs):
lines.insert(i, f"Requires-Dist: {req}\n")
# If we're changing the version, do that too
if version != out_version:
for i in range(len(lines)):
if lines[i].startswith("Version:"):
lines[i] = f"Version: {out_version}\n"
break

print("-" * 72)
for line in lines:
Expand All @@ -70,7 +83,24 @@ def add_requirements_to_wheel(
if fname.endswith("so") or ".so." in fname:
args = [strip_exe, full_fname]
print("+", *args)
subprocess.check_call(args)
try:
subprocess.check_call(args)
except subprocess.CalledProcessError as e:
if strip_fail_ok:
print(e)
else:
raise

# If we are changing the version, then delete RECORD and rename dist-info
if version != out_version:
os.unlink(os.path.join(dist_info_path, "RECORD"))
new_dist_info_path = os.path.join(
unpacked_root, f"{project}-{out_version}.dist-info"
)
os.rename(dist_info_path, new_dist_info_path)

# Everything worked, delete the old wheel
os.unlink(wheel)

# pack the wheel back up
args = [sys.executable, "-m", "wheel", "pack", unpacked_root, "-d", whldir]
Expand All @@ -95,6 +125,14 @@ def add_requirements_to_wheel(
except KeyError:
parser.error(f"{project} not found in {args.config}")

out_version = pkgdata.get("mod_version", None)
if out_version is None:
out_version = version

strip_fail_ok = pkgdata.get("strip_fail_ok", False)

reqs = pkgdata.get("install_requirements")
if reqs:
add_requirements_to_wheel(args.wheel, project, version, reqs)
if reqs or out_version != version:
add_requirements_to_wheel(
args.wheel, project, version, out_version, strip_fail_ok, reqs
)

0 comments on commit 03e72c6

Please sign in to comment.