OLD | NEW |
---|---|
1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Common python commands used by various build scripts.""" | 5 """Common python commands used by various build scripts.""" |
6 | 6 |
7 import inspect | 7 import inspect |
8 import os | 8 import os |
9 import re | 9 import re |
10 import subprocess | 10 import subprocess |
(...skipping 10 matching lines...) Expand all Loading... | |
21 | 21 |
22 def GetCallerName(): | 22 def GetCallerName(): |
23 """Returns the name of the calling module with __main__.""" | 23 """Returns the name of the calling module with __main__.""" |
24 top_frame = inspect.stack()[-1][0] | 24 top_frame = inspect.stack()[-1][0] |
25 return os.path.basename(top_frame.f_code.co_filename) | 25 return os.path.basename(top_frame.f_code.co_filename) |
26 | 26 |
27 | 27 |
28 def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None, | 28 def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None, |
29 exit_code=False, redirect_stdout=False, redirect_stderr=False, | 29 exit_code=False, redirect_stdout=False, redirect_stderr=False, |
30 cwd=None, input=None, enter_chroot=False, num_retries=0, | 30 cwd=None, input=None, enter_chroot=False, num_retries=0, |
31 log_to_file=None): | 31 log_to_file=None, combine_stdout_stderr=False): |
32 """Runs a shell command. | 32 """Runs a shell command. |
33 | 33 |
34 Arguments: | 34 Arguments: |
35 cmd: cmd to run. Should be input to subprocess.POpen. If a string, | 35 cmd: cmd to run. Should be input to subprocess.POpen. If a string, |
36 converted to an array using split(). | 36 converted to an array using split(). |
37 print_cmd: prints the command before running it. | 37 print_cmd: prints the command before running it. |
38 error_ok: does not raise an exception on error. | 38 error_ok: does not raise an exception on error. |
39 error_message: prints out this message when an error occurrs. | 39 error_message: prints out this message when an error occurrs. |
40 exit_code: returns the return code of the shell command. | 40 exit_code: returns the return code of the shell command. |
41 redirect_stdout: returns the stdout. | 41 redirect_stdout: returns the stdout. |
42 redirect_stderr: holds stderr output until input is communicated. | 42 redirect_stderr: holds stderr output until input is communicated. |
43 cwd: the working directory to run this cmd. | 43 cwd: the working directory to run this cmd. |
44 input: input to pipe into this command through stdin. | 44 input: input to pipe into this command through stdin. |
45 enter_chroot: this command should be run from within the chroot. If set, | 45 enter_chroot: this command should be run from within the chroot. If set, |
46 cwd must point to the scripts directory. | 46 cwd must point to the scripts directory. |
47 num_retries: the number of retries to perform before dying | 47 num_retries: the number of retries to perform before dying |
48 log_to_file: Redirects all stderr and stdout to file specified by this path. | 48 log_to_file: Redirects all stderr and stdout to file specified by this path. |
49 combine_stdout_stderr: Combines stdout and stdin streams into stdout. Auto | |
dgarrett
2011/04/18 23:13:21
'. Auto' should be:
'. Auto'
| |
50 set to true if log_to_file specifies a file. | |
49 | 51 |
50 Returns: | 52 Returns: |
51 If exit_code is True, returns the return code of the shell command. | 53 If exit_code is True, returns the return code of the shell command. |
52 Else returns the output of the shell command. | 54 Else returns the output of the shell command. |
53 | 55 |
54 Raises: | 56 Raises: |
55 Exception: Raises RunCommandException on error with optional error_message, | 57 Exception: Raises RunCommandException on error with optional error_message, |
56 | 58 |
57 but only if exit_code, and error_ok are both False. | 59 but only if exit_code, and error_ok are both False. |
58 """ | 60 """ |
59 # Set default for variables. | 61 # Set default for variables. |
60 stdout = None | 62 stdout = None |
61 stderr = None | 63 stderr = None |
62 stdin = None | 64 stdin = None |
63 file_handle = None | 65 file_handle = None |
64 output = '' | 66 output = '' |
65 | 67 |
66 # Modify defaults based on parameters. | 68 # Modify defaults based on parameters. |
67 if log_to_file: | 69 if log_to_file: |
68 file_handle = open(log_to_file, 'w+') | 70 file_handle = open(log_to_file, 'w+') |
69 stdout = file_handle | 71 stdout = file_handle |
70 stderr = file_handle | 72 stderr = file_handle |
71 else: | 73 else: |
72 if redirect_stdout: stdout = subprocess.PIPE | 74 if redirect_stdout: stdout = subprocess.PIPE |
73 if redirect_stderr: stderr = subprocess.PIPE | 75 if redirect_stderr: stderr = subprocess.PIPE |
76 if combine_stdout_stderr: stderr = subprocess.STDOUT | |
74 | 77 |
75 if input: stdin = subprocess.PIPE | 78 if input: stdin = subprocess.PIPE |
76 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd | 79 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd |
77 | 80 |
78 # Print out the command before running. | 81 # Print out the command before running. |
79 cmd_string = 'PROGRAM(%s) -> RunCommand: %r in dir %s' % (GetCallerName(), | 82 cmd_string = 'PROGRAM(%s) -> RunCommand: %r in dir %s' % (GetCallerName(), |
80 cmd, cwd) | 83 cmd, cwd) |
81 if print_cmd: | 84 if print_cmd: |
82 if not log_to_file: | 85 if not log_to_file: |
83 Info(cmd_string) | 86 Info(cmd_string) |
(...skipping 16 matching lines...) Expand all Loading... | |
100 break | 103 break |
101 | 104 |
102 if file_handle: file_handle.close() | 105 if file_handle: file_handle.close() |
103 | 106 |
104 # If they asked for an exit_code, give it to them on success or failure | 107 # If they asked for an exit_code, give it to them on success or failure |
105 if exit_code: | 108 if exit_code: |
106 return proc.returncode | 109 return proc.returncode |
107 | 110 |
108 # If the command (and all retries) failed, handle error result | 111 # If the command (and all retries) failed, handle error result |
109 if proc.returncode != 0: | 112 if proc.returncode != 0: |
110 if error_ok: | 113 if not error_ok: |
dgarrett
2011/04/18 23:13:21
Might as well do this...
if proc.returncode != 0
| |
111 Warning('Command "%r" failed.\n' % (cmd) + | |
112 (error_message or error or output or '')) | |
113 else: | |
114 raise RunCommandException('Command "%r" failed.\n' % (cmd) + | 114 raise RunCommandException('Command "%r" failed.\n' % (cmd) + |
115 (error_message or error or output or '')) | 115 (error_message or error or output or '')) |
116 | 116 |
117 # return final result | 117 # return final result |
118 return output | 118 return output |
119 | 119 |
120 | 120 |
121 def RunCommandCaptureOutput(cmd, print_cmd=True, cwd=None, input=None, | 121 def RunCommandCaptureOutput(cmd, print_cmd=True, cwd=None, input=None, |
122 enter_chroot=False, | 122 enter_chroot=False, |
123 combine_stdout_stderr=True, | 123 combine_stdout_stderr=True, |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
354 return os.path.join(GetCrosUtilsPath(source_dir_path), 'bin') | 354 return os.path.join(GetCrosUtilsPath(source_dir_path), 'bin') |
355 | 355 |
356 | 356 |
357 def IsInsideChroot(): | 357 def IsInsideChroot(): |
358 """Returns True if we are inside chroot.""" | 358 """Returns True if we are inside chroot.""" |
359 return os.path.exists('/etc/debian_chroot') | 359 return os.path.exists('/etc/debian_chroot') |
360 | 360 |
361 | 361 |
362 # TODO(sosa): Remove once all callers use method. | 362 # TODO(sosa): Remove once all callers use method. |
363 CROSUTILS_DIRECTORY = GetCrosUtilsPath(True) | 363 CROSUTILS_DIRECTORY = GetCrosUtilsPath(True) |
OLD | NEW |