Skip to content
Snippets Groups Projects
Commit 4f406746 authored by Ben Morrice's avatar Ben Morrice
Browse files

Add some pylint suggestions

parent d394d988
No related branches found
No related tags found
1 merge request!10Add almalinux support
Pipeline #6580564 passed
#!/usr/bin/python3 #!/usr/bin/python3
""" script to alert us via mm on new 'interesting' packages """
import os import os
import glob import glob
...@@ -9,7 +10,6 @@ import signal ...@@ -9,7 +10,6 @@ import signal
import subprocess import subprocess
import sys import sys
import tempfile import tempfile
import time
from collections import ChainMap from collections import ChainMap
from datetime import datetime from datetime import datetime
import dnf import dnf
...@@ -32,7 +32,7 @@ if ( ...@@ -32,7 +32,7 @@ if (
) )
sys.exit(1) sys.exit(1)
with open(f"/root/{interesting_packages_file}") as f: with open(f"/root/{interesting_packages_file}", encoding='utf-8') as f:
interesting_packages = yaml.safe_load(f) interesting_packages = yaml.safe_load(f)
try: try:
interesting_packages = interesting_packages["packages"] interesting_packages = interesting_packages["packages"]
...@@ -44,7 +44,7 @@ with open(f"/root/{interesting_packages_file}") as f: ...@@ -44,7 +44,7 @@ with open(f"/root/{interesting_packages_file}") as f:
# In case somebody screws up the input format, make sure it's a dict and not a list of dicts # In case somebody screws up the input format, make sure it's a dict and not a list of dicts
for package in interesting_packages: for package in interesting_packages:
if type(interesting_packages[package]) is list: if isinstance(interesting_packages[package], list):
interesting_packages[package] = dict(ChainMap(*interesting_packages[package])) interesting_packages[package] = dict(ChainMap(*interesting_packages[package]))
feeds = unformatted_feeds.split(',') feeds = unformatted_feeds.split(',')
...@@ -52,7 +52,7 @@ feeds = unformatted_feeds.split(',') ...@@ -52,7 +52,7 @@ feeds = unformatted_feeds.split(',')
logfile_retain_days = 30 logfile_retain_days = 30
logfilename = f"/work/{datetime.today().strftime('%Y%m%d-%H%M')}.log" logfilename = f"/work/{datetime.today().strftime('%Y%m%d-%H%M')}.log"
# pylint: disable=consider-using-with # pylint: disable=consider-using-with
logfile = open(logfilename, "w") logfile = open(logfilename, "w", encoding='utf-8')
def generate_temp_file(prefix): def generate_temp_file(prefix):
"""Generate a temp file""" """Generate a temp file"""
...@@ -100,12 +100,11 @@ def get_changelog(token, payload): ...@@ -100,12 +100,11 @@ def get_changelog(token, payload):
attempts += 1 attempts += 1
result = request_url(token, url, stream=True) result = request_url(token, url, stream=True)
if result.status_code == 200: if result.status_code == 200:
# TODO: try and do this with dnf 'add_remote_rpms', although we would need to auth
with open(temp_file, "wb") as f: with open(temp_file, "wb") as f:
try: try:
for chunk in result.iter_content(chunk_size=1024): for chunk in result.iter_content(chunk_size=1024):
if chunk: if chunk:
f.write(chunk) f.write(chunk)
rpm_downloaded = True rpm_downloaded = True
except: except:
print(f"Saving changelog appeared to have failed. Retrying {attempts}/{max_attempts}", flush=True) print(f"Saving changelog appeared to have failed. Retrying {attempts}/{max_attempts}", flush=True)
...@@ -115,7 +114,7 @@ def get_changelog(token, payload): ...@@ -115,7 +114,7 @@ def get_changelog(token, payload):
else: else:
print(f"API call failed, Retrying {attempts}/{max_attempts}", flush=True) print(f"API call failed, Retrying {attempts}/{max_attempts}", flush=True)
if not rpm_downloaded: if not rpm_downloaded:
print(f"Changelog failed to download, exiting", flush=True) print("Changelog failed to download, exiting", flush=True)
sys.exit(1) sys.exit(1)
# TODO: maybe have some better logic instead of the head command # TODO: maybe have some better logic instead of the head command
changelog = do_execute(f"rpm -qp {temp_file} --changelog | head -20", return_output=True) changelog = do_execute(f"rpm -qp {temp_file} --changelog | head -20", return_output=True)
...@@ -160,7 +159,7 @@ def request_url(token, url, params=None, stream=False): ...@@ -160,7 +159,7 @@ def request_url(token, url, params=None, stream=False):
def get_upstream(token, feed): def get_upstream(token, feed):
"""retrieve full list of packages from a cset / package url""" """retrieve full list of packages from a cset / package url"""
packages = [] packages = []
matches = re.match(f"(\w+)-([0-9])-\w+-(\w+)-(\w+)", feed) matches = re.match(r"(\w+)-([0-9])-\w+-(\w+)-(\w+)", feed)
arch = matches.group(3) arch = matches.group(3)
# almalinux is simplier, so we process it earlier # almalinux is simplier, so we process it earlier
if 'almalinux' in feed: if 'almalinux' in feed:
...@@ -173,9 +172,9 @@ def get_upstream(token, feed): ...@@ -173,9 +172,9 @@ def get_upstream(token, feed):
# When querying 8 from a 9 host, libdnf spews warnings about modularity # When querying 8 from a 9 host, libdnf spews warnings about modularity
# to stderr. We don't care about that so let's silence it. # to stderr. We don't care about that so let's silence it.
original_stderr = sys.stderr original_stderr = sys.stderr
f = open('/dev/null', 'w') with open('/dev/null', encoding='utf-8') as f:
sys.stderr = f sys.stderr = f
base.fill_sack(load_system_repo=False) base.fill_sack(load_system_repo=False)
# We can care about stderr again # We can care about stderr again
sys.stderr = original_stderr sys.stderr = original_stderr
q = base.sack.query() q = base.sack.query()
...@@ -227,13 +226,13 @@ def get_local(feed): ...@@ -227,13 +226,13 @@ def get_local(feed):
"""read previously saved packages into a list""" """read previously saved packages into a list"""
try: try:
with open(f"/work/{feed}.txt", "rb") as f: with open(f"/work/{feed}.txt", "rb") as f:
local_packages = [] lp = []
for line in f: for line in f:
x = line[:-1] x = line[:-1]
local_packages.append(x.decode()) lp.append(x.decode())
except FileNotFoundError: except FileNotFoundError:
local_packages = [] lp = []
return local_packages return lp
def set_local(packages, feed): def set_local(packages, feed):
...@@ -300,13 +299,12 @@ def format_release(p, f, token): ...@@ -300,13 +299,12 @@ def format_release(p, f, token):
) )
print(f" -- Payload sent: {payload}", flush=True) print(f" -- Payload sent: {payload}", flush=True)
def timeout_handler(num, stack): def timeout_handler():
""" function to handle timeouts for long running function """ """ function to handle timeouts for long running function """
print("Received SIGALRM") print("Received SIGALRM")
raise TimeoutError raise TimeoutError
if __name__ == "__main__": if __name__ == "__main__":
"""this is the main class, it's amazing."""
for feed in feeds: for feed in feeds:
if 'rhel' in feed: if 'rhel' in feed:
token = get_token(offline_token) token = get_token(offline_token)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment