Skip to content

Commit

Permalink
Make corrections to address issues pointed out by Ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
gridhead committed Apr 4, 2024
1 parent 8e92bf1 commit 10aac95
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 182 deletions.
2 changes: 1 addition & 1 deletion mdapi/confdata/standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"""

# url to the database server:
DB_FOLDER = "/var/tmp"
DB_FOLDER = "/var/tmp" # noqa : S108

LOGGING = {
"version": 1,
Expand Down
8 changes: 0 additions & 8 deletions mdapi/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,3 @@
License and may only be used or replicated with the express permission
of Red Hat, Inc.
"""


def check_archive_validity():
"""
This is a workaround and not a fix.
See https://github.com/fedora-infra/mdapi/issues/161
"""
return True
23 changes: 10 additions & 13 deletions mdapi/database/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


def index_database(name, tempdtbs):
servlogr.logrobjc.info("[%s] Indexing database %s" % (name, tempdtbs))
servlogr.logrobjc.info(f"[{name}] Indexing database {tempdtbs}")
if tempdtbs.endswith("primary.sqlite"):
# TODO: Try the "with sqlite3.connect(tempdtbs) as connobjc" statement here
connobjc = sqlite3.connect(tempdtbs)
Expand Down Expand Up @@ -80,24 +80,21 @@ def change_row_to_package(self, rowe):
def obtain_all_rows(self, location, tableobj, cacheobj):
connobjc = sqlite3.connect(location)
sqlquery = queries.get(tableobj, DEFAULT_QUERY).format(table=tableobj)
for indx, rowe in enumerate(connobjc.execute(sqlquery)):
for indx, rowe in enumerate(connobjc.execute(sqlquery)): # noqa : B007
if tableobj in self.cache_dependent_tables:
if rowe[0] in cacheobj:
yield (cacheobj[rowe[0]], *rowe[1:])
else:
servlogr.logrobjc.debug(
"[%s] %s does not appear in the %s cache for %s"
% (self.name, rowe[0], tableobj, location)
)
servlogr.logrobjc.debug("[%s] Dropping from comparison" % self.name)
servlogr.logrobjc.debug(f"[{self.name}] {rowe[0]} does not appear in the {tableobj} cache for {location}") # noqa : E501
servlogr.logrobjc.debug(f"[{self.name}] Dropping from comparison")
else:
yield rowe
connobjc.close()

def build_cache(self, location, tableobj, cacheobjc):
connobjc = sqlite3.connect(location)
sqlquery = queries.get(tableobj, DEFAULT_QUERY).format(table=tableobj)
for pkgId, pkgname, *args in connobjc.execute(sqlquery):
for pkgId, pkgname, *args in connobjc.execute(sqlquery): # noqa : B007
cacheobjc[pkgId] = pkgname
connobjc.close()

Expand All @@ -108,7 +105,7 @@ def should_compare(self, tableobj):
return True

def main(self):
servlogr.logrobjc.info("[%s] Comparing %s against %s" % (self.name, self.dbsA, self.dbsB))
servlogr.logrobjc.info(f"[{self.name}] Comparing {self.dbsA} against {self.dbsB}")

tablistA = list(self.obtain_table_names(self.dbsA))
tablistB = list(self.obtain_table_names(self.dbsB))
Expand All @@ -121,12 +118,12 @@ def main(self):
# We have never downloaded this before...
# so we have nothing to compare it against. Just return and say there
# are "no differences".
servlogr.logrobjc.warning(
"[%s] Database empty - %s cannot compare" % (self.name, self.dbsB)
)
servlogr.logrobjc.warning(f"[{self.name}] Database empty - {self.dbsB} cannot compare")
return set()

assert len(tablistA) == len(tablistB), "Cannot compare disparate databases"
if len(tablistA) != len(tablistB):
raise ValueError("Cannot compare disparate databases")

# These should be the same
tblelist = tablistA = tablistB

Expand Down
102 changes: 45 additions & 57 deletions mdapi/database/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,65 +32,61 @@
import time
from xml.etree import ElementTree

import pyzstd
import requests
from fedora_messaging.api import publish
from fedora_messaging.exceptions import ConnectionException, PublishReturned
from mdapi_messages.messages import RepoUpdateV1

