OLD | NEW |
---|---|
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 signal | 10 import signal |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 if input: stdin = subprocess.PIPE | 88 if input: stdin = subprocess.PIPE |
89 if isinstance(cmd, basestring): | 89 if isinstance(cmd, basestring): |
90 if enter_chroot: cmd = './enter_chroot.sh -- ' + cmd | 90 if enter_chroot: cmd = './enter_chroot.sh -- ' + cmd |
91 cmd_str = cmd | 91 cmd_str = cmd |
92 else: | 92 else: |
93 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd | 93 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd |
94 cmd_str = ' '.join(cmd) | 94 cmd_str = ' '.join(cmd) |
95 | 95 |
96 # Print out the command before running. | 96 # Print out the command before running. |
97 if print_cmd: | 97 if print_cmd: |
98 Info('RunCommand: %s' % cmd_str) | 98 if cwd: |
99 Info('RunCommand: %s in %s' % (cmd_str, cwd)) | |
100 else: | |
101 Info('RunCommand: %s' % cmd_str) | |
99 cmd_result.cmd = cmd | 102 cmd_result.cmd = cmd |
100 | 103 |
101 try: | 104 try: |
102 proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin, stdout=stdout, | 105 proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin, stdout=stdout, |
103 stderr=stderr, shell=shell, env=env) | 106 stderr=stderr, shell=shell, env=env) |
104 if ignore_sigint: | 107 if ignore_sigint: |
105 old_sigint = signal.signal(signal.SIGINT, signal.SIG_IGN) | 108 old_sigint = signal.signal(signal.SIGINT, signal.SIG_IGN) |
106 try: | 109 try: |
107 (cmd_result.output, cmd_result.error) = proc.communicate(input) | 110 (cmd_result.output, cmd_result.error) = proc.communicate(input) |
108 finally: | 111 finally: |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
291 new_path = os.path.join('/home', os.getenv('USER'), 'trunk', relative_path) | 294 new_path = os.path.join('/home', os.getenv('USER'), 'trunk', relative_path) |
292 return new_path | 295 return new_path |
293 | 296 |
294 | 297 |
295 def GetCallerName(): | 298 def GetCallerName(): |
296 """Returns the name of the calling module with __main__.""" | 299 """Returns the name of the calling module with __main__.""" |
297 top_frame = inspect.stack()[-1][0] | 300 top_frame = inspect.stack()[-1][0] |
298 return os.path.basename(top_frame.f_code.co_filename) | 301 return os.path.basename(top_frame.f_code.co_filename) |
299 | 302 |
300 | 303 |
304 def GetValueFromFile(filename, varname): | |
dgarrett
2011/04/11 20:53:13
This method was left over from DJMM's changes, but
| |
305 """ | |
306 Given a NAME, return the VALUE out of a file. (NAME=VALUE) | |
307 or the awk equivalent: | |
308 $ awk -F= '/varname=/ {print $2}' filename | |
309 """ | |
310 if os.path.isfile(filename): | |
311 file_obj = open(filename, 'r') | |
312 else: | |
313 file_obj = filename | |
314 | |
315 for line in file_obj: | |
316 if line.startswith(varname + '='): | |
317 pat = line.split('=', 1)[1] | |
318 break | |
319 return pat.strip() | |
320 | |
321 | |
301 class RunCommandException(Exception): | 322 class RunCommandException(Exception): |
302 """Raised when there is an error in OldRunCommand.""" | 323 """Raised when there is an error in OldRunCommand.""" |
303 def __init__(self, msg, cmd): | 324 def __init__(self, msg, cmd): |
304 self.cmd = cmd | 325 self.cmd = cmd |
305 Exception.__init__(self, msg) | 326 Exception.__init__(self, msg) |
306 | 327 |
307 def __eq__(self, other): | 328 def __eq__(self, other): |
308 return (type(self) == type(other) and | 329 return (type(self) == type(other) and |
309 str(self) == str(other) and | 330 str(self) == str(other) and |
310 self.cmd == other.cmd) | 331 self.cmd == other.cmd) |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
374 except RunCommandException as e: | 395 except RunCommandException as e: |
375 if not error_ok and retry_count == num_retries: | 396 if not error_ok and retry_count == num_retries: |
376 raise e | 397 raise e |
377 else: | 398 else: |
378 Warning(str(e)) | 399 Warning(str(e)) |
379 if print_cmd: | 400 if print_cmd: |
380 Info('PROGRAM(%s) -> RunCommand: retrying %r in dir %s' % | 401 Info('PROGRAM(%s) -> RunCommand: retrying %r in dir %s' % |
381 (GetCallerName(), cmd, cwd)) | 402 (GetCallerName(), cmd, cwd)) |
382 | 403 |
383 return output | 404 return output |
OLD | NEW |