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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 | 71 |
72 if not only_if_changed or old_dump != new_dump: | 72 if not only_if_changed or old_dump != new_dump: |
73 with open(path, 'w') as outfile: | 73 with open(path, 'w') as outfile: |
74 outfile.write(new_dump) | 74 outfile.write(new_dump) |
75 | 75 |
76 def ReadJson(path): | 76 def ReadJson(path): |
77 with open(path, 'r') as jsonfile: | 77 with open(path, 'r') as jsonfile: |
78 return json.load(jsonfile) | 78 return json.load(jsonfile) |
79 | 79 |
80 | 80 |
81 # This can be used in most cases like subprocess.check_call. The output, | 81 def CheckCall(args, suppress_output=False, cwd=None, fail_if_stderr=False): |
82 # particularly when the command fails, better highlights the command's failure. | |
83 # This call will directly exit on a failure in the subprocess so that no python | |
84 # stacktrace is printed after the output of the failed command (and will | |
85 # instead print a python stack trace before the output of the failed command) | |
86 def CheckCallDie(args, suppress_output=False, cwd=None, fail_if_stderr=False): | |
87 if not cwd: | 82 if not cwd: |
88 cwd = os.getcwd() | 83 cwd = os.getcwd() |
89 | 84 |
90 child = subprocess.Popen(args, | 85 child = subprocess.Popen(args, |
91 stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd) | 86 stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd) |
92 | 87 |
93 stdout, stderr = child.communicate() | 88 stdout, stderr = child.communicate() |
94 | 89 |
95 returncode = child.returncode | 90 returncode = child.returncode |
96 if fail_if_stderr and stderr and returncode == 0: | 91 if fail_if_stderr and stderr and returncode == 0: |
97 returncode = 1 | 92 returncode = 1 |
98 | 93 |
99 if returncode: | 94 if returncode: |
100 stacktrace = traceback.extract_stack() | 95 stacktrace = traceback.extract_stack() |
101 print >> sys.stderr, ''.join(traceback.format_list(stacktrace)) | 96 print >> sys.stderr, ''.join(traceback.format_list(stacktrace)) |
102 # A user should be able to simply copy and paste the command that failed | 97 # A user should be able to simply copy and paste the command that failed |
103 # into their shell. | 98 # into their shell. |
104 copyable_command = ' '.join(map(pipes.quote, args)) | 99 copyable_command = ' '.join(map(pipes.quote, args)) |
105 copyable_command = ('( cd ' + os.path.abspath(cwd) + '; ' | 100 copyable_command = ('( cd ' + os.path.abspath(cwd) + '; ' |
106 + copyable_command + ' )') | 101 + copyable_command + ' )') |
107 print >> sys.stderr, 'Command failed:', copyable_command, '\n' | 102 print >> sys.stderr, 'Command failed:', copyable_command, '\n' |
108 | 103 |
109 if stdout: | 104 if stdout: |
110 print stdout, | 105 print stdout, |
111 if stderr: | 106 if stderr: |
112 print >> sys.stderr, stderr, | 107 print >> sys.stderr, stderr, |
113 | 108 |
114 # Directly exit to avoid printing stacktrace. | |
115 sys.exit(returncode) | |
116 | |
117 else: | 109 else: |
118 if not suppress_output: | 110 if not suppress_output: |
119 if stdout: | 111 if stdout: |
120 print stdout, | 112 print stdout, |
121 if stderr: | 113 if stderr: |
122 print >> sys.stderr, stderr, | 114 print >> sys.stderr, stderr, |
123 return stdout + stderr | 115 |
| 116 return (returncode, stdout + stderr) |
| 117 |
| 118 |
| 119 # This can be used in most cases like subprocess.check_call. The output, |
| 120 # particularly when the command fails, better highlights the command's failure. |
| 121 # This call will directly exit on a failure in the subprocess so that no python |
| 122 # stacktrace is printed after the output of the failed command (and will |
| 123 # instead print a python stack trace before the output of the failed command) |
| 124 def CheckCallDie(args, suppress_output=False, cwd=None, fail_if_stderr=False): |
| 125 returncode, output = CheckCall(args, suppress_output=suppress_output, cwd=cwd, |
| 126 fail_if_stderr=fail_if_stderr) |
| 127 |
| 128 if returncode: |
| 129 # Directly exit to avoid printing stacktrace. |
| 130 sys.exit(returncode) |
| 131 |
| 132 return output |
124 | 133 |
125 | 134 |
126 def GetModifiedTime(path): | 135 def GetModifiedTime(path): |
127 # For a symlink, the modified time should be the greater of the link's | 136 # For a symlink, the modified time should be the greater of the link's |
128 # modified time and the modified time of the target. | 137 # modified time and the modified time of the target. |
129 return max(os.lstat(path).st_mtime, os.stat(path).st_mtime) | 138 return max(os.lstat(path).st_mtime, os.stat(path).st_mtime) |
130 | 139 |
131 | 140 |
132 def IsTimeStale(output, inputs): | 141 def IsTimeStale(output, inputs): |
133 if not os.path.exists(output): | 142 if not os.path.exists(output): |
(...skipping 12 matching lines...) Expand all Loading... |
146 | 155 |
147 | 156 |
148 def PrintWarning(message): | 157 def PrintWarning(message): |
149 print 'WARNING: ' + message | 158 print 'WARNING: ' + message |
150 | 159 |
151 | 160 |
152 def PrintBigWarning(message): | 161 def PrintBigWarning(message): |
153 print '***** ' * 8 | 162 print '***** ' * 8 |
154 PrintWarning(message) | 163 PrintWarning(message) |
155 print '***** ' * 8 | 164 print '***** ' * 8 |
OLD | NEW |