1
1
#!/usr/bin/env python
2
- # -*- coding: utf-8 -*-
3
2
#
4
3
# Copyright (c) nexB Inc. and others. All rights reserved.
5
4
# ScanCode is a trademark of nexB Inc.
@@ -559,7 +558,8 @@ def download(self, dest_dir=THIRDPARTY_DIR):
559
558
Download this distribution into `dest_dir` directory.
560
559
Return the fetched filename.
561
560
"""
562
- assert self .filename
561
+ if not self .filename :
562
+ raise ValueError (f"self.filename has no value but is required: { self .filename !r} " )
563
563
if TRACE_DEEP :
564
564
print (
565
565
f"Fetching distribution of { self .name } =={ self .version } :" ,
@@ -829,10 +829,9 @@ def fetch_license_files(self, dest_dir=THIRDPARTY_DIR, use_cached_index=False):
829
829
urls = LinksRepository .from_url (
830
830
use_cached_index = use_cached_index ).links
831
831
errors = []
832
- extra_lic_names = [l .get ("file" )
833
- for l in self .extra_data .get ("licenses" , {})]
832
+ extra_lic_names = [lic .get ("file" ) for lic in self .extra_data .get ("licenses" , {})]
834
833
extra_lic_names += [self .extra_data .get ("license_file" )]
835
- extra_lic_names = [ln for ln in extra_lic_names if ln ]
834
+ extra_lic_names = [eln for eln in extra_lic_names if eln ]
836
835
lic_names = [f"{ key } .LICENSE" for key in self .get_license_keys ()]
837
836
for filename in lic_names + extra_lic_names :
838
837
floc = os .path .join (dest_dir , filename )
@@ -853,7 +852,7 @@ def fetch_license_files(self, dest_dir=THIRDPARTY_DIR, use_cached_index=False):
853
852
if TRACE :
854
853
print (f"Fetched license from remote: { lic_url } " )
855
854
856
- except :
855
+ except Exception :
857
856
try :
858
857
# try licensedb second
859
858
lic_url = f"{ LICENSEDB_API_URL } /{ filename } "
@@ -866,8 +865,9 @@ def fetch_license_files(self, dest_dir=THIRDPARTY_DIR, use_cached_index=False):
866
865
if TRACE :
867
866
print (f"Fetched license from licensedb: { lic_url } " )
868
867
869
- except :
870
- msg = f'No text for license { filename } in expression "{ self .license_expression } " from { self } '
868
+ except Exception :
869
+ msg = f"No text for license { filename } in expression "
870
+ f"{ self .license_expression !r} from { self } "
871
871
print (msg )
872
872
errors .append (msg )
873
873
@@ -1009,7 +1009,7 @@ def get_license_link_for_filename(filename, urls):
1009
1009
exception if no link is found or if there are more than one link for that
1010
1010
file name.
1011
1011
"""
1012
- path_or_url = [l for l in urls if l .endswith (f"/{ filename } " )]
1012
+ path_or_url = [url for url in urls if url .endswith (f"/{ filename } " )]
1013
1013
if not path_or_url :
1014
1014
raise Exception (f"Missing link to file: { filename } " )
1015
1015
if not len (path_or_url ) == 1 :
@@ -1140,7 +1140,6 @@ def to_filename(self):
1140
1140
1141
1141
@attr .attributes
1142
1142
class Wheel (Distribution ):
1143
-
1144
1143
"""
1145
1144
Represents a wheel file.
1146
1145
@@ -1301,7 +1300,7 @@ def is_pure(self):
1301
1300
def is_pure_wheel (filename ):
1302
1301
try :
1303
1302
return Wheel .from_filename (filename ).is_pure ()
1304
- except :
1303
+ except Exception :
1305
1304
return False
1306
1305
1307
1306
@@ -1489,8 +1488,7 @@ def dists_from_paths_or_urls(cls, paths_or_urls):
1489
1488
)
1490
1489
except InvalidDistributionFilename :
1491
1490
if TRACE_DEEP :
1492
- print (
1493
- f" Skipping invalid distribution from: { path_or_url } " )
1491
+ print (f" Skipping invalid distribution from: { path_or_url } " )
1494
1492
continue
1495
1493
return dists
1496
1494
@@ -1500,8 +1498,7 @@ def get_distributions(self):
1500
1498
"""
1501
1499
if self .sdist :
1502
1500
yield self .sdist
1503
- for wheel in self .wheels :
1504
- yield wheel
1501
+ yield from self .wheels
1505
1502
1506
1503
def get_url_for_filename (self , filename ):
1507
1504
"""
@@ -1632,7 +1629,8 @@ class PypiSimpleRepository:
1632
1629
type = dict ,
1633
1630
default = attr .Factory (lambda : defaultdict (dict )),
1634
1631
metadata = dict (
1635
- help = "Mapping of {name: {version: PypiPackage, version: PypiPackage, etc} available in this repo"
1632
+ help = "Mapping of {name: {version: PypiPackage, version: PypiPackage, etc} "
1633
+ "available in this repo"
1636
1634
),
1637
1635
)
1638
1636
@@ -1647,7 +1645,8 @@ class PypiSimpleRepository:
1647
1645
type = bool ,
1648
1646
default = False ,
1649
1647
metadata = dict (
1650
- help = "If True, use any existing on-disk cached PyPI index files. Otherwise, fetch and cache."
1648
+ help = "If True, use any existing on-disk cached PyPI index files. "
1649
+ "Otherwise, fetch and cache."
1651
1650
),
1652
1651
)
1653
1652
@@ -1656,7 +1655,8 @@ def _get_package_versions_map(self, name):
1656
1655
Return a mapping of all available PypiPackage version for this package name.
1657
1656
The mapping may be empty. It is ordered by version from oldest to newest
1658
1657
"""
1659
- assert name
1658
+ if not name :
1659
+ raise ValueError (f"name is required: { name !r} " )
1660
1660
normalized_name = NameVer .normalize_name (name )
1661
1661
versions = self .packages [normalized_name ]
1662
1662
if not versions and normalized_name not in self .fetched_package_normalized_names :
@@ -1713,7 +1713,7 @@ def fetch_links(self, normalized_name):
1713
1713
)
1714
1714
links = collect_urls (text )
1715
1715
# TODO: keep sha256
1716
- links = [l .partition ("#sha256=" ) for l in links ]
1716
+ links = [link .partition ("#sha256=" ) for link in links ]
1717
1717
links = [url for url , _ , _sha256 in links ]
1718
1718
return links
1719
1719
@@ -1936,7 +1936,7 @@ def get_remote_file_content(
1936
1936
# several redirects and that we can ignore content there. A HEAD request may
1937
1937
# not get us this last header
1938
1938
print (f" DOWNLOADING: { url } " )
1939
- with requests .get (url , allow_redirects = True , stream = True , headers = headers ) as response :
1939
+ with requests .get (url , allow_redirects = True , stream = True , headers = headers ) as response : # noqa: S113
1940
1940
status = response .status_code
1941
1941
if status != requests .codes .ok : # NOQA
1942
1942
if status == 429 and _delay < 20 :
@@ -2161,7 +2161,7 @@ def call(args, verbose=TRACE):
2161
2161
"""
2162
2162
if TRACE_DEEP :
2163
2163
print ("Calling:" , " " .join (args ))
2164
- with subprocess .Popen (
2164
+ with subprocess .Popen ( # noqa: S603
2165
2165
args , stdout = subprocess .PIPE , stderr = subprocess .PIPE , encoding = "utf-8"
2166
2166
) as process :
2167
2167
@@ -2227,7 +2227,7 @@ def download_wheels_with_pip(
2227
2227
cli_args .extend (["--requirement" , req_file ])
2228
2228
2229
2229
if TRACE :
2230
- print (f "Downloading wheels using command:" , " " .join (cli_args ))
2230
+ print ("Downloading wheels using command:" , " " .join (cli_args ))
2231
2231
2232
2232
existing = set (os .listdir (dest_dir ))
2233
2233
error = False
@@ -2260,7 +2260,7 @@ def download_wheels_with_pip(
2260
2260
2261
2261
def check_about (dest_dir = THIRDPARTY_DIR ):
2262
2262
try :
2263
- subprocess .check_output (f"venv/bin/about check { dest_dir } " .split ())
2263
+ subprocess .check_output (f"venv/bin/about check { dest_dir } " .split ()) # noqa: S603
2264
2264
except subprocess .CalledProcessError as cpe :
2265
2265
print ()
2266
2266
print ("Invalid ABOUT files:" )
@@ -2312,5 +2312,5 @@ def get_license_expression(declared_licenses):
2312
2312
return get_only_expression_from_extracted_license (declared_licenses )
2313
2313
except ImportError :
2314
2314
# Scancode is not installed, clean and join all the licenses
2315
- lics = [python_safe_name (l ).lower () for l in declared_licenses ]
2315
+ lics = [python_safe_name (lic ).lower () for lic in declared_licenses ]
2316
2316
return " AND " .join (lics ).lower ()
0 commit comments