Skip to content

Commit af5aeb7

Browse files
committed
extract out url and filename
todo: check later if filename is necessary. Maybe not.
1 parent e7d2f09 commit af5aeb7

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

python/private/pypi/index_sources.bzl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ def index_sources(line):
3232
* `marker` - str; the marker expression, as per PEP508 spec.
3333
* `requirement` - str; a requirement line without the marker. This can
3434
be given to `pip` to install a package.
35+
* `url` - str; URL if the requirement specifies a direct URL.
36+
* `filename` - str; filename if URL is present, extracted from the URL.
3537
"""
3638
line = line.replace("\\", " ")
3739
head, _, maybe_hashes = line.partition(";")
@@ -55,14 +57,26 @@ def index_sources(line):
5557
requirement,
5658
" ".join(["--hash=sha256:{}".format(sha) for sha in shas]),
5759
).strip()
60+
61+
# Extract URL if present
62+
url = ""
63+
filename = ""
5864
if "@" in head:
5965
requirement = requirement_line
60-
shas = []
66+
# Extract URL from direct URL format
67+
url = requirement.split("@")[1].split("#")[0].strip()
68+
# Extract filename from URL
69+
if url:
70+
filename = url.rpartition("/")[2]
71+
if not filename:
72+
filename = url.rpartition("/")[0].rpartition("/")[2]
6173

6274
return struct(
6375
requirement = requirement,
6476
requirement_line = requirement_line,
6577
version = version,
6678
shas = sorted(shas),
6779
marker = marker,
80+
url = url,
81+
filename = filename,
6882
)

tests/pypi/index_sources/index_sources_tests.bzl

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,45 @@ def _test_no_simple_api_sources(env):
2424
"foo==0.0.1": struct(
2525
requirement = "foo==0.0.1",
2626
marker = "",
27+
url = "",
28+
filename = "",
2729
),
2830
"foo==0.0.1 @ https://someurl.org": struct(
2931
requirement = "foo==0.0.1 @ https://someurl.org",
3032
marker = "",
33+
url = "https://someurl.org",
34+
filename = "someurl.org",
3135
),
32-
"foo==0.0.1 @ https://someurl.org --hash=sha256:deadbeef": struct(
33-
requirement = "foo==0.0.1 @ https://someurl.org --hash=sha256:deadbeef",
36+
"foo==0.0.1 @ https://someurl.org/package.whl": struct(
37+
requirement = "foo==0.0.1 @ https://someurl.org/package.whl",
3438
marker = "",
39+
url = "https://someurl.org/package.whl",
40+
filename = "package.whl",
3541
),
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",
42+
"foo==0.0.1 @ https://someurl.org/package.whl --hash=sha256:deadbeef": struct(
43+
requirement = "foo==0.0.1 @ https://someurl.org/package.whl --hash=sha256:deadbeef",
44+
marker = "",
45+
url = "https://someurl.org/package.whl",
46+
filename = "package.whl",
47+
shas = ["deadbeef"],
48+
),
49+
"foo==0.0.1 @ https://someurl.org/package.whl; python_version < \"2.7\"\\ --hash=sha256:deadbeef": struct(
50+
requirement = "foo==0.0.1 @ https://someurl.org/package.whl --hash=sha256:deadbeef",
3851
marker = "python_version < \"2.7\"",
52+
url = "https://someurl.org/package.whl",
53+
filename = "package.whl",
54+
shas = ["deadbeef"],
3955
),
4056
}
4157
for input, want in inputs.items():
4258
got = index_sources(input)
43-
env.expect.that_collection(got.shas).contains_exactly([])
59+
env.expect.that_collection(got.shas).contains_exactly(want.shas if hasattr(want, "shas") else [])
4460
env.expect.that_str(got.version).equals("0.0.1")
4561
env.expect.that_str(got.requirement).equals(want.requirement)
4662
env.expect.that_str(got.requirement_line).equals(got.requirement)
4763
env.expect.that_str(got.marker).equals(want.marker)
64+
env.expect.that_str(got.url).equals(want.url)
65+
env.expect.that_str(got.filename).equals(want.filename)
4866

4967
_tests.append(_test_no_simple_api_sources)
5068

0 commit comments

Comments
 (0)