21
21
from fosslight_util .set_log import init_log
22
22
from fosslight_util .write_excel import write_excel_and_csv
23
23
from fosslight_dependency ._help import print_help_msg
24
+ import base64
25
+
26
+ try :
27
+ from github import Github
28
+ except :
29
+ pass
24
30
25
31
# Package Name
26
32
_PKG_NAME = "fosslight_dependency"
27
33
28
34
# Check the manifest file
29
- SUPPORT_PACKAE = ["pip" , "npm" , "maven" , "gradle" , "pub" , "cocoapods" , "android" ]
35
+ SUPPORT_PACKAE = ["pip" , "npm" , "maven" , "gradle" , "pub" , "cocoapods" , "android" , "swift" ]
30
36
manifest_array = [[SUPPORT_PACKAE [0 ], "requirements.txt" ], [SUPPORT_PACKAE [1 ], "package.json" ], [SUPPORT_PACKAE [2 ], "pom.xml" ],
31
- [SUPPORT_PACKAE [3 ], "build.gradle" ], [SUPPORT_PACKAE [4 ], "pubspec.yaml" ], [SUPPORT_PACKAE [5 ], "Podfile.lock" ]]
37
+ [SUPPORT_PACKAE [3 ], "build.gradle" ], [SUPPORT_PACKAE [4 ], "pubspec.yaml" ], [SUPPORT_PACKAE [5 ], "Podfile.lock" ],
38
+ [SUPPORT_PACKAE [7 ], "Package.resolved" ]]
32
39
33
40
# binary url to check license text
34
41
license_scanner_url_linux = "third_party/nomos/nomossa"
@@ -61,7 +68,8 @@ def check_valid_manifest_file():
61
68
62
69
63
70
def parse_option ():
64
- global MANUAL_DETECT , PIP_ACTIVATE , PIP_DEACTIVATE , PACKAGE , OUTPUT_CUSTOM_DIR , CUR_PATH , OUTPUT_RESULT_DIR , APPNAME
71
+ global MANUAL_DETECT , PIP_ACTIVATE , PIP_DEACTIVATE , PACKAGE , OUTPUT_CUSTOM_DIR , CUR_PATH , OUTPUT_RESULT_DIR , \
72
+ APPNAME , GITHUB_TOKEN
65
73
66
74
default_unspecified = "UNSPECIFIED"
67
75
@@ -75,6 +83,7 @@ def parse_option():
75
83
parser .add_argument ('-v' , '--version' , action = 'store_true' , required = False )
76
84
parser .add_argument ('-o' , '--output' , nargs = 1 , type = str , required = False )
77
85
parser .add_argument ('-n' , '--appname' , nargs = 1 , type = str , required = False )
86
+ parser .add_argument ('-t' , '--token' , nargs = 1 , type = str , required = False )
78
87
79
88
args = parser .parse_args ()
80
89
@@ -147,6 +156,12 @@ def parse_option():
147
156
else :
148
157
APPNAME = "app"
149
158
159
+ # -t option
160
+ if args .token :
161
+ GITHUB_TOKEN = "" .join (args .token )
162
+ else :
163
+ GITHUB_TOKEN = ""
164
+
150
165
151
166
def configure_package ():
152
167
if MANUAL_DETECT == 0 :
@@ -332,6 +347,17 @@ def open_input_file():
332
347
if os .path .isfile (input_file_name ) != 1 :
333
348
logger .warning (input_file_name + " doesn't exist in this directory." )
334
349
350
+ if PACKAGE == "swift" :
351
+ for file_in_swift in os .listdir ("." ):
352
+ if file_in_swift .endswith (".xcodeproj" ):
353
+ input_file_name_in_xcodeproj = os .path .join (file_in_swift ,
354
+ "project.xcworkspace/xcshareddata/swiftpm" ,
355
+ input_file_name )
356
+ if input_file_name_in_xcodeproj != input_file_name :
357
+ if os .path .isfile (input_file_name_in_xcodeproj ):
358
+ input_file_name = input_file_name_in_xcodeproj
359
+ return open_input_file ()
360
+
335
361
if PACKAGE == "gradle" and MANUAL_DETECT == 0 :
336
362
return False
337
363
@@ -785,7 +811,6 @@ def parse_and_generate_output_pub(tmp_file_name):
785
811
786
812
tmp_license_txt = open (tmp_license_txt_file_name , 'w' , encoding = 'utf-8' )
787
813
tmp_license_txt .write (license_txt )
788
- # tmp_license_txt.write(license_txt.encode().decode('utf-8'))
789
814
tmp_license_txt .close ()
790
815
791
816
license_name_with_license_scanner = check_and_run_license_scanner (tmp_license_txt_file_name , os_name )
@@ -923,6 +948,71 @@ def parse_and_generate_output_android(input_fp):
923
948
return sheet_list
924
949
925
950
951
+ def parse_and_generate_output_swift (input_fp ):
952
+ global GITHUB_TOKEN
953
+
954
+ sheet_list = {}
955
+ sheet_list ["SRC" ] = []
956
+
957
+ json_raw = json .load (input_fp )
958
+ json_data = json_raw ["object" ]["pins" ]
959
+
960
+ os_name = check_os ()
961
+ check_license_scanner (os_name )
962
+
963
+ if GITHUB_TOKEN is not None :
964
+ g = Github (GITHUB_TOKEN )
965
+ else :
966
+ g = Github ()
967
+
968
+ for key in json_data :
969
+ oss_origin_name = key ['package' ]
970
+ oss_name = "swift:" + oss_origin_name
971
+
972
+ revision = key ['state' ]['revision' ]
973
+ version = key ['state' ]['version' ]
974
+ if version is None :
975
+ oss_version = revision
976
+ else :
977
+ oss_version = version
978
+
979
+ homepage = key ['repositoryURL' ]
980
+ dn_loc = homepage
981
+ license_name = ''
982
+
983
+ github_repo = "/" .join (homepage .split ('/' )[- 2 :])
984
+ try :
985
+ repository = g .get_repo (github_repo )
986
+ except Exception :
987
+ logger .error ("It cannot find the license name. Please use '-t' option with github token." )
988
+ logger .error ("{0}{1}" .format ("refer:https://docs.github.com/en/github/authenticating-to-github/" ,
989
+ "keeping-your-account-and-data-secure/creating-a-personal-access-token" ))
990
+ repository = ''
991
+
992
+ if repository is not None :
993
+ try :
994
+ license_name = repository .get_license ().license .spdx_id
995
+ except Exception :
996
+ logger .info ("Cannot find the license name with github api." )
997
+
998
+ if license_name == "" :
999
+ try :
1000
+ license_txt_data = base64 .b64decode (repository .get_license ().content ).decode ('utf-8' )
1001
+ tmp_license_txt = open (tmp_license_txt_file_name , 'w' , encoding = 'utf-8' )
1002
+ tmp_license_txt .write (license_txt_data )
1003
+ tmp_license_txt .close ()
1004
+ license_name = check_and_run_license_scanner (tmp_license_txt_file_name , os_name )
1005
+ except Exception :
1006
+ logger .info ("Cannot find the license name with license scanner binary." )
1007
+
1008
+ if os .path .isfile (tmp_license_txt_file_name ):
1009
+ os .remove (tmp_license_txt_file_name )
1010
+
1011
+ sheet_list ["SRC" ].append (['Package.resolved' , oss_name , oss_version , license_name , dn_loc , homepage , '' , '' , '' ])
1012
+
1013
+ return sheet_list
1014
+
1015
+
926
1016
###########################################
927
1017
# Main functions for each package manager #
928
1018
###########################################
@@ -1040,6 +1130,17 @@ def main_android():
1040
1130
return sheet_list
1041
1131
1042
1132
1133
+ def main_swift ():
1134
+
1135
+ input_fp = open_input_file ()
1136
+
1137
+ sheet_list = parse_and_generate_output_swift (input_fp )
1138
+
1139
+ close_input_file (input_fp )
1140
+
1141
+ return sheet_list
1142
+
1143
+
1043
1144
def set_package_variables (package ):
1044
1145
global PACKAGE , dn_url , output_file_name , input_file_name , venv_tmp_dir , pom_backup , is_maven_first_try , \
1045
1146
tmp_license_txt_file_name , source_type
@@ -1083,6 +1184,11 @@ def set_package_variables(package):
1083
1184
input_file_name = os .path .join (APPNAME , "android_dependency_output.txt" )
1084
1185
output_file_name = "android_dependency_output"
1085
1186
1187
+ elif PACKAGE == "swift" :
1188
+ input_file_name = "Package.resolved"
1189
+ output_file_name = "swift_dependency_output"
1190
+ tmp_license_txt_file_name = "tmp_license.txt"
1191
+
1086
1192
else :
1087
1193
logger .error ("### Error Message ###" )
1088
1194
logger .error ("You enter the wrong first argument." )
@@ -1128,6 +1234,8 @@ def main():
1128
1234
sheet_list = main_cocoapods ()
1129
1235
elif PACKAGE == "android" :
1130
1236
sheet_list = main_android ()
1237
+ elif PACKAGE == "swift" :
1238
+ sheet_list = main_swift ()
1131
1239
else :
1132
1240
logger .error ("### Error Message ###" )
1133
1241
logger .error ("Please enter the supported package manager. (Check the help message with (-h) option.)" )
0 commit comments