OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/python | |
2 # Copyright (c) 2012 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. | |
5 | |
6 import os | |
7 import sys | |
8 | |
9 def Main(argv): | |
10 """This is like 'env -i', but it uses a whitelist of env variables to allow | |
11 through to the command being run. It attempts to strip off Xcode added | |
stuartmorgan
2012/08/07 14:26:26
Xcode-added
lliabraa
2012/08/07 14:48:18
Done.
| |
12 values from PATH. | |
13 """ | |
14 # Note: An attempt was made to do something like: env -i bash -lc '[command]' | |
15 # but that fails to set the things set by login (USER, etc.), so instead | |
16 # the only approach that seems to work is to have a whitelist. | |
17 env_key_whitelist = ( | |
18 'HOME', | |
19 'LOGNAME', | |
20 # 'PATH' added below (but filtered). | |
21 'PWD', | |
22 'SHELL', | |
23 'TEMP', | |
24 'TMPDIR', | |
25 'USER' | |
26 ) | |
27 | |
28 # Need something to run. | |
29 assert(len(argv) > 0) | |
stuartmorgan
2012/08/07 14:26:26
It would be nice if this did a usage+exit instead
lliabraa
2012/08/07 14:48:18
Done.
| |
30 | |
31 add_to_path = []; | |
32 first_entry = argv[0]; | |
33 if first_entry.startswith('ADD_TO_PATH='): | |
34 argv = argv[1:]; | |
35 add_to_path = first_entry.replace('ADD_TO_PATH=', '', 1).split(':') | |
36 | |
37 # Still need something to run. | |
38 assert(len(argv) > 0) | |
39 | |
40 clean_env = {} | |
41 | |
42 # Pull over the whitelisted keys. | |
43 for key in env_key_whitelist: | |
44 val = os.environ.get(key, None) | |
45 if not val is None: | |
46 clean_env[key] = val | |
47 | |
48 # Collect the developer dir as set via Xcode, defaulting it. | |
49 dev_prefix = os.environ.get('DEVELOPER_DIR', '/Developer/') | |
50 if dev_prefix[-1:] != '/': | |
51 dev_prefix += '/' | |
52 | |
53 # Now pull in PATH, but remove anything Xcode might have added. | |
54 initial_path = os.environ.get('PATH', '') | |
55 filtered_chunks = \ | |
stuartmorgan
2012/08/07 14:26:26
Is the continuation actually necessary here? I wou
lliabraa
2012/08/07 14:48:18
Nope - omitting the \ will give a syntax error. If
| |
56 [x for x in initial_path.split(':') if not x.startswith(dev_prefix)] | |
57 if filtered_chunks: | |
58 clean_env['PATH'] = ':'.join(add_to_path + filtered_chunks) | |
59 | |
60 # Add any KEY=VALUE args before the command to the cleaned environment. | |
61 args = argv[:] | |
62 while '=' in args[0]: | |
63 (key, val) = args[0].split('=', 1) | |
64 clean_env[key] = val | |
65 args = args[1:] | |
66 | |
67 # Still need something to run. | |
68 assert(len(args) > 0) | |
69 | |
70 # Off it goes... | |
71 os.execvpe(args[0], args, clean_env) | |
72 # Should never get here... | |
73 return 66 | |
stuartmorgan
2012/08/07 14:26:26
Do you know if 66 is a meaningful value, or is it
lliabraa
2012/08/07 14:48:18
It is not meaningful to me and I didn't see anythi
| |
74 | |
75 if __name__ == '__main__': | |
76 sys.exit(Main(sys.argv[1:])) | |
OLD | NEW |