-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathgenattrib.py
More file actions
376 lines (312 loc) · 12.9 KB
/
Copy pathgenattrib.py
File metadata and controls
376 lines (312 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/usr/bin/env python
# -*- coding: utf8 -*-
# ============================================================================
# Copyright (c) 2013-2015 nexB Inc. http://www.nexb.com/ - All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""
This tool is used to generate component attribution based on a set of .ABOUT
files. Optionally, one could pass a subset list of specific components for set
of .ABOUT files to generate attribution.
"""
from __future__ import print_function
import csv
import errno
import logging
import optparse
import os
import sys
from os.path import exists, dirname, join, abspath, isdir, basename, expanduser, normpath
from about import Collector
import genabout
LOG_FILENAME = 'error.log'
logger = logging.getLogger(__name__)
handler = logging.StreamHandler()
handler.setLevel(logging.CRITICAL)
handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
logger.addHandler(handler)
file_logger = logging.getLogger(__name__ + '_file')
__version__ = '2.0.3'
__about_spec_version__ = '1.0.0' # See http://dejacode.org
__copyright__ = """
Copyright (c) 2013-2015 nexB Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
def component_subset_to_sublist(input_list):
sublist = [row["about_file"] for row in input_list
if "about_file" in row.keys()]
return sublist
def update_path_to_about(input_list):
output_list = []
for row in input_list:
if not row.endswith('.ABOUT'):
if row.endswith('/'):
row += basename(dirname(row))
output_list.append(row + '.ABOUT')
else:
output_list.append(row)
return output_list
def convert_dict_key_to_lower_case(input_list):
output_list = []
for line in input_list:
lower_dict = {}
for key in line:
lower_dict[key.lower()] = line[key]
output_list.append(lower_dict)
return output_list
def check_about_file_existence_and_format(input_list):
try:
for row in input_list:
# Force the path to start with the '/' to map with the project
# structure
if not row['about_file'].startswith('/'):
row['about_file'] = '/' + row['about_file']
return input_list
except Exception:
return []
USAGE_SYNTAX = """\
Input can be a file or directory.
Output of rendered template must be a file (e.g. .html).
Optional:
Component List must be a .csv file which has at least an "about_file" column.
"""
VERBOSITY_HELP = """\
Print more or fewer verbose messages while processing ABOUT files
0 - Do not print any warning or error messages, just a total count (default)
1 - Print error messages
2 - Print error and warning messages
"""
TEMPLATE_LOCATION_HELP = """\
Use the custom template for the Attribution Generation
"""
MAPPING_HELP = """\
Configure the mapping key from the MAPPING.CONFIG
"""
VERIFICATION_HELP = """\
Create a verification CSV output for the attribution
"""
def extract_zip(location):
"""
Extract a zip file at location in a temp directory and return the temporary
directory where the archive was extracted.
"""
import zipfile
import tempfile
if not zipfile.is_zipfile(location):
raise Exception('Incorrect zip file %(location)r' % locals())
archive_base_name = os.path.basename(location).replace('.zip', '')
base_dir = tempfile.mkdtemp()
target_dir = os.path.join(base_dir, archive_base_name)
os.makedirs(target_dir)
with zipfile.ZipFile(location) as zipf:
for info in zipf.infolist():
name = info.filename
content = zipf.read(name)
target = os.path.join(target_dir, name)
if not os.path.exists(os.path.dirname(target)):
os.makedirs(os.path.dirname(target))
if not content and target.endswith(os.path.sep):
if not os.path.exists(target):
os.makedirs(target)
if not os.path.exists(target):
with open(target, 'wb') as f:
f.write(content)
return target_dir
def main(parser, options, args):
overwrite = options.overwrite
verbosity = options.verbosity
mapping_config = options.mapping
template_location = options.template_location
verification_location = options.verification_location
if options.version:
print('ABOUT tool {0}\n{1}'.format(__version__, __copyright__))
sys.exit(0)
if verbosity == 1:
handler.setLevel(logging.ERROR)
elif verbosity >= 2:
handler.setLevel(logging.WARNING)
if mapping_config:
if not exists('MAPPING.CONFIG'):
print("The file 'MAPPING.CONFIG' does not exist.")
sys.exit(errno.EINVAL)
if template_location:
template_location = abspath(expanduser(template_location))
if not exists(template_location):
print('The defined template location does not exist.')
parser.print_help()
sys.exit(errno.EINVAL)
if verification_location:
verification_location = abspath(expanduser(verification_location))
if not verification_location.endswith('.csv'):
print('The verification output must ends with ".csv".')
parser.print_help()
sys.exit(errno.EINVAL)
if not exists(dirname(verification_location)):
print('The verification output directory does not exist.')
parser.print_help()
sys.exit(errno.EINVAL)
if not len(args) >= 2 or not len(args) < 4:
print('The number of arguments is incorrect.\n')
parser.print_help()
sys.exit(errno.EEXIST)
input_path = args[0]
output_path = args[1]
if len(args) == 3:
component_subset_path = args[2]
else:
component_subset_path = ""
# TODO: need more path normalization (normpath, expanduser)
input_path = expanduser(normpath(input_path))
output_path = expanduser(normpath(output_path))
# Add the following to solve the
# UnicodeEncodeError: 'ascii' codec can't encode character
# FIXME: these two lines do not make sense
reload(sys)
sys.setdefaultencoding('utf-8') # @UndefinedVariable
if not exists(input_path):
print('Input path does not exist.')
parser.print_help()
sys.exit(errno.EEXIST)
if input_path.lower().endswith('.zip'):
# accept zipped ABOUT files as input
input_path = extract_zip(input_path)
if isdir(output_path):
print('Output must be a HTML file.')
parser.print_help()
sys.exit(errno.EISDIR)
# We only support HTML currently
if not output_path.endswith('.html'):
print('Output must be a HTML file.')
parser.print_help()
sys.exit(errno.EINVAL)
if exists(output_path) and not overwrite:
print('Output file already exists. Select a different file name '
'or use the --overwrite option.')
parser.print_help()
sys.exit(errno.EEXIST)
if component_subset_path and not exists(component_subset_path):
print('Component Subset path does not exist.')
parser.print_help()
sys.exit(errno.EEXIST)
if not exists(output_path) or (exists(output_path) and overwrite):
collector = Collector(input_path)
outlist = None
if not component_subset_path:
sublist = None
else:
input_list = []
with open(component_subset_path, "rU") as f:
input_dict = csv.DictReader(f)
for row in input_dict:
input_list.append(row)
updated_list = convert_dict_key_to_lower_case(input_list)
if mapping_config:
mapping_list = genabout.GenAbout().get_mapping_list()
updated_list = genabout.GenAbout().convert_input_list(updated_list, mapping_list)
if not check_about_file_existence_and_format(updated_list):
print('The required key "about_file" was not found.')
print('Please use the "--mapping" option to map the input '
'keys and verify the mapping information are correct.')
print('OR, correct the header keys from the component list.')
parser.print_help()
sys.exit(errno.EISDIR)
sublist = component_subset_to_sublist(updated_list)
outlist = update_path_to_about(sublist)
attrib_str = collector.generate_attribution(template_path=template_location, limit_to=outlist, verification=verification_location)
errors = collector.get_genattrib_errors()
if attrib_str:
try:
with open(output_path, "w") as f:
f.write(attrib_str)
except Exception as e:
print("Problem occurs. Attribution was not generated.")
print(e)
# Remove the previous log file if exist
log_path = join(dirname(output_path), LOG_FILENAME)
if exists(log_path):
os.remove(log_path)
file_handler = logging.FileHandler(log_path)
file_logger.addHandler(file_handler)
for error_msg in errors:
logger.error(error_msg)
file_logger.error(error_msg)
if errors:
print("%d errors detected." % len(errors))
else:
# we should never reach this
assert False, "Unsupported option(s)."
def get_parser():
class MyFormatter(optparse.IndentedHelpFormatter):
def _format_text(self, text):
"""
Overridden to allow description to be printed without
modification
"""
return text
def format_option(self, option):
"""
Overridden to allow options help text to be printed without
modification
"""
result = []
opts = self.option_strings[option]
opt_width = self.help_position - self.current_indent - 2
if len(opts) > opt_width:
opts = "%*s%s\n" % (self.current_indent, "", opts)
indent_first = self.help_position
else: # start help on same line as opts
opts = "%*s%-*s " % (self.current_indent, "", opt_width, opts)
indent_first = 0
result.append(opts)
if option.help:
help_text = self.expand_default(option)
help_lines = help_text.split('\n')
result.append("%*s%s\n" % (indent_first, "", help_lines[0]))
result.extend(["%*s%s\n" % (self.help_position, "", line)
for line in help_lines[1:]])
elif opts[-1] != "\n":
result.append("\n")
return "".join(result)
parser = optparse.OptionParser(
usage='%prog [options] input_path output_path [component_list]',
description=USAGE_SYNTAX,
add_help_option=False,
formatter=MyFormatter(),
)
parser.add_option("-h", "--help", action="help", help="Display help")
parser.add_option("-v", "--version", action="store_true",
help='Display current version, license notice, and copyright notice')
parser.add_option('--overwrite', action='store_true',
help='Overwrites the output file if it exists')
parser.add_option('--verbosity', type=int,
help=VERBOSITY_HELP)
parser.add_option('--template_location', type='string',
help=TEMPLATE_LOCATION_HELP)
parser.add_option('--mapping', action='store_true',
help=MAPPING_HELP)
parser.add_option('--verification_location', type='string',
help=VERIFICATION_HELP)
return parser
if __name__ == "__main__":
print('\n')
print('Running about-code-tool version ' + __version__)
print('\n')
parser = get_parser()
options, args = parser.parse_args()
main(parser, options, args)