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
5 changes: 2 additions & 3 deletions examples/pip_repository_annotations/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ workspace(name = "pip_repository_annotations_example")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
local_repository(
name = "rules_python",
sha256 = "cd6730ed53a002c56ce4e2f396ba3b3be262fd7cb68339f0377a45e8227fe332",
url = "https://github.com/bazelbuild/rules_python/releases/download/0.5.0/rules_python-0.5.0.tar.gz",
path = "../..",
)

http_archive(
Expand Down
4 changes: 4 additions & 0 deletions examples/pip_repository_annotations/requirements.in
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# This flag allows for regression testing requirements arguments in
# `pip_repository` rules.
--extra-index-url https://pypi.python.org/simple/

wheel
2 changes: 2 additions & 0 deletions examples/pip_repository_annotations/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#
# bazel run //:requirements.update
#
--extra-index-url https://pypi.python.org/simple/

wheel==0.37.1 \
--hash=sha256:4bdcd7d840138086126cd09254dc6195fb4fc6f01c050a1d7236f2630db1d22a \
--hash=sha256:e9a504e793efbca1b8e0e9cb979a249cf4a0a7b5b8c9e8b65a5e39d49529c1c4
Expand Down
6 changes: 3 additions & 3 deletions python/pip_install/parse_requirements_to_bzl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,11 @@ def main() -> None:
arguments.parse_common_args(parser)
args = parser.parse_args()

whl_library_args = parse_whl_library_args(args)

# Check for any annotations which match packages in the locked requirements file
install_requirements = parse_install_requirements(
args.requirements_lock, args.extra_pip_args
args.requirements_lock, whl_library_args["extra_pip_args"]
Copy link
Contributor Author

@UebelAndre UebelAndre Jan 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This matches the expectations of the function parameters before the regression:
https://github.com/bazelbuild/rules_python/blob/0.6.0/python/pip_install/parse_requirements_to_bzl/__init__.py#L68-L76

)
req_names = sorted([req.name for req, _ in install_requirements])
annotations = args.annotations.collect(req_names)
Expand All @@ -230,8 +232,6 @@ def main() -> None:
)

with open("requirements.bzl", "w") as requirement_file:
whl_library_args = parse_whl_library_args(args)

requirement_file.write(
generate_parsed_requirements_contents(
requirements_lock=args.requirements_lock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
import tempfile
import unittest
from pathlib import Path
from textwrap import dedent

from pip._internal.req.req_install import InstallRequirement

from python.pip_install.parse_requirements_to_bzl import (
generate_parsed_requirements_contents,
parse_install_requirements,
parse_whl_library_args,
)


class TestParseRequirementsToBzl(unittest.TestCase):
maxDiff = None

def test_generated_requirements_bzl(self) -> None:
with tempfile.TemporaryDirectory() as temp_dir:
requirements_lock = Path(temp_dir) / "requirements.txt"
Expand Down Expand Up @@ -64,6 +70,55 @@ def test_generated_requirements_bzl(self) -> None:
# Assert it gets set to an empty dict by default.
self.assertIn("'environment': {}", contents, contents)

def test_parse_install_requirements_with_args(self):
# Test requirements files with varying arguments
for requirement_args in ("", "--index-url https://index.python.com"):
with tempfile.TemporaryDirectory() as temp_dir:
requirements_lock = Path(temp_dir) / "requirements.txt"
requirements_lock.write_text(
dedent(
"""\
{}

wheel==0.37.1 \\
--hash=sha256:4bdcd7d840138086126cd09254dc6195fb4fc6f01c050a1d7236f2630db1d22a \\
--hash=sha256:e9a504e793efbca1b8e0e9cb979a249cf4a0a7b5b8c9e8b65a5e39d49529c1c4
# via -r requirements.in
setuptools==58.2.0 \\
--hash=sha256:2551203ae6955b9876741a26ab3e767bb3242dafe86a32a749ea0d78b6792f11 \
--hash=sha256:2c55bdb85d5bb460bd2e3b12052b677879cffcf46c0c688f2e5bf51d36001145
# via -r requirements.in
""".format(
requirement_args
)
)
)

install_req_and_lines = parse_install_requirements(
str(requirements_lock), ["-v"]
)

# There should only be two entries for the two requirements
self.assertEqual(len(install_req_and_lines), 2)

# The first index in each tuple is expected to be an `InstallRequirement` object
self.assertIsInstance(install_req_and_lines[0][0], InstallRequirement)
self.assertIsInstance(install_req_and_lines[1][0], InstallRequirement)

# Ensure the requirements text is correctly parsed with the trailing arguments
self.assertTupleEqual(
install_req_and_lines[0][1:],
(
"wheel==0.37.1 --hash=sha256:4bdcd7d840138086126cd09254dc6195fb4fc6f01c050a1d7236f2630db1d22a --hash=sha256:e9a504e793efbca1b8e0e9cb979a249cf4a0a7b5b8c9e8b65a5e39d49529c1c4",
),
)
self.assertTupleEqual(
install_req_and_lines[1][1:],
(
"setuptools==58.2.0 --hash=sha256:2551203ae6955b9876741a26ab3e767bb3242dafe86a32a749ea0d78b6792f11 --hash=sha256:2c55bdb85d5bb460bd2e3b12052b677879cffcf46c0c688f2e5bf51d36001145",
),
)


if __name__ == "__main__":
unittest.main()