Skip to content

Commit

Permalink
Generate ui mode result file
Browse files Browse the repository at this point in the history
Signed-off-by: Jiyeong Seok <[email protected]>
  • Loading branch information
dd-jy committed Aug 14, 2023
1 parent 60d8db5 commit 1d9b2b1
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ openpyxl
progress
pyyaml
beautifulsoup4
fosslight_util>=1.4.25
fosslight_util>=1.4.29
fosslight_source>=1.6.30
fosslight_dependency>=3.7.4
fosslight_binary>=4.1.24
Expand Down
1 change: 1 addition & 0 deletions src/fosslight_scanner/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
--no_correction\t Enter if you don't want to correct OSS information with sbom-info.yaml
* Correction mode only supported xlsx format.
--correct_fpath <path> Path to the sbom-info.yaml file
--ui\t\t\t Generate UI mode result file
Options for only 'all' or 'bin' mode
-u <db_url>\t\t DB Connection(format :'postgresql://username:password@host:port/database_name')
Expand Down
9 changes: 5 additions & 4 deletions src/fosslight_scanner/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def main():
parser.add_argument('--path', '-p', help='Path to analyze (In compare mode, two FOSSLight reports',
dest='path', nargs='+', default="")
parser.add_argument('--wget', '-w', help='Link to be analyzed', type=str, dest='link', default="")
parser.add_argument('--file', '-f', help='Scanner output file format (excel,yaml), Compare mode (excel,html,yaml,json)',
type=str, dest='file', default="")
parser.add_argument('--format', '-f', help='Scanner output file format (excel,yaml), Compare mode (excel,html,yaml,json)',
type=str, dest='format', default="")
parser.add_argument('--output', '-o', help='Output directory or file', type=str, dest='output', default="")
parser.add_argument('--dependency', '-d', help='Dependency arguments', type=str, dest='dep_argument', default="")
parser.add_argument('--url', '-u', help="DB Url", type=str, dest='db_url', default="")
Expand All @@ -29,6 +29,7 @@ def main():
action='store_true', required=False, default=False)
parser.add_argument('--correct_fpath', help='Path to the sbom-info.yaml',
type=str, required=False, default='')
parser.add_argument('--ui', help='Generate UI mode result file', action='store_true', required=False, default=False)

try:
args = parser.parse_args()
Expand All @@ -40,9 +41,9 @@ def main():
elif args.version:
print_package_version(PKG_NAME, "FOSSLight Scanner Version:")
else:
run_main(args.mode, args.path, args.dep_argument, args.output, args.file,
run_main(args.mode, args.path, args.dep_argument, args.output, args.format,
args.link, args.db_url, args.timer, args.raw, args.core,
not args.no_correction, args.correct_fpath)
not args.no_correction, args.correct_fpath, args.ui)


if __name__ == "__main__":
Expand Down
25 changes: 24 additions & 1 deletion src/fosslight_scanner/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import fosslight_util.constant as constant
from fosslight_util.parsing_yaml import parsing_yml
from fosslight_util.write_yaml import create_yaml_with_ossitem
from fosslight_util.write_scancodejson import write_scancodejson
from fosslight_util.read_excel import read_oss_report
from fosslight_util.output_format import write_output_file

Expand Down Expand Up @@ -173,7 +174,7 @@ def merge_yamls(_output_dir, merge_yaml_files, final_report, remove_src_data=Fal
try:
for mf in merge_yaml_files:
if os.path.exists(os.path.join(_output_dir, mf)):
oss_list, license_list, err_reason = parsing_yml(os.path.join(_output_dir, mf), _output_dir)
oss_list, _, _ = parsing_yml(os.path.join(_output_dir, mf), _output_dir)

if remove_src_data:
existed_yaml = {}
Expand Down Expand Up @@ -201,6 +202,28 @@ def merge_yamls(_output_dir, merge_yaml_files, final_report, remove_src_data=Fal
return success, err_msg


def create_scancodejson(final_report, output_extension, ui_mode_report):
success = True
err_msg = ''

oss_total_list = []
try:
oss_total_list = get_osslist(os.path.dirname(final_report), os.path.basename(final_report),
output_extension, '')

if oss_total_list != []:
write_scancodejson(os.path.dirname(ui_mode_report), os.path.basename(ui_mode_report),
oss_total_list)
else:
success = False
err_msg = "Output file is not created as no oss items detected."
except Exception as ex:
err_msg = ex
success = False

return success, err_msg


def correct_scanner_result(_output_dir, output_files, output_extension, exist_src, exist_bin):
src_oss_list = []
bin_oss_list = []
Expand Down
32 changes: 23 additions & 9 deletions src/fosslight_scanner/fosslight_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
from fosslight_prechecker._precheck import run_lint as prechecker_lint
from .common import (copy_file, call_analysis_api,
overwrite_excel, extract_name_from_link,
merge_yamls, correct_scanner_result)
merge_yamls, correct_scanner_result,
create_scancodejson)
from fosslight_util.write_excel import merge_excels
from ._run_compare import run_compare
import subprocess
Expand Down Expand Up @@ -103,7 +104,7 @@ def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False,
remove_src_data=True, result_log={}, output_file="",
output_extension="", num_cores=-1, db_url="",
default_oss_name="", url="",
correct_mode=True, correct_fpath=""):
correct_mode=True, correct_fpath="", ui_mode=False):
final_excel_dir = output_path
success = True
temp_output_fiiles = []
Expand Down Expand Up @@ -191,6 +192,7 @@ def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False,
try:
output_file_without_ext = os.path.join(final_excel_dir, output_file)
final_report = f"{output_file_without_ext}{output_extension}"
merge_files = [output_files["SRC"], output_files["BIN"], output_files["DEP"]]

