Skip to content

Commit ec18497

Browse files
committed
Fixed #495 - Enhance the usability of attrib
* Remove restriction of requiring `about_resource` field in the input if `attrib` from an inventory Signed-off-by: Chin Yeung Li <[email protected]>
1 parent 53a1841 commit ec18497

File tree

5 files changed

+72
-25
lines changed

5 files changed

+72
-25
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
2022-xx-xx
1+
2022-03-01
22
Release 7.0.0
33

44
* Add '@' as a supported character for filename #451
@@ -12,7 +12,7 @@
1212
* Use readthedocs for documentation
1313
* Add Dockerfile to run aboutcode with docker
1414
* Add new option to choose extract license from ScanCode LicenseDB or DJC License Library
15-
* Add ability to transform Excel formatted file
15+
* Add ability to transform XLSX file
1616
* Support XLSX file format for `inventory`, `gen` and `attrib`
1717
* Add 'spdx_license_key' support
1818
* Add option to save error log in `check` command
@@ -21,6 +21,8 @@
2121
* Add '%" as a supported character
2222
* Update default template
2323
* All errors are logged if and only if the `verbose` option is set. Otherwise, ony 'Critical' and 'Warning' errors will be showed/logged
24+
* Ability to generate attribution notice directly from an input inventory
25+
* Remove the restriction of requiring 'about_resource' field in the input if performing `attrib` from an inventory
2426

2527
2021-04-02
2628
Release 6.0.0

src/attributecode/attrib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def generate(abouts, is_about_input, license_dict, scancode, min_license_score,
182182
licenses_list = sorted(licenses_list, key=lambda x: x.key)
183183

184184
rendered = template.render(
185-
abouts=abouts,
185+
abouts=abouts,
186186
common_licenses=COMMON_LICENSES,
187187
licenses_list=licenses_list,
188188
utcnow=utcnow,

src/attributecode/gen.py

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ def check_about_resource_filename(arp):
116116
return ''
117117

118118

119-
# TODO: this should be either the CSV or the ABOUT files but not both???
120119
def load_inventory(location, from_attrib=False, base_dir=None, scancode=False, reference_dir=None):
121120
"""
122121
Load the inventory file at `location` for ABOUT and LICENSE files stored in
@@ -152,24 +151,24 @@ def load_inventory(location, from_attrib=False, base_dir=None, scancode=False, r
152151
arp_list = []
153152
errors = []
154153
for component in inventory:
155-
arp = component['about_resource']
156-
dup_err = check_duplicated_about_resource(arp, arp_list)
157-
if dup_err:
158-
if not dup_err in errors:
159-
errors.append(dup_err)
160-
else:
161-
arp_list.append(arp)
154+
if not from_attrib:
155+
arp = component['about_resource']
156+
dup_err = check_duplicated_about_resource(arp, arp_list)
157+
if dup_err:
158+
if not dup_err in errors:
159+
errors.append(dup_err)
160+
else:
161+
arp_list.append(arp)
162+
163+
invalid_about_filename = check_about_resource_filename(arp)
164+
if invalid_about_filename and not invalid_about_filename in errors:
165+
errors.append(invalid_about_filename)
162166

163167
newline_in_file_err = check_newline_in_file_field(component)
164168
if newline_in_file_err:
165169
errors.extend(newline_in_file_err)
166-
167-
invalid_about_filename = check_about_resource_filename(arp)
168-
if invalid_about_filename and not invalid_about_filename in errors:
169-
errors.append(invalid_about_filename)
170170
if errors:
171171
return errors, abouts
172-
173172
except Exception as e:
174173
# TODO: why catch ALL Exception
175174
msg = "The essential field 'about_resource' is not found in the <input>"
@@ -183,22 +182,32 @@ def load_inventory(location, from_attrib=False, base_dir=None, scancode=False, r
183182

184183
for f in required_fields:
185184
if f not in fields:
186-
msg = "Required field: %(f)r not found in the <input>" % locals()
187-
errors.append(Error(CRITICAL, msg))
188-
return errors, abouts
189-
afp = fields.get(model.About.ABOUT_RESOURCE_ATTR)
185+
if from_attrib and f == 'about_resource':
186+
continue
187+
else:
188+
msg = "Required field: %(f)r not found in the <input>" % locals()
189+
errors.append(Error(CRITICAL, msg))
190+
return errors, abouts
191+
# Set about file path to '' if no 'about_resource' is provided from
192+
# the input for `attrib`
193+
if not 'about_resource' in fields:
194+
afp = ''
195+
else:
196+
afp = fields.get(model.About.ABOUT_RESOURCE_ATTR)
190197

198+
"""
191199
# FIXME: this should not be a failure condition
192200
if not afp or not afp.strip():
193201
msg = 'Empty column: %(afp)r. Cannot generate .ABOUT file.' % locals()
194202
errors.append(Error(ERROR, msg))
195203
continue
196204
else:
197-
afp = util.to_posix(afp)
198-
if base_dir:
199-
loc = join(base_dir, afp)
200-
else:
201-
loc = afp
205+
"""
206+
afp = util.to_posix(afp)
207+
if base_dir:
208+
loc = join(base_dir, afp)
209+
else:
210+
loc = afp
202211
about = model.About(about_file_path=afp)
203212
about.location = loc
204213

@@ -213,6 +222,11 @@ def load_inventory(location, from_attrib=False, base_dir=None, scancode=False, r
213222
updated_resource_value = basename(resource_path)
214223
fields['about_resource'] = updated_resource_value
215224

225+
# Set 'about_resource' to '.' if no 'about_resource' is provided from
226+
# the input for `attrib`
227+
elif not 'about_resource' in fields and from_attrib:
228+
fields['about_resource'] = u'.'
229+
216230
ld_errors = about.load_dict(
217231
fields,
218232
base_dir,

tests/test_gen.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,35 @@ def test_load_inventory(self):
9393
custom1: |
9494
multi
9595
line
96+
'''
97+
)
98+
result = [a.dumps() for a in abouts]
99+
assert expected == result[0]
100+
101+
def test_load_inventory_without_about_resource(self):
102+
location = get_test_loc('test_gen/inv_no_about_resource.csv')
103+
base_dir = get_temp_dir()
104+
from_attrib = False
105+
errors, abouts = gen.load_inventory(location, base_dir=base_dir, from_attrib=from_attrib)
106+
expected_error = [Error(CRITICAL, "The essential field 'about_resource' is not found in the <input>")]
107+
108+
assert errors == expected_error
109+
assert abouts == []
110+
111+
def test_load_inventory_without_about_resource_from_attrib(self):
112+
location = get_test_loc('test_gen/inv_no_about_resource.csv')
113+
base_dir = get_temp_dir()
114+
from_attrib = True
115+
errors, abouts = gen.load_inventory(location, base_dir=base_dir, from_attrib=from_attrib)
116+
117+
expected_num_errors = 0
118+
assert len(errors) == expected_num_errors
119+
120+
expected = (
121+
'''about_resource: .
122+
name: AboutCode
123+
version: 0.11.0
124+
license_expression: apache-2.0
96125
'''
97126
)
98127
result = [a.dumps() for a in abouts]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name,version,license_expression
2+
AboutCode,0.11.0,apache-2.0

0 commit comments

Comments
 (0)