
The Python 'pipes' module is deprecated and the 'quote' functionality has been moved since Python 3.3 to the 'shlex' module. Replace all instances of 'pipes.quote' with 'shlex.quote' in light of the eventual removal of this module in Python 3.10. Bug: chromium:1453653 Change-Id: I701e24af2fed439c38fdfb305a318165cd410dab Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4606522 Reviewed-by: Sam Maier <smaier@chromium.org> Auto-Submit: Prashanth Swaminathan <prashanthsw@google.com> Commit-Queue: Sam Maier <smaier@chromium.org> Cr-Commit-Position: refs/heads/main@{#1156440}
57 lines
1.7 KiB
Python
Executable File
57 lines
1.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# Copyright 2013 The Chromium Authors
|
|
# 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
|
|
import shlex
|
|
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',
|
|
help='Dump the environment to sys.stdout and exit immediately.')
|
|
|
|
parser.disable_interspersed_args()
|
|
options, args = parser.parse_args()
|
|
if options.dump_mode:
|
|
if args or options.output_json:
|
|
parser.error('Cannot specify args or --output-json with --dump-mode.')
|
|
json.dump(dict(os.environ), sys.stdout)
|
|
else:
|
|
if not options.output_json:
|
|
parser.error('Requires --output-json option.')
|
|
|
|
envsetup_cmd = ' '.join(map(shlex.quote, args))
|
|
full_cmd = [
|
|
'bash', '-c',
|
|
'. %s > /dev/null; %s -d' % (envsetup_cmd, os.path.abspath(__file__))
|
|
]
|
|
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)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|