-
-
Save joshbode/e3d3d47563bc7b9b4ac7 to your computer and use it in GitHub Desktop.
Get access to keg-only commands from homebrew
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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