-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for filenames in source URL fragments
Signed-off-by: Nikola Forró <nforro@redhat.com>
- Loading branch information
Showing
4 changed files
with
63 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Copyright Contributors to the Packit project. | ||
# SPDX-License-Identifier: MIT | ||
|
||
import urllib.parse | ||
from pathlib import Path | ||
|
||
|
||
def get_filename_from_location(location: str) -> str: | ||
""" | ||
Extracts filename from given source location. | ||
Follows RPM logic - target filename can be specified in URL fragment. | ||
Args: | ||
location: Location to extract filename from. | ||
Returns: | ||
Extracted filename that can be empty if there is none. | ||
""" | ||
url = urllib.parse.urlsplit(location) | ||
if url.fragment: | ||
if "/" in url.fragment: | ||
return Path(url.fragment).name.split("=")[-1] | ||
return Path(f"{url.path}#{url.fragment}").name | ||
return Path(url.path).name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Copyright Contributors to the Packit project. | ||
# SPDX-License-Identifier: MIT | ||
|
||
import pytest | ||
|
||
from specfile.utils import get_filename_from_location | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"location, filename", | ||
[ | ||
("", ""), | ||
("tarball-0.1.tar.gz", "tarball-0.1.tar.gz"), | ||
("https://example.com", ""), | ||
("https://example.com/archive/tarball-0.1.tar.gz", "tarball-0.1.tar.gz"), | ||
( | ||
"https://example.com/archive/tarball-0.1.tar.gz#fragment", | ||
"tarball-0.1.tar.gz#fragment", | ||
), | ||
( | ||
"https://example.com/download_tarball.cgi#/tarball-0.1.tar.gz", | ||
"tarball-0.1.tar.gz", | ||
), | ||
( | ||
"https://example.com/tarball-latest.tar.gz#/file=tarball-0.1.tar.gz", | ||
"tarball-0.1.tar.gz", | ||
), | ||
], | ||
) | ||
def test_get_filename_from_location(location, filename): | ||
assert get_filename_from_location(location) == filename |