Initial commit

This commit is contained in:
germax26 2024-10-08 23:26:11 +11:00
commit 37874e5355
Signed by: germax26
SSH Key Fingerprint: SHA256:N3w+8798IMWBt7SYH8G1C0iJlIa2HIIcRCXwILT5FvM
3 changed files with 41 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.dot
*.svg

7
README.md Normal file
View File

@ -0,0 +1,7 @@
# Pip Graph
Generates a Graphviz graph for your pip packages.
## Quick start
```bash
$ ./graph.py | dot -Tsvg > out.svg
```

32
graph.py Executable file
View File

@ -0,0 +1,32 @@
#!/usr/bin/env python3
from subprocess import run
import sys
def exec(*command: str) -> str:
process = run(list(command), capture_output=True, text=True)
if process.returncode:
print(process.stderr, file=sys.stderr, end='')
exit(process.returncode)
return process.stdout
list_output = exec("pip3", "list")
packages = [package.split(' ')[0] for package in list_output.split('\n')[2:] if package]
def get_dependencies(package: str) -> list[str]:
show_output = exec("pip3", "show", package)
for line in show_output.split('\n'):
REQUIRES = 'Requires: '
if line.startswith(REQUIRES):
dependencies = line.removeprefix(REQUIRES)
if not dependencies: return []
return dependencies.split(', ')
raise RuntimeError(f"`pip3 show {package}` did not list the dependencies of {package}!")
print('digraph {')
for package in packages:
print(f'\t"{package}";')
for dependency in get_dependencies(package):
print(f'\t"{dependency}" -> "{package}";')
print('}')