Skip to content

Commit 4aa8c44

Browse files
authored
Merge pull request #37 from fosslight/develop
Add format(-f) option and modify the function to write output
2 parents 5fd3a27 + 6488180 commit 4aa8c44

File tree

6 files changed

+142
-100
lines changed

6 files changed

+142
-100
lines changed

.github/workflows/pull-request.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ name: Pull requests fosslight_source_scanner
44

55
on:
66
pull_request:
7-
branches: [ main ]
7+
branches:
8+
- '*'
89

910
jobs:
1011
build:

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ tox
22
pytest
33
pytest-cov
44
pytest-flake8
5+
flake8==3.9.2

src/fosslight_source/_help.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
-h\t\t\t\t Print help message
2222
-j\t\t\t\t Generate additional result of executing ScanCode in json format
2323
-m\t\t\t\t Print the Matched text for each license on a separate sheet
24-
-o <file_name>\t\t Output file name"""
24+
-o <output_path>\t\t Output path
25+
\t\t\t\t (If you want to generate the specific file name, add the output path with file name.)
26+
-f <format>\t\t\t Output file format (excel, csv, opossum)"""
2527

2628
_HELP_MESSAGE_CONVERT = """
2729
Usage: fosslight_convert [option1] <arg1> [option2] <arg2>...
@@ -35,7 +37,9 @@
3537
Optional
3638
-h\t\t\t\t Print help message
3739
-m\t\t\t\t Print the Matched text for each license on a separate sheet
38-
-o <file_name>\t\t Output file name"""
40+
-o <output_path>\t\t Output path
41+
\t\t\t\t (If you want to generate the specific file name, add the output path with file name.)
42+
-f <format>\t\t\t Output file format (excel, csv, opossum)"""
3943

4044

4145
def print_help_msg_source():

src/fosslight_source/convert_scancode.py

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,46 @@
1313
from fosslight_util.set_log import init_log
1414
import yaml
1515
from ._parsing_scancode_file_item import parsing_file_item, get_error_from_header
16-
from fosslight_util.write_excel import write_excel_and_csv
16+
from fosslight_util.output_format import check_output_format, write_output_file
1717
from ._help import print_help_msg_convert
1818
from ._license_matched import get_license_list_to_print
1919

2020
logger = logging.getLogger(constant.LOGGER_NAME)
2121
_PKG_NAME = "fosslight_source"
2222

2323

24-
def convert_json_to_excel(scancode_json, excel_name, result_log, need_license=False):
24+
def convert_json_to_output_report(scancode_json, output_file_name, need_license=False, format=""):
25+
global logger
26+
2527
sheet_license_prefix = "matched_text"
26-
sheet_SRC_prefix = "SRC"
28+
sheet_SRC_prefix = "SRC_FL_Source"
2729
file_list = []
2830
lic_list = {}
2931
msg = ""
3032
success = True
33+
start_time = datetime.now().strftime('%Y%m%d_%H%M%S')
34+
_json_ext = ".json"
35+
36+
success, msg, output_path, output_file, output_extension = check_output_format(output_file_name, format)
37+
if success:
38+
if output_path == "":
39+
output_path = os.getcwd()
40+
else:
41+
output_path = os.path.abspath(output_path)
42+
43+
if output_file == "":
44+
if output_extension == _json_ext:
45+
output_file = "Opossum_input_" + start_time
46+
else:
47+
output_file = "FOSSLight-Report_" + start_time
48+
else:
49+
output_path = os.getcwd()
50+
51+
logger, result_log = init_log(os.path.join(output_path, "fosslight_src_log_" + start_time + ".txt"),
52+
True, logging.INFO, logging.DEBUG, _PKG_NAME)
53+
if not success:
54+
logger.error("Fail to convert scancode: " + msg)
55+
return []
3156

3257
try:
3358
sheet_list = {}
@@ -60,10 +85,11 @@ def convert_json_to_excel(scancode_json, excel_name, result_log, need_license=Fa
6085
except Exception as ex:
6186
logger.warning("Error parsing "+file+":" + str(ex))
6287

63-
success_to_write, writing_msg = write_excel_and_csv(excel_name, sheet_list)
64-
logger.info("Writing excel :" + str(success_to_write) + " " + writing_msg)
88+
output_file_without_ext = os.path.join(output_path, output_file)
89+
success_to_write, writing_msg = write_output_file(output_file_without_ext, output_extension, sheet_list)
90+
logger.info("Writing Output file(" + output_file + output_extension + "):" + str(success_to_write) + " " + writing_msg)
6591
if success_to_write:
66-
result_log["FOSSLight Report"] = excel_name + ".xlsx"
92+
result_log["Output file"] = output_file_without_ext + output_extension
6793

6894
except Exception as ex:
6995
success = False
@@ -100,16 +126,14 @@ def get_detected_licenses_from_scancode(scancode_json_file, need_license):
100126

101127

102128
def main():
103-
global logger
104-
105129
argv = sys.argv[1:]
106130
path_to_find_json = ""
107-
start_time = datetime.now().strftime('%Y%m%d_%H%M%S')
108131
output_file_name = ""
109132
print_matched_text = False
133+
format = ""
110134

111135
try:
112-
opts, args = getopt.getopt(argv, 'hmp:o:')
136+
opts, args = getopt.getopt(argv, 'hmp:o:f:')
113137
for opt, arg in opts:
114138
if opt == "-h":
115139
print_help_msg_convert()
@@ -119,22 +143,15 @@ def main():
119143
output_file_name = arg
120144
elif opt == "-m":
121145
print_matched_text = True
146+
elif opt == "-f":
147+
format = arg
122148
except Exception:
123149
print_help_msg_convert()
124150

125151
if path_to_find_json == "":
126152
print_help_msg_convert()
127153

128-
if output_file_name == "":
129-
output_dir = os.getcwd()
130-
oss_report_name = "FOSSLight-Report_" + start_time
131-
else:
132-
oss_report_name = output_file_name
133-
output_dir = os.path.dirname(os.path.abspath(output_file_name))
134-
135-
logger, _result_log = init_log(os.path.join(output_dir, "fosslight_src_log_" + start_time + ".txt"),
136-
True, logging.INFO, logging.DEBUG, _PKG_NAME)
137-
convert_json_to_excel(path_to_find_json, oss_report_name, _result_log, print_matched_text)
154+
convert_json_to_output_report(path_to_find_json, output_file_name, print_matched_text, format)
138155

139156

140157
if __name__ == '__main__':

src/fosslight_source/run_scancode.py

Lines changed: 86 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
from fosslight_util.timer_thread import TimerThread
1919
from ._parsing_scancode_file_item import parsing_file_item
2020
from ._parsing_scancode_file_item import get_error_from_header
21-
from fosslight_util.write_excel import write_excel_and_csv
2221
from ._help import print_help_msg_source
2322
from ._license_matched import get_license_list_to_print
23+
from fosslight_util.output_format import check_output_format, write_output_file
2424

2525
logger = logging.getLogger(constant.LOGGER_NAME)
2626
warnings.filterwarnings("ignore", category=FutureWarning)
@@ -33,9 +33,10 @@ def main():
3333
write_json_file = False
3434
output_file = ""
3535
print_matched_text = False
36+
format = ""
3637

3738
try:
38-
opts, args = getopt.getopt(argv, 'hmjp:o:')
39+
opts, args = getopt.getopt(argv, 'hmjp:o:f:')
3940
for opt, arg in opts:
4041
if opt == "-h":
4142
print_help_msg_source()
@@ -47,108 +48,124 @@ def main():
4748
output_file = arg
4849
elif opt == "-m":
4950
print_matched_text = True
51+
elif opt == "-f":
52+
format = arg
5053
except Exception:
5154
print_help_msg_source()
5255

5356
timer = TimerThread()
5457
timer.setDaemon(True)
5558
timer.start()
56-
run_scan(path_to_scan, output_file, write_json_file, -1, False, print_matched_text)
59+
60+
run_scan(path_to_scan, output_file, write_json_file, -1, False, print_matched_text, format)
5761

5862

5963
def run_scan(path_to_scan, output_file_name="",
60-
_write_json_file=False, num_cores=-1, return_results=False, need_license=False):
64+
_write_json_file=False, num_cores=-1, return_results=False, need_license=False, format=""):
6165
global logger
6266

6367
success = True
6468
msg = ""
6569
_str_final_result_log = ""
6670
_result_log = {}
6771
result_list = []
72+
_json_ext = ".json"
6873

6974
_windows = platform.system() == "Windows"
7075
start_time = datetime.now().strftime('%Y%m%d_%H%M%S')
7176

72-
if output_file_name == "":
73-
output_file = "FOSSLight-Report_" + start_time
74-
output_json_file = "scancode_" + start_time
75-
output_dir = os.getcwd()
76-
else:
77-
output_file = output_file_name
78-
output_json_file = output_file_name
79-
output_dir = os.path.dirname(os.path.abspath(output_file_name))
80-
81-
logger, _result_log = init_log(os.path.join(output_dir, "fosslight_src_log_"+start_time+".txt"),
82-
True, logging.INFO, logging.DEBUG, _PKG_NAME, path_to_scan)
83-
84-
if path_to_scan == "":
85-
if _windows:
86-
path_to_scan = os.getcwd()
77+
success, msg, output_path, output_file, output_extension = check_output_format(output_file_name, format)
78+
if success:
79+
if output_path == "":
80+
output_path = os.getcwd()
8781
else:
88-
print_help_msg_source()
82+
output_path = os.path.abspath(output_path)
8983

90-
num_cores = multiprocessing.cpu_count() - 1 if num_cores < 0 else num_cores
84+
if output_file == "":
85+
if output_extension == _json_ext:
86+
output_file = "Opossum_input_" + start_time
87+
else:
88+
output_file = "FOSSLight-Report_" + start_time
9189

92-
if os.path.isdir(path_to_scan):
93-
try:
94-
output_json_file = output_json_file+".json" if _write_json_file\
95-
else ""
90+
if _write_json_file:
91+
output_json_file = os.path.join(output_path, "scancode_raw_result.json")
92+
else:
93+
output_json_file = ""
9694

97-
rc, results = cli.run_scan(path_to_scan, max_depth=100,
98-
strip_root=True, license=True,
99-
copyright=True, return_results=True,
100-
processes=num_cores,
101-
output_json_pp=output_json_file,
102-
only_findings=True, license_text=True)
95+
logger, _result_log = init_log(os.path.join(output_path, "fosslight_src_log_"+start_time+".txt"),
96+
True, logging.INFO, logging.DEBUG, _PKG_NAME, path_to_scan)
10397

104-
if not rc:
105-
msg = "Source code analysis failed."
106-
success = False
98+
if path_to_scan == "":
99+
if _windows:
100+
path_to_scan = os.getcwd()
101+
else:
102+
print_help_msg_source()
107103

108-
if results:
109-
sheet_list = {}
110-
has_error = False
111-
if "headers" in results:
112-
has_error, error_msg = get_error_from_header(results["headers"])
113-
if has_error:
114-
_result_log["Error_files"] = error_msg
115-
msg = "Failed to analyze :" + error_msg
116-
if "files" in results:
117-
rc, result_list, parsing_msg, license_list = parsing_file_item(results["files"], has_error, need_license)
118-
_result_log["Parsing Log"] = parsing_msg
119-
if rc:
120-
if not success:
121-
success = True
122-
result_list = sorted(
123-
result_list, key=lambda row: (''.join(row.licenses)))
124-
sheet_list["SRC"] = [scan_item.get_row_to_print() for scan_item in result_list]
125-
if need_license:
126-
sheet_list["matched_text"] = get_license_list_to_print(license_list)
127-
128-
success_to_write, writing_msg = write_excel_and_csv(
129-
output_file, sheet_list)
130-
logger.info("Writing excel :" + str(success_to_write) + " " + writing_msg)
131-
if success_to_write:
132-
_result_log["FOSSLight Report"] = output_file + ".xlsx"
133-
except Exception as ex:
104+
num_cores = multiprocessing.cpu_count() - 1 if num_cores < 0 else num_cores
105+
106+
if os.path.isdir(path_to_scan):
107+
try:
108+
rc, results = cli.run_scan(path_to_scan, max_depth=100,
109+
strip_root=True, license=True,
110+
copyright=True, return_results=True,
111+
processes=num_cores,
112+
output_json_pp=output_json_file,
113+
only_findings=True, license_text=True)
114+
115+
if not rc:
116+
msg = "Source code analysis failed."
117+
success = False
118+
119+
if results:
120+
sheet_list = {}
121+
has_error = False
122+
if "headers" in results:
123+
has_error, error_msg = get_error_from_header(results["headers"])
124+
if has_error:
125+
_result_log["Error_files"] = error_msg
126+
msg = "Failed to analyze :" + error_msg
127+
if "files" in results:
128+
rc, result_list, parsing_msg, license_list = parsing_file_item(results["files"], has_error, need_license)
129+
_result_log["Parsing Log"] = parsing_msg
130+
if rc:
131+
if not success:
132+
success = True
133+
result_list = sorted(
134+
result_list, key=lambda row: (''.join(row.licenses)))
135+
sheet_list["SRC_FL_Source"] = [scan_item.get_row_to_print() for scan_item in result_list]
136+
if need_license:
137+
sheet_list["matched_text"] = get_license_list_to_print(license_list)
138+
139+
output_file_without_ext = os.path.join(output_path, output_file)
140+
success_to_write, writing_msg = write_output_file(output_file_without_ext, output_extension,
141+
sheet_list)
142+
logger.info("Writing Output file(" + output_file + output_extension + "):" + str(success_to_write)
143+
+ " " + writing_msg)
144+
if success_to_write:
145+
_result_log["Output file"] = output_file_without_ext + output_extension
146+
147+
except Exception as ex:
148+
success = False
149+
msg = str(ex)
150+
logger.error("Analyze " + path_to_scan + ":" + msg)
151+
else:
134152
success = False
135-
msg = str(ex)
136-
logger.error("Analyze " + path_to_scan + ":" + msg)
137-
else:
138-
success = False
139-
msg = "Check the path to scan. :" + path_to_scan
153+
msg = "Check the path to scan. :" + path_to_scan
140154

141-
if not return_results:
142-
result_list = []
155+
if not return_results:
156+
result_list = []
143157

144-
scan_result_msg = str(success) if msg == "" else str(success) + "," + msg
158+
scan_result_msg = str(success) if msg == "" else str(success) + ", " + msg
145159
_result_log["Scan Result"] = scan_result_msg
146-
_result_log["Output Directory"] = output_dir
160+
_result_log["Output Directory"] = output_path
147161
try:
148162
_str_final_result_log = yaml.safe_dump(_result_log, allow_unicode=True, sort_keys=True)
149163
logger.info(_str_final_result_log)
150164
except Exception as ex:
151165
logger.warning("Failed to print result log. " + str(ex))
166+
167+
if not success:
168+
logger.error("Failed to run:" + str(scan_result_msg))
152169
return success, _result_log["Scan Result"], result_list
153170

154171

tox.ini

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ filterwarnings = ignore::DeprecationWarning
2020

2121
[testenv:test_run]
2222
commands =
23-
fosslight_source -p tests/test_files -j -o test_scan/scan_result -m
24-
cat test_scan/scan_result_SRC.csv
23+
fosslight_source -p tests/test_files -j -o test_scan/scan_result.csv -m
24+
cat test_scan/scan_result.csv
25+
fosslight_source -p tests/test_files -o test_scan/scan_result2 -f opossum -m
2526
fosslight_convert -p tests/json_result/scan_has_error.json -o test_convert/convert_result2
26-
fosslight_convert -p test_scan/scan_result.json -o test_convert/convert_result -m
27-
cat test_convert/convert_result_SRC.csv
27+
fosslight_convert -p test_scan/scancode_raw_result.json -o test_convert/convert_result.csv -f csv -m
28+
cat test_convert/convert_result.csv
2829
python tests/cli_test.py
2930

3031
[testenv:release]
@@ -34,10 +35,11 @@ deps =
3435
commands =
3536
fosslight_source -h
3637
fosslight_convert -h
37-
fosslight_source -p tests/test_files -j -o test_scan/scan_result -m
38-
cat test_scan/scan_result_SRC.csv
38+
fosslight_source -p tests/test_files -j -o test_scan/scan_result.csv -m
39+
cat test_scan/scan_result.csv
40+
fosslight_source -p tests/test_files -o test_scan/scan_result2 -f opossum -m
3941
fosslight_convert -p tests/json_result/scan_has_error.json -o test_convert/convert_result2
40-
fosslight_convert -p test_scan/scan_result.json -o test_convert/convert_result -m
41-
cat test_convert/convert_result_SRC.csv
42+
fosslight_convert -p test_scan/scancode_raw_result.json -o test_convert/convert_result.csv -f csv -m
43+
cat test_convert/convert_result.csv
4244
python tests/cli_test.py
4345
pytest -v --flake8

0 commit comments

Comments
 (0)