Skip to content

Commit be1e13d

Browse files
authored
Specify the output file name
2 parents b5b8e48 + 25190d7 commit be1e13d

File tree

3 files changed

+43
-30
lines changed

3 files changed

+43
-30
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ FOSSLight Scanner is run with the **fosslight** command.
5757
-r Keep raw data
5858
-s <source_path> Path to analyze source
5959
-w <link> Link to be analyzaed can be downloaded by wget or git clone
60-
-o <dir_name> Output Directory
60+
-o <output> Output Directory or file
6161
-d <dependency_path> Path to analyze dependencies
6262
-d <dependency_path> -a <additional_arg> (Using with -d option) Additional arguments for running dependency analysis
6363
```

src/fosslight_scanner/_help.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
-r\t\t\t\t\t Keep raw data
1717
-s <source_path>\t\t\t Path to analyze source
1818
-w <link>\t\t\t\t Link to be analyzaed can be downloaded by wget or git clone
19-
-o <dir_name>\t\t\t\t Output Directory
19+
-o <output>\t\t\t\t Output directory or file
2020
-d <dependency_path> \t\t\t Path to analyze dependencies
2121
-d <dependency_path> -a <additional_arg> (Using with -d option) Additional arguments for running dependency analysis"""
2222

src/fosslight_scanner/fosslight_scanner.py

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,29 @@
3232
_start_time = ""
3333
_executed_path = ""
3434
_SRC_DIR_FROM_LINK_PREFIX = "fosslight_src_dir_"
35+
OUTPUT_FILE_EXTENSION = ".xlsx"
3536

3637

37-
def run_analysis(path_to_run, params, func, str_run_start):
38+
def run_analysis(path_to_run, params, func, str_run_start, output, exe_path):
3839
# This function will be replaced by call_analysis_api().
3940
logger.info("## Start to run "+str_run_start)
4041
try:
4142
if path_to_run != "":
4243
logger.info("|--- Path to analyze :"+path_to_run)
43-
os.chdir(_output_dir)
44+
os.chdir(output)
4445
sys.argv = params
4546
func()
46-
os.chdir(_executed_path)
47+
os.chdir(exe_path)
4748
else:
4849
logger.info("Analyzing path is missing...")
4950
except SystemExit:
5051
pass
5152
except Exception as ex:
52-
logger.error(str(ex))
53+
logger.error(str_run_start + ":" + str(ex))
5354

5455

5556
def call_analysis_api(path_to_run, str_run_start, func, *args):
5657
logger.info("## Start to run " + str_run_start)
57-
5858
try:
5959
if path_to_run != "":
6060
logger.info("|--- Path to analyze :"+path_to_run)
@@ -66,7 +66,7 @@ def call_analysis_api(path_to_run, str_run_start, func, *args):
6666
except SystemExit:
6767
pass
6868
except Exception as ex:
69-
logger.error(str(ex))
69+
logger.error(str_run_start + ":" + str(ex))
7070

7171

7272
def set_sub_parameter(default_params, params): # For dependency
@@ -83,32 +83,35 @@ def set_sub_parameter(default_params, params): # For dependency
8383

8484

8585
def run(src_path, dep_path, dep_arguments, output_path, remove_raw_data=True,
86-
remove_src_data=True, need_init=True, result_log={}):
86+
remove_src_data=True, need_init=True, result_log={}, output_file=""):
8787
try:
8888
success = True
8989
if need_init:
9090
success, final_excel_dir, result_log = init(output_path)
9191
else:
9292
final_excel_dir = output_path
93+
if output_file == "":
94+
output_file = _OUTPUT_FILE_PREFIX + _start_time + OUTPUT_FILE_EXTENSION
9395

9496
if success:
9597
output_files = {"SRC": "FL_Source",
9698
"BIN": "FL_Binary.xlsx",
9799
"DEP": "FL_Dependency",
98100
"REUSE": "reuse.xml",
99-
"FINAL": _OUTPUT_FILE_PREFIX + _start_time + '.xlsx'}
100-
101-
call_analysis_api(src_path, "Source Analysis",
102-
run_scancode.run_scan,
103-
os.path.abspath(src_path), os.path.join(
104-
_output_dir, output_files["SRC"]),
105-
False)
106-
107-
run_analysis(dep_path,
108-
set_sub_parameter(["DEP",
109-
"-p", os.path.abspath(dep_path),
110-
"-o", _output_dir], dep_arguments),
111-
dep_main, "Dependency Analysis")
101+
"FINAL": output_file}
102+
if src_path != "":
103+
call_analysis_api(src_path, "Source Analysis",
104+
run_scancode.run_scan,
105+
os.path.abspath(src_path),
106+
os.path.join(_output_dir, output_files["SRC"]),
107+
False)
108+
if dep_path != "":
109+
run_analysis(dep_path,
110+
set_sub_parameter(["DEP",
111+
"-p", os.path.abspath(dep_path),
112+
"-o", _output_dir], dep_arguments),
113+
dep_main, "Dependency Analysis",
114+
_output_dir, _executed_path)
112115

113116
ouput_file = os.path.join(final_excel_dir, output_files["FINAL"])
114117
success, error_msg = merge_excels(_output_dir, ouput_file)
@@ -137,7 +140,7 @@ def run(src_path, dep_path, dep_arguments, output_path, remove_raw_data=True,
137140
logger.debug("Error to remove temp files:"+str(ex))
138141

139142

140-
def run_after_download_source(link, out_dir, remove_raw_data):
143+
def run_after_download_source(link, out_dir, remove_raw_data, output_file=""):
141144
start_time = datetime.now().strftime('%Y%m%d_%H%M%S')
142145
try:
143146
success, final_excel_dir, result_log = init(out_dir)
@@ -151,7 +154,8 @@ def run_after_download_source(link, out_dir, remove_raw_data):
151154
if success:
152155
logger.info("Downloaded Dir:"+temp_src_dir)
153156
run(temp_src_dir, temp_src_dir,
154-
"", final_excel_dir, remove_raw_data, remove_raw_data, False, result_log)
157+
"", final_excel_dir, remove_raw_data, remove_raw_data, False,
158+
result_log, output_file)
155159
else:
156160
logger.error("Download failed:" + msg)
157161
except Exception as ex:
@@ -194,8 +198,10 @@ def main():
194198
dep_arguments = ""
195199
url_to_analyze = ""
196200
_executed_path = os.getcwd()
197-
output_dir = _executed_path
198201
remove_raw_data = True
202+
output_dir = _executed_path
203+
output_file = ""
204+
output_file_or_dir = ""
199205

200206
try:
201207
argv = sys.argv[1:]
@@ -218,22 +224,29 @@ def main():
218224
_cli_mode = True
219225
url_to_analyze = arg
220226
elif opt == "-o":
221-
output_dir = os.path.abspath(arg)
227+
output_file_or_dir = arg
222228
elif opt == "-r":
223229
remove_raw_data = False
224230

225231
try:
232+
if output_file_or_dir != "":
233+
if output_file_or_dir.endswith(OUTPUT_FILE_EXTENSION):
234+
output_file = Path(output_file_or_dir).name
235+
output_dir = os.path.dirname(os.path.abspath(output_file_or_dir))
236+
else:
237+
output_dir = os.path.abspath(output_file_or_dir)
238+
226239
if not _cli_mode:
227240
src_path, dep_path, dep_arguments, url_to_analyze = get_input_mode()
228241
timer = TimerThread()
229242
timer.setDaemon(True)
230243
timer.start()
231-
if url_to_analyze != "":
232-
run_after_download_source(url_to_analyze, output_dir, remove_raw_data)
233244

234-
if src_path != "" or dep_path != "":
245+
if url_to_analyze != "":
246+
run_after_download_source(url_to_analyze, output_dir, remove_raw_data, output_file)
247+
elif src_path != "" or dep_path != "":
235248
run(src_path, dep_path,
236-
dep_arguments, output_dir, remove_raw_data, False)
249+
dep_arguments, output_dir, remove_raw_data, False, True, {}, output_file)
237250

238251
except Exception as ex:
239252
logger.warning(str(ex))

0 commit comments

Comments
 (0)