Skip to content

Instantly share code, notes, and snippets.

@stuartlangridge
Created July 26, 2021 19:47
Show Gist options
  • Save stuartlangridge/9a4ceaba1f7c08a4a225d9f8f69b2f3a to your computer and use it in GitHub Desktop.
Save stuartlangridge/9a4ceaba1f7c08a4a225d9f8f69b2f3a to your computer and use it in GitHub Desktop.
Parse FabioLolix/LinuxTimeline to get the chain of ancestry for Linux distros and find the longest
#!/usr/bin/env python3
import requests
try:
from requests_cache import CachedSession as MySession
except:
MySession = requests.Session
def unstring(s):
if s.startswith('"') and s.endswith('"'): return s[1:-1]
return s
def decorate(distros, name, depth=0, chain=[]):
distros[name]["depth"] = depth
distros[name]["chain"] = chain[:]
children = [x for x in distros if distros[x]["p"] == name]
for c in children:
cchain = chain[:]
cchain.append(name)
decorate(distros, c, depth + 1, cchain)
distros = {}
session = MySession()
resp = session.get("https://raw.githubusercontent.com/FabioLolix/LinuxTimeline/master/ldt.csv")
c = resp.content.decode("utf-8")
for line in c.split("\n"):
parts = [unstring(x) for x in line.split(",")]
distro = None
if parts[0] in ["N"]: #, "#N", "//N"]:
distro = parts[1].strip()
parent = parts[3].strip()
if distro:
distros[distro] = {"p": parent}
unparents = [x for x in distros if not distros[x]["p"]]
for u in sorted(unparents):
decorate(distros, u, 0, [])
bydepth = {}
for name, details in distros.items():
d = details["depth"]
if d not in bydepth:
bydepth[d] = []
bydepth[d].append(name)
for n in sorted(list(bydepth.keys()))[-3:]:
print("")
print("Chains of length", n + 1)
print("==================")
s = []
for d in bydepth[n]:
s.append(' -> '.join((distros[d]["chain"] + [d])))
print("\n".join(sorted(s)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment