|
27 | 27 | import json |
28 | 28 | import os |
29 | 29 | import posixpath |
30 | | -from requests import get |
| 30 | +import requests |
31 | 31 | import traceback |
32 | 32 | from itertools import zip_longest |
33 | 33 |
|
@@ -2069,24 +2069,30 @@ def pre_process_and_fetch_license_dict(abouts, from_check=False, api_url=None, a |
2069 | 2069 | license_url = url + lic_key + '.json' |
2070 | 2070 | license_text_url = url + lic_key + '.LICENSE' |
2071 | 2071 | try: |
2072 | | - json_url_content = get(license_url).text |
2073 | | - # We don't want to actually get the license |
2074 | | - # information from the check utility |
2075 | | - if from_check: |
2076 | | - continue |
2077 | | - data = json.loads(json_url_content) |
2078 | | - license_name = data['short_name'] |
2079 | | - license_text = get(license_text_url).text |
2080 | | - license_filename = data['key'] + '.LICENSE' |
2081 | | - lic_url = url + license_filename |
2082 | | - spdx_license_key = data['spdx_license_key'] |
2083 | | - except: |
2084 | | - if afp: |
2085 | | - msg = afp + u" : Invalid 'license': " + lic_key |
| 2072 | + response = requests.head(license_url) |
| 2073 | + if response.status_code < 400: |
| 2074 | + json_url_content = requests.get( |
| 2075 | + license_url).text |
| 2076 | + # We don't want to actually get the license |
| 2077 | + # information from the check utility |
| 2078 | + if from_check: |
| 2079 | + continue |
| 2080 | + data = json.loads(json_url_content) |
| 2081 | + license_name = data['short_name'] |
| 2082 | + license_text = get(license_text_url).text |
| 2083 | + license_filename = data['key'] + '.LICENSE' |
| 2084 | + lic_url = url + license_filename |
| 2085 | + spdx_license_key = data['spdx_license_key'] |
2086 | 2086 | else: |
2087 | | - msg = u"Invalid 'license': " + lic_key |
| 2087 | + if afp: |
| 2088 | + msg = afp + u" : Invalid 'license': " + lic_key |
| 2089 | + else: |
| 2090 | + msg = u"Invalid 'license': " + lic_key |
| 2091 | + errors.append(Error(ERROR, msg)) |
| 2092 | + continue |
| 2093 | + except requests.exceptions.RequestException as e: |
| 2094 | + msg = f"An error occurred while trying to access the URL: {e}" |
2088 | 2095 | errors.append(Error(ERROR, msg)) |
2089 | | - continue |
2090 | 2096 | if not from_check: |
2091 | 2097 | detail_list.append(license_name) |
2092 | 2098 | detail_list.append(license_filename) |
@@ -2148,7 +2154,7 @@ def detect_special_char(expression): |
2148 | 2154 |
|
2149 | 2155 | def valid_api_url(api_url): |
2150 | 2156 | try: |
2151 | | - response = get(api_url) |
| 2157 | + response = requests.get(api_url) |
2152 | 2158 | # The 403 error code is expected if the api_url is pointing to DJE as no |
2153 | 2159 | # API key is provided. The 200 status code represent connection success |
2154 | 2160 | # to scancode's LicenseDB. All other exception yield to invalid api_url |
|
0 commit comments