Skip to content

Commit 7e0eb73

Browse files
committed
Merge branch '364_remove_about_file_path_attr' into develop
2 parents 2474f22 + c05602b commit 7e0eb73

File tree

21 files changed

+148
-122
lines changed

21 files changed

+148
-122
lines changed

src/attributecode/cmd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747

4848
__copyright__ = """
49-
Copyright (c) 2013-2018 nexB Inc and others. All rights reserved.
49+
Copyright (c) 2013-2019 nexB Inc and others. All rights reserved.
5050
Licensed under the Apache License, Version 2.0 (the "License");
5151
you may not use this file except in compliance with the License.
5252
You may obtain a copy of the License at

src/attributecode/gen.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,21 @@ def check_duplicated_columns(location):
7878
return unique(errors)
7979

8080

81-
def check_duplicated_about_file_path(inventory_dict):
81+
def check_duplicated_about_resource(inventory_dict):
8282
"""
83-
Return a list of errors for duplicated about_file_path in a CSV file at location.
83+
Return a list of errors for duplicated about_resource in a CSV file at location.
8484
"""
85-
afp_list = []
85+
arp_list = []
8686
errors = []
8787
for component in inventory_dict:
8888
# Ignore all the empty path
89-
if component['about_file_path']:
90-
if component['about_file_path'] in afp_list:
91-
msg = ("The input has duplicated values in 'about_file_path' "
92-
"field: " + component['about_file_path'])
89+
if component['about_resource']:
90+
if component['about_resource'] in arp_list:
91+
msg = ("The input has duplicated values in 'about_resource' "
92+
"field: " + component['about_resource'])
9393
errors.append(Error(CRITICAL, msg))
9494
else:
95-
afp_list.append(component['about_file_path'])
95+
arp_list.append(component['about_resource'])
9696
return errors
9797

9898

@@ -122,13 +122,13 @@ def load_inventory(location, base_dir, reference_dir=None):
122122

123123
try:
124124
# FIXME: this should not be done here.
125-
dup_about_paths_err = check_duplicated_about_file_path(inventory)
126-
if dup_about_paths_err:
127-
errors.extend(dup_about_paths_err)
125+
dup_about_resource_err = check_duplicated_about_resource(inventory)
126+
if dup_about_resource_err:
127+
errors.extend(dup_about_resource_err)
128128
return errors, abouts
129129
except Exception as e:
130130
# TODO: why catch ALL Exception
131-
msg = "The essential field 'about_file_path' is not found in the <input>"
131+
msg = "The essential field 'about_resource' is not found in the <input>"
132132
errors.append(Error(CRITICAL, msg))
133133
return errors, abouts
134134

@@ -141,7 +141,7 @@ def load_inventory(location, base_dir, reference_dir=None):
141141
msg = "Required field: %(f)r not found in the <input>" % locals()
142142
errors.append(Error(ERROR, msg))
143143
return errors, abouts
144-
afp = fields.get(model.About.ABOUT_FILE_PATH_ATTR)
144+
afp = fields.get(model.About.ABOUT_RESOURCE_ATTR)
145145

146146
# FIXME: this should not be a failure condition
147147
if not afp or not afp.strip():
@@ -154,24 +154,39 @@ def load_inventory(location, base_dir, reference_dir=None):
154154
about = model.About(about_file_path=afp)
155155
about.location = loc
156156

157+
# Update value for 'about_resource'
158+
# keep only the filename or '.' if it's a directory
159+
if 'about_resource' in fields:
160+
updated_resource_value = u''
161+
resource_path = fields['about_resource']
162+
if resource_path.endswith(u'/'):
163+
updated_resource_value = u'.'
164+
else:
165+
updated_resource_value = basename(resource_path)
166+
fields['about_resource'] = updated_resource_value
167+
157168
ld_errors = about.load_dict(
158169
fields,
159170
base_dir,
160171
running_inventory=False,
161172
reference_dir=reference_dir,
162173
)
174+
"""
163175
# 'about_resource' field will be generated during the process.
164176
# No error need to be raise for the missing 'about_resource'.
165177
for e in ld_errors:
166178
if e.message == 'Field about_resource is required':
167179
ld_errors.remove(e)
180+
"""
168181
for e in ld_errors:
169182
if not e in errors:
170183
errors.extend(ld_errors)
171184
abouts.append(about)
172185

173186
return unique(errors), abouts
174187

188+
def update_about_resource(self):
189+
pass
175190

176191
def generate(location, base_dir, reference_dir=None, fetch_license=False):
177192
"""

