Source

envcc / envcc.py

Full commit
#!/usr/bin/env python
import re, os, sys
import subprocess

DEF_PATH = []

def getpath(program):
    proc = subprocess.Popen(["which", program], shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    output,err = proc.communicate()
    if proc.returncode == 0:
        return os.path.dirname(output.strip())
    return None
def setpath(paths, prompt=None):
    if len(paths) == 0:
        return None
    path = os.pathsep.join(paths).strip()
    if path:
        os.environ['PATH'] = path
# bash related stuff
        if prompt:
            os.environ['PS1'] = "(%s) \h $ " % prompt
        os.execle("/bin/bash", "", os.environ)
    return None

def usage():
    print 'envcc <program> +|-<program>'
    print ''

if len(sys.argv) <= 1:
    usage()
    sys.exit(255)

if sys.argv[1] == '-c':
    setpath(DEF_PATH)
    sys.exit(0)

preg = re.compile('(\+|\-)?([a-z]+)')

paths = {}
for arg in sys.argv[1:]:
    m = preg.match(arg)
    try:
        if m:
            p = m.group(2)
            if m.group(1) == '-' and p in paths:
                del paths[p]
            elif m.group(1) == '+':
                paths[p] = getpath(p)
            else:
                paths = {p: getpath(p)}
    except OSError:
        print 'Error occured'

setpath(paths.values(), " ".join(sys.argv[1:]))