From aee327c18d85545f983c772c477ef43674e87d9a Mon Sep 17 00:00:00 2001 From: "John M. Horan" Date: Wed, 29 May 2024 17:27:08 -0700 Subject: [PATCH] Add code and tests to handle cocoapods PURL with no name #143 Reference: https://github.com/package-url/packageurl-python/issues/143 Signed-off-by: John M. Horan --- src/packageurl/contrib/purl2url.py | 25 ++++++++++++++++++------- tests/contrib/test_purl2url.py | 2 ++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/packageurl/contrib/purl2url.py b/src/packageurl/contrib/purl2url.py index 54c5e37..7d0bd52 100644 --- a/src/packageurl/contrib/purl2url.py +++ b/src/packageurl/contrib/purl2url.py @@ -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 @@ -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) @@ -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: diff --git a/tests/contrib/test_purl2url.py b/tests/contrib/test_purl2url.py index 20086f9..bd78ee1 100644 --- a/tests/contrib/test_purl2url.py +++ b/tests/contrib/test_purl2url.py @@ -66,6 +66,7 @@ def test_purl2url_get_repo_url(): "pkg:golang/gopkg.in/ldap.v3@v3.1.0": "https://pkg.go.dev/gopkg.in/ldap.v3@v3.1.0", "pkg:cocoapods/AFNetworking@4.0.1": "https://cocoapods.org/pods/AFNetworking", "pkg:cocoapods/MapsIndoors@3.24.0": "https://cocoapods.org/pods/MapsIndoors", + "pkg:cocoapods/": None, } for purl, url in purls_url.items(): @@ -140,6 +141,7 @@ def test_purl2url_get_inferred_urls(): "https://pypi.org/project/sortedcontainers/2.4.0/" ], "pkg:cocoapods/AFNetworking@4.0.1": ["https://cocoapods.org/pods/AFNetworking"], + "pkg:cocoapods/": [], "pkg:composer/psr/log@1.1.3": ["https://packagist.org/packages/psr/log#1.1.3"], "pkg:rubygems/package-name": ["https://rubygems.org/gems/package-name"], "pkg:bitbucket/birkenfeld": [],