Skip to content

Commit 1980952

Browse files
committed
Fix the xlsx, html result for same compared result
Signed-off-by: Jiyeong Seok <[email protected]>
1 parent 5079279 commit 1980952

File tree

4 files changed

+179
-33
lines changed

4 files changed

+179
-33
lines changed

src/fosslight_scanner/_run_compare.py

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def get_sample_html():
6464
RESOURCES_DIR = 'resources'
6565
SAMPLE_HTML = 'bom_compare.html'
6666
html_file = os.path.join(RESOURCES_DIR, SAMPLE_HTML)
67+
html_f = ''
6768

6869
try:
6970
base_dir = sys._MEIPASS
@@ -81,29 +82,49 @@ def get_sample_html():
8182

8283
def write_result_html(output_file, compared_result, before_yaml, after_yaml):
8384
ret = True
84-
85-
f = BeautifulSoup(get_sample_html().read(), 'html.parser')
86-
f.find("li", {"class": "before_f"}).append(before_yaml)
87-
f.find("li", {"class": "after_f"}).append(after_yaml)
88-
89-
table_html = f.find("table", {"id": "comp_result"})
90-
91-
status = [ADD, DELETE, CHANGE]
92-
row = 2
93-
for st in status:
94-
for oi in compared_result[st]:
95-
compared_row = parse_result_for_table(oi, st)
96-
tr = f.new_tag('tr')
97-
for i, ci in enumerate(compared_row):
85+
html_f = get_sample_html()
86+
if html_f != '':
87+
try:
88+
f = BeautifulSoup(html_f.read(), 'html.parser')
89+
f.find("li", {"class": "before_f"}).append(before_yaml)
90+
f.find("li", {"class": "after_f"}).append(after_yaml)
91+
92+
table_html = f.find("table", {"id": "comp_result"})
93+
94+
status = [ADD, DELETE, CHANGE]
95+
row = 2
96+
for st in status:
97+
for oi in compared_result[st]:
98+
compared_row = parse_result_for_table(oi, st)
99+
tr = f.new_tag('tr')
100+
for i, ci in enumerate(compared_row):
101+
td = f.new_tag('td')
102+
td.string = ci
103+
td.attrs = {"style": "padding:5px;"}
104+
tr.insert(i, td)
105+
table_html.insert(row, tr)
106+
row += 1
107+
108+
if row == 2:
109+
tr = f.new_tag('tr')
98110
td = f.new_tag('td')
99-
td.string = ci
111+
td.string = 'Same'
100112
td.attrs = {"style": "padding:5px;"}
101-
tr.insert(i, td)
102-
table_html.insert(row, tr)
103-
row += 1
104-
105-
with open(output_file, "wb") as f_out:
106-
f_out.write(f.prettify("utf-8"))
113+
tr.insert(0, td)
114+
for i in range(1, 5):
115+
td = f.new_tag('td')
116+
td.string = ''
117+
td.attrs = {"style": "padding:5px;"}
118+
tr.insert(i, td)
119+
table_html.insert(row, tr)
120+
121+
with open(output_file, "wb") as f_out:
122+
f_out.write(f.prettify("utf-8"))
123+
except Exception as e:
124+
logger.error(f'Fail to write html file: {e}')
125+
ret = False
126+
else:
127+
ret = False
107128
return ret
108129

109130

@@ -127,8 +148,11 @@ def write_result_xlsx(output_file, compared_result):
127148
compared_row = parse_result_for_table(oi, st)
128149
worksheet.write_row(row, 0, compared_row)
129150
row += 1
151+
if row == 1:
152+
worksheet.write_row(row, 0, ['Same', '', '', '', ''])
130153
workbook.close()
131-
except Exception:
154+
except Exception as e:
155+
logger.error(f'Fail to write xlsx file: {e}')
132156
ret = False
133157

134158
return ret

src/fosslight_scanner/fosslight_scanner.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def download_source(link, out_dir):
239239
return success, temp_src_dir
240240

241241

242-
def init(output_path=""):
242+
def init(output_path="", make_outdir=True):
243243
global _output_dir, _log_file, _start_time, logger
244244

245245
result_log = {}
@@ -253,8 +253,9 @@ def init(output_path=""):
253253
else:
254254
output_root_dir = _executed_path
255255

256-
Path(_output_dir).mkdir(parents=True, exist_ok=True)
257-
_output_dir = os.path.abspath(_output_dir)
256+
if make_outdir:
257+
Path(_output_dir).mkdir(parents=True, exist_ok=True)
258+
_output_dir = os.path.abspath(_output_dir)
258259

