From 15ef0c90f2abc1dd8818509cf974e1d227824d30 Mon Sep 17 00:00:00 2001 From: TG1999 Date: Tue, 14 Apr 2020 18:45:02 +0530 Subject: [PATCH] Add purl2url support for rust Signed-off-by: TG1999 --- purl2url/rust.py | 38 ++++++++++++++++++++++++++++++ requirements.txt | 1 - tests/purl2url/test_rust.py | 47 +++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 purl2url/rust.py create mode 100644 tests/purl2url/test_rust.py diff --git a/purl2url/rust.py b/purl2url/rust.py new file mode 100644 index 00000000..d328f50c --- /dev/null +++ b/purl2url/rust.py @@ -0,0 +1,38 @@ +# fetchcode is a free software tool from nexB Inc. and others. +# Visit https://github.com/nexB/fetchcode for support and download. +# +# Copyright (c) nexB Inc. and others. All rights reserved. +# http://nexb.com and http://aboutcode.org +# +# This software is licensed under the Apache License version 2.0. +# +# You may not use this software except in compliance with the License. +# You may obtain a copy of the License at: +# http://apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software distributed +# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +# CONDITIONS OF ANY KIND, either express or implied. See the License for the +# specific language governing permissions and limitations under the License. + +from urllib.parse import urlparse + + +def get_rust_url(purl): + """ + Return a downloadable normalized URL for a given rust based `purl` + """ + url_parts = urlparse(purl) + scheme = url_parts.scheme + if scheme != 'pkg': + raise Exception('Not a valid PURL, `scheme` of URL should be `pkg` to be it a PURL for more info refer https://github.com/package-url/purl-spec/blob/master/README.rst') + + path = url_parts.path + part_paths = path.split('@') + version = part_paths[1] + + part_paths = part_paths[0].split('/') + if part_paths[0] != 'cargo': + raise Exception('Not a valid cargo PURL, `type` of PURL should be `cargo` for more info refer https://github.com/package-url/purl-spec/blob/master/README.rst') + + name = part_paths[1] + return 'https://crates.io/api/v1/crates/{}/{}/download'.format(name,version) diff --git a/requirements.txt b/requirements.txt index 1e662359..c423d901 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,7 +6,6 @@ importlib-metadata==1.5.0 more-itertools==8.2.0 packaging==20.1 pathlib2==2.3.5 -pkg-resources==0.0.0 pluggy==0.13.1 py==1.8.1 pyparsing==2.4.6 diff --git a/tests/purl2url/test_rust.py b/tests/purl2url/test_rust.py new file mode 100644 index 00000000..4b22c8a5 --- /dev/null +++ b/tests/purl2url/test_rust.py @@ -0,0 +1,47 @@ +# fetchcode is a free software tool from nexB Inc. and others. +# Visit https://github.com/nexB/fetchcode for support and download. +# +# Copyright (c) nexB Inc. and others. All rights reserved. +# http://nexb.com and http://aboutcode.org +# +# This software is licensed under the Apache License version 2.0. +# +# You may not use this software except in compliance with the License. +# You may obtain a copy of the License at: +# http://apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software distributed +# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +# CONDITIONS OF ANY KIND, either express or implied. See the License for the +# specific language governing permissions and limitations under the License. + +from purl2url import rust +import pytest + + +def test_url_2_purl_rust(): + get_url = rust.get_rust_url + purl = 'pkg:cargo/clap@2.33.0' + expected_url = 'https://crates.io/api/v1/crates/clap/2.33.0/download' + url = get_url(purl=purl) + assert expected_url == url + +def test_url_2_purl_rust_special_characters(): + get_url = rust.get_rust_url + purl = 'pkg:cargo/a3mo_lib@0.3.0' + expected_url = 'https://crates.io/api/v1/crates/a3mo_lib/0.3.0/download' + url = get_url(purl=purl) + assert expected_url == url + +def test_url_2_purl_rust_empty(): + with pytest.raises(Exception) as e_info: + get_url = rust.get_rust_url + purl = '' + url = get_url(purl=purl) + assert e_info == 'Not a valid PURL, `scheme` of URL should be `pkg` to be it a PURL for more info refer https://github.com/package-url/purl-spec/blob/master/README.rst' + +def test_url_2_purl_rust_with_invalid_crate_purl(): + with pytest.raises(Exception) as e_info: + get_url = rust.get_rust_url + purl = 'pkg:github/a3mo_lib@0.3.0' + url = get_url(purl=purl) + assert e_info == 'Not a valid cargo PURL, `type` of PURL should be `cargo` for more info refer https://github.com/package-url/purl-spec/blob/master/README.rst'