@@ -56,35 +56,33 @@ def _validate_key(self, key: Tuple[str, str]) -> Tuple[str, str]:
56
56
"""Ensure our keys are hashable."""
57
57
if len (key ) != 2 or key != (str (key [0 ]), str (key [1 ])):
58
58
raise KeyError (f'{ self .__class__ .__name__ } key must consist of'
59
- 'a project name and a version string' )
59
+ 'a project name and a version string' )
60
60
return key
61
61
62
- def add (self , project : str , version :str , tag : str ) -> None :
62
+ def add (self , project : str , version : str , tag : str ) -> None :
63
63
"""Cache a tag for a specific project and version."""
64
64
key = self ._validate_key ((project , version ))
65
65
tags = self .data .setdefault (key , set ())
66
66
tags .add (tag )
67
67
68
- def filter (self , project : str , version : str , data : Any ) -> List [str ]:
68
+ def filter (self , project : str , version : str , data : Any ) -> List [str ]:
69
69
"""Remove all cached entries from @data."""
70
70
if isinstance (data , str ):
71
71
data = [data ]
72
72
elif not isinstance (data , Iterable ):
73
- raise ValueError ('Expecting an interable of tags!' )
73
+ raise ValueError ('Expecting an iterable of tags!' )
74
74
key = self ._validate_key ((project , version ))
75
75
return [item for item in data
76
- if item not in self .data .get (key ,[])
76
+ if item not in self .data .get (key , [])
77
77
and len (item ) > 0 ]
78
78
79
- def filter_and_cache (self , project : str , version : str , data : Any
80
- ) -> List [str ]:
79
+ def filter_and_cache (self , project : str , version : str , data : Any ) -> List [str ]:
81
80
"""Convenience method to to filtering and adding in one run."""
82
81
candidates = set (self .filter (project , version , data ))
83
82
for tag in candidates :
84
83
self .add (project , version , tag )
85
84
return list (candidates )
86
85
87
-
88
86
def __init__ (self ) -> None :
89
87
self .verbose : bool = False
90
88
self .version_regex = re .compile (r"(\d+[._])+\d+" )
@@ -123,18 +121,19 @@ def is_sourcefile_accessible(self, sourcefile_url: str) -> bool:
123
121
def github_request (url : str , username : str = "" , token : str = "" ,
124
122
return_response : bool = False ,
125
123
allow_redirects : bool = True , # default in requests
126
- ) -> Any :
124
+ ) -> Any :
127
125
try :
128
126
headers = {}
129
127
if token :
130
128
headers ["Authorization" ] = "token " + token
131
129
if username :
132
130
headers ["Username" ] = username
133
- response = requests .get (url , headers = headers , allow_redirects = allow_redirects )
131
+ response = requests .get (url , headers = headers ,
132
+ allow_redirects = allow_redirects )
134
133
if not response .ok :
135
134
if response .status_code == 429 \
136
- or 'rate limit exceeded' in response .reason \
137
- or 'API rate limit exceeded' in response .json ().get ('message' , '' ):
135
+ or 'rate limit exceeded' in response .reason \
136
+ or 'API rate limit exceeded' in response .json ().get ('message' , '' ):
138
137
print (
139
138
Fore .LIGHTYELLOW_EX +
140
139
" Github API rate limit exceeded - wait 60s and retry ... " +
@@ -222,7 +221,7 @@ def _get_github_repo(self, github_ref: str) -> Dict[str, Any]:
222
221
"""
223
222
url = 'api.github.com/repos/'
224
223
gh_ref = urlparse (github_ref , scheme = 'no_scheme' )
225
- if gh_ref .scheme == 'no_scheme' : # interprete @github_ref as OWNER/REPO
224
+ if gh_ref .scheme == 'no_scheme' : # interpret @github_ref as OWNER/REPO
226
225
url += gh_ref .path
227
226
elif not gh_ref .netloc .endswith ('github.com' ):
228
227
raise ValueError (f'{ github_ref } is not an expected @github_ref!' )
@@ -241,7 +240,7 @@ def _get_github_repo(self, github_ref: str) -> Dict[str, Any]:
241
240
raise ValueError (f"Unable to make @github_ref { github_ref } work!" )
242
241
return repo
243
242
244
- def _get_link_page (self , res : requests .Response , which : str = 'next' ) -> int :
243
+ def _get_link_page (self , res : requests .Response , which : str = 'next' ) -> int :
245
244
"""Fetch only page number from link-header."""
246
245
try :
247
246
url = urlparse (res .links [which ]['url' ])
@@ -250,8 +249,8 @@ def _get_link_page(self, res: requests.Response, which: str='next') -> int:
250
249
return 1
251
250
252
251
def get_matching_source_url (self , version : Any , github_ref : str ,
253
- version_prefix : Any = None
254
- ) -> str :
252
+ version_prefix : Any = None
253
+ ) -> str :
255
254
"""Find a URL to download source code from GitHub. We are
256
255
looking for the source code in @github_ref at @version.
257
256
@@ -271,23 +270,23 @@ def get_matching_source_url(self, version: Any, github_ref: str,
271
270
print_yellow (" " + str (err ))
272
271
return ""
273
272
274
- tags_url = repo ['tags_url' ] + '?per_page=100'
273
+ tags_url = repo ['tags_url' ] + '?per_page=100'
275
274
git_refs_url_tpl = repo ['git_refs_url' ].replace ('{/sha}' , '{sha}' , 1 )
276
275
277
276
res = self .github_request (tags_url , self .github_name ,
278
- self .github_token , return_response = True )
277
+ self .github_token , return_response = True )
279
278
pages = self ._get_link_page (res , 'last' )
280
279
for _ in range (pages ): # we prefer this over "while True"
281
280
# note: in res.json() we already have the first results page
282
281
try :
283
282
tags = [tag for tag in res .json ()
284
- if version_prefix is None \
283
+ if version_prefix is None
285
284
or tag ['name' ].startswith (version_prefix )]
286
285
source_url = self .get_matching_tag (tags , version , tags_url )
287
286
if len (source_url ) > 0 : # we found what we believe is
288
287
return source_url # the correct source_url
289
288
290
- except (TypeError , KeyError , AttributeError ) as err :
289
+ except (TypeError , KeyError , AttributeError ):
291
290
# res.json() did not give us an iterable of things where
292
291
# 'name' is a viable index, for instance an error message
293
292
tags = []
@@ -307,23 +306,23 @@ def get_matching_source_url(self, version: Any, github_ref: str,
307
306
308
307
# ORDER BY tag-name-length DESC
309
308
by_size = sorted ([(len (tag ['ref' ]), tag ) for tag in w_prefix ],
310
- key = lambda x :x [0 ])
309
+ key = lambda x : x [0 ])
311
310
w_prefix = [itm [1 ] for itm in reversed (by_size )]
312
311
313
- transformed_for_get_matching_tags = [{
314
- 'name' : tag ['ref' ].replace ('refs/tags/' , '' , 1 ),
315
- 'zipball_url' : tag ['url' ].replace (
312
+ transformed_for_get_matching_tags = [
313
+ { 'name' : tag ['ref' ].replace ('refs/tags/' , '' , 1 ),
314
+ 'zipball_url' : tag ['url' ].replace (
316
315
'/git/refs/tags/' , '/zipball/refs/tags/' , 1 ),
317
- } for tag in w_prefix ]
316
+ } for tag in w_prefix ]
318
317
source_url = self .get_matching_tag (
319
318
transformed_for_get_matching_tags , version , tags_url )
320
319
if len (source_url ) > 0 : # we found what we believe is
321
320
return source_url # the correct source_url
322
321
try :
323
322
url = res .links ['next' ]['url' ]
324
323
res = self .github_request (url , self .github_name ,
325
- self .github_token , return_response = True )
326
- except KeyError as err : # no more result pages
324
+ self .github_token , return_response = True )
325
+ except KeyError : # no more result pages
327
326
break
328
327
print_yellow (" No matching tag for version " + version + " found" )
329
328
return ""
@@ -364,7 +363,6 @@ def find_github_url(self, component: Component, use_language: bool = True) -> st
364
363
name_match = [r for r in repositories .get ("items" ) if component_name in r .get ("name" , "" )]
365
364
if len (name_match ):
366
365
for match in name_match :
367
- tag_info = self .github_request (match ["tags_url" ], self .github_name , self .github_token )
368
366
source_url = self .get_matching_source_url (component .version , match ["tags_url" ])
369
367
if len (name_match ) == 1 :
370
368
return source_url
@@ -457,7 +455,7 @@ def get_github_source_url(self, github_url: str, version: str) -> str:
457
455
def check_for_github_error (self , tag_info : get_github_info_type ) -> List [Dict [str , Any ]]:
458
456
"""This method was introduced to check the output of
459
457
get_github_info() for errors.
460
- Removed, becasue get_github_info was removed.
458
+ Removed, because get_github_info was removed.
461
459
"""
462
460
raise NotImplementedError (
463
461
"Removed with introduction of get_matchting_source_tag!" )
0 commit comments