Skip to content

Commit 374be04

Browse files
authored
Merge pull request #51 from fosslight/develop
Print pip-licenses, PTable packages if it already exists
2 parents 69f8e51 + 367c357 commit 374be04

File tree

1 file changed

+83
-22
lines changed

1 file changed

+83
-22
lines changed

src/fosslight_dependency/analyze_dependency.py

Lines changed: 83 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def check_valid_manifest_file():
5959
PACKAGE = manifest_array[int(found_idx[0])][0]
6060
logger.info("### Info Message ###")
6161
logger.info("Found the manifest file(" + manifest_array[int(found_idx[0])][1] + ")automatically.")
62-
logger.warning("Set PACKAGE =" + PACKAGE)
62+
logger.warning("Set PACKAGE = " + PACKAGE)
6363
ret = 0
6464
else:
6565
ret = 1
@@ -450,7 +450,14 @@ def start_license_checker():
450450
def start_pip_licenses():
451451
global PIP_ACTIVATE
452452

453-
tmp_file_name = "tmp_pip_license_output.json"
453+
pip_licenses = 'pip-licenses'
454+
ptable = 'PTable'
455+
pip_licenses_default_options = ' --from=mixed --with-url --format=json --with-license-file'
456+
pip_licenses_system_option = ' --with-system -p '
457+
pip_license_dependency = [pip_licenses, ptable]
458+
459+
tmp_pip_list = "tmp_list.txt"
460+
tmp_ret_file_name = []
454461

455462
if PIP_ACTIVATE.startswith("source "):
456463
tmp_activate = PIP_ACTIVATE[7:]
@@ -464,20 +471,72 @@ def start_pip_licenses():
464471
command_separator = "&"
465472
else:
466473
command_separator = ";"
474+
467475
activate_command = PIP_ACTIVATE
468-
install_pip_command = "pip install pip-licenses"
469-
pip_licenses_command = "pip-licenses --from=mixed --with-url --format=json --with-license-file > " + tmp_file_name
470-
uninstall_pip_command = "pip uninstall -y pip-licenses PTable"
476+
pip_list_command = "pip freeze > " + tmp_pip_list
471477
deactivate_command = PIP_DEACTIVATE
472478

473-
command_list = [activate_command, install_pip_command, pip_licenses_command, uninstall_pip_command,
474-
deactivate_command]
479+
command_list = [activate_command, pip_list_command, deactivate_command]
475480
command = command_separator.join(command_list)
476481

477482
ret = subprocess.call(command, shell=True)
483+
if ret != 0:
484+
logger.error("### Error Message ###")
485+
logger.error("This command(" + command + ") returns an error.")
486+
sys.exit(1)
478487

488+
exists_pip_licenses = False
489+
exists_ptable = False
490+
491+
if os.path.isfile(tmp_pip_list):
492+
with open(tmp_pip_list, 'r', encoding='utf-8') as pip_list_file:
493+
for pip_list in pip_list_file.readlines():
494+
pip_list_name = pip_list.split('==')[0]
495+
if pip_list_name == pip_licenses:
496+
exists_pip_licenses = True
497+
elif pip_list_name == ptable:
498+
exists_ptable = True
499+
os.remove(tmp_pip_list)
500+
501+
command_list = []
502+
command_list.append(activate_command)
503+
if not exists_pip_licenses:
504+
install_pip_command = "pip install " + pip_licenses
505+
command_list.append(install_pip_command)
506+
507+
tmp_file_name = "tmp_pip_license_output.json"
508+
pip_licenses_command = pip_licenses + pip_licenses_default_options + " > " + tmp_file_name
509+
command_list.append(pip_licenses_command)
510+
511+
tmp_pip_license_info_file_name = ""
512+
if exists_ptable:
513+
tmp_pip_license_info_file_name = "tmp_pip_license_info_output.json"
514+
pip_licenses_info_command = pip_licenses + pip_licenses_default_options + pip_licenses_system_option
515+
if exists_pip_licenses:
516+
pip_licenses_info_command += " ".join(pip_license_dependency)
517+
else:
518+
pip_licenses_info_command += ptable
519+
pip_licenses_info_command += " > " + tmp_pip_license_info_file_name
520+
command_list.append(pip_licenses_info_command)
521+
522+
if not exists_pip_licenses:
523+
uninstall_pip_command = "pip uninstall -y "
524+
if exists_ptable:
525+
uninstall_pip_command += pip_licenses
526+
else:
527+
uninstall_pip_command += " ".join(pip_license_dependency)
528+
command_list.append(uninstall_pip_command)
529+
530+
command_list.append(deactivate_command)
531+
532+
command = command_separator.join(command_list)
533+
534+
ret = subprocess.call(command, shell=True)
479535
if ret == 0:
480-
return tmp_file_name
536+
tmp_ret_file_name.append(tmp_file_name)
537+
if tmp_pip_license_info_file_name:
538+
tmp_ret_file_name.append(tmp_pip_license_info_file_name)
539+
return tmp_ret_file_name
481540
else:
482541
logger.error("### Error Message ###")
483542
logger.error("This command(" + command + ") returns an error.")
@@ -540,6 +599,7 @@ def check_and_run_license_scanner(file_dir, os_name):
540599
else:
541600
ret = os.system(run_license_scanner)
542601
if ret != 0:
602+
logger.error("This command(" + run_license_scanner + ") returns error.")
543603
return ""
544604

