OLD | NEW |
---|---|
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 # This script can either source a file and dump the enironment changes done by | 6 # This script can either source a file and dump the enironment changes done by |
7 # it, or just simply dump the current environment as JSON into a file. | 7 # it, or just simply dump the current environment as JSON into a file. |
8 | 8 |
9 import json | 9 import json |
10 import optparse | 10 import optparse |
11 import os | 11 import os |
12 import pipes | 12 import pipes |
13 import subprocess | 13 import subprocess |
14 import sys | 14 import sys |
15 | 15 |
16 | 16 |
17 def main(): | 17 def main(): |
18 parser = optparse.OptionParser() | 18 parser = optparse.OptionParser() |
19 parser.add_option('-f', '--output-json', | 19 parser.add_option('-f', '--output-json', |
20 help='File to dump the environment as JSON into.') | 20 help='File to dump the environment as JSON into.') |
21 parser.add_option( | 21 parser.add_option( |
22 '-d', '--dump-mode', action='store_true', | 22 '-d', '--dump-mode', action='store_true', |
23 help='Dump the environment to the file and exit immediately.') | 23 help='Dump the environment to sys.stdout and exit immediately.') |
iannucci
2013/09/18 00:55:37
just 'stdout' in help string :)
| |
24 | 24 |
25 options, args = parser.parse_args() | 25 options, args = parser.parse_args() |
26 if not options.output_json: | 26 if not options.output_json: |
27 parser.error('Requires --output-json option.') | 27 parser.error('Requires --output-json option.') |
iannucci
2013/09/18 00:55:37
we should move this error into the 'else' clause b
| |
28 | 28 |
29 if options.dump_mode: | 29 if options.dump_mode: |
iannucci
2013/09/18 00:55:37
we should make it an error to pass options.output_
| |
30 if args: | 30 if args: |
31 parser.error("Cannot specify args with --dump-mode") | 31 parser.error("Cannot specify args with --dump-mode") |
32 with open(options.output_json, 'w') as f: | 32 json.dump(dict(os.environ), sys.stdout) |
33 json.dump(dict(os.environ), f) | |
34 else: | 33 else: |
35 envsetup_cmd = ' '.join(map(pipes.quote, args)) | 34 envsetup_cmd = ' '.join(map(pipes.quote, args)) |
36 full_cmd = [ | 35 full_cmd = [ |
37 'bash', '-c', | 36 'bash', '-c', |
38 '. %s; %s -d -f %s' % (envsetup_cmd, os.path.abspath(__file__), | 37 '. %s; %s -d -f %s' % (envsetup_cmd, os.path.abspath(__file__), |
iannucci
2013/09/18 00:55:37
I don't think we need to pass -f any more, since i
| |
39 options.output_json) | 38 options.output_json) |
40 ] | 39 ] |
41 ret = subprocess.call(full_cmd) | 40 try: |
42 if ret: | 41 output = subprocess.check_output(full_cmd) |
43 sys.exit('Error running %s and dumping env' % envsetup_cmd) | 42 except Exception as e: |
43 sys.exit('Error running % and dumping environment.' % envsetup_cmd) | |
iannucci
2013/09/18 00:55:37
missing %s
| |
44 | |
45 env_diff = {} | |
46 new_env = json.loads(output) | |
47 for k, val in new_env.items(): | |
48 if k == '_' or (k in os.environ and os.environ[k] == val): | |
49 continue | |
50 env_diff[k] = val | |
51 with open(options.output_json, 'w') as f: | |
52 json.dump(env_diff, f) | |
44 | 53 |
45 | 54 |
46 if __name__ == '__main__': | 55 if __name__ == '__main__': |
47 sys.exit(main()) | 56 sys.exit(main()) |
OLD | NEW |