Skip to content

Commit

Permalink
Merge pull request #210 from fosslight/dev_multi_f_option
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinWonjaePark authored Jul 22, 2024
2 parents 68d4568 + 32ecac8 commit 195eb43
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 37 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ lxml
virtualenv
pyyaml
lastversion
fosslight_util>=1.4.43
fosslight_util>=1.4.47
PyGithub
requirements-parser
defusedxml
Expand Down
3 changes: 2 additions & 1 deletion src/fosslight_dependency/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
-e <exclude_path>\t\t Enter the path where the analysis will not be performed.
-o <output_path>\t\t Output path
\t\t\t\t\t(If you want to generate the specific file name, add the output path with file name.)
-f <format>\t\t\t Output file format (excel, csv, opossum, yaml, spdx-tag, spdx-yaml, spdx-json, spdx-xml)
-f <format> [<format> ...]\t Output formats (excel, csv, opossum, yaml, spdx-tag, spdx-yaml, spdx-json, spdx-xml)
\t\t\t\t Multiple formats can be specified separated by space.
--direct\t\t\t Print the direct/transitive dependency type in comment.
\t\tChoice 'True' or 'False'. (default:True)
--notice\t\t\t Print the open source license notice text.
Expand Down
93 changes: 58 additions & 35 deletions src/fosslight_dependency/run_dependency_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import fosslight_util.constant as constant
from fosslight_dependency._help import print_help_msg
from fosslight_dependency._analyze_dependency import analyze_dependency
from fosslight_util.output_format import check_output_format, write_output_file
from fosslight_util.output_format import check_output_formats, write_output_file
if platform.system() != 'Windows':
from fosslight_util.write_spdx import write_spdx
from fosslight_util.cover import CoverItem
Expand Down Expand Up @@ -92,7 +92,7 @@ def find_package_manager(input_dir, abs_path_to_exclude=[]):

def run_dependency_scanner(package_manager='', input_dir='', output_dir_file='', pip_activate_cmd='',
pip_deactivate_cmd='', output_custom_dir='', app_name=const.default_app_name,
github_token='', format='', direct=True, path_to_exclude=[]):
github_token='', formats=[], direct=True, path_to_exclude=[]):
global logger

ret = True
Expand All @@ -101,25 +101,42 @@ def run_dependency_scanner(package_manager='', input_dir='', output_dir_file='',
_json_ext = ".json"
_start_time = datetime.now().strftime('%y%m%d_%H%M')

success, msg, output_path, output_file, output_extension = check_output_format(output_dir_file, format, CUSTOMIZED_FORMAT)
success, msg, output_path, output_files, output_extensions = check_output_formats(output_dir_file, formats, CUSTOMIZED_FORMAT)
if success:
if output_path == "":
output_path = os.getcwd()
else:
output_path = os.path.abspath(output_path)

if output_file == "":
if format.startswith('spdx'):
if platform.system() != 'Windows':
output_file = f"fosslight_spdx_dep_{_start_time}"
else:
logger.error('Windows not support spdx format.')
sys.exit(0)
else:
if output_extension == _json_ext:
output_file = f"fosslight_opossum_dep_{_start_time}"
if not output_files:
while len(output_files) < len(output_extensions):
output_files.append(None)
to_remove = [] # elements of spdx format on windows that should be removed
for i, output_extension in enumerate(output_extensions):
if formats:
if formats[i].startswith('spdx'):
if platform.system() != 'Windows':
output_files[i] = f"fosslight_spdx_dep_{_start_time}"
else:
logger.warning('spdx format is not supported on Windows. Please remove spdx from format.')
to_remove.append(i)
else:
if output_extension == _json_ext:
output_files[i] = f"fosslight_opossum_dep_{_start_time}"
else:
output_files[i] = f"fosslight_report_dep_{_start_time}"
else:
output_file = f"fosslight_report_dep_{_start_time}"
if output_extension == _json_ext:
output_files[i] = f"fosslight_opossum_dep_{_start_time}"
else:
output_files[i] = f"fosslight_report_dep_{_start_time}"
for index in sorted(to_remove, reverse=True):
# remove elements of spdx format on windows
del output_files[index]
del output_extensions[index]
del formats[index]
if len(output_extensions) < 1:
sys.exit(0)
else:
logger.error(msg)
sys.exit(1)
Expand Down Expand Up @@ -216,27 +233,33 @@ def run_dependency_scanner(package_manager='', input_dir='', output_dir_file='',
if cover_comment:
cover.comment += f', {cover_comment}'

output_file_without_ext = os.path.join(output_path, output_file)
if format.startswith('spdx'):
if platform.system() != 'Windows':
success_write, err_msg, result_file = write_spdx(output_file_without_ext, output_extension, sheet_list,
_PKG_NAME, pkg_resources.get_distribution(_PKG_NAME).version,
spdx_version=(2, 3))
combined_paths_and_files = [os.path.join(output_path, file) for file in output_files]
results = []
for i, output_extension in enumerate(output_extensions):
if formats:
if formats[i].startswith('spdx'):
if platform.system() != 'Windows':
results.append(write_spdx(combined_paths_and_files[i], output_extension, sheet_list, _PKG_NAME,
pkg_resources.get_distribution(_PKG_NAME).version, spdx_version=(2, 3)))
else:
logger.error('Windows not support spdx format.')
else:
results.append(write_output_file(combined_paths_and_files[i], output_extension, sheet_list, EXTENDED_HEADER,
'', cover))
else:
logger.error('Windows not support spdx format.')
else:
success_write, err_msg, result_file = write_output_file(output_file_without_ext, output_extension,
sheet_list, EXTENDED_HEADER, '', cover)
if success_write:
if result_file:
logger.info(f"Output file: {result_file}")
results.append(write_output_file(combined_paths_and_files[i], output_extension, sheet_list, EXTENDED_HEADER,
'', cover))
for success_write, err_msg, result_file in results:
if success_write:
if result_file:
logger.info(f"Output file: {result_file}")
else:
logger.warning(f"{err_msg}")
for i in cover_comment_arr:
logger.info(i.strip())
else:
logger.warning(f"{err_msg}")
for i in cover_comment_arr:
logger.info(i.strip())
else:
ret = False
logger.error(f"Fail to generate result file. msg:({err_msg})")
ret = False
logger.error(f"Fail to generate result file. msg:({err_msg})")

logger.warning("### FINISH ###")
return ret, sheet_list
Expand Down Expand Up @@ -267,7 +290,7 @@ def main():
parser.add_argument('-c', '--customized', nargs=1, type=str, required=False)
parser.add_argument('-n', '--appname', nargs=1, type=str, required=False)
parser.add_argument('-t', '--token', nargs=1, type=str, required=False)
parser.add_argument('-f', '--format', nargs=1, type=str, required=False)
parser.add_argument('-f', '--format', nargs="*", type=str, required=False)
parser.add_argument('--direct', choices=('true', 'false'), default='True', required=False)
parser.add_argument('--notice', action='store_true', required=False)

Expand Down Expand Up @@ -300,7 +323,7 @@ def main():
if args.token: # -t option
github_token = ''.join(args.token)
if args.format: # -f option
format = ''.join(args.format)
format = list(args.format)
if args.direct: # --direct option
if args.direct == 'true':
direct = True
Expand Down

0 comments on commit 195eb43

Please sign in to comment.