Skip to content

Instantly share code, notes, and snippets.

@djpadz
Last active January 4, 2023 23:05
Show Gist options
  • Save djpadz/044ab74ba954aea387eaf9b3383fd1c2 to your computer and use it in GitHub Desktop.
Save djpadz/044ab74ba954aea387eaf9b3383fd1c2 to your computer and use it in GitHub Desktop.
Homebrew: make a bottle dependent-only
#!/usr/bin/env python3
import json
import sys
import os
kvp = (("installed_as_dependency", True), ("installed_on_request", False))
for i in sys.argv[1:]:
filename = f"/usr/local/opt/{i}/INSTALL_RECEIPT.json"
with open(filename, "r", encoding="utf-8") as f:
j = json.load(f)
if "installed_as_dependency" not in j or "installed_on_request" not in j:
raise ValueError(f"{filename} doesn't look like an INSTALL_RECEIPT")
print(f"{i}:")
old = {}
rewrite = False
for key, val in kvp:
print(f" {key}: {j[key]}{f' --> {val}' if j[key] != val else ''}")
rewrite |= j[key] != val
j[key] = val
if rewrite:
os.rename(filename, f"{filename}.bak")
with open(filename, "w", encoding="utf-8") as f:
f.write(json.dumps(j, indent=2))
os.chmod(filename, 0o0644)
else:
print(" (no change)")
@djpadz
Copy link
Author

djpadz commented Jan 4, 2023

This is a script I wrote after realizing that running brew install <everything that was on the other machine> breaks housekeeping features like brew autoremove. Use this to make a formula or cask dependency-only, setting that flag, and clearing the flag that says that you explicitly installed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment