Skip to content

Commit

Permalink
Add support for building wheels (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattclay authored Jul 22, 2023
1 parent ba3d335 commit b54d31b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 16 additions & 1 deletion files/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import dataclasses
import json
import os
import pathlib
import re
import shutil
import subprocess
import tempfile
import typing as t
import urllib.request

Expand Down Expand Up @@ -49,7 +51,6 @@ class Pip:

_OPTIONS = (
'--disable-pip-version-check',
'--no-cache-dir',
)

_DEFAULT_PACKAGES = dict(
Expand Down Expand Up @@ -105,6 +106,15 @@ def show_version(self) -> None:
"""Show the pip version."""
subprocess.run(self._pip_command + ['--version'] + list(self._OPTIONS), check=True)

def wheel(self, args: t.List[str], constraints: pathlib.Path) -> None:
"""Build Python wheels with the given constraints file, storing them in the pip cache."""
env = os.environ.copy()
env.update(PIP_CONSTRAINT=str(constraints))

with tempfile.TemporaryDirectory() as temp_dir:
with self._install_options_context() as options:
subprocess.run(self._pip_command + ['wheel'] + options + list(self._OPTIONS) + args, check=True, env=env, cwd=temp_dir)

def install(self, args: t.List[str]) -> None:
"""Install Python packages."""
with self._install_options_context() as options:
Expand All @@ -121,6 +131,11 @@ def check(self) -> None:
"""Check installed Python packages."""
subprocess.run(self._pip_command + ['check'] + list(self._OPTIONS), check=True)

@staticmethod
def purge_cache() -> None:
"""Purge the pip cache."""
shutil.rmtree(os.path.expanduser('~/.cache/pip')) # The `pip cache purge` command leaves behind directories.

@contextlib.contextmanager
def _install_options_context(self) -> t.List[str]:
"""Create a pip install context for the specified Python interpreter and return options needed for the installation."""
Expand Down
2 changes: 2 additions & 0 deletions files/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def setup_python(python: Python) -> None:

display.section(f'Completed setup of Python {python.version}')

pip.purge_cache()


if __name__ == '__main__':
main()

0 comments on commit b54d31b

Please sign in to comment.