| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium 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 import fnmatch | 5 import fnmatch |
| 6 import json | 6 import json |
| 7 import os | 7 import os |
| 8 import pipes | 8 import pipes |
| 9 import shlex | 9 import shlex |
| 10 import shutil | 10 import shutil |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 | 64 |
| 65 def ReadJson(path): | 65 def ReadJson(path): |
| 66 with open(path, 'r') as jsonfile: | 66 with open(path, 'r') as jsonfile: |
| 67 return json.load(jsonfile) | 67 return json.load(jsonfile) |
| 68 | 68 |
| 69 | 69 |
| 70 # This can be used in most cases like subprocess.check_call. The output, | 70 # This can be used in most cases like subprocess.check_call. The output, |
| 71 # particularly when the command fails, better highlights the command's failure. | 71 # particularly when the command fails, better highlights the command's failure. |
| 72 # This call will directly exit on a failure in the subprocess so that no python | 72 # This call will directly exit on a failure in the subprocess so that no python |
| 73 # stacktrace is printed after the output of the failed command. | 73 # stacktrace is printed after the output of the failed command. |
| 74 def CheckCallDie(args, cwd=None): | 74 def CheckCallDie(args, suppress_output=False, cwd=None): |
| 75 if not cwd: | 75 if not cwd: |
| 76 cwd = os.getcwd() | 76 cwd = os.getcwd() |
| 77 | 77 |
| 78 child = subprocess.Popen(args, | 78 child = subprocess.Popen(args, |
| 79 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cwd) | 79 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cwd) |
| 80 | 80 |
| 81 stdout, _ = child.communicate() | 81 stdout, _ = child.communicate() |
| 82 | 82 |
| 83 if child.returncode: | 83 if child.returncode: |
| 84 stacktrace = traceback.extract_stack() | 84 stacktrace = traceback.extract_stack() |
| 85 print >> sys.stderr, ''.join(traceback.format_list(stacktrace)) | 85 print >> sys.stderr, ''.join(traceback.format_list(stacktrace)) |
| 86 # A user should be able to simply copy and paste the command that failed | 86 # A user should be able to simply copy and paste the command that failed |
| 87 # into their shell. | 87 # into their shell. |
| 88 copyable_command = ' '.join(map(pipes.quote, args)) | 88 copyable_command = ' '.join(map(pipes.quote, args)) |
| 89 copyable_command = ('( cd ' + os.path.abspath(cwd) + '; ' | 89 copyable_command = ('( cd ' + os.path.abspath(cwd) + '; ' |
| 90 + copyable_command + ' )') | 90 + copyable_command + ' )') |
| 91 print >> sys.stderr, 'Command failed:', copyable_command, '\n' | 91 print >> sys.stderr, 'Command failed:', copyable_command, '\n' |
| 92 | 92 |
| 93 if stdout: | 93 if stdout: |
| 94 print stdout, | 94 print stdout, |
| 95 | 95 |
| 96 # Directly exit to avoid printing stacktrace. | 96 # Directly exit to avoid printing stacktrace. |
| 97 sys.exit(child.returncode) | 97 sys.exit(child.returncode) |
| 98 | 98 |
| 99 else: | 99 else: |
| 100 if stdout: | 100 if stdout and not suppress_output: |
| 101 print stdout, | 101 print stdout, |
| 102 return stdout | 102 return stdout |
| 103 | 103 |
| OLD | NEW |