Skip to content

Commit ff16a20

Browse files
authored
Merge pull request #71 from fosslight/temp2
Fix cli.py to remove redundant report
2 parents 703b440 + 160ca5a commit ff16a20

File tree

3 files changed

+34
-65
lines changed

3 files changed

+34
-65
lines changed

src/fosslight_source/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def create_report_file(start_time, scanned_result, license_list, selected_scanne
154154

155155

156156
def run_all_scanners(path_to_scan, output_file_name="", _write_json_file=False, num_cores=-1,
157-
need_license=False, format="", called_by_cli=False):
157+
need_license=False, format="", called_by_cli=True):
158158
"""
159159
Run Scancode and scanoss.py for the given path.
160160

src/fosslight_source/run_scanoss.py

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@
1313
from fosslight_util.set_log import init_log
1414
from fosslight_util.output_format import check_output_format # , write_output_file
1515
from ._parsing_scanoss_file import parsing_scanResult # scanoss
16-
# from ._help import print_help_msg_source
16+
import shutil
17+
from pathlib import Path
1718

1819
logger = logging.getLogger(constant.LOGGER_NAME)
1920
warnings.filterwarnings("ignore", category=FutureWarning)
2021
_PKG_NAME = "fosslight_source"
22+
SCANOSS_RESULT_FILE = "scanner_output.wfp"
23+
SCANOSS_OUTPUT_FILE = "scanoss_raw_result.json"
24+
SCANOSS_COMMAND_PREFIX = "scanoss-py scan -o "
2125

2226

2327
def run_scanoss_py(path_to_scan, output_file_name="", format="", called_by_cli=False, write_json_file=False, num_threads=-1):
@@ -31,62 +35,53 @@ def run_scanoss_py(path_to_scan, output_file_name="", format="", called_by_cli=F
3135
:param write_json_file: if requested, keep the raw files.
3236
:return scanoss_file_list: list of ScanItem (scanned result by files).
3337
"""
38+
success, msg, output_path, output_file, output_extension = check_output_format(output_file_name, format)
3439
if not called_by_cli:
3540
global logger
41+
start_time = datetime.now().strftime('%Y%m%d_%H%M%S')
42+
logger, _result_log = init_log(os.path.join(output_path, f"fosslight_src_log_{start_time}.txt"),
43+
True, logging.INFO, logging.DEBUG, _PKG_NAME, path_to_scan)
3644

3745
scanoss_file_list = []
3846
try:
3947
pkg_resources.get_distribution("scanoss")
4048
except Exception as error:
41-
logger.warning(str(error) + ". Skipping scan with scanoss.")
49+
logger.warning(f"{error}. Skipping scan with scanoss.")
4250
logger.warning("Please install scanoss and dataclasses before run fosslight_source with scanoss option.")
4351
return scanoss_file_list
44-
scan_command = "scanoss-py scan -o "
45-
46-
start_time = datetime.now().strftime('%Y%m%d_%H%M%S')
47-
48-
success, msg, output_path, output_file, output_extension = check_output_format(output_file_name, format)
49-
if not called_by_cli:
50-
logger, _result_log = init_log(os.path.join(output_path, "fosslight_src_log_"+start_time+".txt"),
51-
True, logging.INFO, logging.DEBUG, _PKG_NAME, path_to_scan)
5252

5353
if output_path == "": # if json output with _write_json_file not used, output_path won't be needed.
5454
output_path = os.getcwd()
5555
else:
5656
output_path = os.path.abspath(output_path)
57+
if not os.path.isdir(output_path):
58+
Path(output_path).mkdir(parents=True, exist_ok=True)
59+
output_json_file = os.path.join(output_path, SCANOSS_OUTPUT_FILE)
5760

58-
output_file = "scanoss_raw_result.json"
59-
60-
output_json_file = os.path.join(output_path, output_file)
61-
62-
scan_command += f"{output_json_file} {path_to_scan}"
63-
61+
scan_command = f"{SCANOSS_COMMAND_PREFIX} {output_json_file} {path_to_scan}"
6462
if num_threads > 0:
6563
scan_command += " -T " + str(num_threads)
6664
else:
6765
scan_command += " -T " + "30"
6866