545605
fp = open(tmp_output_file_name, "r", encoding='utf8')
@@ -624,14 +684,11 @@ def parse_and_generate_output_pip(tmp_file_name):
624684
os_name = check_os()
625685
check_license_scanner(os_name)
626686

627-
sheet_list = {}
628-
687+
sheet_list = []
629688
try:
630689
with open(tmp_file_name, 'r', encoding='utf-8') as json_file:
631690
json_data = json.load(json_file)
632691

633-
sheet_list["SRC"] = []
634-
635692
for d in json_data:
636693
oss_init_name = d['Name']
637694
oss_name = "pypi:" + oss_init_name
@@ -649,15 +706,11 @@ def parse_and_generate_output_pip(tmp_file_name):
649706
if license_name_with_license_scanner != "":
650707
license_name = license_name_with_license_scanner
651708

652-
sheet_list["SRC"].append(['pip', oss_name, oss_version, license_name, dn_loc, homepage, '', '', ''])
709+
sheet_list.append(['pip', oss_name, oss_version, license_name, dn_loc, homepage, '', '', ''])
653710

654711
except Exception as ex:
655712
logger.error("Error:" + str(ex))
656713

657-
if os.path.isdir(venv_tmp_dir):
658-
shutil.rmtree(venv_tmp_dir)
659-
logger.info("remove tmp directory: " + venv_tmp_dir)
660-
661714
return sheet_list
662715

663716

@@ -1090,14 +1143,22 @@ def main_pip():
10901143
check_virtualenv_arg()
10911144

10921145
# Run the command 'pip-licenses with option'.
1093-
tmp_file_name = start_pip_licenses()
1146+
tmp_ret_file_name = start_pip_licenses()
1147+
1148+
sheet_list = {}
1149+
sheet_list["SRC"] = []
10941150

10951151
# Make output file for OSS report using temporary output file for pip-licenses.
1096-
sheet_list = parse_and_generate_output_pip(tmp_file_name)
1152+
for tmp_file in tmp_ret_file_name:
1153+
package_sheet_list = parse_and_generate_output_pip(tmp_file)
1154+
sheet_list["SRC"].extend(package_sheet_list)
10971155

1098-
# Remove temporary output file.
1099-
if os.path.isfile(tmp_file_name):
1100-
os.remove(tmp_file_name)
1156+
if os.path.isfile(tmp_file):
1157+
os.remove(tmp_file)
1158+
1159+
if os.path.isdir(venv_tmp_dir):
1160+
shutil.rmtree(venv_tmp_dir)
1161+
logger.info("remove tmp directory: " + venv_tmp_dir)
11011162

11021163
return sheet_list
11031164

0 commit comments

Comments
 (0)