if output_extension == ".xlsx":
tmp_dir = f"tmp_{datetime.now().strftime('%y%m%d_%H%M')}"
Expand All @@ -210,7 +212,7 @@ def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False,
if remove_src_data:
overwrite_excel(_output_dir, default_oss_name, "OSS Name")
overwrite_excel(_output_dir, url, "Download Location")
success, err_msg = merge_excels(_output_dir, final_report)
success, err_msg = merge_excels(_output_dir, final_report, merge_files)

if correct_mode:
if exist_src:
Expand All @@ -221,17 +223,25 @@ def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False,
os.path.join(_output_dir, output_files['BIN']))
shutil.rmtree(os.path.join(_output_dir, tmp_dir), ignore_errors=True)
elif output_extension == ".yaml":
merge_yaml_files = [output_files["SRC"], output_files["BIN"], output_files["DEP"]]
success, err_msg = merge_yamls(_output_dir, merge_yaml_files, final_report,
success, err_msg = merge_yamls(_output_dir, merge_files, final_report,
remove_src_data, default_oss_name, url)
if success:
if os.path.isfile(final_report):
logger.info(f'Generated the result file: {final_report}')
result_log["Output File"] = final_report
else:
result_log["Output File"] = 'Nothing is detected from the scanner so output file is not generated.'
else:
logger.error(f"Fail to generate a result file({final_report}): {err_msg}")

if ui_mode:
ui_mode_report = f"{output_file_without_ext}.json"
success, err_msg = create_scancodejson(final_report, output_extension, ui_mode_report)
if success and os.path.isfile(ui_mode_report):
logger.info(f'Generated the ui mode result file: {ui_mode_report}')
else:
logger.error(f'Fail to generate a ui mode result file({ui_mode_report}): {err_msg}')

_str_final_result_log = yaml.safe_dump(result_log, allow_unicode=True, sort_keys=True)
logger.info(_str_final_result_log)
except Exception as ex:
Expand Down Expand Up @@ -295,7 +305,7 @@ def init(output_path="", make_outdir=True):


def run_main(mode, path_arg, dep_arguments, output_file_or_dir, file_format, url_to_analyze, db_url,
hide_progressbar=False, keep_raw_data=False, num_cores=-1, correct_mode=True, correct_fpath=""):
hide_progressbar=False, keep_raw_data=False, num_cores=-1, correct_mode=True, correct_fpath="", ui_mode=False):
global _executed_path, _start_time

output_file = ""
Expand Down Expand Up @@ -376,16 +386,20 @@ def run_main(mode, path_arg, dep_arguments, output_file_or_dir, file_format, url
default_oss_name = extract_name_from_link(url_to_analyze)
success, src_path = download_source(url_to_analyze, output_path)

if not correct_fpath:
correct_fpath = src_path
if output_extension == ".yaml":
correct_mode = False
correct_fpath = ""
else:
if not correct_fpath:
correct_fpath = src_path

if src_path != "":
run_scanner(src_path, dep_arguments, output_path, keep_raw_data,
run_src, run_bin, run_dep, run_prechecker,
remove_downloaded_source, {}, output_file,
output_extension, num_cores, db_url,
default_oss_name, url_to_analyze,
correct_mode, correct_fpath)
correct_mode, correct_fpath, ui_mode)
try:
if not keep_raw_data:
logger.debug(f"Remove temporary files: {_output_dir}")
Expand Down

0 comments on commit 1d9b2b1

Please sign in to comment.