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
4 # found in the LICENSE file.
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.
18 parser
= optparse
.OptionParser()
19 parser
.add_option('-f', '--output-json',
20 help='File to dump the environment as JSON into.')
22 '-d', '--dump-mode', action
='store_true',
23 help='Dump the environment to sys.stdout and exit immediately.')
25 options
, args
= parser
.parse_args()
27 if args
or options
.output_json
:
28 parser
.error('Cannot specify args or --output-json with --dump-mode.')
29 json
.dump(dict(os
.environ
), sys
.stdout
)
31 if not options
.output_json
:
32 parser
.error('Requires --output-json option.')
34 envsetup_cmd
= ' '.join(map(pipes
.quote
, args
))
37 '. %s > /dev/null; %s -d' % (envsetup_cmd
, os
.path
.abspath(__file__
))
40 output
= subprocess
.check_output(full_cmd
)
41 except Exception as e
:
42 sys
.exit('Error running %s and dumping environment.' % envsetup_cmd
)
45 new_env
= json
.loads(output
)
46 for k
, val
in new_env
.items():
47 if k
== '_' or (k
in os
.environ
and os
.environ
[k
] == val
):
50 with
open(options
.output_json
, 'w') as f
:
51 json
.dump(env_diff
, f
)
54 if __name__
== '__main__':