Skip to content

Commit 8b50a79

Browse files
authored
Merge pull request #709 from pmattsso/urlutils
Added helper to extract filenames from url
2 parents 1eff983 + f4219fe commit 8b50a79

5 files changed

Lines changed: 27 additions & 10 deletions

File tree

src/fromager/commands/build.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
wheels,
3535
)
3636

37+
from .. import resolver
3738
from ..log import VERBOSE_LOG_FMT, ThreadLogFilter, req_ctxvar_context
3839

3940
logger = logging.getLogger(__name__)
@@ -477,7 +478,7 @@ def _is_wheel_built(
477478
pbi = wkctx.package_build_info(req)
478479
build_tag_from_settings = pbi.build_tag(resolved_version)
479480
build_tag = build_tag_from_settings if build_tag_from_settings else (0, "")
480-
wheel_basename = urlparse(url).path.rsplit("/", 1)[-1]
481+
wheel_basename = resolver.extract_filename_from_url(url)
481482
_, _, build_tag_from_name, _ = parse_wheel_filename(wheel_basename)
482483
existing_build_tag = build_tag_from_name if build_tag_from_name else (0, "")
483484
if (

src/fromager/resolver.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ def default_resolver_provider(
103103
)
104104

105105

106+
def extract_filename_from_url(url: str) -> str:
107+
"""Extract filename from URL and decode it."""
108+
path = urlparse(url).path
109+
filename = os.path.basename(path)
110+
return unquote(filename)
111+
112+
106113
class LogReporter(resolvelib.BaseReporter):
107114
"""Report resolution events
108115
@@ -186,9 +193,8 @@ def get_project_from_pypi(
186193
)
187194
# PEP 592: Check if package was yanked
188195
reason_data_yanked = i.attrib.get("data-yanked")
189-
path = urlparse(candidate_url).path
190196
# file names are URL quoted, "1.0%2Blocal" -> "1.0+local"
191-
filename = unquote(path.rsplit("/", 1)[-1])
197+
filename = extract_filename_from_url(candidate_url)
192198
found_candidates.add(filename)
193199
if DEBUG_RESOLVER:
194200
logger.debug("%s: candidate %r -> %r", project, candidate_url, filename)

src/fromager/sources.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
import inspect
44
import json
55
import logging
6-
import os.path
76
import pathlib
87
import shutil
98
import tarfile
109
import typing
1110
import zipfile
12-
from urllib.parse import unquote, urlparse
1311

1412
import resolvelib
1513
from packaging.requirements import Requirement
@@ -272,7 +270,7 @@ def download_url(
272270
basename = (
273271
destination_filename
274272
if destination_filename
275-
else unquote(os.path.basename(urlparse(url).path))
273+
else resolver.extract_filename_from_url(url)
276274
)
277275
outfile = pathlib.Path(destination_dir) / basename
278276
logger.debug(

src/fromager/wheels.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import textwrap
1111
import typing
1212
import zipfile
13-
from urllib.parse import unquote, urlparse
1413

1514
import elfdeps
1615
import tomlkit
@@ -372,9 +371,7 @@ def download_wheel(
372371
wheel_url: str,
373372
output_directory: pathlib.Path,
374373
) -> pathlib.Path:
375-
wheel_filename = output_directory / unquote(
376-
os.path.basename(urlparse(wheel_url).path)
377-
)
374+
wheel_filename = output_directory / resolver.extract_filename_from_url(wheel_url)
378375
if not wheel_filename.exists():
379376
logger.info(f"downloading pre-built wheel {wheel_url}")
380377
wheel_filename = _download_wheel_check(req, output_directory, wheel_url)

tests/test_resolver.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,3 +910,18 @@ def test_pep592_support_constraint_mismatch():
910910

911911
with pytest.raises(resolvelib.resolvers.ResolverException):
912912
rslvr.resolve([Requirement("setuptools-scm")])
913+
914+
915+
@pytest.mark.parametrize(
916+
"url,filename",
917+
[
918+
("http://example.com/path/to/file.txt", "file.txt"),
919+
(
920+
"http://localhost:8080/simple/vllm/vllm-0.10.0%2Brhai1-15-cp312-cp312-linux_x86_64.whl",
921+
"vllm-0.10.0+rhai1-15-cp312-cp312-linux_x86_64.whl",
922+
),
923+
],
924+
)
925+
def test_extract_filename_from_url(url, filename):
926+
result = resolver.extract_filename_from_url(url)
927+
assert result == filename

0 commit comments

Comments
 (0)