Skip to content

Commit ee02163

Browse files
committed
#518 - Add scancode option support for gen
Signed-off-by: Chin Yeung Li <[email protected]>
1 parent aa2ec9d commit ee02163

File tree

5 files changed

+34
-26
lines changed

5 files changed

+34
-26
lines changed

src/attributecode/cmd.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ def inventory(location, output, format, quiet, verbose): # NOQA
224224
metavar='api_url api_key',
225225
help='Fetch license data and text files from a DejaCode License Library '
226226
'API URL using the API KEY.')
227+
@click.option('--scancode',
228+
is_flag=True,
229+
help='Indicate the input JSON file is from scancode_toolkit.')
227230
@click.option('--reference',
228231
metavar='DIR',
229232
type=click.Path(exists=True, file_okay=False,
@@ -239,7 +242,7 @@ def inventory(location, output, format, quiet, verbose): # NOQA
239242
is_flag=True,
240243
help='Show all error and warning messages.')
241244
@click.help_option('-h', '--help')
242-
def gen(location, output, android, fetch_license, fetch_license_djc, reference, worksheet, quiet, verbose):
245+
def gen(location, output, android, fetch_license, fetch_license_djc, scancode, reference, worksheet, quiet, verbose):
243246
"""
244247
Given a CSV/JSON/XLSX inventory, generate ABOUT files in the output location.
245248
@@ -267,6 +270,7 @@ def gen(location, output, android, fetch_license, fetch_license_djc, reference,
267270
reference_dir=reference,
268271
fetch_license=fetch_license,
269272
fetch_license_djc=fetch_license_djc,
273+
scancode=scancode,
270274
worksheet=worksheet
271275
)
272276

src/attributecode/gen.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def check_newline_in_file_field(component):
9797
try:
9898
if '\n' in component[k]:
9999
msg = ("New line character detected in '%s' for '%s' which is not supported."
100-
"\nPlease use ',' to declare multiple files.") % (k, component['about_resource'])
100+
"\nPlease use ',' to declare multiple files.") % (k, component['about_resource'])
101101
errors.append(Error(CRITICAL, msg))
102102
except:
103103
pass
@@ -111,7 +111,7 @@ def check_about_resource_filename(arp):
111111
"""
112112
if invalid_chars(arp):
113113
msg = ("Invalid characters present in 'about_resource' "
114-
"field: " + arp)
114+
"field: " + arp)
115115
return (Error(ERROR, msg))
116116
return ''
117117

