Skip to content

Commit

Permalink
url2purl: add cran ecosystem
Browse files Browse the repository at this point in the history
As defined here: https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst#cran
Make sure we handle urls from both cran and rstduio.

Test plan: added unit tests
  • Loading branch information
schischi committed Mar 11, 2024
1 parent dea7760 commit aedc29c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/packageurl/contrib/purl2url.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ def build_rubygems_repo_url(purl):
elif name:
return f"https://rubygems.org/gems/{name}"

@repo_router.route("pkg:cran/.*")
def build_cran_repo_url(purl):
"""
Return a cran repo URL from the `purl` string.
"""
purl_data = PackageURL.from_string(purl)

name = purl_data.name
version = purl_data.version

return f"https://cran.r-project.org/src/contrib/{name}_{version}.tar.gz"


@repo_router.route("pkg:npm/.*")
def build_npm_repo_url(purl):
Expand Down
16 changes: 16 additions & 0 deletions src/packageurl/contrib/url2purl.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,22 @@ def build_rubygems_purl(uri):
return purl_from_pattern("gem", rubygems_pattern, uri)


# https://cran.r-project.org/src/contrib/jsonlite_1.8.8.tar.gz
# https://packagemanager.rstudio.com/cran/2022-06-23/src/contrib/curl_4.3.2.tar.gz"
@purl_router.route(
"https?://cran.r-project.org/.*",
"https?://packagemanager.rstudio.com/cran/.*",
)
def build_cran_purl(uri):
cran_pattern = (
r"^https?://(cran\.r-project\.org|packagemanager\.rstudio\.com/cran)/.*?src/contrib/(?P<name>.+)_(?P<version>.+)\.tar.gz$"
)
qualifiers = {}
if "//cran.r-project.org/" not in uri:
qualifiers["download_url"] = uri
return purl_from_pattern("cran", cran_pattern, uri, qualifiers)


# https://pypi.org/packages/source/a/anyjson/anyjson-0.3.3.tar.gz
# https://pypi.python.org/packages/source/a/anyjson/anyjson-0.3.3.tar.gz
# https://pypi.python.org/packages/2.6/t/threadpool/threadpool-1.2.7-py2.6.egg
Expand Down
4 changes: 3 additions & 1 deletion tests/contrib/data/url2purl.json
Original file line number Diff line number Diff line change
Expand Up @@ -267,5 +267,7 @@
"https://salsa.debian.org/lxc-team/lxc/-/archive/master/lxc-master.tar.gz": "pkg:generic/lxc-master.tar.gz?download_url=https://salsa.debian.org/lxc-team/lxc/-/archive/master/lxc-master.tar.gz",
"http://apt-rpm.org/": null,
"": null,
"https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/android-notifier/android-notifier-desktop-0.5.1-1.i386.rpm": "pkg:generic/code.google.com/android-notifier?download_url=https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/android-notifier/android-notifier-desktop-0.5.1-1.i386.rpm"
"https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/android-notifier/android-notifier-desktop-0.5.1-1.i386.rpm": "pkg:generic/code.google.com/android-notifier?download_url=https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/android-notifier/android-notifier-desktop-0.5.1-1.i386.rpm",
"https://cran.r-project.org/src/contrib/jsonlite_1.8.8.tar.gz": "pkg:cran/[email protected]",
"https://packagemanager.rstudio.com/cran/2022-06-23/src/contrib/curl_4.3.2.tar.gz": "pkg:cran/[email protected]?download_url=https://packagemanager.rstudio.com/cran/2022-06-23/src/contrib/curl_4.3.2.tar.gz"
}

0 comments on commit aedc29c

Please sign in to comment.