Skip to content

Commit ac68acb

Browse files
committed
Add support to fetch purl
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent b4b9426 commit ac68acb

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ MarkupSafe==2.0.1
4141
more-itertools==8.13.0
4242
normality==2.3.3
4343
packagedcode-msitools==0.101.210706
44-
packageurl-python==0.9.9
45-
packaging==21.3
44+
packageurl-python==0.17.4
45+
packaging==24.0
4646
parameter-expansion-patched==0.3.1
4747
patch==1.16
4848
pdfminer-six==20220506

src/fetchcode/__init__.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from urllib.parse import urlparse
2222

2323
import requests
24+
from packageurl.contrib.purl2url import get_download_url
2425

2526

2627
class Response:
@@ -89,19 +90,34 @@ def fetch_ftp(url, location):
8990
return resp
9091

9192

93+
def fetch_purl(purl, location=None):
94+
"""
95+
Return a `Response` object built from fetching the content at a PURL based `purl` URL string
96+
saving the content in a file at `location`
97+
"""
98+
from fetchcode.download_urls import download_url as get_download_url_from_fetchcode
99+
100+
for resolver in (get_download_url, get_download_url_from_fetchcode):
101+
url = resolver(purl)
102+
if url:
103+
return fetch(url=url)
104+
return
105+
106+
92107
def fetch(url):
93108
"""
94109
Return a `Response` object built from fetching the content at the `url` URL string and
95110
store content at a temporary file.
96111
"""
97-
98-
temp = tempfile.NamedTemporaryFile(delete=False)
99-
location = temp.name
100-
101112
url_parts = urlparse(url)
102113
scheme = url_parts.scheme
114+
location = None
115+
116+
if scheme != "purl":
117+
temp = tempfile.NamedTemporaryFile(delete=False)
118+
location = temp.name
103119

104-
fetchers = {"ftp": fetch_ftp, "http": fetch_http, "https": fetch_http}
120+
fetchers = {"ftp": fetch_ftp, "http": fetch_http, "https": fetch_http, "pkg": fetch_purl}
105121

106122
if scheme in fetchers:
107123
return fetchers.get(scheme)(url, location)

0 commit comments

Comments
 (0)