@@ -185,7 +185,8 @@ def load_inventory(location, from_attrib=False, base_dir=None, scancode=False, r
185185
if from_attrib and f == 'about_resource':
186186
continue
187187
else:
188-
msg = "Required field: %(f)r not found in the <input>" % locals()
188+
msg = "Required field: %(f)r not found in the <input>" % locals(
189+
)
189190
errors.append(Error(CRITICAL, msg))
190191
return errors, abouts
191192
# Set about file path to '' if no 'about_resource' is provided from
@@ -238,19 +239,9 @@ def load_inventory(location, from_attrib=False, base_dir=None, scancode=False, r
238239

239240
abouts.append(about)
240241
if custom_fields_list:
241-
custom_fields_err_msg = 'Field ' + str(custom_fields_list) + ' is a custom field.'
242+
custom_fields_err_msg = 'Field ' + \
243+
str(custom_fields_list) + ' is a custom field.'
242244
errors.append(Error(INFO, custom_fields_err_msg))
243-
# Covert the license_score value from string to list of int
244-
# The licesne_score is not in the spec but is specify in the scancode license scan.
245-
# This key will be treated as a custom string field. Therefore, we need to
246-
# convert back to the list with float type for score.
247-
if scancode:
248-
for about in abouts:
249-
try:
250-
score_list = list(map(float, about.license_score.value.replace('[', '').replace(']', '').split(',')))
251-
about.license_score.value = score_list
252-
except:
253-
pass
254245

255246
return errors, abouts
256247

@@ -259,7 +250,7 @@ def update_about_resource(self):
259250
pass
260251

261252

262-
def generate(location, base_dir, android=None, reference_dir=None, fetch_license=False, fetch_license_djc=False, worksheet=None):
253+
def generate(location, base_dir, android=None, reference_dir=None, fetch_license=False, fetch_license_djc=False, scancode=False, worksheet=None):
263254
"""
264255
Load ABOUT data from a CSV inventory at `location`. Write ABOUT files to
265256
base_dir. Return errors and about objects.
@@ -287,11 +278,13 @@ def generate(location, base_dir, android=None, reference_dir=None, fetch_license
287278
location=location,
288279
base_dir=bdir,
289280
reference_dir=reference_dir,
281+
scancode=scancode,
290282
worksheet=worksheet
291283
)
292284

293285
if gen_license:
294-
license_dict, err = model.pre_process_and_fetch_license_dict(abouts, api_url, api_key)
286+
license_dict, err = model.pre_process_and_fetch_license_dict(
287+
abouts, api_url, api_key)
295288
if err:
296289
for e in err:
297290
# Avoid having same error multiple times
@@ -337,7 +330,8 @@ def generate(location, base_dir, android=None, reference_dir=None, fetch_license
337330
# be validated when creating the about object
338331
loc = util.to_posix(dump_loc)
339332
about_file_loc = loc
340-
path = join(dirname(util.to_posix(about_file_loc)), about_resource_value)
333+
path = join(dirname(util.to_posix(about_file_loc)),
334+
about_resource_value)
341335
if not exists(path):
342336
path = util.to_posix(path.strip(UNC_PREFIX_POSIX))
343337
path = normpath(path)
@@ -349,10 +343,12 @@ def generate(location, base_dir, android=None, reference_dir=None, fetch_license
349343
licenses_dict = {}
350344
if gen_license:
351345
# Write generated LICENSE file
352-
license_key_name_context_url_list = about.dump_lic(dump_loc, license_dict)
346+
license_key_name_context_url_list = about.dump_lic(
347+
dump_loc, license_dict)
353348
if license_key_name_context_url_list:
354349
for lic_key, lic_name, lic_filename, lic_context, lic_url, spdx_lic_key in license_key_name_context_url_list:
355-
licenses_dict[lic_key] = [lic_name, lic_filename, lic_context, lic_url, spdx_lic_key]
350+
licenses_dict[lic_key] = [
351+
lic_name, lic_filename, lic_context, lic_url, spdx_lic_key]
356352
if not lic_name in about.license_name.value:
357353
about.license_name.value.append(lic_name)
358354
about.license_file.value[lic_filename] = lic_filename

src/attributecode/model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,7 @@ def dumps(self, licenses_dict=None):
12621262
# Handle license information that have not been handled.
12631263
license_group = list(zip_longest(
12641264
lic_key_copy, license_name, license_file, license_url, spdx_license_key))
1265+
12651266
for lic_group in license_group:
12661267
lic_dict = {}
12671268
if lic_group[0]:

tests/test_cmd.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ def test_report_errors(capsys):
4343
Error(DEBUG, 'msg4'),
4444
Error(NOTSET, 'msg4'),
4545
]
46-
ec = cmd.report_errors(errors, quiet=False, verbose=True, log_file_loc=None)
46+
ec = cmd.report_errors(errors, quiet=False,
47+
verbose=True, log_file_loc=None)
4748
assert 6 == ec
4849
out, err = capsys.readouterr()
4950
expected_out = [
@@ -67,7 +68,8 @@ def test_report_errors_without_verbose(capsys):
6768
Error(DEBUG, 'msg4'),
6869
Error(NOTSET, 'msg4'),
6970
]
70-
ec = cmd.report_errors(errors, quiet=False, verbose=False, log_file_loc=None)
71+
ec = cmd.report_errors(errors, quiet=False,
72+
verbose=False, log_file_loc=None)
7173
assert 3 == ec
7274
out, err = capsys.readouterr()
7375
expected_out = [
@@ -153,7 +155,7 @@ def test_report_errors_can_write_to_logfile():
153155

154156
result_file = get_temp_file()
155157
_ec = cmd.report_errors(errors, quiet=False, verbose=True,
156-
log_file_loc=result_file)
158+
log_file_loc=result_file)
157159
with open(result_file, 'r', encoding='utf-8', errors='replace') as rf:
158160
result = rf.read()
159161
expected = [
@@ -287,7 +289,7 @@ def test_parse_key_values_simple(self):
287289
expected = {
288290
'key': 'bar',
289291
'this': 'THat'
290-
}
292+
}
291293
keyvals, errors = cmd.parse_key_values(test)
292294
assert expected == keyvals
293295
assert not errors
@@ -332,6 +334,8 @@ def check_about_stdout(options, expected_loc, regen=False):
332334
with open(expected_file, 'r') as ef:
333335
expected = ef.read()
334336

337+
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
338+
print(result.output)
335339
assert expected.splitlines(False) == result.output.splitlines(False)
336340

337341

@@ -356,6 +360,7 @@ def test_about_gen_license_help_text():
356360
['gen-license', '--help'],
357361
'test_cmd/help/about_gen_license_help.txt', regen=False)
358362

363+
359364
def test_about_check_help_text():
360365
check_about_stdout(
361366
['check', '--help'],

tests/testdata/test_cmd/help/about_gen_help.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ Options:
1616
Fetch license data and text files from a
1717
DejaCode License Library API URL using the API
1818
KEY.
19+
--scancode Indicate the input JSON file is from
20+
scancode_toolkit.
1921
--reference DIR Path to a directory with reference license
2022
data and text files.
2123
--worksheet name The worksheet name from the INPUT. (Default:
2224
the "active" worksheet)
2325
-q, --quiet Do not print error or warning messages.
2426
--verbose Show all error and warning messages.
25-
-h, --help Show this message and exit.
27+
-h, --help Show this message and exit.

0 commit comments

Comments
 (0)