Make fetch-wheels PEP8 compliant

This is just a little cleanup.

Change-Id: I148369869e9aac1b3f4ba25d3780d6bca380d4c9
This commit is contained in:
Johannes Kulik 2023-11-29 16:17:08 +01:00
parent 6537fe480e
commit f4cc70548a
1 changed files with 20 additions and 16 deletions

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python
import json
import os
import platform
@ -18,10 +17,12 @@ ARCH_MAP = {
'aarch64': 'arm64',
}
# Clone from the now-deprecated distutils
def strtobool(v):
# Clone from the now-deprecated distutils
return str(v).lower() in ("yes", "true", "t", "1")
def registry_urlopen(r):
if strtobool(os.environ.get('REGISTRY_INSECURE', "False")):
resp = urllib2.urlopen(r, context=ssl._create_unverified_context())
@ -29,6 +30,7 @@ def registry_urlopen(r):
resp = urllib2.urlopen(r)
return resp
def registry_request(r, token=None):
try:
if token:
@ -54,6 +56,7 @@ def registry_request(r, token=None):
return registry_request(r, token)
raise
def get_sha(repo, tag, registry, protocol):
headers = {
'Accept': ', '.join([MANIFEST_V2_LIST, MANIFEST_V2, MANIFEST_V1])
@ -104,13 +107,14 @@ def get_blob(repo, tag, protocol, registry=DOCKER_REGISTRY):
resp = registry_request(r)
return resp.read()
def protocol_detection(registry, protocol='http'):
PROTOCOLS = ('http', 'https')
index = PROTOCOLS.index(protocol)
try:
url = "{}://{}".format(protocol, registry)
r = urllib2.Request(url)
resp = urllib2.urlopen(r)
urllib2.urlopen(r)
except (urllib2.URLError, urllib2.HTTPError) as err:
if err.reason == 'Forbidden':
return protocol
@ -118,11 +122,10 @@ def protocol_detection(registry, protocol='http'):
return protocol_detection(registry, PROTOCOLS[index + 1])
else:
raise Exception("Cannot detect protocol for registry: {} due to error: {}".format(registry, err))
except:
raise
else:
return protocol
def get_wheels(url):
r = urllib2.Request(url=url)
resp = registry_request(r)
@ -134,6 +137,7 @@ def get_wheels(url):
buf = e.partial
return buf
def parse_image(full_image):
slash_occurrences = len(re.findall('/', full_image))
repo = None
@ -153,6 +157,7 @@ def parse_image(full_image):
tag = 'latest'
return registry, repo + '/' + image if repo else image, tag
def main():
if 'WHEELS' in os.environ:
wheels = os.environ['WHEELS']
@ -190,4 +195,3 @@ def main():
if __name__ == '__main__':
main()