Skip to content

Instantly share code, notes, and snippets.

@joshbode
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save joshbode/e3d3d47563bc7b9b4ac7 to your computer and use it in GitHub Desktop.

Select an option

Save joshbode/e3d3d47563bc7b9b4ac7 to your computer and use it in GitHub Desktop.
Get access to keg-only commands from homebrew
#!/usr/bin/env python
import sys
import os
import os.path
import json
import signal
import subprocess
# process arguments
if len(sys.argv) < 2:
sys.exit(1)
formula = sys.argv[1]
command = formula if (len(sys.argv) == 2 or sys.argv[2:] == '--') else sys.argv[2]
args = sys.argv[3:]
# get the info
try:
prefix = subprocess.check_output(['brew', '--prefix']).strip()
info = json.loads(
subprocess.check_output(['brew', 'info', '--json=v1', formula])
)
except subprocess.CalledProcessError:
sys.exit()
# get highest version if installed
try:
version = info['installed'][-1]['version']
except IndexError:
print "Error: Not installed: {0}".format(formula)
sys.exit(1)
path = os.path.join(prefix, 'Cellar', formula, version, 'bin', command)
if not (os.path.exists(path) and os.access(path, os.X_OK)):
print "Error: Not executable: {0}".format(path)
sys.exit(1)
# stop python from processing control-C - it's up to the subprocess
def dummy(signum, frame):
pass
signal.signal(signal.SIGINT, dummy)
# execute the command
sys.exit(subprocess.call([path] + args))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment