Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Side by Side Diff: lib/cros_build_lib.py

Issue 6579048: Reintroduce RunCommand cleanup and unit tests. Fixed previous issue, and (Closed) Base URL: http://git.chromium.org/git/crosutils.git@master
Patch Set: Remove bogus whitespace. Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | lib/cros_build_lib_unittest.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 18 matching lines...) Expand all
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 """Runs a shell command. 31 """Runs a shell command.
32 32
33 Arguments: 33 Arguments:
34 cmd: cmd to run. Should be input to subprocess.POpen. If a string, 34 cmd: cmd to run. Should be input to subprocess.POpen. If a string,
35 converted to an array using split(). 35 converted to an array using split().
36 print_cmd: prints the command before running it. 36 print_cmd: prints the command before running it.
37 error_ok: does not raise an exception on error. 37 error_ok: does not raise an exception on error.
38 error_message: prints out this message when an error occurrs. 38 error_message: prints out this message when an error occurrs.
39 exit_code: returns the return code of the shell command. 39 exit_code: returns the return code of the shell command.
sosa 2011/03/01 19:42:42 Update docsring perhaps to say returns exit code .
40 redirect_stdout: returns the stdout. 40 redirect_stdout: returns the stdout.
41 redirect_stderr: holds stderr output until input is communicated. 41 redirect_stderr: holds stderr output until input is communicated.
42 cwd: the working directory to run this cmd. 42 cwd: the working directory to run this cmd.
43 input: input to pipe into this command through stdin. 43 input: input to pipe into this command through stdin.
44 enter_chroot: this command should be run from within the chroot. If set, 44 enter_chroot: this command should be run from within the chroot. If set,
45 cwd must point to the scripts directory. 45 cwd must point to the scripts directory.
46 num_retries: the number of retries to perform before dying 46 num_retries: the number of retries to perform before dying
47 47
48 Returns: 48 Returns:
49 If exit_code is True, returns the return code of the shell command. 49 If exit_code is True, returns the return code of the shell command.
(...skipping 13 matching lines...) Expand all
63 if redirect_stderr: stderr = subprocess.PIPE 63 if redirect_stderr: stderr = subprocess.PIPE
64 if input: stdin = subprocess.PIPE 64 if input: stdin = subprocess.PIPE
65 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd 65 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd
66 66
67 # Print out the command before running. 67 # Print out the command before running.
68 if print_cmd: 68 if print_cmd:
69 Info('PROGRAM(%s) -> RunCommand: %r in dir %s' % 69 Info('PROGRAM(%s) -> RunCommand: %r in dir %s' %
70 (GetCallerName(), cmd, cwd)) 70 (GetCallerName(), cmd, cwd))
71 71
72 for retry_count in range(num_retries + 1): 72 for retry_count in range(num_retries + 1):
73 try:
74 proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin,
75 stdout=stdout, stderr=stderr)
76 (output, error) = proc.communicate(input)
77 if exit_code and retry_count == num_retries:
78 return proc.returncode
79 73
80 if proc.returncode == 0: 74 # If it's not the first attempt, it's a retry
81 break 75 if retry_count > 0 and print_cmd:
76 Info('PROGRAM(%s) -> RunCommand: retrying %r in dir %s' %
77 (GetCallerName(), cmd, cwd))
82 78
79 proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin,
80 stdout=stdout, stderr=stderr)
81 (output, error) = proc.communicate(input)
82
83 # if the command worked, don't retry any more.
84 if proc.returncode == 0:
85 break
86
87 # If they asked for an exit_code, give it to them on success or failure
88 if exit_code:
89 return proc.returncode
90
91 # If the command (and all retries) failed, handle error result
92 if proc.returncode != 0:
93 if error_ok:
94 Warning('Command "%r" failed.\n' % (cmd) +
95 (error_message or error or output or ''))
96 else:
83 raise RunCommandException('Command "%r" failed.\n' % (cmd) + 97 raise RunCommandException('Command "%r" failed.\n' % (cmd) +
84 (error_message or error or output or '')) 98 (error_message or error or output or ''))
85 except RunCommandException as e:
86 if not error_ok and retry_count == num_retries:
87 raise e
88 else:
89 Warning(str(e))
90 if print_cmd:
91 Info('PROGRAM(%s) -> RunCommand: retrying %r in dir %s' %
92 (GetCallerName(), cmd, cwd))
93 99
100 # return final result
94 return output 101 return output
95 102
96 103
97 def RunCommandCaptureOutput(cmd, print_cmd=True, cwd=None, input=None, 104 def RunCommandCaptureOutput(cmd, print_cmd=True, cwd=None, input=None,
98 enter_chroot=False, 105 enter_chroot=False,
99 combine_stdout_stderr=True, 106 combine_stdout_stderr=True,
100 verbose=False): 107 verbose=False):
101 """Runs a shell command. Differs from RunCommand, because it allows 108 """Runs a shell command. Differs from RunCommand, because it allows
102 you to run a command and capture the exit code, output, and stderr 109 you to run a command and capture the exit code, output, and stderr
103 all at the same time. 110 all at the same time.
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 either via a VM or remote machine on the same network. 264 either via a VM or remote machine on the same network.
258 """ 265 """
259 ifconfig_output = RunCommand(['/sbin/ifconfig', device], 266 ifconfig_output = RunCommand(['/sbin/ifconfig', device],
260 redirect_stdout=True, print_cmd=False) 267 redirect_stdout=True, print_cmd=False)
261 match = re.search('.*inet addr:(\d+\.\d+\.\d+\.\d+).*', ifconfig_output) 268 match = re.search('.*inet addr:(\d+\.\d+\.\d+\.\d+).*', ifconfig_output)
262 if match: 269 if match:
263 return match.group(1) 270 return match.group(1)
264 else: 271 else:
265 Warning('Failed to find ip address in %s' % ifconfig_output) 272 Warning('Failed to find ip address in %s' % ifconfig_output)
266 return None 273 return None
OLDNEW
« no previous file with comments | « no previous file | lib/cros_build_lib_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698