2017-07-05 17:42:43 +03:00
|
|
|
#!/usr/bin/env python
|
2013-09-14 05:02:25 +04:00
|
|
|
# Copyright 2013 The Chromium Authors. All rights reserved.
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
|
|
|
# This script can either source a file and dump the enironment changes done by
|
|
|
|
# it, or just simply dump the current environment as JSON into a file.
|
|
|
|
|
|
|
|
import json
|
|
|
|
import optparse
|
|
|
|
import os
|
2013-09-18 01:59:02 +04:00
|
|
|
import pipes
|
2013-09-14 05:02:25 +04:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option('-f', '--output-json',
|
|
|
|
help='File to dump the environment as JSON into.')
|
|
|
|
parser.add_option(
|
|
|
|
'-d', '--dump-mode', action='store_true',
|
2013-09-18 22:01:30 +04:00
|
|
|
help='Dump the environment to sys.stdout and exit immediately.')
|
2013-09-14 05:02:25 +04:00
|
|
|
|
2013-09-24 03:08:09 +04:00
|
|
|
parser.disable_interspersed_args()
|
2013-09-14 05:02:25 +04:00
|
|
|
options, args = parser.parse_args()
|
|
|
|
if options.dump_mode:
|
2013-09-18 22:01:30 +04:00
|
|
|
if args or options.output_json:
|
|
|
|
parser.error('Cannot specify args or --output-json with --dump-mode.')
|
|
|
|
json.dump(dict(os.environ), sys.stdout)
|
2013-09-14 05:02:25 +04:00
|
|
|
else:
|
2013-09-18 22:01:30 +04:00
|
|
|
if not options.output_json:
|
|
|
|
parser.error('Requires --output-json option.')
|
|
|
|
|
2013-09-18 01:59:02 +04:00
|
|
|
envsetup_cmd = ' '.join(map(pipes.quote, args))
|
2013-09-14 05:02:25 +04:00
|
|
|
full_cmd = [
|
|
|
|
'bash', '-c',
|
2013-09-18 22:01:30 +04:00
|
|
|
'. %s > /dev/null; %s -d' % (envsetup_cmd, os.path.abspath(__file__))
|
2013-09-18 01:59:02 +04:00
|
|
|
]
|
2013-09-18 22:01:30 +04:00
|
|
|
try:
|
|
|
|
output = subprocess.check_output(full_cmd)
|
|
|
|
except Exception as e:
|
|
|
|
sys.exit('Error running %s and dumping environment.' % envsetup_cmd)
|
|
|
|
|
|
|
|
env_diff = {}
|
|
|
|
new_env = json.loads(output)
|
|
|
|
for k, val in new_env.items():
|
|
|
|
if k == '_' or (k in os.environ and os.environ[k] == val):
|
|
|
|
continue
|
|
|
|
env_diff[k] = val
|
|
|
|
with open(options.output_json, 'w') as f:
|
|
|
|
json.dump(env_diff, f)
|
2013-09-14 05:02:25 +04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|