Initial commit
This commit is contained in:
commit
37874e5355
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
*.dot
|
||||||
|
*.svg
|
7
README.md
Normal file
7
README.md
Normal 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
32
graph.py
Executable 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('}')
|
Loading…
Reference in New Issue
Block a user