Skip to content

Commit

Permalink
Add code and tests to handle cocoapods PURL with no name package-url#143
Browse files Browse the repository at this point in the history


Reference: package-url#143

Signed-off-by: John M. Horan <[email protected]>
  • Loading branch information
johnmhoran committed May 30, 2024
1 parent 4bd76d5 commit aee327c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/packageurl/contrib/purl2url.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
# Visit https://github.com/package-url/packageurl-python for support and
# download.


from packageurl import PackageURL
from packageurl.contrib.route import NoRouteAvailable
from packageurl.contrib.route import Router
Expand Down Expand Up @@ -75,7 +76,12 @@ def get_download_url(purl):
return download_url

# Fallback on the `download_url` qualifier when available.
purl_data = PackageURL.from_string(purl)
purl_data = None
try:
purl_data = PackageURL.from_string(purl)
except Exception as e:
print(f"An error occurred in get_download_url(): {e}")
return
return purl_data.qualifiers.get("download_url", None)


Expand Down Expand Up @@ -309,13 +315,18 @@ def build_cocoapods_repo_url(purl):
"""
Return a CocoaPods repo URL from the `purl` string.
"""
purl_data = PackageURL.from_string(purl)
name = purl_data.name

if name:
repository_homepage_url = f"https://cocoapods.org/pods/{name}"
purl_data = None
name = None
try:
purl_data = PackageURL.from_string(purl)
name = purl_data.name
except Exception as e:
print(f"An error occurred in build_cocoapods_repo_url(): {e}")
return

return repository_homepage_url
if not name:
return
return f"https://cocoapods.org/pods/{name}"


# Download URLs:
Expand Down
2 changes: 2 additions & 0 deletions tests/contrib/test_purl2url.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def test_purl2url_get_repo_url():
"pkg:golang/gopkg.in/[email protected]": "https://pkg.go.dev/gopkg.in/[email protected]",
"pkg:cocoapods/[email protected]": "https://cocoapods.org/pods/AFNetworking",
"pkg:cocoapods/[email protected]": "https://cocoapods.org/pods/MapsIndoors",
"pkg:cocoapods/": None,
}

for purl, url in purls_url.items():
Expand Down Expand Up @@ -140,6 +141,7 @@ def test_purl2url_get_inferred_urls():
"https://pypi.org/project/sortedcontainers/2.4.0/"
],
"pkg:cocoapods/[email protected]": ["https://cocoapods.org/pods/AFNetworking"],
"pkg:cocoapods/": [],
"pkg:composer/psr/[email protected]": ["https://packagist.org/packages/psr/log#1.1.3"],
"pkg:rubygems/package-name": ["https://rubygems.org/gems/package-name"],
"pkg:bitbucket/birkenfeld": [],
Expand Down

0 comments on commit aee327c

Please sign in to comment.