src/attributecode/model.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,10 @@ def _validate(self, *args, **kwargs):
430430

431431
name = self.name
432432

433-
# FIXME: Why is the PathField an ordered dict?
433+
# Why is the PathField an ordered dict?
434+
# Ans: The reason why the PathField use an ordered dict is because
435+
# for the FileTextField, the key is used as the path to the file and
436+
# the value is used as the context of the file
434437
# dict of normalized paths to a location or None
435438
paths = OrderedDict()
436439

@@ -691,9 +694,12 @@ class About(object):
691694

692695
# name of the attribute containing the resolved relative Resources paths
693696
about_resource_path_attr = 'about_resource_path'
697+
698+
# name of the attribute containing the resolved relative Resources paths
699+
ABOUT_RESOURCE_ATTR = 'about_resource'
694700

695701
# Required fields
696-
required_fields = [ABOUT_FILE_PATH_ATTR, 'name']
702+
required_fields = ['name', ABOUT_RESOURCE_ATTR]
697703

698704
def get_required_fields(self):
699705
return [f for f in self.fields if f.required]
@@ -910,7 +916,6 @@ def process(self, fields, about_file_path, running_inventory=False,
910916
self.base_dir,
911917
self.reference_dir)
912918
errors.extend(validation_errors)
913-
914919
return errors
915920

