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

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: Fixup RunCommand DocString Created 9 years, 9 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
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.
50 Else returns the output of the shell command. 50 Else returns the output of the shell command.
51 51
52 Raises: 52 Raises:
53 Exception: Raises RunCommandException on error with optional error_message. 53 Exception: Raises RunCommandException on error with optional error_message,
54 but only if exit_code, and error_ok are both False.
54 """ 55 """
55 # Set default for variables. 56 # Set default for variables.
56 stdout = None 57 stdout = None
57 stderr = None 58 stderr = None
58 stdin = None 59 stdin = None
59 output = '' 60 output = ''
60 61
61 # Modify defaults based on parameters. 62 # Modify defaults based on parameters.
62 if redirect_stdout: stdout = subprocess.PIPE 63 if redirect_stdout: stdout = subprocess.PIPE
63 if redirect_stderr: stderr = subprocess.PIPE 64 if redirect_stderr: stderr = subprocess.PIPE
64 if input: stdin = subprocess.PIPE 65 if input: stdin = subprocess.PIPE
65 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd 66 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd
66 67
67 # Print out the command before running. 68 # Print out the command before running.
68 if print_cmd: 69 if print_cmd:
69 Info('PROGRAM(%s) -> RunCommand: %r in dir %s' % 70 Info('PROGRAM(%s) -> RunCommand: %r in dir %s' %
70 (GetCallerName(), cmd, cwd)) 71 (GetCallerName(), cmd, cwd))
71 72
72 for retry_count in range(num_retries + 1): 73 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 74
80 if proc.returncode == 0: 75 # If it's not the first attempt, it's a retry
81 break 76 if retry_count > 0 and print_cmd:
77 Info('PROGRAM(%s) -> RunCommand: retrying %r in dir %s' %
78 (GetCallerName(), cmd, cwd))
82 79
80 proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin,
81 stdout=stdout, stderr=stderr)
82 (output, error) = proc.communicate(input)
83
84 # if the command worked, don't retry any more.
85 if proc.returncode == 0:
86 break
87
88 # If they asked for an exit_code, give it to them on success or failure
89 if exit_code:
90 return proc.returncode
91
92 # If the command (and all retries) failed, handle error result
93 if proc.returncode != 0:
94 if error_ok:
95 Warning('Command "%r" failed.\n' % (cmd) +
96 (error_message or error or output or ''))
97 else:
83 raise RunCommandException('Command "%r" failed.\n' % (cmd) + 98 raise RunCommandException('Command "%r" failed.\n' % (cmd) +
84 (error_message or error or output or '')) 99 (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 100
101 # return final result
94 return output 102 return output
95 103
96 104
97 def RunCommandCaptureOutput(cmd, print_cmd=True, cwd=None, input=None, 105 def RunCommandCaptureOutput(cmd, print_cmd=True, cwd=None, input=None,
98 enter_chroot=False, 106 enter_chroot=False,
99 combine_stdout_stderr=True, 107 combine_stdout_stderr=True,
100 verbose=False): 108 verbose=False):
101 """Runs a shell command. Differs from RunCommand, because it allows 109 """Runs a shell command. Differs from RunCommand, because it allows
102 you to run a command and capture the exit code, output, and stderr 110 you to run a command and capture the exit code, output, and stderr
103 all at the same time. 111 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. 265 either via a VM or remote machine on the same network.
258 """ 266 """
259 ifconfig_output = RunCommand(['/sbin/ifconfig', device], 267 ifconfig_output = RunCommand(['/sbin/ifconfig', device],
260 redirect_stdout=True, print_cmd=False) 268 redirect_stdout=True, print_cmd=False)
261 match = re.search('.*inet addr:(\d+\.\d+\.\d+\.\d+).*', ifconfig_output) 269 match = re.search('.*inet addr:(\d+\.\d+\.\d+\.\d+).*', ifconfig_output)
262 if match: 270 if match:
263 return match.group(1) 271 return match.group(1)
264 else: 272 else:
265 Warning('Failed to find ip address in %s' % ifconfig_output) 273 Warning('Failed to find ip address in %s' % ifconfig_output)
266 return None 274 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