Skip to content

Instantly share code, notes, and snippets.

@fnx-ableton
Created November 2, 2017 16:17
Show Gist options
  • Save fnx-ableton/37c7ed15449d89a9f2686d096ad781ff to your computer and use it in GitHub Desktop.
Save fnx-ableton/37c7ed15449d89a9f2686d096ad781ff to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
import subprocess
import re
cmd = ' '.join(sys.argv[1:])
lines = subprocess.Popen(
"shopt -s expand_aliases && source ~/.bash_profile && alias",
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT).stdout.readlines()
alias = {}
for line in lines:
m = re.search('([^=]+)=\'(.*)\'', line)
if m:
alias[m.group(1)] = m.group(2)
def value_contains_key(key):
value = alias[key]
return key in value.split(' ')
def substitue_value(value):
return ' '.join([(alias[s] if s in alias else s) for s in value.split(' ')])
def substitute_all(force=False):
done = True
for key, value in alias.iteritems():
new_val = substitue_value(value)
if new_val != alias[key] and (force or not value_contains_key(key)):
alias[key] = new_val
done = False
return done
done = substitute_all(True)
while not done:
done = substitute_all()
print substitue_value(cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment