Skip to content

Commit 5671563

Browse files
committed
Treat text files as text
And not a possible binaries Also Ensure that we craft a minimally parsable license expression, even if not correct. Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
1 parent c41196a commit 5671563

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

etc/scripts/utils_thirdparty.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@
2424
import attr
2525
import license_expression
2626
import packageurl
27-
import utils_pip_compatibility_tags
28-
import utils_pypi_supported_tags
2927
import requests
3028
import saneyaml
29+
import utils_pip_compatibility_tags
30+
import utils_pypi_supported_tags
3131

3232
from commoncode import fileutils
3333
from commoncode.hash import multi_checksums
34+
from commoncode.text import python_safe_name
3435
from packaging import tags as packaging_tags
3536
from packaging import version as packaging_version
3637
from utils_requirements import load_requirements
@@ -172,11 +173,20 @@ def fetch_wheels(
172173
else:
173174
force_pinned = False
174175

175-
rrp = list(get_required_remote_packages(
176-
requirements_file=requirements_file,
177-
force_pinned=force_pinned,
178-
remote_links_url=remote_links_url,
179-
))
176+
try:
177+
rrp = list(get_required_remote_packages(
178+
requirements_file=requirements_file,
179+
force_pinned=force_pinned,
180+
remote_links_url=remote_links_url,
181+
))
182+
except Exception as e:
183+
raise Exception(
184+
dict(
185+
requirements_file=requirements_file,
186+
force_pinned=force_pinned,
187+
remote_links_url=remote_links_url,
188+
)
189+
) from e
180190

181191
fetched_filenames = set()
182192
for name, version, package in rrp:
@@ -211,6 +221,7 @@ def fetch_wheels(
211221
print(f'Missed package {nv} in remote repo, has only:')
212222
for pv in rr.get_versions(n):
213223
print(' ', pv)
224+
raise Exception('Missed some packages in remote repo')
214225

215226

216227
def fetch_sources(
@@ -261,6 +272,8 @@ def fetch_sources(
261272
fetched = package.fetch_sdist(dest_dir=dest_dir)
262273
error = f'Failed to fetch' if not fetched else None
263274
yield package, error
275+
if missed:
276+
raise Exception(f'Missing source packages in {remote_links_url}', missed)
264277

265278
################################################################################
266279
#
@@ -693,8 +706,7 @@ def save_if_modified(location, content):
693706
return False
694707

695708
if TRACE: print(f'Saving ABOUT (and NOTICE) files for: {self}')
696-
wmode = 'wb' if isinstance(content, bytes) else 'w'
697-
with open(location, wmode, encoding="utf-8") as fo:
709+
with open(location, 'w') as fo:
698710
fo.write(content)
699711
return True
700712

@@ -905,16 +917,16 @@ def load_pkginfo_data(self, dest_dir=THIRDPARTY_DIR):
905917
other_classifiers = [c for c in classifiers if not c.startswith('License')]
906918

907919
holder = raw_data['Author']
908-
holder_contact=raw_data['Author-email']
909-
copyright = f'Copyright (c) {holder} <{holder_contact}>'
920+
holder_contact = raw_data['Author-email']
921+
copyright_statement = f'Copyright (c) {holder} <{holder_contact}>'
910922

911923
pkginfo_data = dict(
912924
name=raw_data['Name'],
913925
declared_license=declared_license,
914926
version=raw_data['Version'],
915927
description=raw_data['Summary'],
916928
homepage_url=raw_data['Home-page'],
917-
copyright=copyright,
929+
copyright=copyright_statement,
918930
license_expression=license_expression,
919931
holder=holder,
920932
holder_contact=holder_contact,
@@ -1845,7 +1857,7 @@ def get(self, path_or_url, as_text=True):
18451857
if not os.path.exists(cached):
18461858
content = get_file_content(path_or_url=path_or_url, as_text=as_text)
18471859
wmode = 'w' if as_text else 'wb'
1848-
with open(cached, wmode, encoding="utf-8") as fo:
1860+
with open(cached, wmode) as fo:
18491861
fo.write(content)
18501862
return content
18511863
else:
@@ -1857,7 +1869,7 @@ def put(self, filename, content):
18571869
"""
18581870
cached = os.path.join(self.directory, filename)
18591871
wmode = 'wb' if isinstance(content, bytes) else 'w'
1860-
with open(cached, wmode, encoding="utf-8") as fo:
1872+
with open(cached, wmode) as fo:
18611873
fo.write(content)
18621874

18631875

@@ -2331,7 +2343,7 @@ def get_required_remote_packages(
23312343
repo = get_remote_repo(remote_links_url=remote_links_url)
23322344
else:
23332345
# a local path
2334-
assert os.path.exists(remote_links_url)
2346+
assert os.path.exists(remote_links_url), f'Path does not exist: {remote_links_url}'
23352347
repo = get_local_repo(directory=remote_links_url)
23362348

23372349
for name, version in required_name_versions:
@@ -2365,7 +2377,7 @@ def update_requirements(name, version=None, requirements_file='requirements.txt'
23652377
updated_name_versions = sorted(updated_name_versions)
23662378
nvs = '\n'.join(f'{name}=={version}' for name, version in updated_name_versions)
23672379

2368-
with open(requirements_file, 'w', encoding="utf-8") as fo:
2380+
with open(requirements_file, 'w') as fo:
23692381
fo.write(nvs)
23702382

23712383

@@ -2383,7 +2395,7 @@ def hash_requirements(dest_dir=THIRDPARTY_DIR, requirements_file='requirements.t
23832395
raise Exception(f'Missing required package {name}=={version}')
23842396
hashed.append(package.specifier_with_hashes)
23852397

2386-
with open(requirements_file, 'w', encoding="utf-8") as fo:
2398+
with open(requirements_file, 'w') as fo:
23872399
fo.write('\n'.join(hashed))
23882400

23892401
################################################################################
@@ -2961,5 +2973,6 @@ def compute_normalized_license_expression(declared_licenses):
29612973
from packagedcode import pypi
29622974
return pypi.compute_normalized_license(declared_licenses)
29632975
except ImportError:
2964-
# Scancode is not installed, we join all license strings and return it
2965-
return ' '.join(declared_licenses)
2976+
# Scancode is not installed, clean and join all the licenses
2977+
lics = [python_safe_name(l).lower() for l in declared_licenses]
2978+
return ' AND '.join(lics).lower()

0 commit comments

Comments
 (0)