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

Side by Side Diff: lib/cros_build_lib.py

Issue 4864001: Change _ArchiveTestResults to upload to Google Storage (Closed) Base URL: http://git.chromium.org/git/crosutils.git@master
Patch Set: Add num retries to RunCommand Created 10 years, 1 month 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
« bin/cbuildbot.py ('K') | « bin/cbuildbot.py ('k') | no next file » | 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 subprocess 9 import subprocess
10 import sys 10 import sys
11 11
12 _STDOUT_IS_TTY = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() 12 _STDOUT_IS_TTY = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
13 13
14 # TODO(sosa): Move logging to logging module. 14 # TODO(sosa): Move logging to logging module.
15 15
16 def GetCallerName(): 16 def GetCallerName():
17 """Returns the name of the calling module with __main__.""" 17 """Returns the name of the calling module with __main__."""
18 top_frame = inspect.stack()[-1][0] 18 top_frame = inspect.stack()[-1][0]
19 return os.path.basename(top_frame.f_code.co_filename) 19 return os.path.basename(top_frame.f_code.co_filename)
20 20
21 21
22 def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None, 22 def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None,
23 exit_code=False, redirect_stdout=False, redirect_stderr=False, 23 exit_code=False, redirect_stdout=False, redirect_stderr=False,
24 cwd=None, input=None, enter_chroot=False): 24 cwd=None, input=None, enter_chroot=False, num_retries=0):
25 """Runs a shell command. 25 """Runs a shell command.
26 26
27 Keyword arguments: 27 Arguments:
28 cmd - cmd to run. Should be input to subprocess.POpen. If a string, 28 cmd: cmd to run. Should be input to subprocess.POpen. If a string,
29 converted to an array using split(). 29 converted to an array using split().
30 print_cmd -- prints the command before running it. 30 print_cmd: prints the command before running it.
31 error_ok -- does not raise an exception on error. 31 error_ok: does not raise an exception on error.
32 error_message -- prints out this message when an error occurrs. 32 error_message: prints out this message when an error occurrs.
33 exit_code -- returns the return code of the shell command. 33 exit_code: returns the return code of the shell command.
34 redirect_stdout -- returns the stdout. 34 redirect_stdout: returns the stdout.
35 redirect_stderr -- holds stderr output until input is communicated. 35 redirect_stderr: holds stderr output until input is communicated.
36 cwd -- the working directory to run this cmd. 36 cwd: the working directory to run this cmd.
37 input -- input to pipe into this command through stdin. 37 input: input to pipe into this command through stdin.
38 enter_chroot -- this command should be run from within the chroot. If set, 38 enter_chroot: this command should be run from within the chroot. If set,
39 cwd must point to the scripts directory. 39 cwd must point to the scripts directory.
40 num_retries: the number of retries to perform before dying
41
42 Returns:
43 If exit_code is True, returns the return code of the shell command.
44 Else returns the output of the shell command.
45
40 Raises: 46 Raises:
41 Exception: Raises generic exception on error with optional error_message. 47 Exception: Raises generic exception on error with optional error_message.
42 """ 48 """
43 # Set default for variables. 49 # Set default for variables.
44 stdout = None 50 stdout = None
45 stderr = None 51 stderr = None
46 stdin = None 52 stdin = None
47 output = '' 53 output = ''
48 54
49 # Modify defaults based on parameters. 55 # Modify defaults based on parameters.
50 if redirect_stdout: stdout = subprocess.PIPE 56 if redirect_stdout: stdout = subprocess.PIPE
51 if redirect_stderr: stderr = subprocess.PIPE 57 if redirect_stderr: stderr = subprocess.PIPE
52 if input: stdin = subprocess.PIPE 58 if input: stdin = subprocess.PIPE
53 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd 59 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd
54 60
55 # Print out the command before running. 61 # Print out the command before running.
56 if print_cmd: 62 if print_cmd:
57 Info('PROGRAM(%s) -> RunCommand: %r in dir %s' % 63 Info('PROGRAM(%s) -> RunCommand: %r in dir %s' %
58 (GetCallerName(), cmd, cwd)) 64 (GetCallerName(), cmd, cwd))
59 65
60 try: 66 for retry_count in range(num_retries + 1):
61 proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin, 67 try:
62 stdout=stdout, stderr=stderr) 68 proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin,
63 (output, error) = proc.communicate(input) 69 stdout=stdout, stderr=stderr)
64 if exit_code: 70 (output, error) = proc.communicate(input)
65 return proc.returncode 71 if exit_code and retry_count == num_retries:
72 return proc.returncode
66 73
67 if not error_ok and proc.returncode: 74 if proc.returncode == 0:
68 raise Exception('Command "%r" failed.\n' % (cmd) + 75 break
69 (error_message or error or output or '')) 76 else:
scottz-goog 2010/11/12 23:37:13 No need for the else here.
thieule 2010/11/13 00:21:32 Done.
70 except Exception, e: 77 raise Exception('Command "%r" failed.\n' % (cmd) +
71 if not error_ok: 78 (error_message or error or output or ''))
72 raise 79 except Exception, e:
scottz-goog 2010/11/12 23:37:13 Ugg do we really not raise a RunCommand Exception?
thieule 2010/11/13 00:21:32 Done.
73 else: 80 if not error_ok and retry_count == num_retries:
74 Warning(str(e)) 81 raise
82 else:
83 Warning(str(e))
84
85 if print_cmd:
sosa 2010/11/12 23:33:09 Little easier to read if this is in the else: of y
thieule 2010/11/13 00:21:32 Done.
86 Info('PROGRAM(%s) -> RunCommand: retrying %r in dir %s' %
87 (GetCallerName(), cmd, cwd))
75 88
76 return output 89 return output
77 90
78 91
79 class Color(object): 92 class Color(object):
80 """Conditionally wraps text in ANSI color escape sequences.""" 93 """Conditionally wraps text in ANSI color escape sequences."""
81 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) 94 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
82 BOLD = -1 95 BOLD = -1
83 COLOR_START = '\033[1;%dm' 96 COLOR_START = '\033[1;%dm'
84 BOLD_START = '\033[1m' 97 BOLD_START = '\033[1m'
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 175
163 # Strip the repository root from the path and strip first /. 176 # Strip the repository root from the path and strip first /.
164 relative_path = path_abs_path.replace(root_abs_path, '')[1:] 177 relative_path = path_abs_path.replace(root_abs_path, '')[1:]
165 178
166 if relative_path == path_abs_path: 179 if relative_path == path_abs_path:
167 raise Exception('Error: path is outside your src tree, cannot reinterpret.') 180 raise Exception('Error: path is outside your src tree, cannot reinterpret.')
168 181
169 new_path = os.path.join('/home', os.getenv('USER'), 'trunk', relative_path) 182 new_path = os.path.join('/home', os.getenv('USER'), 'trunk', relative_path)
170 return new_path 183 return new_path
171 184
OLDNEW
« bin/cbuildbot.py ('K') | « bin/cbuildbot.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698