Mercurial > hg > config
comparison python/readenv.py @ 634:4ae9b16a1483
read env variables
| author | Jeff Hammel <k0scist@gmail.com> |
|---|---|
| date | Wed, 26 Feb 2014 13:44:02 -0800 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 633:d37391e845f5 | 634:4ae9b16a1483 |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 # -*- coding: utf-8 -*- | |
| 3 | |
| 4 """ | |
| 5 parse environment variables | |
| 6 """ | |
| 7 | |
| 8 import argparse | |
| 9 import os | |
| 10 import subprocess | |
| 11 import sys | |
| 12 | |
| 13 def parse_env(output): | |
| 14 return dict([line.split('=',1) for line in output.splitlines()]) | |
| 15 | |
| 16 def read_env(script): | |
| 17 """read environment following source script""" | |
| 18 command = 'bash -c "(. {} &> /dev/null) && /usr/bin/env"'.format(script) | |
| 19 command = "(. {} &> /dev/null) && /usr/bin/env".format(script) | |
| 20 output = subprocess.check_output(command, shell=True, env={}) | |
| 21 return parse_env(output) | |
| 22 | |
| 23 def diff_env(script): | |
| 24 before = parse_env(subprocess.check_output('/usr/bin/env', shell=True)) | |
| 25 after = read_env(script) | |
| 26 | |
| 27 | |
| 28 if __name__ == '__main__': | |
| 29 """test""" | |
| 30 | |
| 31 import tempfile | |
| 32 from pprint import pprint | |
| 33 | |
| 34 example = """export FOO=bar | |
| 35 export FLEEM=foobar""" | |
| 36 | |
| 37 | |
| 38 fd, name = tempfile.mkstemp() | |
| 39 os.write(fd, example) | |
| 40 os.close(fd) | |
| 41 print (open(name).read()) | |
| 42 | |
| 43 pprint(name) | |
| 44 pprint(read_env(name)) |
