|
| 1 | + |
| 2 | +# Copyright (c) 2019 nexB Inc. and others. All rights reserved. |
| 3 | +# http://nexb.com and https://github.com/nexB/scancode-toolkit/ |
| 4 | +# The ScanCode software is licensed under the Apache License version 2.0. |
| 5 | +# Data generated with ScanCode require an acknowledgment. |
| 6 | +# ScanCode is a trademark of nexB Inc. |
| 7 | +# |
| 8 | +# You may not use this software except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 |
| 10 | +# Unless required by applicable law or agreed to in writing, software distributed |
| 11 | +# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | +# CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | +# specific language governing permissions and limitations under the License. |
| 14 | +# |
| 15 | +# When you publish or redistribute any data created with ScanCode or any ScanCode |
| 16 | +# derivative work, you must accompany this data with the following acknowledgment: |
| 17 | +# |
| 18 | +# Generated with ScanCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 19 | +# OR CONDITIONS OF ANY KIND, either express or implied. No content created from |
| 20 | +# ScanCode should be considered or used as legal advice. Consult an Attorney |
| 21 | +# for any legal advice. |
| 22 | +# ScanCode is a free software code scanning tool from nexB Inc. and others. |
| 23 | +# Visit https://github.com/nexB/scancode-toolkit/ for support and download. |
| 24 | + |
| 25 | +from __future__ import absolute_import |
| 26 | +from __future__ import print_function |
| 27 | +from __future__ import unicode_literals |
| 28 | + |
| 29 | +from collections import OrderedDict |
| 30 | + |
| 31 | +import io |
| 32 | +import toml |
| 33 | +import logging |
| 34 | + |
| 35 | +import attr |
| 36 | + |
| 37 | +from commoncode import filetype |
| 38 | +from commoncode import fileutils |
| 39 | +from packagedcode import models |
| 40 | + |
| 41 | + |
| 42 | +""" |
| 43 | +Handle Rust cargo crates |
| 44 | +""" |
| 45 | + |
| 46 | +TRACE = False |
| 47 | + |
| 48 | +logger = logging.getLogger(__name__) |
| 49 | + |
| 50 | +if TRACE: |
| 51 | + import sys |
| 52 | + logging.basicConfig(stream=sys.stdout) |
| 53 | + logger.setLevel(logging.DEBUG) |
| 54 | + |
| 55 | + |
| 56 | +@attr.s() |
| 57 | +class RustCargoCrate(models.Package): |
| 58 | + metafiles = ('Cargo.toml',) |
| 59 | + default_type = 'cargo' |
| 60 | + default_primary_language = 'Rust' |
| 61 | + default_web_baseurl = "https://crates.io/" |
| 62 | + default_download_baseurl = "https://crates.io/api/v1/" |
| 63 | + default_api_baseurl = "https://crates.io/api/v1/" |
| 64 | + |
| 65 | + @classmethod |
| 66 | + def recognize(cls, location): |
| 67 | + return parse(location) |
| 68 | + |
| 69 | + @classmethod |
| 70 | + def get_package_root(cls, manifest_resource, codebase): |
| 71 | + return manifest_resource.parent(codebase) |
| 72 | + |
| 73 | + def repository_homepage_url(self, baseurl=default_web_baseurl): |
| 74 | + return '{}/crates/{}'.format(baseurl, self.name) |
| 75 | + |
| 76 | + def repository_download_url(self, baseurl=default_download_baseurl): |
| 77 | + return '{}/crates/{}/{}/download'.format(baseurl, self.name, self.version) |
| 78 | + |
| 79 | + def api_data_url(self, baseurl=default_api_baseurl): |
| 80 | + return '{}/crates/{}'.format(baseurl, self.name) |
| 81 | + |
| 82 | + def compute_normalized_license(self): |
| 83 | + return models.compute_normalized_license(self.declared_license) |
| 84 | + |
| 85 | + |
| 86 | +def is_cargo_toml(location): |
| 87 | + return (filetype.is_file(location) and fileutils.file_name(location).lower() == 'cargo.toml') |
| 88 | + |
| 89 | + |
| 90 | +def parse(location): |
| 91 | + """ |
| 92 | + Return a Package object from a Cargo.toml file or None. |
| 93 | + """ |
| 94 | + if not is_cargo_toml(location): |
| 95 | + return |
| 96 | + |
| 97 | + with io.open(location, encoding='utf-8') as loc: |
| 98 | + package_data = toml.load(location, _dict=OrderedDict) |
| 99 | + |
| 100 | + return build_package(package_data) |
| 101 | + |
| 102 | + |
| 103 | +def build_package(package_data): |
| 104 | + """ |
| 105 | + Return a Pacakge object from a package data mapping or None. |
| 106 | + """ |
| 107 | + |
| 108 | + name = package_data.get('package').get('name') |
| 109 | + version = package_data.get('package').get('version') |
| 110 | + |
| 111 | + # TODO: Remove this ordered_dict_map once cargo.py is able to handle |
| 112 | + # the appropriate data (source_packages, dependencies, etc..) |
| 113 | + # At the moment, this is only useful for making tests pass |
| 114 | + ordered_dict_map = {} |
| 115 | + for key in ("source_packages", "dependencies", "keywords", "parties"): |
| 116 | + ordered_dict_map[key] = OrderedDict() |
| 117 | + |
| 118 | + package = RustCargoCrate( |
| 119 | + name=name, |
| 120 | + version=version, |
| 121 | + **ordered_dict_map |
| 122 | + ) |
| 123 | + |
| 124 | + return package |
0 commit comments