Commit e13f65da authored by shadMod's avatar shadMod 💬

replaced old 'self.config[releases]' with new 'self.config[distros][distro][archs][arch]'

to get determinate dict with all the required keys due to the latest changes in downloads.toml

replaced md5 with sha256 and http with https

Fixes: #16
parent 73dfc935
# Source code of the Ubuntu-it website
# Copyright (C) 2016 Pietro Albini <pietroalbini@ubuntu.com>
# Copyright (C) 2023 shadMod
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
......@@ -75,6 +76,8 @@ class Downloads:
found_mirrors = list(sorted(launchpad.get_cdimage_mirrors(
distro, self.config["mirrors"]["country"]
)))
if "it1.mirror.vhosting-it.com" in found_mirrors[0]:
found_mirrors = ["https://releases.ubuntu.com/"]
if found_mirrors:
self._mirrors[distro] = found_mirrors
if not self._mirrors:
......@@ -85,14 +88,14 @@ class Downloads:
return self._mirrors
@property
def md5sums(self):
"""Get a list of all the MD5SUMS"""
if not hasattr(self, "_md5sums"):
def sha256sums(self):
"""Get a list of all the SHA256SUMS"""
if getattr(self, "_sha256sums") is None:
if self._cache is not None:
self._md5sums = self._cache["md5sums"]
self._sha256sums = self._cache["sha256sums"]
else:
self._md5sums = self._fetch_md5sums()
return self._md5sums
self._sha256sums = self._fetch_sha256sums()
return self._sha256sums
def _strip_non_lts_releases(self):
"""Process the lts-only distro configuration"""
......@@ -104,14 +107,14 @@ class Downloads:
if not self.config["releases"][release]["lts"]:
config["releases"].remove(release)
def _fetch_md5sums(self):
"""Fetch all the needed MD5SUMS"""
def _fetch_sha256sums(self):
"""Fetch all the needed SHA256SUMS"""
result = {}
files_content = {}
for distro, config in self.config["distros"].items():
for release in config["releases"]:
if config["lts-only"] and not self.config["releases"][release]["lts"]:
if config["lts-only"]:
continue
for arch in config["archs"].keys():
......@@ -120,7 +123,7 @@ class Downloads:
# Fetch the file from the remote if it wasn't done already
if path not in files_content:
md5s = {}
sha_hash = {}
response = requests.get("%s/SHA256SUMS" % path)
if response.status_code == 404:
......@@ -132,19 +135,19 @@ class Downloads:
if line.strip() == "":
continue
hash, name = line.split(" ", 1)
_hash, name = line.split(" ", 1)
if name.startswith("*"):
name = name[1:]
md5s[name] = hash
files_content[path] = md5s
sha_hash[name] = _hash
files_content[path] = sha_hash
# Add the MD5 to the result if it was found
# Add the SHA256 to the result if it was found
if file in files_content[path]:
key = "%s:%s:%s" % (distro, release, arch)
result[key] = files_content[path][file]
else:
print(files_content[path])
raise RuntimeError("Missing MD5 for {}".format(file))
raise RuntimeError("Missing SHA256SUMS for {}".format(file))
return result
......@@ -178,7 +181,7 @@ class Downloads:
"version": CACHE_FILE_VERSION,
"content": {
"mirrors": self.mirrors,
"md5sums": self.md5sums,
"sha256sums": self.sha256sums,
},
}, f, separators=(",", ":"))
......@@ -188,12 +191,13 @@ class Downloads:
def url_for(self, distro, release, arch, torrent=False, use_random=True):
"""Get the URL of a download file"""
# Build the dict of keys to replace
data_rel = self.config["distros"][distro]["archs"][arch][release]
replaces = {
"distro": distro,
"release": release,
"arch": arch,
"version": self.config["releases"][release]["version"],
"codename": self.config["releases"][release]["codename"],
"version": data_rel["version"],
"codename": data_rel["codename"],
}
# Add mirrors to the replaces key
......@@ -204,14 +208,14 @@ class Downloads:
replaces["mirror:%s" % name] = choices[0]
# Get the right source
source_name = self.config["distros"][distro]["archs"][arch]
source = self.config["sources"][source_name]
host = data_rel["host"]
source = self.config["sources"][host]
# Download URLs are different for torrent and HTTP
if torrent:
url = source["torrent"]
else:
url = source["http"]
url = source["https"]
for key, value in replaces.items():
url = url.replace("{%s}" % key, value)
......@@ -238,11 +242,12 @@ class Downloads:
if distro not in self.config["distros"]:
flask.abort(404)
data = self.config["distros"][distro]
return flask.render_template(
"download/landing.html",
distro_name=distro,
distro=self.config["distros"][distro],
releases=self.config["releases"],
distro=data,
releases=data["archs"]["amd64"],
archs=self.config["archs"],
)
......@@ -266,16 +271,19 @@ class Downloads:
torrent = "torrent" in flask.request.form
payload = signer.dumps({
data_rel = self.config["distros"][distro]["archs"][arch][release]
payload = signer.dumps(
{
"url": self.url_for(distro, release, arch, torrent),
"md5": self.md5sums["%s:%s:%s" % (distro, release, arch)],
"sha256": self.sha256sums[f"{distro}:{release}:{arch}"],
"name": "%s %s%s" % (
self.config["distros"][distro]["name"],
self.config["releases"][release]["version"],
" LTS" if self.config["releases"][release]["lts"] else "",
data_rel["version"],
" LTS" if data_rel["lts"] else "",
),
"theme": distro,
})
}
)
return flask.redirect(flask.url_for(".thanks", payload=payload))
@bp.route("/grazie/<payload>")
......@@ -293,10 +301,11 @@ class Downloads:
"""Generate the sub-navbar for /download"""
result = []
for name, distro in self.config["distros"].items():
result.append({
result.append(
{
"name": distro["name"],
"endpoint": "download.landing",
"endpoint-args": {"distro": name},
})
}
)
return result
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment