From e5fce4b0609fd7b14f0241b17cf22f427f566e3b Mon Sep 17 00:00:00 2001 From: Adrijaned Date: Sun, 2 Dec 2018 19:58:49 +0100 Subject: [PATCH] Added dependency visualizer --- moduleDependencyGraphDrawer/.gitignore | 2 + moduleDependencyGraphDrawer/githubScraper.py | 44 ++++++++++++++++++++ moduleDependencyGraphDrawer/main.py | 11 +++++ 3 files changed, 57 insertions(+) create mode 100644 moduleDependencyGraphDrawer/.gitignore create mode 100644 moduleDependencyGraphDrawer/githubScraper.py create mode 100644 moduleDependencyGraphDrawer/main.py diff --git a/moduleDependencyGraphDrawer/.gitignore b/moduleDependencyGraphDrawer/.gitignore new file mode 100644 index 0000000..dfd65d5 --- /dev/null +++ b/moduleDependencyGraphDrawer/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +deps.png diff --git a/moduleDependencyGraphDrawer/githubScraper.py b/moduleDependencyGraphDrawer/githubScraper.py new file mode 100644 index 0000000..9e0b038 --- /dev/null +++ b/moduleDependencyGraphDrawer/githubScraper.py @@ -0,0 +1,44 @@ +import requests +import json + +modules = [] +dependencies = [] # list of tuples: (dependantPackage, dependency) + + +def getUrls(): + nextPage = "https://api.github.com/orgs/Terasology/repos?per_page=100" + repos = [] + while nextPage is not None: + r = requests.get(nextPage) + header_link: str = r.headers.get("Link") + if header_link is None: + nextPage = None + else: + nextPageLink = [x.split(";")[0] for x in header_link.split(",") if "next" in x.split(";")[1]] + if len(nextPageLink) == 0: + nextPage = None + else: + nextPage = nextPageLink[0][1:-1] + repos += json.loads(r.content) + return [(x["html_url"]+"/"+x["default_branch"]+"/module.txt").replace("github", "raw.githubusercontent", 1) for x in repos] + + +def init(): + print("Fetching list of modules... ", end="") + urls = getUrls() + print("Done") + modules.clear() + dependencies.clear() + print("Fetching module files:") + modulesfetched = 0 + for url in urls: + r = requests.get(url) + modulesfetched += 1 + if r.status_code != 200: + print(f" Skipping non-module - [{modulesfetched}/{len(urls)}]") + continue + module = json.loads(r.content) + modules.append(module["id"]) + for dependency in module["dependencies"]: + dependencies.append((module["id"], dependency["id"])) + print(f" Done {module['id']} - [{modulesfetched}/{len(urls)}]") diff --git a/moduleDependencyGraphDrawer/main.py b/moduleDependencyGraphDrawer/main.py new file mode 100644 index 0000000..e28bf18 --- /dev/null +++ b/moduleDependencyGraphDrawer/main.py @@ -0,0 +1,11 @@ +import networkx as nx +import matplotlib.pyplot as plt +import githubScraper + +githubScraper.init() +G = nx.DiGraph() +G.add_nodes_from(githubScraper.modules) +G.add_edges_from(githubScraper.dependencies) +plt.figure(1, figsize=(100, 60)) +nx.draw_kamada_kawai(G, with_labels=True, node_size=60, font_size=8, font_color="b", font_weight="bold") +plt.savefig("deps.png", dpi=300)