5
5
6
6
import logging
7
7
import re
8
+ import os
8
9
import fosslight_util .constant as constant
9
10
import fosslight_dependency .constant as const
10
11
from fosslight_dependency ._package_manager import PackageManager
11
12
from fosslight_dependency ._package_manager import connect_github
12
13
from fosslight_dependency ._package_manager import get_github_license
14
+ from fosslight_dependency ._package_manager import check_and_run_license_scanner
13
15
14
16
logger = logging .getLogger (constant .LOGGER_NAME )
15
17
16
18
19
+ checkout_dir = os .path .join ("Carthage" , 'Checkouts' )
20
+ license_file_regs = ['licen[cs]e[s]?' , 'notice[s]?' , 'copying*' , 'copyright[s]?' ,
21
+ 'mit' , 'bsd[-]?[0-4]?' , 'bsd[-]?[0-4][-]?clause[s]?' , 'apache[-,_]?[1-2]?[.,-,_]?[0-2]?' ,
22
+ 'unlicen[cs]e' , '[a,l]?gpl[-]?[1-3]?[.,-,_]?[0-1]?' , 'legal' ]
23
+
24
+
17
25
class Carthage (PackageManager ):
18
26
package_manager_name = const .CARTHAGE
19
27
@@ -26,30 +34,75 @@ def __init__(self, input_dir, output_dir, github_token):
26
34
self .append_input_package_list_file (self .input_file_name )
27
35
28
36
def parse_oss_information (self , f_name ):
37
+ github = "github"
38
+ checkout_dir_list = get_checkout_dirname ()
29
39
with open (f_name , 'r' , encoding = 'utf8' ) as input_fp :
30
40
sheet_list = []
31
-
32
- g = connect_github (self .github_token )
41
+ g = ''
42
+ if not checkout_dir_list :
43
+ g = connect_github (self .github_token )
33
44
34
45
for i , line in enumerate (input_fp .readlines ()):
35
-
36
- re_result = re .findall (r'github[\s]\"(\S*)\"[\s]\"(\S*)\"' , line )
46
+ # Cartfile origin : github, git
47
+ # Ref. https://github.com/Carthage/Carthage/blob/master/Documentation/Artifacts.md
48
+ re_result = re .findall (r'(github|git)[\s]\"(\S*)\"[\s]\"(\S*)\"' , line )
37
49
try :
38
- github_repo = re_result [0 ][0 ]
39
- oss_origin_name = github_repo .split ('/' )[1 ]
50
+ repo = re_result [0 ][0 ]
51
+ oss_path = re_result [0 ][1 ]
52
+ if oss_path .endswith ('.git' ):
53
+ oss_path = oss_path [:- 4 ]
54
+ oss_origin_name = oss_path .split ('/' )[- 1 ]
40
55
oss_name = self .package_manager_name + ":" + oss_origin_name
41
- oss_version = re_result [0 ][1 ]
42
- homepage = self .dn_url + github_repo
56
+
57
+ if repo == github :
58
+ homepage = self .dn_url + oss_path
59
+ else :
60
+ homepage = oss_path
43
61
dn_loc = homepage
44
62
63
+ oss_version = re_result [0 ][2 ]
64
+
45
65
license_name = ''
46
- license_name = get_github_license (g , github_repo , self .platform , self .license_scanner_bin )
66
+ find_license = False
67
+ if oss_origin_name in checkout_dir_list :
68
+ oss_path_in_checkout = os .path .join (checkout_dir , oss_origin_name )
69
+ for filename_in_dir in os .listdir (oss_path_in_checkout ):
70
+ if find_license :
71
+ break
72
+ filename_with_checkout_path = os .path .join (oss_path_in_checkout , filename_in_dir )
73
+ if os .path .isfile (filename_with_checkout_path ):
74
+ for license_file_reg in license_file_regs :
75
+ match_result = re .match (license_file_reg , filename_in_dir .lower ())
76
+ if match_result is not None :
77
+ license_name = check_and_run_license_scanner (self .platform ,
78
+ self .license_scanner_bin ,
79
+ filename_with_checkout_path )
80
+ find_license = True
81
+ break
82
+ if license_name == '' :
83
+ if repo == github :
84
+ try :
85
+ if not g :
86
+ g = connect_github (self .github_token )
87
+ license_name = get_github_license (g , oss_path , self .platform , self .license_scanner_bin )
88
+ except Exception as e :
89
+ logger .warning ("Failed to get license with github api:" + str (e ))
90
+ license_name == ''
47
91
48
- except Exception :
49
- logger .error ("It cannot find the github oss information. So skip it." )
50
- break
92
+ sheet_list .append ([const .SUPPORT_PACKAE .get (self .package_manager_name ),
93
+ oss_name , oss_version , license_name , dn_loc , homepage , '' , '' , '' ])
51
94
52
- sheet_list . append ([ const . SUPPORT_PACKAE . get ( self . package_manager_name ),
53
- oss_name , oss_version , license_name , dn_loc , homepage , '' , '' , '' ] )
95
+ except Exception as e :
96
+ logger . warning ( "Failed to parse oss information:" + str ( e ) )
54
97
55
98
return sheet_list
99
+
100
+
101
+ def get_checkout_dirname ():
102
+ checkout_dir_list = []
103
+ if os .path .isdir (checkout_dir ):
104
+ for item in os .listdir (checkout_dir ):
105
+ if os .path .isdir (os .path .join (checkout_dir , item )):
106
+ checkout_dir_list .append (item )
107
+
108
+ return checkout_dir_list
0 commit comments