Skip to content

Commit c1ed496

Browse files
committed
Fixed #562 - inventory is able to collect ABOUT files that don't have about_resource
Signed-off-by: Chin Yeung Li <[email protected]>
1 parent 226263c commit c1ed496

File tree

1 file changed

+28
-31
lines changed

1 file changed

+28
-31
lines changed

src/attributecode/model.py

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import json
2828
import os
2929
import posixpath
30-
import requests
30+
from requests import get, head, exceptions
3131
import traceback
3232
from itertools import zip_longest
3333

@@ -1819,31 +1819,27 @@ def about_object_to_list_of_dictionary(abouts):
18191819
# TODO: this wholeblock should be under sd_dict()
18201820
ad = about.as_dict()
18211821

1822-
# Update the 'about_resource' field with the relative path
1823-
# from the output location
1824-
try:
1825-
if ad['about_resource']:
1826-
if 'about_file_path' in ad.keys():
1827-
afp = ad['about_file_path']
1828-
afp_parent = posixpath.dirname(afp)
1829-
afp_parent = '/' + \
1830-
afp_parent if not afp_parent.startswith(
1831-
'/') else afp_parent
1832-
about_resource = ad['about_resource']
1833-
for resource in about_resource:
1834-
updated_about_resource = posixpath.normpath(
1835-
posixpath.join(afp_parent, resource))
1836-
if resource == u'.':
1837-
if not updated_about_resource == '/':
1838-
updated_about_resource = updated_about_resource + '/'
1839-
ad['about_resource'] = dict(
1840-
[(updated_about_resource, None)])
1841-
del ad['about_file_path']
1842-
serialized.append(ad)
1843-
except Exception as e:
1844-
# The missing required field, about_resource, has already been checked
1845-
# and the error has already been logged.
1846-
pass
1822+
if 'about_file_path' in ad.keys():
1823+
afp = ad['about_file_path']
1824+
afp_parent = posixpath.dirname(afp)
1825+
afp_parent = '/' + \
1826+
afp_parent if not afp_parent.startswith(
1827+
'/') else afp_parent
1828+
1829+
# Update the 'about_resource' field with the relative path
1830+
# from the output location
1831+
if 'about_resource' in ad.keys():
1832+
about_resource = ad['about_resource']
1833+
for resource in about_resource:
1834+
updated_about_resource = posixpath.normpath(
1835+
posixpath.join(afp_parent, resource))
1836+
if resource == u'.':
1837+
if not updated_about_resource == '/':
1838+
updated_about_resource = updated_about_resource + '/'
1839+
ad['about_resource'] = dict(
1840+
[(updated_about_resource, None)])
1841+
del ad['about_file_path']
1842+
serialized.append(ad)
18471843
return serialized
18481844

18491845

@@ -2069,17 +2065,18 @@ def pre_process_and_fetch_license_dict(abouts, from_check=False, api_url=None, a
20692065
license_url = url + lic_key + '.json'
20702066
license_text_url = url + lic_key + '.LICENSE'
20712067
try:
2072-
response = requests.head(license_url)
2068+
response = head(license_url)
20732069
if response.status_code < 400:
2074-
json_url_content = requests.get(
2070+
json_url_content = get(
20752071
license_url).text
20762072
# We don't want to actually get the license
20772073
# information from the check utility
20782074
if from_check:
20792075
continue
20802076
data = json.loads(json_url_content)
20812077
license_name = data['short_name']
2082-
license_text = get(license_text_url).text
2078+
license_text = get(
2079+
license_text_url).text
20832080
license_filename = data['key'] + '.LICENSE'
20842081
lic_url = url + license_filename
20852082
spdx_license_key = data['spdx_license_key']
@@ -2090,7 +2087,7 @@ def pre_process_and_fetch_license_dict(abouts, from_check=False, api_url=None, a
20902087
msg = u"Invalid 'license': " + lic_key
20912088
errors.append(Error(ERROR, msg))
20922089
continue
2093-
except requests.exceptions.RequestException as e:
2090+
except exceptions.RequestException as e:
20942091
msg = f"An error occurred while trying to access the URL: {e}"
20952092
errors.append(Error(ERROR, msg))
20962093
if not from_check:
@@ -2154,7 +2151,7 @@ def detect_special_char(expression):
21542151

21552152
def valid_api_url(api_url):
21562153
try:
2157-
response = requests.get(api_url)
2154+
response = get(api_url)
21582155
# The 403 error code is expected if the api_url is pointing to DJE as no
21592156
# API key is provided. The 200 status code represent connection success
21602157
# to scancode's LicenseDB. All other exception yield to invalid api_url

0 commit comments

Comments
 (0)