6967
try:
7068
os.system(scan_command)
71-
st_json = open(output_json_file, "r")
72-
logger.info(f"SCANOSS Start parsing {path_to_scan}")
73-
with open(output_json_file, "r") as st_json:
74-
st_python = json.load(st_json)
75-
scanoss_file_list = parsing_scanResult(st_python)
69+
if os.path.isfile(output_json_file):
70+
with open(output_json_file, "r") as st_json:
71+
st_python = json.load(st_json)
72+
scanoss_file_list = parsing_scanResult(st_python)
7673
except Exception as error:
77-
logger.warning(f"Parsing {path_to_scan}: {error}")
74+
logger.warning(f"SCANOSS Parsing {path_to_scan}: {error}")
75+
7876
logger.info(f"|---Number of files detected with SCANOSS: {(len(scanoss_file_list))}")
7977

80-
if not write_json_file:
81-
try:
82-
os.system(f"rm {output_json_file}")
83-
os.system("rm scanner_output.wfp")
84-
except Exception as error:
85-
logger.debug(f"Deleting scanoss result failed.: {error}")
86-
else:
87-
try:
88-
os.system(f"mv scanner_output.wfp {output_path}/scanoss_fingerprint.wfp")
89-
except Exception as error:
90-
logger.debug(f"Moving scanoss fingerprint file failed.: {error}")
78+
try:
79+
if write_json_file:
80+
shutil.move(SCANOSS_RESULT_FILE, output_path)
81+
else:
82+
os.remove(output_json_file)
83+
os.remove(SCANOSS_RESULT_FILE)
84+
except Exception as error:
85+
logger.debug(f"Moving scanoss raw files failed.: {error}")
9186

9287
return scanoss_file_list

tox.ini

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,7 @@ deps =
2626

2727
commands =
2828
rm -rf test_scan
29-
fosslight_source -p tests/test_files -o test_scan/scan_result.csv -m
30-
cat test_scan/scan_result.csv
31-
32-
fosslight_source -p tests/test_files -o test_scan/scan_result.csv -f csv
33-
cat test_scan/scan_result.csv
34-
35-
fosslight_source -p tests/test_files -o test_scan/scan_result.json -f opossum
36-
cat test_scan/scan_result.json
37-
38-
fosslight_source -p tests/test_files -o test_scan/scan_result.xlsx -f excel
39-
ls test_scan/scan_result.xlsx
40-
41-
fosslight_source -p tests/test_files -j -o test_scan/
42-
ls test_scan/scancode_raw_result.json
43-
ls test_scan/scanoss_fingerprint.wfp
44-
ls test_scan/scanoss_raw_result.json
45-
46-
python tests/cli_test.py
29+
fosslight_source -p tests/test_files -j -m -o test_scan
4730

4831
[testenv:release]
4932
deps =
@@ -52,22 +35,13 @@ deps =
5235
commands =
5336
fosslight_source -h
5437
fosslight_convert -h
55-
fosslight_source -p tests/test_files -o test_scan/scan_result.csv -m
56-
cat test_scan/scan_result.csv
5738

58-
fosslight_source -p tests/test_files -o test_scan/scan_result.csv -f csv
39+
fosslight_source -p tests/test_files -o test_scan/scan_result.csv
5940
cat test_scan/scan_result.csv
6041

61-
fosslight_source -p tests/test_files -o test_scan/scan_result.json -f opossum
62-
cat test_scan/scan_result.json
63-
64-
fosslight_source -p tests/test_files -o test_scan/scan_result.xlsx -f excel
65-
ls test_scan/scan_result.xlsx
42+
fosslight_source -p tests/test_files -m -j -o test_scan2/
43+
ls test_scan2/
6644

67-
fosslight_source -p tests/test_files -j -o test_scan/
68-
ls test_scan/scancode_raw_result.json
69-
ls test_scan/scanoss_fingerprint.wfp
70-
ls test_scan/scanoss_raw_result.json
7145
python tests/cli_test.py
7246
pytest -v --flake8
73-
47+

0 commit comments

Comments
 (0)