media/webrtc/trunk/build/ios/clean_env.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/webrtc/trunk/build/ios/clean_env.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,77 @@
     1.4 +#!/usr/bin/python
     1.5 +# Copyright (c) 2012 The Chromium Authors. All rights reserved.
     1.6 +# Use of this source code is governed by a BSD-style license that can be
     1.7 +# found in the LICENSE file.
     1.8 +
     1.9 +import os
    1.10 +import sys
    1.11 +
    1.12 +def Main(argv):
    1.13 +  """This is like 'env -i', but it uses a whitelist of env variables to allow
    1.14 +  through to the command being run.  It attempts to strip off Xcode-added
    1.15 +  values from PATH.
    1.16 +  """
    1.17 +  # Note: An attempt was made to do something like: env -i bash -lc '[command]'
    1.18 +  # but that fails to set the things set by login (USER, etc.), so instead
    1.19 +  # the only approach that seems to work is to have a whitelist.
    1.20 +  env_key_whitelist = (
    1.21 +    'HOME',
    1.22 +    'LOGNAME',
    1.23 +    # 'PATH' added below (but filtered).
    1.24 +    'PWD',
    1.25 +    'SHELL',
    1.26 +    'TEMP',
    1.27 +    'TMPDIR',
    1.28 +    'USER'
    1.29 +  )
    1.30 +
    1.31 +  # Need something to run.
    1.32 +  # TODO(lliabraa): Make this output a usage string and exit (here and below).
    1.33 +  assert(len(argv) > 0)
    1.34 +
    1.35 +  add_to_path = [];
    1.36 +  first_entry = argv[0];
    1.37 +  if first_entry.startswith('ADD_TO_PATH='):
    1.38 +    argv = argv[1:];
    1.39 +    add_to_path = first_entry.replace('ADD_TO_PATH=', '', 1).split(':')
    1.40 +
    1.41 +  # Still need something to run.
    1.42 +  assert(len(argv) > 0)
    1.43 +
    1.44 +  clean_env = {}
    1.45 +
    1.46 +  # Pull over the whitelisted keys.
    1.47 +  for key in env_key_whitelist:
    1.48 +    val = os.environ.get(key, None)
    1.49 +    if not val is None:
    1.50 +      clean_env[key] = val
    1.51 +
    1.52 +  # Collect the developer dir as set via Xcode, defaulting it.
    1.53 +  dev_prefix = os.environ.get('DEVELOPER_DIR', '/Developer/')
    1.54 +  if dev_prefix[-1:] != '/':
    1.55 +    dev_prefix += '/'
    1.56 +
    1.57 +  # Now pull in PATH, but remove anything Xcode might have added.
    1.58 +  initial_path = os.environ.get('PATH', '')
    1.59 +  filtered_chunks = \
    1.60 +      [x for x in initial_path.split(':') if not x.startswith(dev_prefix)]
    1.61 +  if filtered_chunks:
    1.62 +    clean_env['PATH'] = ':'.join(add_to_path + filtered_chunks)
    1.63 +
    1.64 +  # Add any KEY=VALUE args before the command to the cleaned environment.
    1.65 +  args = argv[:]
    1.66 +  while '=' in args[0]:
    1.67 +    (key, val) = args[0].split('=', 1)
    1.68 +    clean_env[key] = val
    1.69 +    args = args[1:]
    1.70 +
    1.71 +  # Still need something to run.
    1.72 +  assert(len(args) > 0)
    1.73 +
    1.74 +  # Off it goes...
    1.75 +  os.execvpe(args[0], args, clean_env)
    1.76 +  # Should never get here, so return a distinctive, non-zero status code.
    1.77 +  return 66
    1.78 +
    1.79 +if __name__ == '__main__':
    1.80 +  sys.exit(Main(sys.argv[1:]))

mercurial