File: env_dump.py

package info (click to toggle)
qt6-webengine 6.9.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,111,532 kB
  • sloc: cpp: 21,436,007; ansic: 8,086,803; javascript: 2,747,888; python: 856,612; asm: 848,149; xml: 616,344; java: 222,847; sh: 105,206; objc: 99,183; perl: 70,870; cs: 51,103; sql: 40,087; makefile: 26,374; pascal: 25,140; fortran: 24,137; tcl: 9,609; yacc: 8,132; php: 7,051; lisp: 3,462; lex: 1,327; ruby: 914; awk: 339; csh: 120; sed: 36
file content (56 lines) | stat: -rwxr-xr-x 1,700 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/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())