-
-
Notifications
You must be signed in to change notification settings - Fork 625
Basic Packagedcode module for handling Rust's cargo crates #1426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
pombredanne
merged 5 commits into
aboutcode-org:develop
from
ritiek:cargo-toml-packagedcode
Mar 10, 2019
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eb813e1
Add toml as a dependency
ritiek ba67878
Implement basic subclass for cargo crates
ritiek 5542e15
Conform with codestyle
ritiek d57d877
Add test for RustCargoCrate instance
ritiek d3d6cb5
Add tests for sample Cargo.toml files
ritiek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
|
|
||
| # Copyright (c) 2019 nexB Inc. and others. All rights reserved. | ||
| # http://nexb.com and https://github.com/nexB/scancode-toolkit/ | ||
| # The ScanCode software is licensed under the Apache License version 2.0. | ||
| # Data generated with ScanCode require an acknowledgment. | ||
| # ScanCode is a trademark of nexB Inc. | ||
| # | ||
| # 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. | ||
| # | ||
| # When you publish or redistribute any data created with ScanCode or any ScanCode | ||
| # derivative work, you must accompany this data with the following acknowledgment: | ||
| # | ||
| # Generated with ScanCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
| # OR CONDITIONS OF ANY KIND, either express or implied. No content created from | ||
| # ScanCode should be considered or used as legal advice. Consult an Attorney | ||
| # for any legal advice. | ||
| # ScanCode is a free software code scanning tool from nexB Inc. and others. | ||
| # Visit https://github.com/nexB/scancode-toolkit/ for support and download. | ||
|
|
||
| from __future__ import absolute_import | ||
| from __future__ import print_function | ||
| from __future__ import unicode_literals | ||
|
|
||
| import io | ||
| import toml | ||
| import logging | ||
|
|
||
| import attr | ||
|
|
||
| from commoncode import filetype | ||
| from commoncode import fileutils | ||
| from packagedcode import models | ||
|
|
||
|
|
||
| """ | ||
| Handle Rust cargo crates | ||
| """ | ||
|
|
||
| TRACE = False | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| if TRACE: | ||
| import sys | ||
| logging.basicConfig(stream=sys.stdout) | ||
| logger.setLevel(logging.DEBUG) | ||
|
|
||
|
|
||
| @attr.s() | ||
| class RustCargoCrate(models.Package): | ||
| metafiles = ('Cargo.toml',) | ||
| default_type = 'cargo' | ||
| default_primary_language = 'Rust' | ||
| default_web_baseurl = "https://crates.io/" | ||
| default_download_baseurl = "https://crates.io/api/v1/" | ||
| default_api_baseurl = "https://crates.io/api/v1/" | ||
|
|
||
| @classmethod | ||
| def recognize(cls, location): | ||
| return parse(location) | ||
|
|
||
| @classmethod | ||
| def get_package_root(cls, manifest_resource, codebase): | ||
| return manifest_resource.parent(codebase) | ||
|
|
||
| def repository_homepage_url(self, baseurl=default_web_baseurl): | ||
| return '{}/crates/{}'.format(baseurl, self.name) | ||
|
|
||
| def repository_download_url(self, baseurl=default_download_baseurl): | ||
| return '{}/crates/{}/{}/download'.format(baseurl, self.name, self.version) | ||
|
|
||
| def api_data_url(self, baseurl=default_api_baseurl): | ||
| return '{}/crates/{}'.format(baseurl, self.name) | ||
|
|
||
| def compute_normalized_license(self): | ||
| return models.compute_normalized_license(self.declared_license) | ||
|
|
||
|
|
||
| def is_cargo_toml(location): | ||
| return (filetype.is_file(location) and fileutils.file_name(location).lower() == 'cargo.toml') | ||
|
|
||
|
|
||
| def parse(location): | ||
| """ | ||
| Return a Package object from a Cargo.toml file or None. | ||
| """ | ||
| if not is_cargo_toml(location): | ||
| return | ||
|
|
||
| with io.open(location, encoding='utf-8') as loc: | ||
| package_data = toml.load(location) | ||
|
|
||
| return build_package(package_data) | ||
|
|
||
|
|
||
| def build_package(package_data): | ||
| """ | ||
| Return a Pacakge object from a package data mapping or None. | ||
| """ | ||
|
|
||
| name = package_data.get('package').get('name') | ||
| version = package_data.get('package').get('version') | ||
| package = RustCargoCrate( | ||
| name=name, | ||
| version=version, | ||
| ) | ||
|
|
||
| return package |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| about_resource: toml-0.10.0-py2.py3-none-any.whl | ||
| checksum_md5: 70f2a3ce1fc099c20702b55f44f0b130 | ||
| checksum_sha1: 5a6d4ad5089719e4f567997a24da13204db2eb28 | ||
| contact: [email protected] | ||
| copyright: Copyright 2013-2018 William Pearson | ||
| description: Python Library for Tom's Obvious, Minimal Language | ||
| download_url: https://files.pythonhosted.org/packages/b9/19/5cbd78eac8b1783671c40e34bb0fa83133a06d340a38b55c645076d40094/toml-0.10.0.tar.gz | ||
| homepage_url: https://github.com/uiri/toml | ||
| license_expression: mit | ||
| name: toml | ||
| notes: Used for parsing for TOML configuration files. | ||
| owner: Willam Pearson | ||
| owner_url: https://github.com/uiri | ||
| vcs_url: https://github.com/uiri/toml | ||
| version: 0.10.0 |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| The MIT License | ||
|
|
||
| Copyright 2013-2018 William Pearson | ||
| Copyright 2015-2016 Julien Enselme | ||
| Copyright 2016 Google Inc. | ||
| Copyright 2017 Samuel Vasko | ||
| Copyright 2017 Nate Prewitt | ||
| Copyright 2017 Jack Evans | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not your making but this is a weird test that I will need to fix.