Skip to content

Commit

Permalink
chore: use single quotes by default in Python scripts (#1474)
Browse files Browse the repository at this point in the history
Apparently I've not been as consistent on this as I thought I was, so as
punishment I've gone through all the scripts and made sure they're using
single quotes by default - aside from still being used within `f`
strings due to how that syntax works for string literals within
interopolations, I have retained the usage of double quotes in four
`print` calls which have single quotes in them to avoid doing an escape
  • Loading branch information
G-Rath authored Jan 8, 2025
1 parent 729bb95 commit aebdc23
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 135 deletions.
74 changes: 37 additions & 37 deletions scripts/generators/generate-alpine-versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ def is_unsupported_comparison(line):


def uncomment(line):
if line.startswith("#"):
if line.startswith('#'):
return line[1:]
if line.startswith("//"):
if line.startswith('//'):
return line[2:]
return line


def download_alpine_db():
urllib.request.urlretrieve("https://osv-vulnerabilities.storage.googleapis.com/Alpine/all.zip", "alpine-db.zip")
urllib.request.urlretrieve('https://osv-vulnerabilities.storage.googleapis.com/Alpine/all.zip', 'alpine-db.zip')


def extract_packages_with_versions(osvs):
Expand Down Expand Up @@ -75,7 +75,7 @@ def __init__(self, cache_path, how):
self.cache_path = Path(cache_path)
self.cache = {}

self._alpine_version = "3.10"
self._alpine_version = '3.10'
self._compare_method = how
self._docker_container = None
self._load_cache()
Expand All @@ -91,40 +91,40 @@ def _start_docker_container(self):
if self._docker_container is not None:
return

container_name = f"alpine-{self._alpine_version}-container"
container_name = f'alpine-{self._alpine_version}-container'

cmd = ["docker", "run", "--rm", "--name", container_name, "-d", f"alpine:{self._alpine_version}", "tail", "-f", "/dev/null"]
cmd = ['docker', 'run', '--rm', '--name', container_name, '-d', f'alpine:{self._alpine_version}', 'tail', '-f', '/dev/null']
out = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

if out.returncode != 0:
raise Exception(f"failed to start {container_name} container: {out.stderr.decode('utf-8')}")
raise Exception(f'failed to start {container_name} container: {out.stderr.decode("utf-8")}')
self._docker_container = container_name
atexit.register(self._stop_docker_container)

def _stop_docker_container(self):
if self._docker_container is None:
raise Exception(f"called to stop docker container when none was started")
raise Exception(f'called to stop docker container when none was started')

cmd = ["docker", "stop", "-t", "0", self._docker_container]
cmd = ['docker', 'stop', '-t', '0', self._docker_container]
out = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

if out.returncode != 0:
raise Exception(f"failed to stop {self._docker_container} container: {out.stderr.decode('utf-8')}")
raise Exception(f'failed to stop {self._docker_container} container: {out.stderr.decode("utf-8")}')

def _load_cache(self):
if self.cache_path:
self.cache_path.touch()
with open(self.cache_path, "r") as f:
with open(self.cache_path, 'r') as f:
lines = f.readlines()

for line in lines:
line = line.strip()
key, result = line.split(",")
key, result = line.split(',')

if result == "True":
if result == 'True':
self.cache[key] = True
continue
if result == "False":
if result == 'False':
self.cache[key] = False
continue

Expand All @@ -134,33 +134,33 @@ def _save_to_cache(self, key, result):
self.cache[key] = result
if self.cache_path:
self.cache_path.touch()
with open(self.cache_path, "a") as f:
f.write(f"{key},{result}\n")
with open(self.cache_path, 'a') as f:
f.write(f'{key},{result}\n')

def _compare_command(self, a, b):
if self._compare_method == "run":
return ["docker", "run", "--rm", f"alpine:{self._alpine_version}", "apk", "version", "-t", a, b]
if self._compare_method == 'run':
return ['docker', 'run', '--rm', f'alpine:{self._alpine_version}', 'apk', 'version', '-t', a, b]

self._start_docker_container()

return ["docker", "exec", self._docker_container, "apk", "version", "-t", a, b]
return ['docker', 'exec', self._docker_container, 'apk', 'version', '-t', a, b]

def compare(self, a, op, b):
key = f"{a} {op} {b}"
key = f'{a} {op} {b}'
if key in self.cache:
return self.cache[key]

out = subprocess.run(self._compare_command(a, b), stdout=subprocess.PIPE, stderr=subprocess.PIPE)

if out.returncode != 0:
raise Exception(f"apk did not like comparing {a} {op} {b}: {out.stderr.decode('utf-8')}")
raise Exception(f'apk did not like comparing {a} {op} {b}: {out.stderr.decode("utf-8")}')

r = out.stdout.decode('utf-8').strip() == op
self._save_to_cache(key, r)
return r


alpine_comparer = AlpineVersionComparer("/tmp/alpine-versions-generator-cache.csv", "exec")
alpine_comparer = AlpineVersionComparer('/tmp/alpine-versions-generator-cache.csv', 'exec')


class AlpineVersion:
Expand Down Expand Up @@ -188,39 +188,39 @@ def compare(v1, relate, v2):
return ops[relate](v1, v2)


def compare_versions(lines, select="all"):
def compare_versions(lines, select='all'):
has_any_failed = False

for line in lines:
line = line.strip()

if line == "" or line.startswith('#') or line.startswith('//'):
if line == '' or line.startswith('#') or line.startswith('//'):
maybe_unsupported = uncomment(line).strip()

if is_unsupported_comparison(maybe_unsupported):
print(f"\033[96mS\033[0m: \033[93m{maybe_unsupported}\033[0m")
print(f'\033[96mS\033[0m: \033[93m{maybe_unsupported}\033[0m')
continue

v1, op, v2 = line.strip().split(" ")
v1, op, v2 = line.strip().split(' ')

r = compare(AlpineVersion(v1), op, AlpineVersion(v2))

if not r:
has_any_failed = r

if select == "failures" and r:
if select == 'failures' and r:
continue

if select == "successes" and not r:
if select == 'successes' and not r:
continue

color = '\033[92m' if r else '\033[91m'
rs = "T" if r else "F"
print(f"{color}{rs}\033[0m: \033[93m{line}\033[0m")
rs = 'T' if r else 'F'
print(f'{color}{rs}\033[0m: \033[93m{line}\033[0m')
return has_any_failed


def compare_versions_in_file(filepath, select="all"):
def compare_versions_in_file(filepath, select='all'):
with open(filepath) as f:
lines = f.readlines()
return compare_versions(lines, select)
Expand All @@ -232,10 +232,10 @@ def generate_version_compares(versions):
if i == 0:
continue

comparison = f"{versions[i - 1]} < {version}\n"
comparison = f'{versions[i - 1]} < {version}\n'

if is_unsupported_comparison(comparison.strip()):
comparison = "# " + comparison
comparison = '# ' + comparison
comparisons.append(comparison)
return comparisons

Expand All @@ -262,16 +262,16 @@ def fetch_packages_versions():
return extract_packages_with_versions(osvs)


outfile = "internal/semantic/fixtures/alpine-versions-generated.txt"
outfile = 'internal/semantic/fixtures/alpine-versions-generated.txt'

packs = fetch_packages_versions()
with open(outfile, "w") as f:
with open(outfile, 'w') as f:
f.writelines(generate_package_compares(packs))
f.write("\n")
f.write('\n')

# set this to either "failures" or "successes" to only have those comparison results
# printed; setting it to anything else will have all comparison results printed
show = os.environ.get("VERSION_GENERATOR_PRINT", "failures")
show = os.environ.get('VERSION_GENERATOR_PRINT', 'failures')

did_any_fail = compare_versions_in_file(outfile, show)

Expand Down
54 changes: 27 additions & 27 deletions scripts/generators/generate-debian-versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ def is_unsupported_comparison(line):


def uncomment(line):
if line.startswith("#"):
if line.startswith('#'):
return line[1:]
if line.startswith("//"):
if line.startswith('//'):
return line[2:]
return line


def download_debian_db():
urllib.request.urlretrieve("https://osv-vulnerabilities.storage.googleapis.com/Debian/all.zip", "debian-db.zip")
urllib.request.urlretrieve('https://osv-vulnerabilities.storage.googleapis.com/Debian/all.zip', 'debian-db.zip')


def extract_packages_with_versions(osvs):
Expand Down Expand Up @@ -76,17 +76,17 @@ def __init__(self, cache_path):
def _load_cache(self):
if self.cache_path:
self.cache_path.touch()
with open(self.cache_path, "r") as f:
with open(self.cache_path, 'r') as f:
lines = f.readlines()

for line in lines:
line = line.strip()
key, result = line.split(",")
key, result = line.split(',')

if result == "True":
if result == 'True':
self.cache[key] = True
continue
if result == "False":
if result == 'False':
self.cache[key] = False
continue

Expand All @@ -96,15 +96,15 @@ def _save_to_cache(self, key, result):
self.cache[key] = result
if self.cache_path:
self.cache_path.touch()
with open(self.cache_path, "a") as f:
f.write(f"{key},{result}\n")
with open(self.cache_path, 'a') as f:
f.write(f'{key},{result}\n')

def compare(self, a, op, b):
key = f"{a} {op} {b}"
key = f'{a} {op} {b}'
if key in self.cache:
return self.cache[key]

cmd = ["dpkg", "--compare-versions", a, op, b]
cmd = ['dpkg', '--compare-versions', a, op, b]
out = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

if out.stdout:
Expand All @@ -117,7 +117,7 @@ def compare(self, a, op, b):
return r


debian_comparer = DebianVersionComparer("/tmp/debian-versions-generator-cache.csv")
debian_comparer = DebianVersionComparer('/tmp/debian-versions-generator-cache.csv')


class DebianVersion:
Expand Down Expand Up @@ -145,39 +145,39 @@ def compare(v1, relate, v2):
return ops[relate](v1, v2)


def compare_versions(lines, select="all"):
def compare_versions(lines, select='all'):
has_any_failed = False

for line in lines:
line = line.strip()

if line == "" or line.startswith('#') or line.startswith('//'):
if line == '' or line.startswith('#') or line.startswith('//'):
maybe_unsupported = uncomment(line).strip()

if is_unsupported_comparison(maybe_unsupported):
print(f"\033[96mS\033[0m: \033[93m{maybe_unsupported}\033[0m")
print(f'\033[96mS\033[0m: \033[93m{maybe_unsupported}\033[0m')
continue

v1, op, v2 = line.strip().split(" ")
v1, op, v2 = line.strip().split(' ')

r = compare(DebianVersion(v1), op, DebianVersion(v2))

if not r:
has_any_failed = r

if select == "failures" and r:
if select == 'failures' and r:
continue

if select == "successes" and not r:
if select == 'successes' and not r:
continue

color = '\033[92m' if r else '\033[91m'
rs = "T" if r else "F"
print(f"{color}{rs}\033[0m: \033[93m{line}\033[0m")
rs = 'T' if r else 'F'
print(f'{color}{rs}\033[0m: \033[93m{line}\033[0m')
return has_any_failed


def compare_versions_in_file(filepath, select="all"):
def compare_versions_in_file(filepath, select='all'):
with open(filepath) as f:
lines = f.readlines()
return compare_versions(lines, select)
Expand All @@ -189,10 +189,10 @@ def generate_version_compares(versions):
if i == 0:
continue

comparison = f"{versions[i - 1]} < {version}\n"
comparison = f'{versions[i - 1]} < {version}\n'

if is_unsupported_comparison(comparison.strip()):
comparison = "# " + comparison
comparison = '# ' + comparison
comparisons.append(comparison)
return comparisons

Expand All @@ -219,16 +219,16 @@ def fetch_packages_versions():
return extract_packages_with_versions(osvs)


outfile = "internal/semantic/fixtures/debian-versions-generated.txt"
outfile = 'internal/semantic/fixtures/debian-versions-generated.txt'

packs = fetch_packages_versions()
with open(outfile, "w") as f:
with open(outfile, 'w') as f:
f.writelines(generate_package_compares(packs))
f.write("\n")
f.write('\n')

# set this to either "failures" or "successes" to only have those comparison results
# printed; setting it to anything else will have all comparison results printed
show = os.environ.get("VERSION_GENERATOR_PRINT", "failures")
show = os.environ.get('VERSION_GENERATOR_PRINT', 'failures')

did_any_fail = compare_versions_in_file(outfile, show)

Expand Down
Loading

0 comments on commit aebdc23

Please sign in to comment.