OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/python | |
sosa
2011/02/02 01:16:19
same
diandersAtChromium
2011/02/02 01:49:10
Done.
| |
2 # | |
3 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | |
4 # Use of this source code is governed by a BSD-style license that can be | |
5 # found in the LICENSE file. | |
6 | |
7 """Implementation of the 'shell' chromite command.""" | |
8 | |
9 # Python imports | |
10 import optparse | |
11 import os | |
12 | |
13 | |
14 # Local imports | |
15 from chromite.lib import cros_build_lib as cros_lib | |
16 from chromite.shell import utils | |
17 from chromite.shell import subcmd | |
18 | |
19 | |
20 def _SplitEnvFromArgs(argv): | |
21 """Split environment settings from arguments. | |
22 | |
23 This function will just loop over the arguments, looking for ones with an '=' | |
24 character in them. As long as it finds them, it takes them out of the arg | |
25 list adds them to a dictionary. As soon as it finds the first argument | |
26 without an '=', it stops looping. | |
27 | |
28 NOTE: Some doctests below test for equality with ==, since dicts with more | |
29 than one item may be arbitrarily ordered. | |
30 | |
31 >>> result = _SplitEnvFromArgs(['abc=1', 'def=two', 'three']) | |
32 >>> result == ({'abc': '1', 'def': 'two'}, ['three']) | |
33 True | |
34 | |
35 >>> _SplitEnvFromArgs(['one', 'two', 'three']) | |
36 ({}, ['one', 'two', 'three']) | |
37 | |
38 >>> _SplitEnvFromArgs(['abc=1', 'three', 'def=two']) | |
39 ({'abc': '1'}, ['three', 'def=two']) | |
40 | |
41 >>> result = _SplitEnvFromArgs(['abc=1', 'ghi=4 4', 'def=two']) | |
42 >>> result == ({'abc': '1', 'ghi': '4 4', 'def': 'two'}, []) | |
43 True | |
44 | |
45 >>> _SplitEnvFromArgs(['abc=1', 'abc=2', 'three']) | |
46 ({'abc': '2'}, ['three']) | |
47 | |
48 >>> _SplitEnvFromArgs([]) | |
49 ({}, []) | |
50 | |
51 Args: | |
52 argv: The arguments to parse; this list is not modified. Should | |
53 not include "argv[0]" | |
54 Returns: | |
55 env: A dict containing key=value paris. | |
56 argv: A new list containing anything left after. | |
57 """ | |
58 # Copy the list so we don't screw with caller... | |
59 argv = list(argv) | |
60 | |
61 env = {} | |
62 while argv: | |
63 if '=' in argv[0]: | |
64 key, val = argv.pop(0).split('=', 2) | |
65 env[key] = val | |
66 else: | |
67 break | |
68 | |
69 return env, argv | |
70 | |
71 | |
72 class ShellCmd(subcmd.ChromiteCmd): | |
73 """Start a shell in the chroot. | |
74 | |
75 This can either just start a simple interactive shell, it can be used to | |
76 run an arbirtary command inside the chroot and then exit. | |
77 """ | |
78 | |
79 def Run(self, raw_argv, chroot_config=None): | |
80 """Run the command. | |
81 | |
82 Args: | |
83 raw_argv: Command line arguments, including this command's name, but not | |
84 the chromite command name or chromite options. | |
85 chroot_config: A SafeConfigParser for the chroot config; or None chromite | |
86 was called from within the chroot. | |
87 """ | |
88 # Parse options for command... | |
89 # ...note that OptionParser will eat the '--' if it's there, which is what | |
90 # we want.. | |
91 usage_str = ('usage: %%prog [chromite_options] %s [options] [VAR=value] ' | |
92 '[-- command [arg1] [arg2] ...]') % raw_argv[0] | |
93 parser = optparse.OptionParser(usage=usage_str) | |
94 (_, argv) = parser.parse_args(raw_argv[1:]) | |
95 | |
96 # Enter the chroot if needed... | |
97 if not cros_lib.IsInsideChroot(): | |
98 utils.EnterChroot(chroot_config, (self, 'Run'), raw_argv) | |
99 else: | |
100 # We'll put CWD as src/scripts when running the command. Since everyone | |
101 # running by hand has their cwd there, it is probably the safest. | |
102 cwd = os.path.join(utils.SRCROOT_PATH, 'src', 'scripts') | |
103 | |
104 # By default, no special environment... | |
105 env = None | |
106 | |
107 if not argv: | |
108 # If no arguments, we'll just start bash. | |
109 argv = ['bash'] | |
110 else: | |
111 # Parse the command line, looking at the beginning for VAR=value type | |
112 # statements. I couldn't figure out a way to get bash to do this for | |
113 # me. | |
114 user_env, argv = _SplitEnvFromArgs(argv) | |
115 if not argv: | |
116 cros_lib.Die('No command specified') | |
117 | |
118 # If there was some environment, use it to override the standard | |
119 # environment. | |
120 if user_env: | |
121 env = dict(os.environ) | |
122 env.update(user_env) | |
123 | |
124 # Don't show anything special for errors; we'll let the shell report them. | |
125 cros_lib.RunCommand(argv, cwd=cwd, env=env, error_ok=True, ignore_sigint=T rue) | |
sosa
2011/02/02 01:16:19
80 char
diandersAtChromium
2011/02/02 01:49:10
Done.
| |
OLD | NEW |