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

Side by Side Diff: chromite/lib/cros_build_lib.py

Issue 3323019: Add shell parameter to allow running commands directly. (Closed) Base URL: http://git.chromium.org/git/crosutils.git
Patch Set: Address review comments. Created 10 years, 3 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 | 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 subprocess 7 import subprocess
8 import sys 8 import sys
9 9
10 _STDOUT_IS_TTY = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() 10 _STDOUT_IS_TTY = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
11 11
12 def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None, 12 def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None,
13 exit_code=False, redirect_stdout=False, redirect_stderr=False, 13 exit_code=False, redirect_stdout=False, redirect_stderr=False,
14 cwd=None, input=None, enter_chroot=False): 14 cwd=None, input=None, enter_chroot=False, shell=False):
15 """Runs a shell command. 15 """Runs a command.
16 16
17 Keyword arguments: 17 Keyword arguments:
18 cmd - cmd to run. Should be input to subprocess.POpen. If a string, 18 cmd - cmd to run. Should be input to subprocess.Popen.
19 converted to an array using split().
20 print_cmd -- prints the command before running it. 19 print_cmd -- prints the command before running it.
21 error_ok -- does not raise an exception on error. 20 error_ok -- does not raise an exception on error.
22 error_message -- prints out this message when an error occurrs. 21 error_message -- prints out this message when an error occurrs.
23 exit_code -- returns the return code of the shell command. 22 exit_code -- returns the return code of the shell command.
24 redirect_stdout -- returns the stdout. 23 redirect_stdout -- returns the stdout.
25 redirect_stderr -- holds stderr output until input is communicated. 24 redirect_stderr -- holds stderr output until input is communicated.
26 cwd -- the working directory to run this cmd. 25 cwd -- the working directory to run this cmd.
27 input -- input to pipe into this command through stdin. 26 input -- input to pipe into this command through stdin.
28 enter_chroot -- this command should be run from within the chroot. If set, 27 enter_chroot -- this command should be run from within the chroot. If set,
29 cwd must point to the scripts directory. 28 cwd must point to the scripts directory.
29 shell -- If shell is True, the specified command will be executed through th e shell.
sosa 2010/09/09 17:45:21 80 chars :p
30 Raises: 30 Raises:
31 Exception: Raises generic exception on error with optional error_message. 31 Exception: Raises generic exception on error with optional error_message.
32 """ 32 """
33 # Set default for variables. 33 # Set default for variables.
34 stdout = None 34 stdout = None
35 stderr = None 35 stderr = None
36 stdin = None 36 stdin = None
37 output = '' 37 output = ''
38 38
39 # Modify defaults based on parameters. 39 # Modify defaults based on parameters.
40 if redirect_stdout: stdout = subprocess.PIPE 40 if redirect_stdout: stdout = subprocess.PIPE
41 if redirect_stderr: stderr = subprocess.PIPE 41 if redirect_stderr: stderr = subprocess.PIPE
42 if input: stdin = subprocess.PIPE 42 if input: stdin = subprocess.PIPE
43 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd 43 if isinstance(cmd, basestring):
44 if enter_chroot: cmd = './enter_chroot.sh -- ' + cmd
45 cmd_str = cmd
46 else:
47 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd
48 cmd_str = ' '.join(cmd)
44 49
45 # Print out the command before running. 50 # Print out the command before running.
46 if print_cmd: 51 if print_cmd:
47 Info('RunCommand: %s' % ' '.join(cmd)) 52 Info('RunCommand: %s' % cmd_str)
48 53
49 try: 54 try:
50 proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin, 55 proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin,
51 stdout=stdout, stderr=stderr) 56 stdout=stdout, stderr=stderr,
57 shell=shell)
52 (output, error) = proc.communicate(input) 58 (output, error) = proc.communicate(input)
53 if exit_code: 59 if exit_code:
54 return proc.returncode 60 return proc.returncode
55 61
56 if not error_ok and proc.returncode: 62 if not error_ok and proc.returncode:
57 raise Exception('Command "%s" failed.\n' % (' '.join(cmd)) + 63 raise Exception('Command "%s" failed.\n' % cmd_str +
58 (error_message or error or output or '')) 64 (error_message or error or output or ''))
59 except Exception,e: 65 except Exception,e:
60 if not error_ok: 66 if not error_ok:
61 raise 67 raise
62 else: 68 else:
63 Warning(str(e)) 69 Warning(str(e))
64 70
65 return output 71 return output
66 72
67 73
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 124
119 125
120 def Info(message): 126 def Info(message):
121 """Emits a blue informational message and continues execution. 127 """Emits a blue informational message and continues execution.
122 128
123 Keyword arguments: 129 Keyword arguments:
124 message: The message to be emitted. 130 message: The message to be emitted.
125 """ 131 """
126 print >> sys.stderr, ( 132 print >> sys.stderr, (
127 Color(_STDOUT_IS_TTY).Color(Color.BLUE, '\nINFO: ' + message)) 133 Color(_STDOUT_IS_TTY).Color(Color.BLUE, '\nINFO: ' + message))
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698