916921
def load(self, location):
@@ -1143,7 +1148,7 @@ def get_field_names(abouts):
11431148
in any object, including custom fields.
11441149
"""
11451150
fields = []
1146-
fields.append(About.ABOUT_FILE_PATH_ATTR)
1151+
# fields.append(About.ABOUT_FILE_PATH_ATTR)
11471152

11481153
standard_fields = About().fields.keys()
11491154
standards = []
@@ -1185,11 +1190,26 @@ def about_object_to_list_of_dictionary(abouts):
11851190
for about in abouts:
11861191
# TODO: this wholeblock should be under sd_dict()
11871192
ad = about.as_dict()
1188-
if 'about_file_path' in ad.keys():
1189-
afp = ad['about_file_path']
1190-
afp = '/' + afp if not afp.startswith('/') else afp
1191-
ad['about_file_path'] = afp
1192-
serialized.append(ad)
1193+
# Update the 'about_resource' field with the relative path
1194+
# from the output location
1195+
try:
1196+
if ad['about_resource']:
1197+
if 'about_file_path' in ad.keys():
1198+
afp = ad['about_file_path']
1199+
afp_parent = posixpath.dirname(afp)
1200+
afp_parent = '/' + afp_parent if not afp_parent.startswith('/') else afp_parent
1201+
about_resource = ad['about_resource']
1202+
for resource in about_resource:
1203+
updated_about_resource = posixpath.normpath(posixpath.join(afp_parent, resource))
1204+
if resource == u'.':
1205+
updated_about_resource = updated_about_resource + '/'
1206+
ad['about_resource'] = OrderedDict([(updated_about_resource, None)])
1207+
del ad['about_file_path']
1208+
serialized.append(ad)
1209+
except Exception as e:
1210+
# The missing required field, about_resource, has already been checked
1211+
# and the error has already been logged.
1212+
pass
11931213
return serialized
11941214

11951215

tests/test_cmd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf8 -*-
33

44
# ============================================================================
5-
# Copyright (c) 2014-2017 nexB Inc. http://www.nexb.com/ - All rights reserved.
5+
# Copyright (c) 2014-2019 nexB Inc. http://www.nexb.com/ - All rights reserved.
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
88
# You may obtain a copy of the License at

tests/test_gen.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf8 -*-
33

44
# ============================================================================
5-
# Copyright (c) 2014-2017 nexB Inc. http://www.nexb.com/ - All rights reserved.
5+
# Copyright (c) 2014-2019 nexB Inc. http://www.nexb.com/ - All rights reserved.
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
88
# You may obtain a copy of the License at
@@ -45,15 +45,15 @@ def test_check_duplicated_columns_handles_lower_upper_case(self):
4545
result = gen.check_duplicated_columns(test_file)
4646
assert expected == result
4747

48-
def test_check_duplicated_about_file_path(self):
48+
def test_check_duplicated_about_resource(self):
4949
test_dict = [
50-
{'about_file_path': '/test/test.c', 'version': '1.03', 'name': 'test.c'},
51-
{'about_file_path': '/test/abc/', 'version': '1.0', 'name': 'abc'},
52-
{'about_file_path': '/test/test.c', 'version': '1.04', 'name': 'test1.c'}]
50+
{'about_resource': '/test/test.c', 'version': '1.03', 'name': 'test.c'},
51+
{'about_resource': '/test/abc/', 'version': '1.0', 'name': 'abc'},
52+
{'about_resource': '/test/test.c', 'version': '1.04', 'name': 'test1.c'}]
5353
expected = [
5454
Error(CRITICAL,
55-
"The input has duplicated values in 'about_file_path' field: /test/test.c")]
56-
result = gen.check_duplicated_about_file_path(test_dict)
55+
"The input has duplicated values in 'about_resource' field: /test/test.c")]
56+
result = gen.check_duplicated_about_resource(test_dict)
5757
assert expected == result
5858

5959
def test_load_inventory(self):

tests/test_model.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -630,11 +630,10 @@ def test_get_field_names_only_returns_non_empties(self):
630630
b.custom_fields['g'] = model.StringField(
631631
name='g', value='1', present=True)
632632
abouts = [a, b]
633-
# ensure that custom fields and about file path are collected
634-
# and that all fields are in the correct order
633+
# ensure all fields (including custom fields) and
634+
# about_resource are collected in the correct order
635635
expected = [
636-
model.About.ABOUT_FILE_PATH_ATTR,
637-
'about_resource', 'name', 'f', 'g'
636+
model.About.ABOUT_RESOURCE_ATTR, 'name', 'f', 'g'
638637
]
639638
result = model.get_field_names(abouts)
640639
assert expected == result
@@ -651,11 +650,9 @@ def test_get_field_names_does_not_return_duplicates_custom_fields(self):
651650
b.custom_fields['cf'] = model.StringField(name='cf', value='2',
652651
present=True)
653652
abouts = [a, b]
654-
# ensure that custom fields and about file path are collected
655-
# and that all fields are in the correct order
656-
# FIXME: this is not USED
653+
# ensure all fields (including custom fields) and
654+
# about_resource are collected in the correct order
657655
expected = [
658-
'about_file_path',
659656
'about_resource',
660657
'name',
661658
'cf',
@@ -1092,9 +1089,6 @@ def test_collect_inventory_with_no_about_resource_from_directory(self):
10921089
expected_errors = [Error(CRITICAL, 'about/about.ABOUT: Field about_resource is required')]
10931090
assert expected_errors == errors
10941091

1095-
expected = get_test_loc('test_model/inventory/no_about_resource_key/expected.csv')
1096-
check_csv(expected, result)
1097-
10981092
def test_collect_inventory_complex_from_directory(self):
10991093
location = get_test_loc('test_model/inventory/complex')
11001094
result = get_temp_file()
Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
"about_file_path","name","version"
2-
"PyJWT-1.6.4-py2.py3-none-any.whl","PyJWT","1.6.4"
3-
"pyldap-2.4.45.tar.gz","pyldap","2.4.45"
4-
"xmlcatmgr-2.2_2.txz","xmlcatmgr","v 2.2_2"
5-
"sdocbook-xml-1.1_2.2.txz","sdocbook-xml","v 1.1_2,2"
6-
"Pygments-2.2.0-py2.py3-none-any.whl","Pygments","2.2.0"
7-
"ruby22-gems-2.6.4.txz","ruby22-gems","v 2.6.4"
8-
"xorg-macros-1.19.0.txz","xorg-macros","v 1.19.0"
9-
"samba42-4.2.14.txz","samba42","v 4.2.14"
10-
"xmlto-0.0.26_2.txz","xmlto","v 0.0.26_2"
11-
"xtrans-1.3.5.txz","xtrans","v 1.3.5"
12-
"w3m-0.5.3_4.txz","w3m","v 0.5.3_4"
13-
"pyaml-17.12.1-py2.py3-none-any.whl","pyaml","17.12.1"
14-
"siproxd-0.8.1.txz","siproxd","v 0.8.1"
15-
"xcb-util-0.4.0_1.1.txz","xcb-util","v 0.4.0_1,1"
16-
"requests-2.17.3-py2.py3-none-any.whl","requests","2.17.3"
17-
"setuptools-36.6.0-py2.py3-none-any.whl","setuptools","36.6.0"
18-
"ruby-2.2.5_1.1.txz","ruby","v 2.2.5_1,1"
19-
"packageurl_python-0.8.1-py2.py3-none-any.whl","packageurl-python","0.8.1"
20-
"certifi-2017.4.17-py2.py3-none-any.whl","certifi","2017.4.17"
21-
"psycopg2-2.7.5.tar.gz","psycopg2","2.7.5"
22-
"xorg-fonts-truetype-7.7_1.txz","xorg-fonts-truetype","v 7.7_1"
23-
"xerces-c3-3.1.4.txz","xerces-c3","v 3.1.4"
1+
about_resource,name,version
2+
PyJWT-1.6.4-py2.py3-none-any.whl,PyJWT,1.6.4
3+
pyldap-2.4.45.tar.gz,pyldap,2.4.45
4+
xmlcatmgr-2.2_2.txz,xmlcatmgr,v 2.2_2
5+
sdocbook-xml-1.1_2.2.txz,sdocbook-xml,"v 1.1_2,2"
6+
Pygments-2.2.0-py2.py3-none-any.whl,Pygments,2.2.0
7+
ruby22-gems-2.6.4.txz,ruby22-gems,v 2.6.4
8+
xorg-macros-1.19.0.txz,xorg-macros,v 1.19.0
9+
samba42-4.2.14.txz,samba42,v 4.2.14
10+
xmlto-0.0.26_2.txz,xmlto,v 0.0.26_2
11+
xtrans-1.3.5.txz,xtrans,v 1.3.5
12+
w3m-0.5.3_4.txz,w3m,v 0.5.3_4
13+
pyaml-17.12.1-py2.py3-none-any.whl,pyaml,17.12.1
14+
siproxd-0.8.1.txz,siproxd,v 0.8.1
15+
xcb-util-0.4.0_1.1.txz,xcb-util,"v 0.4.0_1,1"
16+
requests-2.17.3-py2.py3-none-any.whl,requests,2.17.3
17+
setuptools-36.6.0-py2.py3-none-any.whl,setuptools,36.6.0
18+
ruby-2.2.5_1.1.txz,ruby,"v 2.2.5_1,1"
19+
packageurl_python-0.8.1-py2.py3-none-any.whl,packageurl-python,0.8.1
20+
certifi-2017.4.17-py2.py3-none-any.whl,certifi,2017.4.17
21+
psycopg2-2.7.5.tar.gz,psycopg2,2.7.5
22+
xorg-fonts-truetype-7.7_1.txz,xorg-fonts-truetype,v 7.7_1
23+
xerces-c3-3.1.4.txz,xerces-c3,v 3.1.4

tests/testdata/test_gen/inv.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
about_file_path,about_resource,name,version,download_url,description,homepage_url,notes,license_name,license_file,license_url,copyright,notice_file,notice_url,redistribute,attribute,track_changes,modified,changelog_file,owner,owner_url,contact,author,vcs_tool,vcs_repository,vcs_path,vcs_tag,vcs_branch,vcs_revision,checksum_md5,checksum_sha1,spec_version,custom1
2-
inv/this.ABOUT,.,AboutCode,0.11.0,,"multi
1+
about_resource,name,version,download_url,description,homepage_url,notes,license_name,license_file,license_url,copyright,notice_file,notice_url,redistribute,attribute,track_changes,modified,changelog_file,owner,owner_url,contact,author,vcs_tool,vcs_repository,vcs_path,vcs_tag,vcs_branch,vcs_revision,checksum_md5,checksum_sha1,spec_version,custom1
2+
/inv/,AboutCode,0.11.0,,"multi
33
line",,,,,,,,,,,,,,,,,,,,,,,,,,,"multi
44
line"

tests/testdata/test_gen/inv2.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
about_file_path,name,version
2-
inv/,AboutCode,0.11.0
1+
about_resource,name,version
2+
/inv/,AboutCode,0.11.0

tests/testdata/test_gen/inv3.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
about_file_path,name,version
1+
about_resource,name,version
22
inv/test.tar.gz,AboutCode,0.11.0

0 commit comments

Comments
 (0)