from mdapi.confdata import servlogr, standard
from mdapi.database import check_archive_validity
from mdapi.database.base import compare_databases, index_database


def list_branches(status="Active"):
urlx = "%s/api/collections?clt_status=%s" % (standard.PKGDB2_URL, status)
resp = requests.get(urlx, verify=standard.PKGDB2_VERIFY)
urlx = f"{standard.PKGDB2_URL}/api/collections?clt_status={status}"
resp = requests.get(urlx, verify=standard.PKGDB2_VERIFY) # noqa : S113
resp.raise_for_status()
data = resp.json()
servlogr.logrobjc.info("Branches metadata acquired.")
return data["collections"]


def fetch_database(name, repmdurl, location):
servlogr.logrobjc.info("[%s] Downloading file %s to %s" % (name, repmdurl, location))
resp = requests.get(repmdurl, verify=standard.DL_VERIFY)
servlogr.logrobjc.info(f"[{name}] Downloading file {repmdurl} to {location}")
resp = requests.get(repmdurl, verify=standard.DL_VERIFY) # noqa : S113
resp.raise_for_status()
with open(location, "wb") as filestrm:
filestrm.write(resp.content)


def extract_database(name, arcvname, location):
servlogr.logrobjc.info("[%s] Extracting %s to %s" % (name, arcvname, location))
servlogr.logrobjc.info(f"[{name}] Extracting {arcvname} to {location}")
if arcvname.endswith(".xz"):
with lzma.open(arcvname) as inp, open(location, "wb") as otptfile:
otptfile.write(inp.read())
elif arcvname.endswith(".tar.gz"):
with tarfile.open(arcvname) as tararchv:
tararchv.extractall(path=location, members=check_archive_validity())
tararchv.extractall(path=location, filter="data")
elif arcvname.endswith(".gz"):
with gzip.open(arcvname, "rb") as inp, open(location, "wb") as otptfile:
otptfile.write(inp.read())
elif arcvname.endswith(".bz2"):
with bz2.open(arcvname) as inp, open(location, "wb") as otptfile:
otptfile.write(inp.read())
elif arcvname.endswith(".zst"):
import pyzstd

with open(arcvname, "rb") as inp, open(location, "wb") as otptfile:
pyzstd.decompress_stream(inp, otptfile)
else:
servlogr.logrobjc.error("Could not extract %s" % (arcvname))
servlogr.logrobjc.error(f"Could not extract {arcvname}")
raise NotImplementedError(arcvname)


def publish_changes(name, packages, repmdurl):
servlogr.logrobjc.info("[%s] Publishing differences to Fedora Messaging bus" % (name))
servlogr.logrobjc.info(f"[{name}] Publishing differences to Fedora Messaging bus")

modified = bool(packages)
if not modified:
servlogr.logrobjc.warning(
"[%s] No real changes detected - Skipping publishing on Fedora Messaging bus" % (name)
)
servlogr.logrobjc.warning(f"[{name}] No real changes detected - Skipping publishing on Fedora Messaging bus") # noqa : E501
return

# Just publish the suffix of the URL. The prefix is dl.fedoraproject.org
Expand All @@ -100,7 +96,7 @@ def publish_changes(name, packages, repmdurl):
# talking about.

urlx = "/".join(repmdurl.split("/")[4:])
servlogr.logrobjc.info("[%s] URL %s" % (name, urlx))
servlogr.logrobjc.info(f"[{name}] URL {urlx}")

