5
5
6
6
import os
7
7
import sys
8
- import platform
9
8
import argparse
10
9
import pkg_resources
11
10
import warnings
16
15
import fosslight_util .constant as constant
17
16
from fosslight_dependency ._help import print_help_msg
18
17
from fosslight_dependency ._analyze_dependency import analyze_dependency
19
- from fosslight_util .write_excel import write_excel_and_csv
18
+ from fosslight_util .output_format import check_output_format , write_output_file
20
19
21
20
# Package Name
22
21
_PKG_NAME = "fosslight_dependency"
23
22
logger = logging .getLogger (constant .LOGGER_NAME )
24
23
warnings .filterwarnings ("ignore" , category = FutureWarning )
25
24
_sheet_name = "SRC_FL_Dependency"
26
- _fosslight_report = "FOSSLight-Report"
27
- _xlsx_extension = '.xlsx'
28
- _csv_extension = '.csv'
29
25
30
26
31
27
def find_package_manager ():
@@ -54,49 +50,36 @@ def find_package_manager():
54
50
return ret , found_package_manager
55
51
56
52
57
- def run_dependency_scanner (package_manager = '' , input_dir = '' , output_dir = '' , pip_activate_cmd = '' , pip_deactivate_cmd = '' ,
58
- output_custom_dir = '' , app_name = const .default_app_name , github_token = '' ):
53
+ def run_dependency_scanner (package_manager = '' , input_dir = '' , output_dir_file = '' , pip_activate_cmd = '' , pip_deactivate_cmd = '' ,
54
+ output_custom_dir = '' , app_name = const .default_app_name , github_token = '' , format = '' ):
59
55
global logger
60
56
61
57
ret = True
62
- output_filename = ''
63
58
sheet_list = {}
64
59
sheet_list [_sheet_name ] = []
60
+ _json_ext = ".json"
61
+ _start_time = datetime .now ().strftime ('%y%m%d_%H%M%S' )
65
62
66
- if output_dir :
67
- dirname = os .path .dirname (output_dir )
68
- basename = os .path .basename (output_dir )
63
+ success , msg , output_path , output_file , output_extension = check_output_format (output_dir_file , format )
64
+ if success :
65
+ if output_path == "" :
66
+ output_path = os .getcwd ()
67
+ else :
68
+ output_path = os .path .abspath (output_path )
69
69
70
- if basename .endswith (_xlsx_extension ) or basename .endswith (_csv_extension ):
71
- output_filename = os .path .splitext (basename )[0 ]
72
- if dirname :
73
- output_dir = dirname
70
+ if output_file == "" :
71
+ if output_extension == _json_ext :
72
+ output_file = "Opossum_input_" + _start_time
74
73
else :
75
- output_dir = os .getcwd ()
76
-
77
- if os .path .isdir (output_dir ):
78
- output_dir = os .path .abspath (output_dir )
79
- else :
80
- try :
81
- os .makedirs (output_dir )
82
- output_dir = os .path .abspath (output_dir )
83
- except Exception as e :
84
- err_msg = str (e )
85
- ret = False
86
- output_dir = os .getcwd ()
87
- else :
88
- output_dir = os .getcwd ()
74
+ output_file = "FOSSLight-Report_" + _start_time
89
75
90
- start_time = datetime .now ().strftime ('%y%m%d_%H%M%S' )
91
- logger , _result_log = init_log (os .path .join (output_dir , "fosslight_dependency_log_" + start_time + ".txt" ),
76
+ logger , _result_log = init_log (os .path .join (output_path , "fosslight_dependency_log_" + _start_time + ".txt" ),
92
77
True , logging .INFO , logging .DEBUG , _PKG_NAME )
93
78
94
79
logger .info ("Tool Info : " + _result_log ["Tool Info" ])
95
80
96
- if not ret :
97
- logger .error ("You entered the wrong output path(" + output_dir + ") to generate output file." )
98
- logger .error ("Please enter the output path that already exists or can be created with the '-o' option." )
99
- logger .error (" > err msg : " + err_msg )
81
+ if not success :
82
+ logger .error (msg )
100
83
return False , sheet_list
101
84
102
85
autodetect = True
@@ -137,27 +120,21 @@ def run_dependency_scanner(package_manager='', input_dir='', output_dir='', pip_
137
120
found_package_manager .append (package_manager )
138
121
139
122
for pm in found_package_manager :
140
- ret , package_sheet_list = analyze_dependency (pm , input_dir , output_dir , pip_activate_cmd , pip_deactivate_cmd ,
123
+ ret , package_sheet_list = analyze_dependency (pm , input_dir , output_path , pip_activate_cmd , pip_deactivate_cmd ,
141
124
output_custom_dir , app_name , github_token )
142
125
if ret :
143
126
sheet_list [_sheet_name ].extend (package_sheet_list )
144
127
145
- if not output_filename :
146
- output_filename = _fosslight_report + '_' + start_time
147
-
148
128
if sheet_list is not None :
149
- success , msg = write_excel_and_csv (os .path .join (output_dir , output_filename ), sheet_list )
150
- if success :
151
- output_xlsx_name = output_filename + _xlsx_extension
152
- if platform .system () == const .WINDOWS :
153
- logger .info ("Generated {0} into {1}!" .format (output_xlsx_name , output_dir ))
154
- else :
155
- output_csv_name = output_filename + '_' + _sheet_name + _csv_extension
156
- logger .info ("Generated {0} and {1} into {2}!"
157
- .format (output_xlsx_name , output_csv_name , output_dir ))
129
+ output_file_without_ext = os .path .join (output_path , output_file )
130
+ success_to_write , writing_msg = write_output_file (output_file_without_ext , output_extension ,
131
+ sheet_list )
132
+ if success_to_write :
133
+ logger .info ("Writing Output file(" + output_file + output_extension + "):" + str (success_to_write ) + " "
134
+ + writing_msg )
158
135
else :
159
136
ret = False
160
- logger .error ("Fail to generate result file. msg:()" + msg )
137
+ logger .error ("Fail to generate result file. msg:()" + writing_msg )
161
138
else :
162
139
logger .error ("Analyzing result is empty." )
163
140
@@ -174,6 +151,7 @@ def main():
174
151
output_custom_dir = ''
175
152
app_name = const .default_app_name
176
153
github_token = ''
154
+ format = ''
177
155
178
156
parser = argparse .ArgumentParser (add_help = False )
179
157
parser .add_argument ('-h' , '--help' , action = 'store_true' , required = False )
@@ -186,6 +164,7 @@ def main():
186
164
parser .add_argument ('-c' , '--customized' , nargs = 1 , type = str , required = False )
187
165
parser .add_argument ('-n' , '--appname' , nargs = 1 , type = str , required = False )
188
166
parser .add_argument ('-t' , '--token' , nargs = 1 , type = str , required = False )
167
+ parser .add_argument ('-f' , '--format' , nargs = 1 , type = str , required = False )
189
168
190
169
args = parser .parse_args ()
191
170
@@ -213,9 +192,11 @@ def main():
213
192
app_name = '' .join (args .appname )
214
193
if args .token : # -t option
215
194
github_token = '' .join (args .token )
195
+ if args .format : # -f option
196
+ format = '' .join (args .format )
216
197
217
198
run_dependency_scanner (package_manager , input_dir , output_dir , pip_activate_cmd , pip_deactivate_cmd ,
218
- output_custom_dir , app_name , github_token )
199
+ output_custom_dir , app_name , github_token , format )
219
200
220
201
221
202
if __name__ == '__main__' :
0 commit comments