Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion python/private/pypi/whl_installer/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ py_library(
name = "lib",
srcs = [
"arguments.py",
"wheel.py",
"wheel_installer.py",
],
visibility = [
Expand Down
5 changes: 0 additions & 5 deletions python/private/pypi/whl_installer/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ def parser(**kwargs: Any) -> argparse.ArgumentParser:
help="Use 'pip download' instead of 'pip wheel'. Disables building wheels from source, but allows use of "
"--platform, --python-version, --implementation, and --abi in --extra_pip_args.",
)
parser.add_argument(
"--whl-file",
type=pathlib.Path,
help="Extract a whl file to be used within Bazel.",
)
return parser


Expand Down
105 changes: 0 additions & 105 deletions python/private/pypi/whl_installer/wheel.py

This file was deleted.

54 changes: 1 addition & 53 deletions python/private/pypi/whl_installer/wheel_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
from tempfile import NamedTemporaryFile
from typing import Dict, List, Optional, Set, Tuple

from pip._vendor.packaging.utils import canonicalize_name

from python.private.pypi.whl_installer import arguments, wheel
from python.private.pypi.whl_installer import arguments


def _configure_reproducible_wheels() -> None:
Expand Down Expand Up @@ -55,63 +53,13 @@ def _configure_reproducible_wheels() -> None:
os.environ["PYTHONHASHSEED"] = "0"


def _parse_requirement_for_extra(
requirement: str,
) -> Tuple[Optional[str], Optional[Set[str]]]:
"""Given a requirement string, returns the requirement name and set of extras, if extras specified.
Else, returns (None, None)
"""

# https://www.python.org/dev/peps/pep-0508/#grammar
extras_pattern = re.compile(
r"^\s*([0-9A-Za-z][0-9A-Za-z_.\-]*)\s*\[\s*([0-9A-Za-z][0-9A-Za-z_.\-]*(?:\s*,\s*[0-9A-Za-z][0-9A-Za-z_.\-]*)*)\s*\]"
)

matches = extras_pattern.match(requirement)
if matches:
return (
canonicalize_name(matches.group(1)),
{extra.strip() for extra in matches.group(2).split(",")},
)

return None, None


def _extract_wheel(
wheel_file: str,
extras: Dict[str, Set[str]],
installation_dir: Path = Path("."),
) -> None:
"""Extracts wheel into given directory and creates py_library and filegroup targets.

Args:
wheel_file: the filepath of the .whl
installation_dir: the destination directory for installation of the wheel.
extras: a list of extras to add as dependencies for the installed wheel
"""

whl = wheel.Wheel(wheel_file)
whl.unzip(installation_dir)


def main() -> None:
args = arguments.parser(description=__doc__).parse_args()
deserialized_args = dict(vars(args))
arguments.deserialize_structured_args(deserialized_args)

_configure_reproducible_wheels()

if args.whl_file:
whl = Path(args.whl_file)

name, extras_for_pkg = _parse_requirement_for_extra(args.requirement)
extras = {name: extras_for_pkg} if extras_for_pkg and name else dict()
_extract_wheel(
wheel_file=whl,
extras=extras,
)
return

pip_args = (
[sys.executable, "-m", "pip"]
+ (["--isolated"] if args.isolated else [])
Expand Down
44 changes: 7 additions & 37 deletions python/private/pypi/whl_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

""

load("@rules_python_internal//:rules_python_config.bzl", rp_config = "config")
load("//python/private:auth.bzl", "AUTH_ATTRS", "get_auth")
load("//python/private:envsubst.bzl", "envsubst")
load("//python/private:is_standalone_interpreter.bzl", "is_standalone_interpreter")
Expand Down Expand Up @@ -261,22 +260,6 @@ def _create_repository_execution_environment(rctx, python_interpreter, logger =
env[_CPPFLAGS] = " ".join(cppflags)
return env

def _extract_whl_py(rctx, *, python_interpreter, args, whl_path, environment, logger):
pypi_repo_utils.execute_checked(
rctx,
op = "whl_library.ExtractWheel({}, {})".format(rctx.attr.name, whl_path),
python = python_interpreter,
arguments = args + [
"--whl-file",
whl_path,
],
srcs = rctx.attr._python_srcs,
environment = environment,
quiet = rctx.attr.quiet,
timeout = rctx.attr.timeout,
logger = logger,
)

def _get_entry_points(rctx, install_dir_path, metadata):
dist_info_dir = "{}-{}.dist-info".format(
metadata.name.replace("-", "_"),
Expand Down Expand Up @@ -376,11 +359,10 @@ def _whl_library_impl(rctx):
# build deps from PyPI (e.g. `flit_core`) if they are missing.
extra_pip_args.extend(["--find-links", "."])

enable_pipstar_extract = rp_config.bazel_8_or_later

# When pipstar is enabled, Python isn't used, so there's no need
# to setup env vars to run Python, unless we need to build an sdist
if enable_pipstar_extract and whl_path and not rctx.attr.whl_patches:
# When we already have a wheel and there are no patches, Python isn't used,
# so there's no need to setup env vars to run Python, unless we need to
# build an sdist or resolve a requirement.
if whl_path and not rctx.attr.whl_patches:
environment = {}
args = []
python_interpreter = None
Expand Down Expand Up @@ -446,17 +428,7 @@ def _whl_library_impl(rctx):
timeout = rctx.attr.timeout,
)

if enable_pipstar_extract:
whl_extract(rctx, whl_path = whl_path, logger = logger)
else:
_extract_whl_py(
rctx,
python_interpreter = python_interpreter,
args = args,
whl_path = whl_path,
environment = environment,
logger = logger,
)
whl_extract(rctx, whl_path = whl_path, logger = logger)

install_dir_path = whl_path.dirname.get_child("site-packages")
metadata = whl_metadata(
Expand Down Expand Up @@ -513,9 +485,8 @@ repo(
_remove_files(rctx, "BUILD", "BUILD.bazel")
rctx.file("BUILD.bazel", build_file_contents)

if enable_pipstar_extract:
if hasattr(rctx, "repo_metadata"):
return rctx.repo_metadata(reproducible = True)
if hasattr(rctx, "repo_metadata"):
return rctx.repo_metadata(reproducible = True)
Comment thread
rickeylev marked this conversation as resolved.

return None

Expand Down Expand Up @@ -632,7 +603,6 @@ way to define whl_library and move whl patching to a separate place. INTERNAL US
"_python_srcs": attr.label_list(
# Used as a default value in a rule to ensure we fetch the dependencies.
default = [
Label("//python/private/pypi/whl_installer:wheel.py"),
Label("//python/private/pypi/whl_installer:wheel_installer.py"),
Label("//python/private/pypi/whl_installer:arguments.py"),
] + record_files.values(),
Expand Down
19 changes: 1 addition & 18 deletions tests/pypi/whl_installer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
load("//python:py_test.bzl", "py_test")

alias(
name = "lib",
actual = "//python/private/pypi/whl_installer:lib",
)

py_test(
name = "arguments_test",
size = "small",
srcs = [
"arguments_test.py",
],
deps = [
":lib",
],
)

py_test(
name = "wheel_installer_test",
size = "small",
srcs = [
"wheel_installer_test.py",
],
data = ["//examples/wheel:minimal_with_py_package"],
deps = [
":lib",
"//python/private/pypi/whl_installer:lib",
],
)
2 changes: 1 addition & 1 deletion tests/pypi/whl_installer/arguments_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import json
import unittest

from python.private.pypi.whl_installer import arguments, wheel
from python.private.pypi.whl_installer import arguments


class ArgumentsTestCase(unittest.TestCase):
Expand Down
Loading