Created
November 24, 2024 10:08
-
-
Save rumpelsepp/877a8dbc565454bc11186c53552be96c to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python3 | |
import json | |
import statistics | |
import sys | |
def parse_list_from_stdin() -> list[str]: | |
raw_packages = [] | |
raw_package = "" | |
for line in sys.stdin: | |
if line == "\n": | |
raw_packages.append(raw_package) | |
raw_package = "" | |
continue | |
raw_package += line | |
return raw_packages | |
def match_and_get(prefix: str, data: str) -> str: | |
if data.startswith(prefix): | |
return data.removeprefix(prefix) | |
return "" | |
def parse_raw_package(data: str) -> dict[str, str]: | |
out = {} | |
out["python-package"] = False | |
for line in data.splitlines(): | |
line = line.strip() | |
if (d := match_and_get("Package: ", line)) != "": | |
out["name"] = d | |
if (d := match_and_get("Depends: ", line)) != "": | |
if "python3:any" in d: | |
out["python-package"] = True | |
deps = d.split(",") | |
deps = list(map(lambda x: x.strip(), deps)) | |
out["depends"] = deps | |
return out | |
def main() -> None: | |
packages = [] | |
for raw_package in parse_list_from_stdin(): | |
package = parse_raw_package(raw_package) | |
if not package["name"].startswith("python3-") or not package["python-package"]: | |
continue | |
package["python-deps"] = 0 | |
if "depends" in package: | |
for dep in package["depends"]: | |
if dep.startswith("python3-"): | |
package["python-deps"] += 1 | |
packages.append(package) | |
print(packages) | |
print(f"number of packages: {len(packages)}") | |
dep_numbers = map(lambda x: x["python-deps"], packages) | |
median = statistics.mean(dep_numbers) | |
print(f"median of python dependencies: {median}") | |
if __name__ == "__main__": | |
try: | |
main() | |
except KeyboardInterrupt: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use this script like this: