Skip to content

Commit 5bdff0e

Browse files
committed
move the logic to the index_sources function
1 parent f016977 commit 5bdff0e

File tree

5 files changed

+80
-56
lines changed

5 files changed

+80
-56
lines changed

examples/pip_parse_vendored/requirements.bzl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ all_data_requirements = [
3333
]
3434

3535
_packages = [
36-
("my_project_pip_deps_vendored_certifi", "certifi==2023.7.22 --hash=sha256:539cc1d13202e33ca466e88b2807e29f4c13049d6d87031a3c110744495cb082 --hash=sha256:92d6037539857d8206b8f6ae472e8b77db8058fec5937a1ef3f54304089edbb9"),
37-
("my_project_pip_deps_vendored_charset_normalizer", "charset-normalizer==2.1.1 --hash=sha256:5a3d016c7c547f69d6f81fb0db9449ce888b418b5b9952cc5e6e66843e9dd845 --hash=sha256:83e9a75d1911279afd89352c68b45348559d1fc0506b054b346651b5e7fee29f"),
38-
("my_project_pip_deps_vendored_idna", "idna==3.4 --hash=sha256:814f528e8dead7d329833b91c5faa87d60bf71824cd12a7530b5526063d02cb4 --hash=sha256:90b77e79eaa3eba6de819a0c442c0b4ceefc341a7a2ab77d7562bf49f425c5c2"),
39-
("my_project_pip_deps_vendored_requests", "requests==2.28.1 --hash=sha256:7c5599b102feddaa661c826c56ab4fee28bfd17f5abca1ebbe3e7f19d7c97983 --hash=sha256:8fefa2a1a1365bf5520aac41836fbee479da67864514bdb821f31ce07ce65349"),
40-
("my_project_pip_deps_vendored_urllib3", "urllib3==1.26.13 --hash=sha256:47cc05d99aaa09c9e72ed5809b60e7ba354e64b59c9c173ac3018642d8bb41fc --hash=sha256:c083dd0dce68dbfbe1129d5271cb90f9447dea7d52097c6e0126120c521ddea8"),
36+
("my_project_pip_deps_vendored_certifi", "certifi==2023.7.22 --hash=sha256:539cc1d13202e33ca466e88b2807e29f4c13049d6d87031a3c110744495cb082 --hash=sha256:92d6037539857d8206b8f6ae472e8b77db8058fec5937a1ef3f54304089edbb9"),
37+
("my_project_pip_deps_vendored_charset_normalizer", "charset-normalizer==2.1.1 --hash=sha256:5a3d016c7c547f69d6f81fb0db9449ce888b418b5b9952cc5e6e66843e9dd845 --hash=sha256:83e9a75d1911279afd89352c68b45348559d1fc0506b054b346651b5e7fee29f"),
38+
("my_project_pip_deps_vendored_idna", "idna==3.4 --hash=sha256:814f528e8dead7d329833b91c5faa87d60bf71824cd12a7530b5526063d02cb4 --hash=sha256:90b77e79eaa3eba6de819a0c442c0b4ceefc341a7a2ab77d7562bf49f425c5c2"),
39+
("my_project_pip_deps_vendored_requests", "requests==2.28.1 --hash=sha256:7c5599b102feddaa661c826c56ab4fee28bfd17f5abca1ebbe3e7f19d7c97983 --hash=sha256:8fefa2a1a1365bf5520aac41836fbee479da67864514bdb821f31ce07ce65349"),
40+
("my_project_pip_deps_vendored_urllib3", "urllib3==1.26.13 --hash=sha256:47cc05d99aaa09c9e72ed5809b60e7ba354e64b59c9c173ac3018642d8bb41fc --hash=sha256:c083dd0dce68dbfbe1129d5271cb90f9447dea7d52097c6e0126120c521ddea8"),
4141
]
4242
_config = {
4343
"download_only": False,

python/private/pypi/extension.bzl

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def _create_whl_repos(
292292
args.pop("download_only", None)
293293

294294
repo_name = whl_repo_name(pip_name, distribution.filename, distribution.sha256)
295-
args["requirement"] = _requirement_without_marker(requirement.srcs.requirement)
295+
args["requirement"] = requirement.srcs.requirement
296296
args["urls"] = [distribution.url]
297297
args["sha256"] = distribution.sha256
298298
args["filename"] = distribution.filename
@@ -324,7 +324,7 @@ def _create_whl_repos(
324324
logger.warn(lambda: "falling back to pip for installing the right file for {}".format(requirement.requirement_line))
325325

326326
args = dict(whl_library_args) # make a copy
327-
args["requirement"] = _requirement_without_marker(requirement.requirement_line)
327+
args["requirement"] = requirement.srcs.requirement_line
328328
if requirement.extra_pip_args:
329329
args["extra_pip_args"] = requirement.extra_pip_args
330330

@@ -535,25 +535,6 @@ You cannot use both the additive_build_content and additive_build_content_file a
535535
is_reproducible = is_reproducible,
536536
)
537537

538-
def _requirement_without_marker(requirement):
539-
requirement, _, maybe_hashes = requirement.partition(";")
540-
ret = requirement.strip(" ")
541-
_, marker, maybe_hashes = maybe_hashes.partition("--hash=")
542-
if not maybe_hashes:
543-
ret, marker, maybe_hashes = ret.partition("--hash=")
544-
545-
if maybe_hashes:
546-
return "{} {}{}".format(
547-
ret.strip(" "),
548-
marker.strip(" "),
549-
" --hash=".join([
550-
h.strip(" ")
551-
for h in maybe_hashes.split("--hash=")
552-
]),
553-
)
554-
else:
555-
return ret
556-
557538
def _pip_impl(module_ctx):
558539
"""Implementation of a class tag that creates the pip hub and corresponding pip spoke whl repositories.
559540

python/private/pypi/index_sources.bzl

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,43 @@ def index_sources(line):
2626
line(str): The requirements.txt entry.
2727
2828
Returns:
29-
A struct with shas attribute containing a list of shas to download from pypi_index.
29+
A struct with shas attribute containing:
30+
* `shas` - a list of shas to download from pypi_index.
31+
* `version` - the version of the package.
32+
* `marker` - the marker as per PEP508 spec.
33+
* `requirement` - a requirement line without the marker. This can
34+
be given to `pip` to install a package.
3035
"""
36+
line = line.replace("\\", " ")
3137
head, _, maybe_hashes = line.partition(";")
3238
_, _, version = head.partition("==")
3339
version = version.partition(" ")[0].strip()
3440

35-
if "@" in head:
36-
shas = []
37-
else:
38-
maybe_hashes = maybe_hashes or line
39-
shas = [
40-
sha.strip()
41-
for sha in maybe_hashes.split("--hash=sha256:")[1:]
42-
]
41+
marker, _, _ = maybe_hashes.partition("--hash=")
42+
maybe_hashes = maybe_hashes or line
43+
shas = [
44+
sha.strip()
45+
for sha in maybe_hashes.split("--hash=sha256:")[1:]
46+
]
4347

48+
marker = marker.strip()
4449
if head == line:
45-
head = line.partition("--hash=")[0].strip()
50+
requirement = line.partition("--hash=")[0].strip()
4651
else:
47-
head = head + ";" + maybe_hashes.partition("--hash=")[0].strip()
52+
requirement = head.strip()
53+
54+
requirement_line = "{} {}".format(
55+
requirement,
56+
" ".join(["--hash=sha256:{}".format(sha) for sha in shas]),
57+
).strip()
58+
if "@" in head:
59+
requirement = requirement_line
60+
shas = []
4861

4962
return struct(
50-
requirement = line if not shas else head,
63+
requirement = requirement,
64+
requirement_line = requirement_line,
5165
version = version,
5266
shas = sorted(shas),
67+
marker = marker,
5368
)

python/private/pypi/pip_repository.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def _pip_repository_impl(rctx):
101101
if not r:
102102
continue
103103
options = options or r.extra_pip_args
104-
selected_requirements[name] = r.requirement_line
104+
selected_requirements[name] = r.srcs.requirement_line
105105

106106
bzl_packages = sorted(selected_requirements.keys())
107107

tests/pypi/index_sources/index_sources_tests.bzl

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,62 @@ load("//python/private/pypi:index_sources.bzl", "index_sources") # buildifier:
2020
_tests = []
2121

2222
def _test_no_simple_api_sources(env):
23-
inputs = [
24-
"foo==0.0.1",
25-
"foo==0.0.1 @ https://someurl.org",
26-
"foo==0.0.1 @ https://someurl.org --hash=sha256:deadbeef",
27-
"foo==0.0.1 @ https://someurl.org; python_version < 2.7 --hash=sha256:deadbeef",
28-
]
29-
for input in inputs:
23+
inputs = {
24+
"foo==0.0.1": struct(
25+
requirement = "foo==0.0.1",
26+
marker = "",
27+
),
28+
"foo==0.0.1 @ https://someurl.org": struct(
29+
requirement = "foo==0.0.1 @ https://someurl.org",
30+
marker = "",
31+
),
32+
"foo==0.0.1 @ https://someurl.org --hash=sha256:deadbeef": struct(
33+
requirement = "foo==0.0.1 @ https://someurl.org --hash=sha256:deadbeef",
34+
marker = "",
35+
),
36+
"foo==0.0.1 @ https://someurl.org; python_version < \"2.7\"\\ --hash=sha256:deadbeef": struct(
37+
requirement = "foo==0.0.1 @ https://someurl.org --hash=sha256:deadbeef",
38+
marker = "python_version < \"2.7\"",
39+
),
40+
}
41+
for input, want in inputs.items():
3042
got = index_sources(input)
3143
env.expect.that_collection(got.shas).contains_exactly([])
3244
env.expect.that_str(got.version).equals("0.0.1")
45+
env.expect.that_str(got.requirement).equals(want.requirement)
46+
env.expect.that_str(got.requirement_line).equals(got.requirement)
47+
env.expect.that_str(got.marker).equals(want.marker)
3348

3449
_tests.append(_test_no_simple_api_sources)
3550

3651
def _test_simple_api_sources(env):
3752
tests = {
38-
"foo==0.0.2 --hash=sha256:deafbeef --hash=sha256:deadbeef": [
39-
"deadbeef",
40-
"deafbeef",
41-
],
42-
"foo[extra]==0.0.2; (python_version < 2.7 or something_else == \"@\") --hash=sha256:deafbeef --hash=sha256:deadbeef": [
43-
"deadbeef",
44-
"deafbeef",
45-
],
53+
"foo==0.0.2 --hash=sha256:deafbeef --hash=sha256:deadbeef": struct(
54+
shas = [
55+
"deadbeef",
56+
"deafbeef",
57+
],
58+
marker = "",
59+
requirement = "foo==0.0.2",
60+
requirement_line = "foo==0.0.2 --hash=sha256:deafbeef --hash=sha256:deadbeef",
61+
),
62+
"foo[extra]==0.0.2; (python_version < 2.7 or extra == \"@\") --hash=sha256:deafbeef --hash=sha256:deadbeef": struct(
63+
shas = [
64+
"deadbeef",
65+
"deafbeef",
66+
],
67+
marker = "(python_version < 2.7 or extra == \"@\")",
68+
requirement = "foo[extra]==0.0.2",
69+
requirement_line = "foo[extra]==0.0.2 --hash=sha256:deafbeef --hash=sha256:deadbeef",
70+
),
4671
}
47-
for input, want_shas in tests.items():
72+
for input, want in tests.items():
4873
got = index_sources(input)
49-
env.expect.that_collection(got.shas).contains_exactly(want_shas)
74+
env.expect.that_collection(got.shas).contains_exactly(want.shas)
5075
env.expect.that_str(got.version).equals("0.0.2")
76+
env.expect.that_str(got.requirement).equals(want.requirement)
77+
env.expect.that_str(got.requirement_line).equals(want.requirement_line)
78+
env.expect.that_str(got.marker).equals(want.marker)
5179

5280
_tests.append(_test_simple_api_sources)
5381

0 commit comments

Comments
 (0)