Skip to content

Commit

Permalink
Fix the xlsx, html result for same compared result
Browse files Browse the repository at this point in the history
Signed-off-by: Jiyeong Seok <jiyeong.seok@lge.com>
  • Loading branch information
dd-jy committed Jul 13, 2022
1 parent 5079279 commit 1980952
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 33 deletions.
68 changes: 46 additions & 22 deletions src/fosslight_scanner/_run_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def get_sample_html():
RESOURCES_DIR = 'resources'
SAMPLE_HTML = 'bom_compare.html'
html_file = os.path.join(RESOURCES_DIR, SAMPLE_HTML)
html_f = ''

try:
base_dir = sys._MEIPASS
Expand All @@ -81,29 +82,49 @@ def get_sample_html():

def write_result_html(output_file, compared_result, before_yaml, after_yaml):
ret = True

f = BeautifulSoup(get_sample_html().read(), 'html.parser')
f.find("li", {"class": "before_f"}).append(before_yaml)
f.find("li", {"class": "after_f"}).append(after_yaml)

table_html = f.find("table", {"id": "comp_result"})

status = [ADD, DELETE, CHANGE]
row = 2
for st in status:
for oi in compared_result[st]:
compared_row = parse_result_for_table(oi, st)
tr = f.new_tag('tr')
for i, ci in enumerate(compared_row):
html_f = get_sample_html()
if html_f != '':
try:
f = BeautifulSoup(html_f.read(), 'html.parser')
f.find("li", {"class": "before_f"}).append(before_yaml)
f.find("li", {"class": "after_f"}).append(after_yaml)

table_html = f.find("table", {"id": "comp_result"})

status = [ADD, DELETE, CHANGE]
row = 2
for st in status:
for oi in compared_result[st]:
compared_row = parse_result_for_table(oi, st)
tr = f.new_tag('tr')
for i, ci in enumerate(compared_row):
td = f.new_tag('td')
td.string = ci
td.attrs = {"style": "padding:5px;"}
tr.insert(i, td)
table_html.insert(row, tr)
row += 1

if row == 2:
tr = f.new_tag('tr')
td = f.new_tag('td')
td.string = ci
td.string = 'Same'
td.attrs = {"style": "padding:5px;"}
tr.insert(i, td)
table_html.insert(row, tr)
row += 1

with open(output_file, "wb") as f_out:
f_out.write(f.prettify("utf-8"))
tr.insert(0, td)
for i in range(1, 5):
td = f.new_tag('td')
td.string = ''
td.attrs = {"style": "padding:5px;"}
tr.insert(i, td)
table_html.insert(row, tr)

with open(output_file, "wb") as f_out:
f_out.write(f.prettify("utf-8"))
except Exception as e:
logger.error(f'Fail to write html file: {e}')
ret = False
else:
ret = False
return ret


Expand All @@ -127,8 +148,11 @@ def write_result_xlsx(output_file, compared_result):
compared_row = parse_result_for_table(oi, st)
worksheet.write_row(row, 0, compared_row)
row += 1
if row == 1:
worksheet.write_row(row, 0, ['Same', '', '', '', ''])
workbook.close()
except Exception:
except Exception as e:
logger.error(f'Fail to write xlsx file: {e}')
ret = False

return ret
Expand Down
18 changes: 7 additions & 11 deletions src/fosslight_scanner/fosslight_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def download_source(link, out_dir):
return success, temp_src_dir


def init(output_path=""):
def init(output_path="", make_outdir=True):
global _output_dir, _log_file, _start_time, logger

result_log = {}
Expand All @@ -253,8 +253,9 @@ def init(output_path=""):
else:
output_root_dir = _executed_path

Path(_output_dir).mkdir(parents=True, exist_ok=True)
_output_dir = os.path.abspath(_output_dir)
if make_outdir:
Path(_output_dir).mkdir(parents=True, exist_ok=True)
_output_dir = os.path.abspath(_output_dir)

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


def check_comapre_output_file(output_file_name, format):
def check_compare_output_file(output_file_name, format):
CUSTOMIZED_FORMAT_FOR_COMPARE_MODE = {'excel': '.xlsx', 'html': '.html', 'json': '.json', 'yaml': '.yaml'}

if format == "":
logger.info(" * Default result format : excel")
format = "excel"

