Skip to content

Commit 498c150

Browse files
committed
properly handle direct URLs without shas
1 parent b4ffa24 commit 498c150

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

python/private/pypi/whl_library.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,12 @@ def _whl_library_impl(rctx):
270270
sha256 = rctx.attr.sha256,
271271
auth = get_auth(rctx, urls),
272272
)
273+
if not rctx.attr.sha256:
274+
# this is only seen when there is a direct URL reference without sha256
275+
logger.warn("Please update the requirement line to include the hash:\n{} \\\n --hash=sha256:{}".format(
276+
rctx.attr.requirement,
277+
result.sha256,
278+
))
273279

274280
if not result.success:
275281
fail("could not download the '{}' from {}:\n{}".format(filename, urls, result))

python/private/pypi/whl_repo_name.bzl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,19 @@ def whl_repo_name(filename, sha256):
3232

3333
if not filename.endswith(".whl"):
3434
# Then the filename is basically foo-3.2.1.<ext>
35-
parts.append(normalize_name(filename.rpartition("-")[0]))
36-
parts.append("sdist")
35+
name, _, tail = filename.rpartition("-")
36+
parts.append(normalize_name(name))
37+
if sha256:
38+
parts.append("sdist")
39+
version = ""
40+
else:
41+
for ext in [".tar", ".zip"]:
42+
tail, _, _ = tail.partition(ext)
43+
version = tail.replace(".", "_").replace("!", "_")
3744
else:
3845
parsed = parse_whl_name(filename)
3946
name = normalize_name(parsed.distribution)
47+
version = parsed.version.replace(".", "_").replace("!", "_")
4048
python_tag, _, _ = parsed.python_tag.partition(".")
4149
abi_tag, _, _ = parsed.abi_tag.partition(".")
4250
platform_tag, _, _ = parsed.platform_tag.partition(".")
@@ -46,7 +54,10 @@ def whl_repo_name(filename, sha256):
4654
parts.append(abi_tag)
4755
parts.append(platform_tag)
4856

49-
parts.append(sha256[:8])
57+
if sha256:
58+
parts.append(sha256[:8])
59+
elif version:
60+
parts.insert(1, version)
5061

5162
return "_".join(parts)
5263

tests/pypi/extension/extension_tests.bzl

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ simple==0.0.1 \
678678
--hash=sha256:deadb00f
679679
some_pkg==0.0.1 @ example-direct.org/some_pkg-0.0.1-py3-none-any.whl \
680680
--hash=sha256:deadbaaf
681+
direct_without_sha==0.0.1 @ example-direct.org/direct_without_sha-0.0.1-py3-none-any.whl
681682
some_other_pkg==0.0.1
682683
pip_fallback==0.0.1
683684
""",
@@ -690,10 +691,20 @@ pip_fallback==0.0.1
690691
)
691692

692693
pypi.is_reproducible().equals(False)
693-
pypi.exposed_packages().contains_exactly({"pypi": ["pip_fallback", "simple", "some_other_pkg", "some_pkg"]})
694+
pypi.exposed_packages().contains_exactly({"pypi": ["direct_without_sha", "pip_fallback", "simple", "some_other_pkg", "some_pkg"]})
694695
pypi.hub_group_map().contains_exactly({"pypi": {}})
695696
pypi.hub_whl_map().contains_exactly({
696697
"pypi": {
698+
"direct_without_sha": {
699+
"pypi_315_direct_without_sha_py3_none_any_00000000": [
700+
struct(
701+
config_setting = None,
702+
filename = "direct_without_sha-0.0.1-py3-none-any.whl",
703+
target_platforms = None,
704+
version = "3.15",
705+
),
706+
],
707+
},
697708
"pip_fallback": {
698709
"pypi_315_pip_fallback": [
699710
struct(
@@ -745,6 +756,16 @@ pip_fallback==0.0.1
745756
},
746757
})
747758
pypi.whl_libraries().contains_exactly({
759+
"pypi_315_direct_without_sha_py3_none_any_00000000": {
760+
"dep_template": "@pypi//{name}:{target}",
761+
"experimental_target_platforms": ["cp315_linux_aarch64", "cp315_linux_arm", "cp315_linux_ppc", "cp315_linux_s390x", "cp315_linux_x86_64", "cp315_osx_aarch64", "cp315_osx_x86_64", "cp315_windows_x86_64"],
762+
"filename": "direct_without_sha-0.0.1-py3-none-any.whl",
763+
"python_interpreter_target": "unit_test_interpreter_target",
764+
"repo": "pypi_315",
765+
"requirement": "direct_without_sha==0.0.1 @ example-direct.org/direct_without_sha-0.0.1-py3-none-any.whl",
766+
"sha256": "",
767+
"urls": ["example-direct.org/direct_without_sha-0.0.1-py3-none-any.whl"],
768+
},
748769
"pypi_315_pip_fallback": {
749770
"dep_template": "@pypi//{name}:{target}",
750771
"extra_pip_args": ["--extra-args-for-sdist-building"],

tests/pypi/whl_repo_name/whl_repo_name_tests.bzl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,24 @@ def _test_simple(env):
2525

2626
_tests.append(_test_simple)
2727

28+
def _test_simple_no_sha(env):
29+
got = whl_repo_name("foo-1.2.3-py3-none-any.whl", "")
30+
env.expect.that_str(got).equals("foo_1_2_3_py3_none_any")
31+
32+
_tests.append(_test_simple_no_sha)
33+
2834
def _test_sdist(env):
2935
got = whl_repo_name("foo-1.2.3.tar.gz", "deadbeef000deadbeef")
3036
env.expect.that_str(got).equals("foo_sdist_deadbeef")
3137

3238
_tests.append(_test_sdist)
3339

40+
def _test_sdist_no_sha(env):
41+
got = whl_repo_name("foo-1.2.3.tar.gz", "")
42+
env.expect.that_str(got).equals("foo_1_2_3")
43+
44+
_tests.append(_test_sdist_no_sha)
45+
3446
def _test_platform_whl(env):
3547
got = whl_repo_name(
3648
"foo-1.2.3-cp39.cp310-abi3-manylinux1_x86_64.manylinux_2_17_x86_64.whl",

0 commit comments

Comments
 (0)