Skip to content

Commit 790e9c2

Browse files
authored
Merge pull request #1426 from ritiek/cargo-toml-packagedcode
Basic Packagedcode module for handling Rust's cargo crates Signed-off-by: Philippe Ombredanne <[email protected]>
2 parents fb9734d + d3d6cb5 commit 790e9c2

23 files changed

+936
-2
lines changed

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ def read(*names, **kwargs):
178178
'packageurl-python >= 0.7.0',
179179
'xmltodict >= 0.11.0',
180180
'javaproperties >= 0.5',
181+
'toml >= 0.10.0',
181182

182183
# scancode
183184
'click >= 6.0.0, < 7.0.0',

src/packagedcode/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from __future__ import unicode_literals
2727

2828
from packagedcode import models
29+
from packagedcode import cargo
2930
from packagedcode import freebsd
3031
from packagedcode import haxe
3132
from packagedcode import maven
@@ -54,6 +55,7 @@
5455
npm.NpmPackage,
5556
phpcomposer.PHPComposerPackage,
5657
haxe.HaxePackage,
58+
cargo.RustCargoCrate,
5759
models.MeteorPackage,
5860
models.BowerPackage,
5961
freebsd.FreeBSDPackage,

src/packagedcode/cargo.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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

src/packagedcode/npm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def build_package(package_data):
219219
if homepage:
220220
homepage = homepage[0]
221221
else:
222-
homepage =''
222+
homepage = ''
223223
namespace, name = split_scoped_package_name(name)
224224
package = NpmPackage(
225225
namespace=namespace or None,
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
[package]
2+
3+
name = "clap"
4+
version = "2.32.0"
5+
authors = ["Kevin K. <[email protected]>"]
6+
exclude = ["examples/*", "clap-test/*", "tests/*", "benches/*", "*.png", "clap-perf/*", "*.dot"]
7+
repository = "https://github.com/clap-rs/clap"
8+
documentation = "https://docs.rs/clap/"
9+
homepage = "https://clap.rs/"
10+
readme = "README.md"
11+
license = "MIT"
12+
keywords = ["argument", "cli", "arg", "parser", "parse"]
13+
categories = ["command-line-interface"]
14+
description = """
15+
A simple to use, efficient, and full featured Command Line Argument Parser
16+
"""
17+
18+
[badges]
19+
travis-ci = { repository = "clap-rs/clap" }
20+
appveyor = { repository = "clap-rs/clap" }
21+
coveralls = { repository = "clap-rs/clap", branch = "master" }
22+
is-it-maintained-issue-resolution = { repository = "clap-rs/clap" }
23+
is-it-maintained-open-issues = { repository = "clap-rs/clap" }
24+
maintenance = {status = "actively-developed"}
25+
26+
[dependencies]
27+
bitflags = "1.0"
28+
unicode-width = "0.1.4"
29+
textwrap = "0.10.0"
30+
strsim = { version = "0.8", optional = true }
31+
yaml-rust = { version = "0.3.5", optional = true }
32+
clippy = { version = "~0.0.166", optional = true }
33+
atty = { version = "0.2.2", optional = true }
34+
vec_map = { version = "0.8", optional = true }
35+
term_size = { version = "0.3.0", optional = true }
36+
37+
[target.'cfg(not(windows))'.dependencies]
38+
ansi_term = { version = "0.11", optional = true }
39+
40+
[dev-dependencies]
41+
regex = "1"
42+
# lazy_static 1.2 requires Rust 1.24.1+
43+
lazy_static = "~1.1"
44+
version-sync = "0.5"
45+
46+
[features]
47+
default = ["suggestions", "color", "vec_map"]
48+
suggestions = ["strsim"]
49+
color = ["ansi_term", "atty"]
50+
wrap_help = ["term_size", "textwrap/term_size"]
51+
yaml = ["yaml-rust"]
52+
unstable = [] # for building with unstable clap features (doesn't require nightly Rust) (currently none)
53+
nightly = [] # for building with unstable Rust features (currently none)
54+
lints = ["clippy"] # Requires nightly Rust
55+
debug = [] # Enables debug messages
56+
no_cargo = [] # Enable if you're not using Cargo, disables Cargo-env-var-dependent macros
57+
doc = ["yaml"] # All the features which add to documentation
58+
59+
[profile.release]
60+
opt-level = 3
61+
debug = false
62+
rpath = false
63+
lto = true
64+
debug-assertions = false
65+
codegen-units = 1
66+
67+
[profile.dev]
68+
opt-level = 0
69+
debug = true
70+
rpath = false
71+
lto = false
72+
debug-assertions = true
73+
codegen-units = 4
74+
75+
[profile.test]
76+
opt-level = 1
77+
debug = true
78+
rpath = false
79+
lto = false
80+
debug-assertions = true
81+
codegen-units = 4
82+
83+
[profile.bench]
84+
opt-level = 3
85+
debug = false
86+
rpath = false
87+
lto = true
88+
debug-assertions = false
89+
codegen-units = 1
90+
91+
[package.metadata.docs.rs]
92+
features = ["doc"]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"type": "cargo",
3+
"namespace": null,
4+
"name": "clap",
5+
"version": "2.32.0",
6+
"qualifiers": null,
7+
"subpath": null,
8+
"primary_language": "Rust",
9+
"description": null,
10+
"release_date": null,
11+
"parties": {},
12+
"keywords": {},
13+
"homepage_url": null,
14+
"download_url": null,
15+
"size": null,
16+
"sha1": null,
17+
"md5": null,
18+
"sha256": null,
19+
"sha512": null,
20+
"bug_tracking_url": null,
21+
"code_view_url": null,
22+
"vcs_url": null,
23+
"copyright": null,
24+
"license_expression": null,
25+
"declared_license": null,
26+
"notice_text": null,
27+
"manifest_path": null,
28+
"dependencies": {},
29+
"contains_source_code": null,
30+
"source_packages": {},
31+
"purl": "pkg:cargo/[email protected]",
32+
"repository_homepage_url": "https://crates.io//crates/clap",
33+
"repository_download_url": "https://crates.io/api/v1//crates/clap/2.32.0/download",
34+
"api_data_url": "https://crates.io/api/v1//crates/clap"
35+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
[package]
2+
name = "clippy"
3+
version = "0.0.212"
4+
authors = [
5+
"Manish Goregaokar <[email protected]>",
6+
"Andre Bogus <[email protected]>",
7+
"Georg Brandl <[email protected]>",
8+
"Martin Carton <[email protected]>",
9+
"Oliver Schneider <[email protected]>"
10+
]
11+
description = "A bunch of helpful lints to avoid common pitfalls in Rust"
12+
repository = "https://github.com/rust-lang/rust-clippy"
13+
readme = "README.md"
14+
license = "MIT/Apache-2.0"
15+
keywords = ["clippy", "lint", "plugin"]
16+
categories = ["development-tools", "development-tools::cargo-plugins"]
17+
build = "build.rs"
18+
edition = "2018"
19+
publish = false
20+
21+
[badges]
22+
travis-ci = { repository = "rust-lang/rust-clippy" }
23+
appveyor = { repository = "rust-lang/rust-clippy" }
24+
25+
[lib]
26+
name = "clippy"
27+
plugin = true
28+
test = false
29+
30+
[[bin]]
31+
name = "cargo-clippy"
32+
test = false
33+
path = "src/main.rs"
34+
35+
[[bin]]
36+
name = "clippy-driver"
37+
path = "src/driver.rs"
38+
39+
[dependencies]
40+
# begin automatic update
41+
clippy_lints = { version = "0.0.212", path = "clippy_lints" }
42+
# end automatic update
43+
regex = "1"
44+
semver = "0.9"
45+
rustc_tools_util = { version = "0.1.1", path = "rustc_tools_util"}
46+
47+
[dev-dependencies]
48+
clippy_dev = { version = "0.0.1", path = "clippy_dev" }
49+
cargo_metadata = "0.7.1"
50+
compiletest_rs = "0.3.19"
51+
lazy_static = "1.0"
52+
serde_derive = "1.0"
53+
clippy-mini-macro-test = { version = "0.2", path = "mini-macro" }
54+
serde = "1.0"
55+
derive-new = "0.5"
56+
57+
# A noop dependency that changes in the Rust repository, it's a bit of a hack.
58+
# See the `src/tools/rustc-workspace-hack/README.md` file in `rust-lang/rust`
59+
# for more information.
60+
rustc-workspace-hack = "1.0.0"
61+
62+
[build-dependencies]
63+
rustc_tools_util = { version = "0.1.1", path = "rustc_tools_util"}
64+
65+
[features]
66+
debugging = []
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"type": "cargo",
3+
"namespace": null,
4+
"name": "clippy",
5+
"version": "0.0.212",
6+
"qualifiers": null,
7+
"subpath": null,
8+
"primary_language": "Rust",
9+
"description": null,
10+
"release_date": null,
11+
"parties": {},
12+
"keywords": {},
13+
"homepage_url": null,
14+
"download_url": null,
15+
"size": null,
16+
"sha1": null,
17+
"md5": null,
18+
"sha256": null,
19+
"sha512": null,
20+
"bug_tracking_url": null,
21+
"code_view_url": null,
22+
"vcs_url": null,
23+
"copyright": null,
24+
"license_expression": null,
25+
"declared_license": null,
26+
"notice_text": null,
27+
"manifest_path": null,
28+
"dependencies": {},
29+
"contains_source_code": null,
30+
"source_packages": {},
31+
"purl": "pkg:cargo/[email protected]",
32+
"repository_homepage_url": "https://crates.io//crates/clippy",
33+
"repository_download_url": "https://crates.io/api/v1//crates/clippy/0.0.212/download",
34+
"api_data_url": "https://crates.io/api/v1//crates/clippy"
35+
}

0 commit comments

Comments
 (0)