Skip to content

Commit

Permalink
Avoid redundant installation of pip and setuptools
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsmith committed Jul 24, 2023
1 parent 6034adf commit cb3371b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
4 changes: 1 addition & 3 deletions server/pypi/build-wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ class Abi:


class BuildWheel:

def main(self):
try:
self.parse_args()
Expand Down Expand Up @@ -126,8 +125,7 @@ def unpack_and_build(self):
assert_isdir(self.build_dir)
else:
ensure_empty(self.build_dir)
run(f"python{self.python} -m venv {build_env_dir}")
run(f"{self.pip} install -r {PYPI_DIR}/requirements-build-env.txt")
run(f"python{self.python} {PYPI_DIR}/create-build-env.py {build_env_dir}")
self.unpack_source()
self.apply_patches()

Expand Down
33 changes: 33 additions & 0 deletions server/pypi/create-build-env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python3

import argparse
import ensurepip
from glob import glob
import os
from os.path import abspath, dirname, exists
from shlex import split
import subprocess
import venv


PYPI_DIR = abspath(dirname(__file__))

ap = argparse.ArgumentParser()
ap.add_argument("build_env_dir")
args = ap.parse_args()

# Installing Python's bundled pip and setuptools into the environment is pointless since
# we're just going to replace them anyway. Instead, use the bundled pip to install our
# requirements file directly. This saves about 3.5 seconds on Python 3.8, and 6 seconds
# on Python 3.11.
assert not exists(args.build_env_dir)
venv.create(args.build_env_dir, with_pip=False)

pip_whl, = glob(f"{ensurepip.__path__[0]}/_bundled/pip-*.whl") # Note comma
env = os.environ.copy()
env["PYTHONPATH"] = pip_whl

subprocess.run(
split(f"{args.build_env_dir}/bin/python -m") +
split(f"pip install -r {PYPI_DIR}/requirements-build-env.txt"),
env=env, check=True)

0 comments on commit cb3371b

Please sign in to comment.