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

Side by Side Diff: build/android/gyp/util/build_utils.py

Issue 106923002: Don't use sys.exit() in build_utils.CheckCallDie(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 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 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
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 class CalledProcessError(Exception):
82 """This exception is raised when the process run by CheckOutput
83 exits with a non-zero exit code."""
84
85 def __init__(self, cwd, args, output):
86 self.cwd = cwd
87 self.args = args
88 self.output = output
89
90 def __str__(self):
91 # A user should be able to simply copy and paste the command that failed
92 # into their shell.
93 copyable_command = '( cd {}; {} )'.format(os.path.abspath(self.cwd),
94 ' '.join(map(pipes.quote, self.args)))
95 return 'Command failed: {}\n{}'.format(copyable_command, self.output)
96
97
98 # This can be used in most cases like subprocess.check_output(). The output,
82 # particularly when the command fails, better highlights the command's failure. 99 # 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 100 # If the command fails, raises a build_utils.CalledProcessError.
84 # stacktrace is printed after the output of the failed command (and will 101 def CheckOutput(args, cwd=None, print_stdout=False, print_stderr=True,
85 # instead print a python stack trace before the output of the failed command) 102 fail_if_stderr=False):
86 def CheckCallDie(args, suppress_output=False, cwd=None, fail_if_stderr=False):
87 if not cwd: 103 if not cwd:
88 cwd = os.getcwd() 104 cwd = os.getcwd()
89 105
90 child = subprocess.Popen(args, 106 child = subprocess.Popen(args,
91 stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd) 107 stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd)
92
93 stdout, stderr = child.communicate() 108 stdout, stderr = child.communicate()
94 109
95 returncode = child.returncode 110 if child.returncode or (stderr and fail_if_stderr):
96 if fail_if_stderr and stderr and returncode == 0: 111 raise CalledProcessError(cwd, args, stdout + stderr)
97 returncode = 1
98 112
99 if returncode: 113 if print_stdout:
100 stacktrace = traceback.extract_stack() 114 sys.stdout.write(stdout)
101 print >> sys.stderr, ''.join(traceback.format_list(stacktrace)) 115 if print_stderr:
102 # A user should be able to simply copy and paste the command that failed 116 sys.stderr.write(stderr)
103 # into their shell.
104 copyable_command = ' '.join(map(pipes.quote, args))
105 copyable_command = ('( cd ' + os.path.abspath(cwd) + '; '
106 + copyable_command + ' )')
107 print >> sys.stderr, 'Command failed:', copyable_command, '\n'
108 117
109 if stdout: 118 return stdout
110 print stdout,
111 if stderr:
112 print >> sys.stderr, stderr,
113
114 # Directly exit to avoid printing stacktrace.
115 sys.exit(returncode)
116
117 else:
118 if not suppress_output:
119 if stdout:
120 print stdout,
121 if stderr:
122 print >> sys.stderr, stderr,
123 return stdout + stderr
124 119
125 120
126 def GetModifiedTime(path): 121 def GetModifiedTime(path):
127 # For a symlink, the modified time should be the greater of the link's 122 # For a symlink, the modified time should be the greater of the link's
128 # modified time and the modified time of the target. 123 # modified time and the modified time of the target.
129 return max(os.lstat(path).st_mtime, os.stat(path).st_mtime) 124 return max(os.lstat(path).st_mtime, os.stat(path).st_mtime)
130 125
131 126
132 def IsTimeStale(output, inputs): 127 def IsTimeStale(output, inputs):
133 if not os.path.exists(output): 128 if not os.path.exists(output):
134 return True 129 return True
135 130
136 output_time = GetModifiedTime(output) 131 output_time = GetModifiedTime(output)
137 for input in inputs: 132 for input in inputs:
138 if GetModifiedTime(input) > output_time: 133 if GetModifiedTime(input) > output_time:
139 return True 134 return True
140 return False 135 return False
141 136
142 137
143 def IsDeviceReady(): 138 def IsDeviceReady():
144 device_state = CheckCallDie(['adb', 'get-state'], suppress_output=True) 139 device_state = CheckOutput(['adb', 'get-state'])
145 return device_state.strip() == 'device' 140 return device_state.strip() == 'device'
146 141
147 142
148 def PrintWarning(message): 143 def PrintWarning(message):
149 print 'WARNING: ' + message 144 print 'WARNING: ' + message
150 145
151 146
152 def PrintBigWarning(message): 147 def PrintBigWarning(message):
153 print '***** ' * 8 148 print '***** ' * 8
154 PrintWarning(message) 149 PrintWarning(message)
155 print '***** ' * 8 150 print '***** ' * 8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698