Merge env via python
This commit is contained in:
		
							parent
							
								
									40f37f648d
								
							
						
					
					
						commit
						a371b5bed4
					
				@ -9,6 +9,7 @@ import shutil
 | 
				
			|||||||
import multiprocessing
 | 
					import multiprocessing
 | 
				
			||||||
import platform
 | 
					import platform
 | 
				
			||||||
import re
 | 
					import re
 | 
				
			||||||
 | 
					import ast
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
print = functools.partial(print, flush=True)
 | 
					print = functools.partial(print, flush=True)
 | 
				
			||||||
@ -192,22 +193,17 @@ def ensure_got_path(path):
 | 
				
			|||||||
def apply_cmd_env(cmd):
 | 
					def apply_cmd_env(cmd):
 | 
				
			||||||
    """Run cmd and apply its modified environment"""
 | 
					    """Run cmd and apply its modified environment"""
 | 
				
			||||||
    print('>> Applying env after', cmd)
 | 
					    print('>> Applying env after', cmd)
 | 
				
			||||||
    env_cmd = 'env' if which('env') else 'set'
 | 
					    separator = 'env follows'
 | 
				
			||||||
    env = sub.run('{} && {}'.format(cmd, env_cmd), shell=True, universal_newlines=True,
 | 
					    script = 'import os,sys;sys.stdout.buffer.write(str(dict(os.environ)).encode(\\\"utf-8\\\"))'
 | 
				
			||||||
                  stdout=sub.PIPE)
 | 
					    env = sub.run('{} && echo "{}" && python -c "{}"'.format(cmd, separator, script),
 | 
				
			||||||
 | 
					                  shell=True, stdout=sub.PIPE, encoding='utf-8')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    lines = env.stdout.split('\n')
 | 
					    stringed = env.stdout[env.stdout.index(separator) + len(separator) + 1:]
 | 
				
			||||||
    for line in lines:
 | 
					    parsed = ast.literal_eval(stringed)
 | 
				
			||||||
        match = re.match(r"^([a-zA-Z0-9_-]+)=(.*)$", line)
 | 
					
 | 
				
			||||||
        if not match:
 | 
					    for key, value in parsed.items():
 | 
				
			||||||
            continue
 | 
					 | 
				
			||||||
        key, value = match.groups()
 | 
					 | 
				
			||||||
        if key in os.environ and os.environ[key] == value:
 | 
					        if key in os.environ and os.environ[key] == value:
 | 
				
			||||||
            continue
 | 
					            continue
 | 
				
			||||||
        if key.upper().find('PATH') != -1 and value.find('/') != -1:
 | 
					 | 
				
			||||||
            value = value.replace(':', ';')
 | 
					 | 
				
			||||||
            value = re.sub(r'/(\w)/', r'\1:\\', value)
 | 
					 | 
				
			||||||
            value = value.replace('/', '\\')
 | 
					 | 
				
			||||||
        if key in os.environ:
 | 
					        if key in os.environ:
 | 
				
			||||||
            print('>>> Changing env', key, '\nfrom\n',
 | 
					            print('>>> Changing env', key, '\nfrom\n',
 | 
				
			||||||
                  os.environ[key], '\nto\n', value)
 | 
					                  os.environ[key], '\nto\n', value)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user