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

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

Issue 6005004: WIP Chromite supporting "LEGACY" mode, as requested by Anush. (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/crosutils.git@master
Patch Set: Use Info instead of printing to stderr. Created 10 years 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
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 os 7 import os
8 import re 8 import re
9 import signal
9 import subprocess 10 import subprocess
10 import sys 11 import sys
11 12
12 _STDOUT_IS_TTY = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() 13 _STDOUT_IS_TTY = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
13 14
14 15
15 class CommandResult(object): 16 class CommandResult(object):
16 """An object to store various attributes of a child process.""" 17 """An object to store various attributes of a child process."""
17 18
18 def __init__(self): 19 def __init__(self):
19 self.cmd = None 20 self.cmd = None
20 self.error = None 21 self.error = None
21 self.output = None 22 self.output = None
22 self.returncode = None 23 self.returncode = None
23 24
24 25
25 class RunCommandError(Exception): 26 class RunCommandError(Exception):
26 """Error caught in RunCommand() method.""" 27 """Error caught in RunCommand() method."""
27 pass 28 pass
28 29
29 30
30 def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None, 31 def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None,
31 exit_code=False, redirect_stdout=False, redirect_stderr=False, 32 exit_code=False, redirect_stdout=False, redirect_stderr=False,
32 cwd=None, input=None, enter_chroot=False, shell=False): 33 cwd=None, input=None, enter_chroot=False, shell=False,
34 ignore_sigint=False):
33 """Runs a command. 35 """Runs a command.
34 36
35 Args: 37 Args:
36 cmd: cmd to run. Should be input to subprocess.Popen. 38 cmd: cmd to run. Should be input to subprocess.Popen.
37 print_cmd: prints the command before running it. 39 print_cmd: prints the command before running it.
38 error_ok: does not raise an exception on error. 40 error_ok: does not raise an exception on error.
39 error_message: prints out this message when an error occurrs. 41 error_message: prints out this message when an error occurrs.
40 exit_code: returns the return code of the shell command. 42 exit_code: returns the return code of the shell command.
41 redirect_stdout: returns the stdout. 43 redirect_stdout: returns the stdout.
42 redirect_stderr: holds stderr output until input is communicated. 44 redirect_stderr: holds stderr output until input is communicated.
43 cwd: the working directory to run this cmd. 45 cwd: the working directory to run this cmd.
44 input: input to pipe into this command through stdin. 46 input: input to pipe into this command through stdin.
45 enter_chroot: this command should be run from within the chroot. If set, 47 enter_chroot: this command should be run from within the chroot. If set,
46 cwd must point to the scripts directory. 48 cwd must point to the scripts directory.
47 shell: If shell is True, the specified command will be executed through 49 shell: If shell is True, the specified command will be executed through
48 the shell. 50 the shell.
51 ignore_sigint: If True, we'll ignore signal.SIGINT before calling the
52 child. This is the desired behavior if we know our child will handle
53 Ctrl-C. If we don't do this, I think we and the child will both get
54 Ctrl-C at the same time, which means we'll forcefully kill the child.
49 55
50 Returns: 56 Returns:
51 A CommandResult object. 57 A CommandResult object.
52 58
53 Raises: 59 Raises:
54 Exception: Raises generic exception on error with optional error_message. 60 Exception: Raises generic exception on error with optional error_message.
55 """ 61 """
56 # Set default for variables. 62 # Set default for variables.
57 stdout = None 63 stdout = None
58 stderr = None 64 stderr = None
(...skipping 14 matching lines...) Expand all
73 cmd_str = ' '.join(cmd) 79 cmd_str = ' '.join(cmd)
74 80
75 # Print out the command before running. 81 # Print out the command before running.
76 if print_cmd: 82 if print_cmd:
77 Info('RunCommand: %s' % cmd_str) 83 Info('RunCommand: %s' % cmd_str)
78 cmd_result.cmd = cmd_str 84 cmd_result.cmd = cmd_str
79 85
80 try: 86 try:
81 proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin, stdout=stdout, 87 proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin, stdout=stdout,
82 stderr=stderr, shell=shell) 88 stderr=stderr, shell=shell)
83 (cmd_result.output, cmd_result.error) = proc.communicate(input) 89 if ignore_sigint:
90 old_sigint = signal.signal(signal.SIGINT, signal.SIG_IGN)
91 try:
92 (cmd_result.output, cmd_result.error) = proc.communicate(input)
93 finally:
94 if ignore_sigint:
95 signal.signal(signal.SIGINT, old_sigint)
96
84 if exit_code: 97 if exit_code:
85 cmd_result.returncode = proc.returncode 98 cmd_result.returncode = proc.returncode
86 99
87 if not error_ok and proc.returncode: 100 if not error_ok and proc.returncode:
88 msg = ('Command "%s" failed.\n' % cmd_str + 101 msg = ('Command "%s" failed.\n' % cmd_str +
89 (error_message or cmd_result.error or cmd_result.output or '')) 102 (error_message or cmd_result.error or cmd_result.output or ''))
90 raise RunCommandError(msg) 103 raise RunCommandError(msg)
91 # TODO(sosa): is it possible not to use the catch-all Exception here? 104 # TODO(sosa): is it possible not to use the catch-all Exception here?
92 except Exception, e: 105 except Exception, e:
93 if not error_ok: 106 if not error_ok:
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 Returns: 255 Returns:
243 a string: absolute path to output directory. 256 a string: absolute path to output directory.
244 """ 257 """
245 src_root = GetSrcRoot() 258 src_root = GetSrcRoot()
246 rel_path = 'build/images/%s' % board 259 rel_path = 'build/images/%s' % board
247 # ASSUME: --build_attempt always sets to 1 260 # ASSUME: --build_attempt always sets to 1
248 version_str = '-'.join([cros_version, 'a1']) 261 version_str = '-'.join([cros_version, 'a1'])
249 output_dir = os.path.join(os.path.dirname(src_root), rel_path, version_str) 262 output_dir = os.path.join(os.path.dirname(src_root), rel_path, version_str)
250 Info ('output_dir = %s' % output_dir) 263 Info ('output_dir = %s' % output_dir)
251 return output_dir 264 return output_dir
OLDNEW
« chromite/chromite.py ('K') | « chromite/chromite.sh ('k') | chromite/lib/text_menu.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698