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,32 +22,98 @@ 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 os .path .exists (self .input_file_name ):
41
+ logger .info (f"Found { self .input_file_name } , skip the flutter cmd to analyze dependency." )
42
+ return True
43
+
44
+ if not os .path .exists (const .SUPPORT_PACKAE .get (self .package_manager_name )):
45
+ logger .error (f"Cannot find the file({ const .SUPPORT_PACKAE .get (self .package_manager_name )} )" )
46
+ return False
47
+
48
+ if os .path .exists (self .tmp_dir ):
49
+ shutil .rmtree (self .tmp_dir )
50
+ os .mkdir (self .tmp_dir )
51
+ shutil .copy (const .SUPPORT_PACKAE .get (self .package_manager_name ),
52
+ os .path .join (self .tmp_dir , const .SUPPORT_PACKAE .get (self .package_manager_name )))
53
+
54
+ self .cur_path = os .getcwd ()
55
+ os .chdir (self .tmp_dir )
56
+
57
+ with open (const .SUPPORT_PACKAE .get (self .package_manager_name ), 'r' , encoding = 'utf8' ) as f :
58
+ tmp_yml = yaml .safe_load (f )
59
+ tmp_yml ['dev_dependencies' ] = {'flutter_oss_licenses' : '^2.0.1' }
60
+ with open (const .SUPPORT_PACKAE .get (self .package_manager_name ), 'w' , encoding = 'utf8' ) as f :
61
+ f .write (yaml .dump (tmp_yml ))
62
+
63
+ cmd = "flutter pub get"
64
+ ret = subprocess .call (cmd , shell = True )
65
+ if ret != 0 :
66
+ logger .error (f"Failed to run: { cmd } " )
67
+ os .chdir (self .cur_path )
68
+ return False
69
+ cmd = "flutter pub deps --no-dev"
70
+ ret = subprocess .check_output (cmd , shell = True )
71
+ if ret != 0 :
72
+ pub_deps = ret .decode ('utf8' )
73
+ self .parse_no_deps_file (pub_deps )
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
+
84
+ def parse_no_deps_file (self , f ):
85
+ for line in f .split ('\n ' ):
86
+ re_result = re .findall (r'\-\-\s(\S+[^.\s])[.|\s]' , line )
87
+ if re_result :
88
+ self .total_dep_list .append (re_result [0 ])
89
+ self .total_dep_list = list (set (self .total_dep_list ))
90
+
28
91
def parse_oss_information (self , f_name ):
29
92
tmp_license_txt_file_name = 'tmp_license.txt'
30
93
json_data = ''
31
94
comment = ''
95
+ tmp_no_deps_file = 'tmp_no_deps_result.txt'
96
+
97
+ if os .path .exists (tmp_no_deps_file ):
98
+ with open (tmp_no_deps_file , 'r' , encoding = 'utf8' ) as deps_f :
99
+ deps_l = deps_f .read ()
100
+ self .parse_no_deps_file (deps_l )
32
101
33
102
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 )
103
+ json_f = json .load (pub_file )
37
104
38
105
try :
39
106
sheet_list = []
40
107
41
- for key in json_data :
42
- oss_origin_name = json_data [key ]['name' ]
108
+ for json_data in json_f :
109
+ oss_origin_name = json_data ['name' ]
110
+ if self .total_dep_list != [] and oss_origin_name not in self .total_dep_list :
111
+ continue
43
112
oss_name = f"{ self .package_manager_name } :{ oss_origin_name } "
44
- oss_version = json_data [key ][ 'version' ]
45
- homepage = json_data [key ][ 'homepage' ]
113
+ oss_version = json_data ['version' ]
114
+ homepage = json_data ['homepage' ]
46
115
dn_loc = f"{ self .dn_url } { oss_origin_name } /versions/{ oss_version } "
47
- license_txt = json_data [key ][ 'license' ]
116
+ license_txt = json_data ['license' ]
48
117
49
118
tmp_license_txt = open (tmp_license_txt_file_name , 'w' , encoding = 'utf-8' )
50
119
tmp_license_txt .write (license_txt )
@@ -60,14 +129,13 @@ def parse_oss_information(self, f_name):
60
129
license_name = ''
61
130
62
131
if self .direct_dep :
63
- if json_data [key ][ 'isDirectDependency' ]:
132
+ if json_data ['isDirectDependency' ]:
64
133
comment = 'direct'
65
134
else :
66
135
comment = 'transitive'
67
136
68
137
sheet_list .append ([const .SUPPORT_PACKAE .get (self .package_manager_name ),
69
138
oss_name , oss_version , license_name , dn_loc , homepage , '' , '' , comment ])
70
-
71
139
except Exception as e :
72
140
logger .error (f"Fail to parse pub oss information: { e } " )
73
141
@@ -78,12 +146,3 @@ def parse_oss_information(self, f_name):
78
146
79
147
def parse_direct_dependencies (self ):
80
148
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