Skip to content

Instantly share code, notes, and snippets.

@righthandabacus
Last active September 23, 2022 23:42
Show Gist options
  • Save righthandabacus/00db7cfe061873dc3f484b6f3bb555cd to your computer and use it in GitHub Desktop.
Save righthandabacus/00db7cfe061873dc3f484b6f3bb555cd to your computer and use it in GitHub Desktop.
Homebrew show reversed dependencies
"""
Reverse dependencies for homebrew
Homebrew gives the package dependencies with `brew deps --installed` which each line of output
is `package: dependencies`. It is easy to see which package depends on nothing but difficult to
tell if nothing depends on it. Output of this script reversed it. For example, if we want to tell
which package can be uninstalled without breaking anything else in homebrew (i.e., output of
`brew leaves`), we can get the list with
python brewrdeps.py | grep ': $'
"""
import subprocess
rdeps = {}
deps = subprocess.check_output(["brew", "deps", "--installed"], text=True)
for pkg, deps in [ln.split(":") for ln in deps.splitlines()]:
pkg = pkg.strip()
if pkg not in rdeps:
rdeps[pkg] = []
for dep_pkg in deps.strip().split():
if dep_pkg not in rdeps:
rdeps[dep_pkg] = []
rdeps[dep_pkg].append(pkg)
for pkg in sorted(rdeps.keys()):
print(f"{pkg}: {' '.join(rdeps[pkg])}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment