7
7
import logging
8
8
import json
9
9
import re
10
+ import shutil
11
+ import yaml
12
+ import subprocess
10
13
import fosslight_util .constant as constant
11
14
import fosslight_dependency .constant as const
12
15
from fosslight_dependency ._package_manager import PackageManager
@@ -19,55 +22,106 @@ class Pub(PackageManager):
19
22
package_manager_name = const .PUB
20
23
21
24
dn_url = 'https://pub.dev/packages/'
22
- input_file_name = os .path .join ('lib' , 'oss_licenses.dart' )
25
+ input_file_name = 'tmp_flutter_oss_licenses.json'
26
+ tmp_dir = "fl_dependency_tmp_dir"
27
+ cur_path = ''
23
28
24
29
def __init__ (self , input_dir , output_dir ):
25
30
super ().__init__ (self .package_manager_name , self .dn_url , input_dir , output_dir )
26
31
self .append_input_package_list_file (self .input_file_name )
27
32
33
+ def __del__ (self ):
34
+ if self .cur_path != '' :
35
+ os .chdir (self .cur_path )
36
+ if os .path .exists (self .tmp_dir ):
37
+ shutil .rmtree (self .tmp_dir )
38
+
39
+ def run_plugin (self ):
40
+ if not os .path .exists (const .SUPPORT_PACKAE .get (self .package_manager_name )):
41
+ logger .error (f"Cannot find the file({ const .SUPPORT_PACKAE .get (self .package_manager_name )} )" )
42
+ return False
43
+
44
+ if os .path .exists (self .tmp_dir ):
45
+ shutil .rmtree (self .tmp_dir )
46
+ os .mkdir (self .tmp_dir )
47
+ shutil .copy (const .SUPPORT_PACKAE .get (self .package_manager_name ),
48
+ os .path .join (self .tmp_dir , const .SUPPORT_PACKAE .get (self .package_manager_name )))
49
+
50
+ self .cur_path = os .getcwd ()
51
+ os .chdir (self .tmp_dir )
52
+
53
+ with open (const .SUPPORT_PACKAE .get (self .package_manager_name ), 'r' , encoding = 'utf8' ) as f :
54
+ tmp_yml = yaml .safe_load (f )
55
+ tmp_yml ['dev_dependencies' ] = {'flutter_oss_licenses' : '^2.0.1' }
56
+ with open (const .SUPPORT_PACKAE .get (self .package_manager_name ), 'w' , encoding = 'utf8' ) as f :
57
+ f .write (yaml .dump (tmp_yml ))
58
+
59
+ cmd = "flutter pub get"
60
+ ret = subprocess .call (cmd , shell = True )
61
+ if ret != 0 :
62
+ logger .error (f"Failed to run: { cmd } " )
63
+ os .chdir (self .cur_path )
64
+ return False
65
+ cmd = "flutter pub deps --no-dev"
66
+ ret = subprocess .check_output (cmd , shell = True )
67
+ if ret != 0 :
68
+ pub_deps = ret .decode ('utf8' )
69
+ for line in pub_deps .split ('\n ' ):
70
+ re_result = re .findall (r'\-\-\s(\S+[^.\s])[.|\s]' , line )
71
+ if re_result :
72
+ self .total_dep_list .append (re_result [0 ])
73
+ self .total_dep_list = list (set (self .total_dep_list ))
74
+
75
+ cmd = f"flutter pub run flutter_oss_licenses:generate.dart -o { self .input_file_name } --json"
76
+ ret = subprocess .call (cmd , shell = True )
77
+ if ret != 0 :
78
+ logger .error (f"Failed to run: { cmd } " )
79
+ os .chdir (self .cur_path )
80
+ return False
81
+
82
+ return True
83
+
28
84
def parse_oss_information (self , f_name ):
29
85
tmp_license_txt_file_name = 'tmp_license.txt'
30
86
json_data = ''
31
87
comment = ''
32
88
33
89
with open (f_name , 'r' , encoding = 'utf8' ) as pub_file :
34
- json_txt = preprocess_pub_result (pub_file )
35
- if json_txt :
36
- json_data = json .loads (json_txt )
90
+ json_f = json .load (pub_file )
37
91
38
92
try :
39
93
sheet_list = []
40
94
41
- for key in json_data :
42
- oss_origin_name = json_data [key ]['name' ]
43
- oss_name = f"{ self .package_manager_name } :{ oss_origin_name } "
44
- oss_version = json_data [key ]['version' ]
45
- homepage = json_data [key ]['homepage' ]
46
- dn_loc = f"{ self .dn_url } { oss_origin_name } /versions/{ oss_version } "
47
- license_txt = json_data [key ]['license' ]
48
-
49
- tmp_license_txt = open (tmp_license_txt_file_name , 'w' , encoding = 'utf-8' )
50
- tmp_license_txt .write (license_txt )
51
- tmp_license_txt .close ()
52
-
53
- license_name_with_license_scanner = check_and_run_license_scanner (self .platform ,
54
- self .license_scanner_bin ,
55
- tmp_license_txt_file_name )
56
-
57
- if license_name_with_license_scanner != "" :
58
- license_name = license_name_with_license_scanner
59
- else :
60
- license_name = ''
61
-
62
- if self .direct_dep :
63
- if json_data [key ]['isDirectDependency' ]:
64
- comment = 'direct'
95
+ for json_data in json_f :
96
+ oss_origin_name = json_data ['name' ]
97
+ if oss_origin_name in self .total_dep_list :
98
+ oss_name = f"{ self .package_manager_name } :{ oss_origin_name } "
99
+ oss_version = json_data ['version' ]
100
+ homepage = json_data ['homepage' ]
101
+ dn_loc = f"{ self .dn_url } { oss_origin_name } /versions/{ oss_version } "
102
+ license_txt = json_data ['license' ]
103
+
104
+ tmp_license_txt = open (tmp_license_txt_file_name , 'w' , encoding = 'utf-8' )
105
+ tmp_license_txt .write (license_txt )
106
+ tmp_license_txt .close ()
107
+
108
+ license_name_with_license_scanner = check_and_run_license_scanner (self .platform ,
109
+ self .license_scanner_bin ,
110
+ tmp_license_txt_file_name )
111
+
112
+ if license_name_with_license_scanner != "" :
113
+ license_name = license_name_with_license_scanner
65
114
else :
66
- comment = 'transitive '
115
+ license_name = ''
67
116
68
- sheet_list .append ([const .SUPPORT_PACKAE .get (self .package_manager_name ),
69
- oss_name , oss_version , license_name , dn_loc , homepage , '' , '' , comment ])
117
+ if self .direct_dep :
118
+ if json_data ['isDirectDependency' ]:
119
+ comment = 'direct'
120
+ else :
121
+ comment = 'transitive'
70
122
123
+ sheet_list .append ([const .SUPPORT_PACKAE .get (self .package_manager_name ),
124
+ oss_name , oss_version , license_name , dn_loc , homepage , '' , '' , comment ])
71
125
except Exception as e :
72
126
logger .error (f"Fail to parse pub oss information: { e } " )
73
127
@@ -78,12 +132,3 @@ def parse_oss_information(self, f_name):
78
132
79
133
def parse_direct_dependencies (self ):
80
134
self .direct_dep = True
81
-
82
-
83
- def preprocess_pub_result (input_file ):
84
- matched_json = re .findall (r'final ossLicenses = <String, dynamic>({[\s\S]*});' , input_file .read ())
85
- if len (matched_json ) > 0 :
86
- return matched_json [0 ]
87
- else :
88
- logger .error ("Fail to parse the result json from pub input file." )
89
- return ''
0 commit comments