Skip to content

Commit 7cfef2d

Browse files
committed
Fix the error of missing directory for ScanOSS
1 parent c7f613a commit 7cfef2d

File tree

1 file changed

+28
-33
lines changed

1 file changed

+28
-33
lines changed

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

0 commit comments

Comments
 (0)