leafsummer
3/17/2016 - 6:47 AM

add costom env to the sys env

add costom env to the sys env

def add_env(env_sh) :
	pystring = "import os ; import json ; print json.dumps(dict(os.environ))"
	lx = os.popen('source "%s" && python -c "%s"' % (env_sh , pystring)).read()
	for attr , value in ujson.loads(lx).items() :
		if attr in ['JAVA_HOME' , 'PATH'] :
			os.environ[attr] = value
		if not attr in os.environ :
			os.environ[attr] = value


def add_env2():
    base_dir = get_base_dir()
    env_path = os.path.join(base_dir, 'env.sh')
    env_name_list = []
    with open(env_path) as env_file:
        for ln in env_file.readlines():
            ln = str(ln).strip()
            if str(ln).startswith('export '):
                ename = ln.split('=')[0][len('export '):]
                env_name_list.append(ename)

    pys = "import os;import json;print json.dumps(dict(os.environ))"
    cmd = "source %s && /usr/bin/python -c '%s'" % (env_path, pys)
    env_dict = json.loads(os.popen(cmd).read())
    for attr, value in env_dict.items():
        if attr in env_name_list:
            os.environ[attr] = value
  
  def run_command() :
    cmd = 'which %s' % (sys.argv[1])
    prog = os.popen(cmd).readline().strip()
    args = [prog] + sys.argv[1:] + [os.environ]
    os.execle(*args)