success, msg, output_path, output_file, output_extension = check_output_format(output_file_name, format,
CUSTOMIZED_FORMAT_FOR_COMPARE_MODE)
success, final_excel_dir, result_log = init(output_path)
rmdir(_output_dir)
ret, final_excel_dir, result_log = init(output_path, False)
if success:
result_file = ""
if output_path == "":
Expand Down Expand Up @@ -321,7 +317,7 @@ def run_main(mode, src_path, dep_arguments, output_file_or_dir, file_format, url
if not os.path.exists(os.path.join(_executed_path, after_yaml)):
logger.error("Cannot find after yaml file (2nd param with -y option).")
return False
output_compare_file, output_compare_ext = check_comapre_output_file(output_file_or_dir, file_format)
output_compare_file, output_compare_ext = check_compare_output_file(output_file_or_dir, file_format)
run_compare(os.path.join(_executed_path, before_yaml), os.path.join(_executed_path, after_yaml),
output_compare_file, output_compare_ext)
else:
Expand Down
73 changes: 73 additions & 0 deletions tests/after.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
same-oss:
- version: 0.0.0
source name or path:
- go.mod
license:
- BSD-3-Clause
download location: https://github.com/alecthomas/template
homepage: https://pkg.go.dev/github.com/alecthomas/template
comment: transitive, Cannot connect https://pkg.go.dev/github.com/alecthomas/template@v0.0.0-20160405071501-a0175ee3bccc,
get info from the latest version.
add-license-oss:
- version: 0.5.6
source name or path:
- go.mod
license:
- MPL-2.0
download location: https://github.com/hashicorp/go-retryablehttp
homepage: https://pkg.go.dev/github.com/hashicorp/go-retryablehttp
comment: transitive
- version: 0.5.3
source name or path:
- go.mod
license:
- MPL-2.0
download location: https://github.com/hashicorp/go-retryablehttp
homepage: https://pkg.go.dev/github.com/hashicorp/go-retryablehttp
comment: transitive
- version: 0.5.3
source name or path:
- go.mod
license:
- Apache-2.0
download location: https://github.com/hashicorp/go-retryablehttp
homepage: https://pkg.go.dev/github.com/hashicorp/go-retryablehttp
comment: transitive
- version: 0.5.4
source name or path:
- go.mod
license:
- Apache-2.0
download location: https://github.com/hashicorp/go-retryablehttp
homepage: https://pkg.go.dev/github.com/hashicorp/go-retryablehttp
comment: transitive
remove-version-oss:
- version: 1.0.1
source name or path:
- go.mod
license:
- MIT
- Apache-2.0
download location: https://github.com/konsorten/go-windows-terminal-sequences
homepage: https://pkg.go.dev/github.com/konsorten/go-windows-terminal-sequences
comment: transitive
new-test-oss:
- version: 1.0.1
source name or path:
- hi
license:
- MIT
- Apache-2.0
download location: https://github.com/new-test-oss/new-test-oss
homepage: https://github.com/new-test-oss/new-test-oss
new-test-oss2:
- version: 1.0.1
source name or path:
- hi2
license:
- MIT
- version: 1.0.2
source name or path:
- hi2
license:
- MIT
53 changes: 53 additions & 0 deletions tests/before.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
same-oss:
- version: 0.0.0
source name or path:
- go.mod
license:
- BSD-3-Clause
download location: https://github.com/alecthomas/template
homepage: https://pkg.go.dev/github.com/alecthomas/template
comment: transitive, Cannot connect https://pkg.go.dev/github.com/alecthomas/template@v0.0.0-20160405071501-a0175ee3bccc,
get info from the latest version.
add-license-oss:
- version: 0.5.6
source name or path:
- go.mod
license:
- MPL-2.0
download location: https://github.com/hashicorp/go-retryablehttp
homepage: https://pkg.go.dev/github.com/hashicorp/go-retryablehttp
comment: transitive
- version: 0.5.6
source name or path:
- go.mod
license:
- MPL-2.0
- Apache-2.0
download location: https://github.com/hashicorp/go-retryablehttp
homepage: https://pkg.go.dev/github.com/hashicorp/go-retryablehttp
comment: transitive
remove-version-oss:
- version: 1.0.1
source name or path:
- go.mod
license:
- MIT
download location: https://github.com/konsorten/go-windows-terminal-sequences
homepage: https://pkg.go.dev/github.com/konsorten/go-windows-terminal-sequences
comment: transitive
- version: 2.0.1
source name or path:
- go.mod
license:
- MIT
download location: https://github.com/konsorten/go-windows-terminal-sequences
homepage: https://pkg.go.dev/github.com/konsorten/go-windows-terminal-sequences
del-test-oss:
- version: 1.0.1
source name or path:
- hi
license:
- MIT
- Apache-2.0
download location: https://github.com/del-test-oss/del-test-oss
homepage: https://github.com/del-test-oss/del-test-oss

0 comments on commit 1980952

Please sign in to comment.