@@ -59,7 +59,7 @@ def check_valid_manifest_file():
59
59
PACKAGE = manifest_array [int (found_idx [0 ])][0 ]
60
60
logger .info ("### Info Message ###" )
61
61
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 )
63
63
ret = 0
64
64
else :
65
65
ret = 1
@@ -450,7 +450,14 @@ def start_license_checker():
450
450
def start_pip_licenses ():
451
451
global PIP_ACTIVATE
452
452
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 = []
454
461
455
462
if PIP_ACTIVATE .startswith ("source " ):
456
463
tmp_activate = PIP_ACTIVATE [7 :]
@@ -464,20 +471,72 @@ def start_pip_licenses():
464
471
command_separator = "&"
465
472
else :
466
473
command_separator = ";"
474
+
467
475
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
471
477
deactivate_command = PIP_DEACTIVATE
472
478
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 ]
475
480
command = command_separator .join (command_list )
476
481
477
482
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 )
478
487
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 )
479
535
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
481
540
else :
482
541
logger .error ("### Error Message ###" )
483
542
logger .error ("This command(" + command + ") returns an error." )
@@ -540,6 +599,7 @@ def check_and_run_license_scanner(file_dir, os_name):
540
599
else :
541
600
ret = os .system (run_license_scanner )
542
601
if ret != 0 :
602
+ logger .error ("This command(" + run_license_scanner + ") returns error." )
543
603
return ""
544
604
545
605
fp = open (tmp_output_file_name , "r" , encoding = 'utf8' )
@@ -624,14 +684,11 @@ def parse_and_generate_output_pip(tmp_file_name):
624
684
os_name = check_os ()
625
685
check_license_scanner (os_name )
626
686
627
- sheet_list = {}
628
-
687
+ sheet_list = []
629
688
try :
630
689
with open (tmp_file_name , 'r' , encoding = 'utf-8' ) as json_file :
631
690
json_data = json .load (json_file )
632
691
633
- sheet_list ["SRC" ] = []
634
-
635
692
for d in json_data :
636
693
oss_init_name = d ['Name' ]
637
694
oss_name = "pypi:" + oss_init_name
@@ -649,15 +706,11 @@ def parse_and_generate_output_pip(tmp_file_name):
649
706
if license_name_with_license_scanner != "" :
650
707
license_name = license_name_with_license_scanner
651
708
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 , '' , '' , '' ])
653
710
654
711
except Exception as ex :
655
712
logger .error ("Error:" + str (ex ))
656
713
657
- if os .path .isdir (venv_tmp_dir ):
658
- shutil .rmtree (venv_tmp_dir )
659
- logger .info ("remove tmp directory: " + venv_tmp_dir )
660
-
661
714
return sheet_list
662
715
663
716
@@ -1090,14 +1143,22 @@ def main_pip():
1090
1143
check_virtualenv_arg ()
1091
1144
1092
1145
# 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" ] = []
1094
1150
1095
1151
# 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 )
1097
1155
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 )
1101
1162
1102
1163
return sheet_list
1103
1164
0 commit comments