Skip to content

Commit

Permalink
fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
alextremblay committed Feb 4, 2024
1 parent 6ac18af commit 97cadfc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/pipx/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from typing import Optional

from pipx.constants import WINDOWS
from pipx.util import PipxError
from pipx.standalone_python import download_python_build_standalone
from pipx.util import PipxError

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -59,10 +59,10 @@ def find_python_interpreter(python_version: str) -> str:

if shutil.which(python_version):
return python_version

try:
py_executable = download_python_build_standalone(python_version)
return py_executable
standalone_executable = download_python_build_standalone(python_version)
return standalone_executable
except PipxError as e:
raise InterpreterResolutionError(source="PATH", version=python_version) from e

Expand Down
41 changes: 17 additions & 24 deletions src/pipx/standalone_python.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import datetime
import hashlib
import json
import shutil
import platform
import re
import tarfile
import urllib.error
from functools import partial
from typing import Any
from urllib.request import urlopen
import datetime
from functools import partial
import hashlib
import tarfile

from pipx.animate import animate
from pipx.constants import PIPX_STANDALONE_PYTHON_CACHEDIR
from pipx.util import PipxError
from pipx.animate import animate

# Much of the code in this module is adapted with extreme gratitude from
# https://github.com/tusharsadhwani/yen/blob/main/src/yen/github.py

MACHINE_SUFFIX = {
MACHINE_SUFFIX: dict[str, dict[str, Any]] = {
"Darwin": {
"arm64": "aarch64-apple-darwin-install_only.tar.gz",
"x86_64": "x86_64-apple-darwin-install_only.tar.gz",
Expand All @@ -35,28 +34,24 @@
"Windows": {"AMD64": "x86_64-pc-windows-msvc-shared-install_only.tar.gz"},
}

GITHUB_API_URL = (
"https://api.github.com/repos/indygreg/python-build-standalone/releases/latest"
)
GITHUB_API_URL = "https://api.github.com/repos/indygreg/python-build-standalone/releases/latest"
PYTHON_VERSION_REGEX = re.compile(r"cpython-(\d+\.\d+\.\d+)")


class NotAvailable(Exception):
"""Raised when the asked Python version is not available."""



def download_python_build_standalone(python_version: str):
"""When all other python executable resolutions have failed,
attempt to download and use an apropriate python build
"""When all other python executable resolutions have failed,
attempt to download and use an appropriate python build
from https://github.com/indygreg/python-build-standalone
and unpack it into the pipx shared directory."""

# python_version can be a bare version number like "3.9" or a "binary name" like python3.10
# let's coerce it to a bare version number
python_version = re.sub(r"[c]?python", "", python_version)


install_dir = PIPX_STANDALONE_PYTHON_CACHEDIR / python_version

if install_dir.exists():
Expand All @@ -65,9 +60,9 @@ def download_python_build_standalone(python_version: str):
try:
full_version, download_link = resolve_python_version(python_version)
except NotAvailable as e:
raise PipxError(f"Unable to aquire a standalone python build matching {python_version}.") from e
download_dir = PIPX_STANDALONE_PYTHON_CACHEDIR / f'{python_version}_install_only'
raise PipxError(f"Unable to acquire a standalone python build matching {python_version}.") from e

download_dir = PIPX_STANDALONE_PYTHON_CACHEDIR / f"{python_version}_install_only"
download_dir.mkdir(parents=True, exist_ok=True)
install_dir.mkdir(parents=True, exist_ok=True)

Expand All @@ -82,7 +77,7 @@ def download_python_build_standalone(python_version: str):
archive_file.write(data)
except urllib.error.URLError as e:
raise PipxError(f"Unable to download python {full_version} build.") from e

# unpack the python build
with animate(f"Unpacking python {full_version} build", True):
# Calculate checksum
Expand All @@ -94,8 +89,7 @@ def download_python_build_standalone(python_version: str):
expected_checksum = urlopen(checksum_link).read().decode().rstrip("\n")
if checksum != expected_checksum:
raise PipxError(
f"Checksum mismatch for python {full_version} build. "
f"Expected {expected_checksum}, got {checksum}."
f"Checksum mismatch for python {full_version} build. " f"Expected {expected_checksum}, got {checksum}."
)

with tarfile.open(archive_path, mode="r:gz") as tar:
Expand Down Expand Up @@ -150,11 +144,9 @@ def list_pythons() -> dict[str, str]:
libc_version = platform.libc_ver()[0] or "musl"
download_link_suffix = download_link_suffix[libc_version]

python_releases = get_or_update_index()['releases']
python_releases = get_or_update_index()["releases"]

available_python_links = [
link for link in python_releases if link.endswith(download_link_suffix)
]
available_python_links = [link for link in python_releases if link.endswith(download_link_suffix)]

python_versions: dict[str, str] = {}
for link in available_python_links:
Expand All @@ -174,6 +166,7 @@ def list_pythons() -> dict[str, str]:
}
return sorted_python_versions


def resolve_python_version(requested_version: str):
pythons = list_pythons()

Expand Down

0 comments on commit 97cadfc

Please sign in to comment.