Skip to content

Commit 28cfb11

Browse files
authored
Fixed extra_pip_arg parsing error in pip_repository rules. (#613)
1 parent 1bbc6ce commit 28cfb11

File tree

5 files changed

+66
-6
lines changed

5 files changed

+66
-6
lines changed

examples/pip_repository_annotations/WORKSPACE

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ workspace(name = "pip_repository_annotations_example")
22

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

5-
http_archive(
5+
local_repository(
66
name = "rules_python",
7-
sha256 = "cd6730ed53a002c56ce4e2f396ba3b3be262fd7cb68339f0377a45e8227fe332",
8-
url = "https://github.com/bazelbuild/rules_python/releases/download/0.5.0/rules_python-0.5.0.tar.gz",
7+
path = "../..",
98
)
109

1110
http_archive(
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
# This flag allows for regression testing requirements arguments in
2+
# `pip_repository` rules.
3+
--extra-index-url https://pypi.python.org/simple/
4+
15
wheel

examples/pip_repository_annotations/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#
55
# bazel run //:requirements.update
66
#
7+
--extra-index-url https://pypi.python.org/simple/
8+
79
wheel==0.37.1 \
810
--hash=sha256:4bdcd7d840138086126cd09254dc6195fb4fc6f01c050a1d7236f2630db1d22a \
911
--hash=sha256:e9a504e793efbca1b8e0e9cb979a249cf4a0a7b5b8c9e8b65a5e39d49529c1c4

python/pip_install/parse_requirements_to_bzl/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,11 @@ def main() -> None:
209209
arguments.parse_common_args(parser)
210210
args = parser.parse_args()
211211

212+
whl_library_args = parse_whl_library_args(args)
213+
212214
# Check for any annotations which match packages in the locked requirements file
213215
install_requirements = parse_install_requirements(
214-
args.requirements_lock, args.extra_pip_args
216+
args.requirements_lock, whl_library_args["extra_pip_args"]
215217
)
216218
req_names = sorted([req.name for req, _ in install_requirements])
217219
annotations = args.annotations.collect(req_names)
@@ -230,8 +232,6 @@ def main() -> None:
230232
)
231233

232234
with open("requirements.bzl", "w") as requirement_file:
233-
whl_library_args = parse_whl_library_args(args)
234-
235235
requirement_file.write(
236236
generate_parsed_requirements_contents(
237237
requirements_lock=args.requirements_lock,

python/pip_install/parse_requirements_to_bzl/parse_requirements_to_bzl_test.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@
33
import tempfile
44
import unittest
55
from pathlib import Path
6+
from textwrap import dedent
7+
8+
from pip._internal.req.req_install import InstallRequirement
69

710
from python.pip_install.parse_requirements_to_bzl import (
811
generate_parsed_requirements_contents,
12+
parse_install_requirements,
913
parse_whl_library_args,
1014
)
1115

1216

1317
class TestParseRequirementsToBzl(unittest.TestCase):
18+
maxDiff = None
19+
1420
def test_generated_requirements_bzl(self) -> None:
1521
with tempfile.TemporaryDirectory() as temp_dir:
1622
requirements_lock = Path(temp_dir) / "requirements.txt"
@@ -64,6 +70,55 @@ def test_generated_requirements_bzl(self) -> None:
6470
# Assert it gets set to an empty dict by default.
6571
self.assertIn("'environment': {}", contents, contents)
6672

73+
def test_parse_install_requirements_with_args(self):
74+
# Test requirements files with varying arguments
75+
for requirement_args in ("", "--index-url https://index.python.com"):
76+
with tempfile.TemporaryDirectory() as temp_dir:
77+
requirements_lock = Path(temp_dir) / "requirements.txt"
78+
requirements_lock.write_text(
79+
dedent(
80+
"""\
81+
{}
82+
83+
wheel==0.37.1 \\
84+
--hash=sha256:4bdcd7d840138086126cd09254dc6195fb4fc6f01c050a1d7236f2630db1d22a \\
85+
--hash=sha256:e9a504e793efbca1b8e0e9cb979a249cf4a0a7b5b8c9e8b65a5e39d49529c1c4
86+
# via -r requirements.in
87+
setuptools==58.2.0 \\
88+
--hash=sha256:2551203ae6955b9876741a26ab3e767bb3242dafe86a32a749ea0d78b6792f11 \
89+
--hash=sha256:2c55bdb85d5bb460bd2e3b12052b677879cffcf46c0c688f2e5bf51d36001145
90+
# via -r requirements.in
91+
""".format(
92+
requirement_args
93+
)
94+
)
95+
)
96+
97+
install_req_and_lines = parse_install_requirements(
98+
str(requirements_lock), ["-v"]
99+
)
100+
101+
# There should only be two entries for the two requirements
102+
self.assertEqual(len(install_req_and_lines), 2)
103+
104+
# The first index in each tuple is expected to be an `InstallRequirement` object
105+
self.assertIsInstance(install_req_and_lines[0][0], InstallRequirement)
106+
self.assertIsInstance(install_req_and_lines[1][0], InstallRequirement)
107+
108+
# Ensure the requirements text is correctly parsed with the trailing arguments
109+
self.assertTupleEqual(
110+
install_req_and_lines[0][1:],
111+
(
112+
"wheel==0.37.1 --hash=sha256:4bdcd7d840138086126cd09254dc6195fb4fc6f01c050a1d7236f2630db1d22a --hash=sha256:e9a504e793efbca1b8e0e9cb979a249cf4a0a7b5b8c9e8b65a5e39d49529c1c4",
113+
),
114+
)
115+
self.assertTupleEqual(
116+
install_req_and_lines[1][1:],
117+
(
118+
"setuptools==58.2.0 --hash=sha256:2551203ae6955b9876741a26ab3e767bb3242dafe86a32a749ea0d78b6792f11 --hash=sha256:2c55bdb85d5bb460bd2e3b12052b677879cffcf46c0c688f2e5bf51d36001145",
119+
),
120+
)
121+
67122

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

0 commit comments

Comments
 (0)