Last active
July 13, 2022 21:54
-
-
Save Jackenmen/8d90f81737e8c7eb834e6a392f028abc to your computer and use it in GitHub Desktop.
Little script for fetching metadata from ALL Ubuntu repositories and search for available Python versions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gzip | |
import os | |
import sys | |
from io import BytesIO | |
from pathlib import Path | |
import requests | |
from debian import deb822 | |
cwd = Path("processed") | |
cwd.mkdir(exist_ok=True) | |
os.chdir(cwd) | |
ARCHES = ( | |
"amd64", | |
# "i386", | |
) | |
CHANNELS = ( | |
"main", | |
"universe", | |
# "restricted", | |
# "multiverse", | |
) | |
POCKETS = ( | |
"release", | |
"security", | |
"updates", | |
# "backports", | |
# "proposed", | |
) | |
def get_base_repository_url(dist_name: str, *, revert: bool = False) -> str: | |
supported_url = "http://archive.ubuntu.com/ubuntu/dists" | |
data = ubuntu_releases[dist_name] | |
is_supported = data["Supported"] == "1" | |
if revert: | |
is_supported = not is_supported | |
if is_supported: | |
return f"{supported_url}/{dist_name}" | |
return f"http://old-releases.ubuntu.com/ubuntu/dists/{dist_name}" | |
def get_packages_url(base_url: str, pocket: str, channel: str, arch: str) -> str: | |
pocket_url = f"{base_url}{f'-{pocket}' if pocket != 'release' else ''}" | |
return f"{pocket_url}/{channel}/binary-{arch}/Packages.gz" | |
def fetch_repos(): | |
for dist_name in ubuntu_releases: | |
revert = False | |
base_url = get_base_repository_url(dist_name, revert=revert) | |
for pocket in POCKETS: | |
pocket_url = f"{base_url}{f'-{pocket}' if pocket != 'release' else ''}" | |
for channel in CHANNELS: | |
for arch in ARCHES: | |
current = f"{dist_name}_{pocket}_{channel}_binary-{arch}" | |
url = get_packages_url(base_url, pocket, channel, arch) | |
print(f"Procesing {current}... {url}") | |
resp = requests.get(url) | |
if resp.status_code != 200 and not revert: | |
revert = True | |
base_url = get_base_repository_url(dist_name, revert=revert) | |
url = get_packages_url(base_url, pocket, channel, arch) | |
resp = requests.get(url) | |
if resp.status_code != 200: | |
print(" ! Received non-200 response") | |
continue | |
with gzip.open(BytesIO(requests.get(url).content)) as gz_fp: | |
with open(f"{current}.txt", "wb") as txt_fp: | |
txt_fp.write(gz_fp.read()) | |
def find_pythons(): | |
results = {} | |
for file in Path.cwd().iterdir(): | |
print(f"Procesing {file.name}...") | |
dist_name, _ = file.stem.split("_", maxsplit=1) | |
with file.open(encoding="utf-8", errors="replace") as fp: | |
for data in deb822.Packages.iter_paragraphs(fp): | |
found_versions = results.setdefault(dist_name, set()) | |
found_versions.update(x for x in range(12) if data["Package"] == f"python3.{x}") | |
parts = [] | |
for dist_name, data in ubuntu_releases.items(): | |
found_versions = ", ".join(f"3.{x}" for x in sorted(results.get(dist_name, []))) or "None" | |
parts.append(f"Ubuntu {data['Version']} ({data['Name']}): {found_versions}") | |
print("\n".join(parts)) | |
COMMANDS = { | |
"fetch-repos": fetch_repos, | |
"find-pythons": find_pythons, | |
} | |
try: | |
command = COMMANDS[sys.argv[1]] | |
except IndexError: | |
print("You need to choose a command to run (valid commands: fetch-repos, find-pythons)") | |
sys.exit(1) | |
except KeyError: | |
print(f"{sys.argv[1]!r} is not a valid command! (valid commands: fetch-repos, find-pythons)") | |
sys.exit(1) | |
ubuntu_releases = {} | |
for release_info in requests.get("https://changelogs.ubuntu.com/meta-release").text.strip().split("\n\n"): | |
data = dict(line.split(": ", maxsplit=1) for line in release_info.split("\n")) | |
ubuntu_releases[data["Dist"]] = data | |
command() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python-debian==0.1.43 | |
requests==2.27.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: