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

Side by Side Diff: lib/cros_build_lib.py

Issue 6877015: Add combine_stdout_stderr as well as make success less verbose. (Closed) Base URL: http://git.chromium.org/git/crosutils.git@master
Patch Set: Cleanup Created 9 years, 8 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_vm_lib.sh » ('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 10 matching lines...) Expand all
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
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 15 matching lines...) Expand all
99 if proc.returncode == 0: 102 if proc.returncode == 0:
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 and not error_ok:
110 if error_ok: 113 error_info = ('Command "%r" failed.\n' % (cmd) +
111 Warning('Command "%r" failed.\n' % (cmd) + 114 (error_message or error or output or ''))
112 (error_message or error or output or '')) 115 if log_to_file: error_info += '\nOutput logged to %s' % log_to_file
113 else: 116 raise RunCommandException(error_info)
114 raise RunCommandException('Command "%r" failed.\n' % (cmd) +
115 (error_message or error or output or ''))
116 117
117 # return final result 118 # return final result
118 return output 119 return output
119 120
120 121
121 def RunCommandCaptureOutput(cmd, print_cmd=True, cwd=None, input=None, 122 def RunCommandCaptureOutput(cmd, print_cmd=True, cwd=None, input=None,
122 enter_chroot=False, 123 enter_chroot=False,
123 combine_stdout_stderr=True, 124 combine_stdout_stderr=True,
124 verbose=False): 125 verbose=False):
125 """Runs a shell command. Differs from RunCommand, because it allows 126 """Runs a shell command. Differs from RunCommand, because it allows
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 return os.path.join(GetCrosUtilsPath(source_dir_path), 'bin') 355 return os.path.join(GetCrosUtilsPath(source_dir_path), 'bin')
355 356
356 357
357 def IsInsideChroot(): 358 def IsInsideChroot():
358 """Returns True if we are inside chroot.""" 359 """Returns True if we are inside chroot."""
359 return os.path.exists('/etc/debian_chroot') 360 return os.path.exists('/etc/debian_chroot')
360 361
361 362
362 # TODO(sosa): Remove once all callers use method. 363 # TODO(sosa): Remove once all callers use method.
363 CROSUTILS_DIRECTORY = GetCrosUtilsPath(True) 364 CROSUTILS_DIRECTORY = GetCrosUtilsPath(True)
OLDNEW
« no previous file with comments | « no previous file | lib/cros_vm_lib.sh » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698