259260
log_dir = os.path.join(output_root_dir, "fosslight_log")
260261
logger, result_log = init_log(os.path.join(log_dir, f"{_log_file}{_start_time}.txt"),
@@ -263,17 +264,12 @@ def init(output_path=""):
263264
return os.path.isdir(_output_dir), output_root_dir, result_log
264265

265266

266-
def check_comapre_output_file(output_file_name, format):
267+
def check_compare_output_file(output_file_name, format):
267268
CUSTOMIZED_FORMAT_FOR_COMPARE_MODE = {'excel': '.xlsx', 'html': '.html', 'json': '.json', 'yaml': '.yaml'}
268269

269-
if format == "":
270-
logger.info(" * Default result format : excel")
271-
format = "excel"
272-
273270
success, msg, output_path, output_file, output_extension = check_output_format(output_file_name, format,
274271
CUSTOMIZED_FORMAT_FOR_COMPARE_MODE)
275-
success, final_excel_dir, result_log = init(output_path)
276-
rmdir(_output_dir)
272+
ret, final_excel_dir, result_log = init(output_path, False)
277273
if success:
278274
result_file = ""
279275
if output_path == "":
@@ -321,7 +317,7 @@ def run_main(mode, src_path, dep_arguments, output_file_or_dir, file_format, url
321317
if not os.path.exists(os.path.join(_executed_path, after_yaml)):
322318
logger.error("Cannot find after yaml file (2nd param with -y option).")
323319
return False
324-
output_compare_file, output_compare_ext = check_comapre_output_file(output_file_or_dir, file_format)
320+
output_compare_file, output_compare_ext = check_compare_output_file(output_file_or_dir, file_format)
325321
run_compare(os.path.join(_executed_path, before_yaml), os.path.join(_executed_path, after_yaml),
326322
output_compare_file, output_compare_ext)
327323
else:

tests/after.yaml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
same-oss:
2+
- version: 0.0.0
3+
source name or path:
4+
- go.mod
5+
license:
6+
- BSD-3-Clause
7+
download location: https://github.com/alecthomas/template
8+
homepage: https://pkg.go.dev/github.com/alecthomas/template
9+
comment: transitive, Cannot connect https://pkg.go.dev/github.com/alecthomas/[email protected],
10+
get info from the latest version.
11+
add-license-oss:
12+
- version: 0.5.6
13+
source name or path:
14+
- go.mod
15+
license:
16+
- MPL-2.0
17+
download location: https://github.com/hashicorp/go-retryablehttp
18+
homepage: https://pkg.go.dev/github.com/hashicorp/go-retryablehttp
19+
comment: transitive
20+
- version: 0.5.3
21+
source name or path:
22+
- go.mod
23+
license:
24+
- MPL-2.0
25+
download location: https://github.com/hashicorp/go-retryablehttp
26+
homepage: https://pkg.go.dev/github.com/hashicorp/go-retryablehttp
27+
comment: transitive
28+
- version: 0.5.3
29+
source name or path:
30+
- go.mod
31+
license:
32+
- Apache-2.0
33+
download location: https://github.com/hashicorp/go-retryablehttp
34+
homepage: https://pkg.go.dev/github.com/hashicorp/go-retryablehttp
35+
comment: transitive
36+
- version: 0.5.4
37+
source name or path:
38+
- go.mod
39+
license:
40+
- Apache-2.0
41+
download location: https://github.com/hashicorp/go-retryablehttp
42+
homepage: https://pkg.go.dev/github.com/hashicorp/go-retryablehttp
43+
comment: transitive
44+
remove-version-oss:
45+
- version: 1.0.1
46+
source name or path:
47+
- go.mod
48+
license:
49+
- MIT
50+
- Apache-2.0
51+
download location: https://github.com/konsorten/go-windows-terminal-sequences
52+
homepage: https://pkg.go.dev/github.com/konsorten/go-windows-terminal-sequences
53+
comment: transitive
54+
new-test-oss:
55+
- version: 1.0.1
56+
source name or path:
57+
- hi
58+
license:
59+
- MIT
60+
- Apache-2.0
61+
download location: https://github.com/new-test-oss/new-test-oss
62+
homepage: https://github.com/new-test-oss/new-test-oss
63+
new-test-oss2:
64+
- version: 1.0.1
65+
source name or path:
66+
- hi2
67+
license:
68+
- MIT
69+
- version: 1.0.2
70+
source name or path:
71+
- hi2
72+
license:
73+
- MIT

tests/before.yaml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
same-oss:
2+
- version: 0.0.0
3+
source name or path:
4+
- go.mod
5+
license:
6+
- BSD-3-Clause
7+
download location: https://github.com/alecthomas/template
8+
homepage: https://pkg.go.dev/github.com/alecthomas/template
9+
comment: transitive, Cannot connect https://pkg.go.dev/github.com/alecthomas/[email protected],
10+
get info from the latest version.
11+
add-license-oss:
12+
- version: 0.5.6
13+
source name or path:
14+
- go.mod
15+
license:
16+
- MPL-2.0
17+
download location: https://github.com/hashicorp/go-retryablehttp
18+
homepage: https://pkg.go.dev/github.com/hashicorp/go-retryablehttp
19+
comment: transitive
20+
- version: 0.5.6
21+
source name or path:
22+
- go.mod
23+
license:
24+
- MPL-2.0
25+
- Apache-2.0
26+
download location: https://github.com/hashicorp/go-retryablehttp
27+
homepage: https://pkg.go.dev/github.com/hashicorp/go-retryablehttp
28+
comment: transitive
29+
remove-version-oss:
30+
- version: 1.0.1
31+
source name or path:
32+
- go.mod
33+
license:
34+
- MIT
35+
download location: https://github.com/konsorten/go-windows-terminal-sequences
36+
homepage: https://pkg.go.dev/github.com/konsorten/go-windows-terminal-sequences
37+
comment: transitive
38+
- version: 2.0.1
39+
source name or path:
40+
- go.mod
41+
license:
42+
- MIT
43+
download location: https://github.com/konsorten/go-windows-terminal-sequences
44+
homepage: https://pkg.go.dev/github.com/konsorten/go-windows-terminal-sequences
45+
del-test-oss:
46+
- version: 1.0.1
47+
source name or path:
48+
- hi
49+
license:
50+
- MIT
51+
- Apache-2.0
52+
download location: https://github.com/del-test-oss/del-test-oss
53+
homepage: https://github.com/del-test-oss/del-test-oss

0 commit comments

Comments
 (0)