-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: jiyeong.seok <[email protected]>
- Loading branch information
Showing
11 changed files
with
259 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,144 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# Copyright (c) 2021 LG Electronics Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import os | ||
import logging | ||
import json | ||
import re | ||
import shutil | ||
import yaml | ||
import subprocess | ||
import fosslight_util.constant as constant | ||
import fosslight_dependency.constant as const | ||
from fosslight_dependency._package_manager import PackageManager | ||
from fosslight_dependency._package_manager import check_and_run_license_scanner, get_url_to_purl | ||
from fosslight_dependency.dependency_item import DependencyItem, change_dependson_to_purl | ||
from fosslight_util.oss_item import OssItem | ||
logger = logging.getLogger(constant.LOGGER_NAME) | ||
|
||
|
||
class Cargo(PackageManager): | ||
package_manager_name = const.CARGO | ||
|
||
dn_url = 'https://crates.io/crates/' | ||
input_file_name = 'tmp_cargo_fosslight_output.json' | ||
tmp_input_file_flag = False | ||
cur_path = '' | ||
cargo_lock_f = 'Cargo.lock' | ||
|
||
def __init__(self, input_dir, output_dir): | ||
super().__init__(self.package_manager_name, self.dn_url, input_dir, output_dir) | ||
self.append_input_package_list_file(self.input_file_name) | ||
|
||
def __del__(self): | ||
if self.tmp_input_file_flag: | ||
os.remove(self.input_file_name) | ||
|
||
def run_plugin(self): | ||
if os.path.exists(self.input_file_name): | ||
logger.info(f"Found {self.input_file_name}, skip the flutter cmd to analyze dependency.") | ||
return True | ||
|
||
if not os.path.exists(const.SUPPORT_PACKAE.get(self.package_manager_name)): | ||
logger.error(f"Cannot find the file({const.SUPPORT_PACKAE.get(self.package_manager_name)})") | ||
return False | ||
|
||
if os.path.exists(self.cargo_lock_f): | ||
cmd = f'cargo metadata --locked --format-version 1 > {self.input_file_name}' | ||
else: | ||
cmd = f'cargo metadata --format-version 1 > {self.input_file_name}' | ||
ret = subprocess.call(cmd, shell=True) | ||
if ret != 0: | ||
logger.error(f"Failed to run: {cmd}") | ||
os.chdir(self.cur_path) | ||
return False | ||
self.tmp_input_file_flag = True | ||
return True | ||
|
||
def parse_oss_information(self, f_name): | ||
json_data = '' | ||
|
||
with open(f_name, 'r', encoding='utf8') as cargo_file: | ||
json_f = json.load(cargo_file) | ||
try: | ||
purl_dict = {} | ||
workspace_members_key = 'workspace_members' | ||
resolve_key = 'resolve' | ||
root_key = 'root' | ||
nodes_key = 'nodes' | ||
workspace_members = [] | ||
root = '' | ||
resolve_node = [] | ||
|
||
if workspace_members_key in json_f: | ||
workspace_members = json_f[workspace_members_key] | ||
|
||
if resolve_key in json_f: | ||
if root_key in json_f[resolve_key]: | ||
root = json_f[resolve_key][root_key] | ||
if nodes_key in json_f[resolve_key]: | ||
resolve_node = json_f[resolve_key][nodes_key] | ||
if root and resolve_node: | ||
self.direct_dep_list.extend(get_matched_dependencies(root, resolve_node)) | ||
else: | ||
self.direct_dep = False | ||
logger.info('Cannot find dependencies relationship (no resolve nodes.)') | ||
|
||
for json_data in json_f['packages']: | ||
dep_item = DependencyItem() | ||
oss_item = OssItem() | ||
pkg_id = json_data['id'] | ||
oss_origin_name = json_data['name'] | ||
|
||
oss_item.name = f"{self.package_manager_name}:{oss_origin_name}" | ||
oss_item.version = json_data['version'] | ||
oss_item.homepage = f"{self.dn_url}{oss_origin_name}" | ||
oss_item.download_location = json_data['repository'] | ||
if oss_item.download_location is None: | ||
oss_item.download_location = oss_item.homepage | ||
dep_item.purl = get_url_to_purl(oss_item.homepage, self.package_manager_name, oss_origin_name, oss_item.version) | ||
purl_dict[f'{oss_origin_name}({oss_item.version})'] = dep_item.purl | ||
if json_data['license'] is not None: | ||
oss_item.license = json_data['license'] | ||
|
||
if self.direct_dep: | ||
if pkg_id == root: | ||
oss_item.comment = 'root package' | ||
if pkg_id in workspace_members: | ||
oss_item.comment = 'local package' | ||
if len(self.direct_dep_list) > 0: | ||
if pkg_id != root: | ||
if f'{oss_origin_name}({oss_item.version})' in self.direct_dep_list: | ||
oss_item.comment = 'direct' | ||
else: | ||
oss_item.comment = 'transitive' | ||
dep_item.depends_on_raw.extend(get_matched_dependencies(pkg_id, resolve_node)) | ||
|
||
dep_item.oss_items.append(oss_item) | ||
self.dep_items.append(dep_item) | ||
except Exception as e: | ||
logger.error(f"Fail to parse pub oss information: {e}") | ||
if self.direct_dep: | ||
self.dep_items = change_dependson_to_purl(purl_dict, self.dep_items) | ||
|
||
return | ||
|
||
def get_matched_dependencies(match_id, resolve_node): | ||
dependencies_list = [] | ||
for node in resolve_node: | ||
if match_id == node['id']: | ||
for dep_pkg in node['dependencies']: | ||
try: | ||
match = re.findall(r'^.*#(\S*)@(\S*)', dep_pkg) | ||
dependencies_list.append(f'{match[0][0]}({match[0][1]})') | ||
except: | ||
try: | ||
match = re.findall(r'^(\S*)\s(\S*)\s', dep_pkg) | ||
dependencies_list.append(f'{match[0][0]}({match[0][1]})') | ||
except: | ||
logger.info(f'cannot find name and version for dependencies: {match_id}') | ||
pass | ||
break | ||
return dependencies_list |
This file contains 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 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,31 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# Copyright (c) 2024 LG Electronics Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import os | ||
import pytest | ||
import subprocess | ||
|
||
DIST_PATH = os.path.join(os.environ.get("TOX_PATH"), "dist", "cli.exe") | ||
|
||
|
||
@pytest.mark.parametrize("input_path, output_path", [ | ||
("tests/test_cargo", "tests/result/cargo") | ||
]) | ||
@pytest.mark.ubuntu | ||
def test_ubuntu(input_path, output_path): | ||
command = f"fosslight_dependency -p {input_path} -o {output_path}" | ||
result = subprocess.run(command, shell=True, capture_output=True, text=True) | ||
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}" | ||
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}" | ||
|
||
|
||
@pytest.mark.parametrize("input_path, output_path, extra_args", [ | ||
(os.path.join("tests", "test_cargo"), os.path.join("tests", "result", "cargo"), "") | ||
]) | ||
@pytest.mark.windows | ||
def test_windows(input_path, output_path): | ||
command = f"{DIST_PATH} -p {input_path} -o {output_path}" | ||
result = subprocess.run(command, capture_output=True, text=True) | ||
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}" | ||
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}" |
This file contains 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,53 @@ | ||
[package] | ||
name = "rustwide" | ||
version = "0.18.0" | ||
edition = "2018" | ||
build = "build.rs" | ||
|
||
documentation = "https://docs.rs/rustwide" | ||
repository = "https://github.com/rust-lang/rustwide" | ||
description = "Execute your code on the Rust ecosystem." | ||
license = "MIT OR Apache-2.0" | ||
readme = "README.md" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[features] | ||
unstable = [] | ||
unstable-toolchain-ci = [] | ||
|
||
[dependencies] | ||
http = "1.1.0" | ||
anyhow = { version = "1.0.68", features = ["backtrace"]} | ||
futures-util = "0.3.5" | ||
log = "0.4.6" | ||
tokio = { version = "1.0", features = ["process", "time", "io-util", "rt", "rt-multi-thread"] } | ||
tokio-stream = { version = "0.1", features = ["io-util"] } | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde_json = "1.0" | ||
scopeguard = "1.0.0" | ||
lazy_static = "1.0.0" | ||
tempfile = "3.0.0" | ||
attohttpc = "0.28.0" | ||
flate2 = "1" | ||
tar = "0.4.0" | ||
percent-encoding = "2.1.0" | ||
walkdir = "2.2" | ||
toml = "0.8.12" | ||
fs2 = "0.4.3" | ||
remove_dir_all = "0.8.2" | ||
base64 = "0.22.0" | ||
getrandom = { version = "0.2", features = ["std"] } | ||
thiserror = "1.0.20" | ||
git2 = "0.19.0" | ||
|
||
[target.'cfg(unix)'.dependencies] | ||
nix = { version = "0.29.0", features = ["signal", "user"]} | ||
|
||
[target.'cfg(windows)'.dependencies] | ||
windows-sys = {version = "0.52.0", features = ["Win32_Foundation", "Win32_System_Threading"]} | ||
|
||
[dev-dependencies] | ||
env_logger = "0.11.3" | ||
rand = "0.8.5" | ||
tiny_http = "0.12.0" |
Large diffs are not rendered by default.
Oops, something went wrong.