mesgobjc = RepoUpdateV1(
body=dict(
Expand All @@ -113,17 +109,13 @@ def publish_changes(name, packages, repmdurl):
try:
publish(mesgobjc)
except PublishReturned as excp:
servlogr.logrobjc.error(
"Fedora Messaging broker rejected message %s - %s" % (mesgobjc.id, excp)
)
servlogr.logrobjc.error(f"Fedora Messaging broker rejected message {mesgobjc.id} - {excp}")
except ConnectionException as excp:
servlogr.logrobjc.error(
"Error occurred while sending message %s - %s" % (mesgobjc.id, excp)
)
servlogr.logrobjc.error(f"Error occurred while sending message {mesgobjc.id} - {excp}")


def install_database(name, srce, dest):
servlogr.logrobjc.info("[%s] Installing %s to %s" % (name, srce, dest))
servlogr.logrobjc.info(f"[{name}] Installing {srce} to {dest}")
shutil.move(srce, dest)


Expand Down Expand Up @@ -155,10 +147,10 @@ def process_repo(repo):
Retrieve the repo metadata at the given URL and store them using the provided name.
"""
urlx, name = repo
repmdurl = "%s/repomd.xml" % urlx
response = requests.get(repmdurl, verify=standard.DL_VERIFY)
repmdurl = f"{urlx}/repomd.xml"
response = requests.get(repmdurl, verify=standard.DL_VERIFY) # noqa : S113
if not response:
servlogr.logrobjc.error("[%s] Failed to obtain %s - %s" % (name, repmdurl, response))
servlogr.logrobjc.error(f"[{name}] Failed to obtain {repmdurl} - {response}")
return

# Parse the XML document and get a list of locations and their SHAsum
Expand All @@ -167,7 +159,7 @@ def process_repo(repo):
node.find("repo:location", standard.repomd_xml_namespace),
node.find("repo:open-checksum", standard.repomd_xml_namespace),
)
for node in ElementTree.fromstring(response.text)
for node in ElementTree.fromstring(response.text) # noqa : S314
)

# Extract out the attributes that we are really interested in
Expand All @@ -189,25 +181,25 @@ def process_repo(repo):
cacA, cacB = {}, {}

if not filelist:
servlogr.logrobjc.warning("No SQLite database could be found in %s" % (urlx))
servlogr.logrobjc.warning(f"No SQLite database could be found in {urlx}")

for filename, hashdata, hashtype in filelist:
repmdurl = "%s/%s" % (urlx, filename)
repmdurl = f"{urlx}/{filename}"

# First, determine if the file has changed by comparing hash
database = None
if "primary.sqlite" in filename:
database = "mdapi-%s-primary.sqlite" % name
database = f"mdapi-{name}-primary.sqlite"
elif "filelists.sqlite" in filename:
database = "mdapi-%s-filelists.sqlite" % name
database = f"mdapi-{name}-filelists.sqlite"
elif "other.sqlite" in filename:
database = "mdapi-%s-other.sqlite" % name
database = f"mdapi-{name}-other.sqlite"

# Have we downloaded this before?
# Did it change?
destfile = os.path.join(standard.DB_FOLDER, database)
if not needs_update(destfile, hashdata, hashtype):
servlogr.logrobjc.info("[%s] No change detected from %s" % (name, repmdurl))
servlogr.logrobjc.info(f"[{name}] No change detected from {repmdurl}")
continue

# Creating temporary directories with formatted names to remove them later easily, if needed
Expand All @@ -224,9 +216,7 @@ def process_repo(repo):
packages = compare_databases(name, tempdtbs, destfile, cacA, cacB).main()
publish_changes(name, packages, repmdurl)
else:
servlogr.logrobjc.warning(
"[%s] Not publishing to Fedora Messaging bus - Not comparing databases" % (name)
)
servlogr.logrobjc.warning(f"[{name}] Not publishing to Fedora Messaging bus - Not comparing databases") # noqa : E501
install_database(name, tempdtbs, destfile)


Expand All @@ -241,32 +231,30 @@ def index_repositories():
versdata = rels["version"]
if versdata == "devel":
versdata = "rawhide"
urlx = "%s/pub/fedora/linux/development/%s/Everything/x86_64/os/repodata" % (
standard.DL_SERVER,
versdata,
)
servlogr.logrobjc.info(
"Acquired repo for %s/%s of '%s' branch at %s"
% (rels["koji_name"], versdata, rels["status"], urlx)
)
urlx = f"{standard.DL_SERVER}/pub/fedora/linux/development/{versdata}/Everything/x86_64/os/repodata" # noqa : E501
servlogr.logrobjc.info(f"Acquired repo for {rels['koji_name']}/{versdata} of '{rels['status']}' branch at {urlx}") # noqa : E501
repolist.append((urlx, rels["koji_name"]))
urlx = urlx.replace("/x86_64/os/", "/source/tree/")
repolist.append((urlx, "src_%s" % rels["koji_name"]))
repolist.append((urlx, f"src_{rels['koji_name']}"))

urls = {
"Fedora Linux": [
"%s/pub/fedora/linux/releases/%s/Everything/x86_64/os/repodata",
"%s/pub/fedora/linux/updates/%s/Everything/x86_64/repodata",
"%s/pub/fedora/linux/updates/testing/%s/Everything/x86_64/repodata",
"{dlserver}/pub/fedora/linux/releases/{versname}/Everything/x86_64/os/repodata",
"{dlserver}/pub/fedora/linux/updates/{versname}/Everything/x86_64/repodata",
"{dlserver}/pub/fedora/linux/updates/testing/{versname}/Everything/x86_64/repodata",
],
"Fedora EPEL": [
"%s/pub/epel/%s/x86_64/repodata/",
"%s/pub/epel/testing/%s/x86_64/repodata",
"{dlserver}/pub/epel/{versname}/x86_64/repodata/",
"{dlserver}/pub/epel/testing/{versname}/x86_64/repodata",
],
}

urls["Fedora"] = urls["Fedora Linux"]
repodict = {"fedora": ["%s", "%s-updates", "%s-updates-testing"], "epel": ["%s", "%s-testing"]}

repodict = {
"fedora": ["{rlid}", "{rlid}-updates", "{rlid}-updates-testing"],
"epel": ["{rlid}", "{rlid}-testing"]
}

# Obtain the stable repos
stable_releases = list_branches(status="Active")
Expand All @@ -276,22 +264,22 @@ def index_repositories():
versdata = rels["version"]
for jndx, urli in enumerate(urls[rels["name"]]):
if rels["name"] in ("Fedora Linux", "Fedora"):
name = repodict["fedora"][jndx] % rels["koji_name"]
name = repodict["fedora"][jndx].format(ridx = rels["koji_name"])
elif rels["name"] == "Fedora EPEL" and versdata == "8":
name = repodict["epel"][jndx] % rels["koji_name"]
name = repodict["epel"][jndx].format(rlid = rels["koji_name"])
urli = urli.replace("/x86_64/", "/Everything/x86_64/")
elif rels["name"] == "Fedora EPEL" and versdata == "9":
name = repodict["epel"][jndx] % rels["koji_name"]
name = repodict["epel"][jndx].format(rlid = rels["koji_name"])
urli = urli.replace("/x86_64/", "/Everything/x86_64/")
else:
name = repodict["epel"][jndx] % rels["koji_name"]
rurl = urli % (standard.DL_SERVER, versdata)
name = repodict["epel"][jndx].format(rlid = rels["koji_name"])
rurl = urli.format(dlserver = standard.DL_SERVER, versname = versdata)
repolist.append((rurl, name))
rurl = rurl.replace("/x86_64/os", "/source/tree")
repolist.append((rurl, "src_%s" % name))
repolist.append((rurl, f"src_{name}"))

# Finish with the koji repo
repolist.append(("%s/rawhide/latest/x86_64/repodata" % standard.KOJI_REPO, "koji"))
repolist.append((f"{standard.KOJI_REPO}/rawhide/latest/x86_64/repodata", "koji"))

# In serial
for repo in repolist:
Expand Down
6 changes: 3 additions & 3 deletions mdapi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def main(conffile=None):
if conffile is not None:
# Load the configuration file to use
CONFIG = {}
with open(conffile, "r") as confobjc:
exec(compile(confobjc.read(), conffile, "exec"), CONFIG)
with open(conffile) as confobjc:
exec(compile(confobjc.read(), conffile, "exec"), CONFIG) # noqa : S102
compile_configuration(CONFIG)


Expand All @@ -70,7 +70,7 @@ def serveapp():
APPSERVE["logging"]["level"],
)
)
subprocess.run(startcmd.split())
subprocess.run(startcmd.split()) # noqa : S603
except KeyError:
print("Invalid configuration detected")
return 1
Expand Down

0 comments on commit 10aac95

Please sign in to comment.