OLD | NEW |
1 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 # for details. All rights reserved. Use of this source code is governed by a | 2 # for details. All rights reserved. Use of this source code is governed by a |
3 # BSD-style license that can be found in the LICENSE file. | 3 # BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 # This file contains a set of utilities functions used by other Python-based | 5 # This file contains a set of utilities functions used by other Python-based |
6 # scripts. | 6 # scripts. |
7 | 7 |
8 import commands | 8 import commands |
9 import os | 9 import os |
10 import platform | 10 import platform |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 errStream: if present, the stderr output of the command will be written to | 160 errStream: if present, the stderr output of the command will be written to |
161 errStream. | 161 errStream. |
162 killOnEarlyReturn: if true and pollFn returns true, then the subprocess will | 162 killOnEarlyReturn: if true and pollFn returns true, then the subprocess will |
163 be killed, otherwise the subprocess will be detached. | 163 be killed, otherwise the subprocess will be detached. |
164 verbose: if true, the command is echoed to stderr. | 164 verbose: if true, the command is echoed to stderr. |
165 debug: if true, prints debugging information to stderr. | 165 debug: if true, prints debugging information to stderr. |
166 printErrorInfo: if true, prints error information when the subprocess | 166 printErrorInfo: if true, prints error information when the subprocess |
167 returns a non-zero exit code. | 167 returns a non-zero exit code. |
168 Returns: the output of the subprocess. | 168 Returns: the output of the subprocess. |
169 | 169 |
170 Raises an exception if the subprocess returns an error code. | 170 Exceptions: |
| 171 Raises Error if the subprocess returns an error code. |
| 172 Raises ValueError if called with invalid arguments. |
171 """ | 173 """ |
172 if verbose: | 174 if verbose: |
173 sys.stderr.write("command %s\n" % command) | 175 sys.stderr.write("command %s\n" % command) |
174 stdin = None | 176 stdin = None |
175 if input is not None: | 177 if input: |
176 stdin = subprocess.PIPE | 178 stdin = subprocess.PIPE |
177 try: | 179 try: |
178 process = subprocess.Popen(args=command, | 180 process = subprocess.Popen(args=command, |
179 stdin=stdin, | 181 stdin=stdin, |
180 bufsize=1, | 182 bufsize=1, |
181 stdout=subprocess.PIPE, | 183 stdout=subprocess.PIPE, |
182 stderr=subprocess.PIPE) | 184 stderr=subprocess.PIPE) |
183 except Exception as e: | 185 except OSError as e: |
184 if not isinstance(command, basestring): | 186 if not isinstance(command, basestring): |
185 command = ' '.join(command) | 187 command = ' '.join(command) |
186 if printErrorInfo: | 188 if printErrorInfo: |
187 sys.stderr.write("Command failed: '%s'\n" % command) | 189 sys.stderr.write("Command failed: '%s'\n" % command) |
188 raise e | 190 raise Error(e) |
189 | 191 |
190 def StartThread(out): | 192 def StartThread(out): |
191 queue = Queue.Queue() | 193 queue = Queue.Queue() |
192 def EnqueueOutput(out, queue): | 194 def EnqueueOutput(out, queue): |
193 for line in iter(out.readline, b''): | 195 for line in iter(out.readline, b''): |
194 queue.put(line) | 196 queue.put(line) |
195 out.close() | 197 out.close() |
196 thread = threading.Thread(target=EnqueueOutput, args=(out, queue)) | 198 thread = threading.Thread(target=EnqueueOutput, args=(out, queue)) |
197 thread.daemon = True | 199 thread.daemon = True |
198 thread.start() | 200 thread.start() |
199 return queue | 201 return queue |
200 outQueue = StartThread(process.stdout) | 202 outQueue = StartThread(process.stdout) |
201 errQueue = StartThread(process.stderr) | 203 errQueue = StartThread(process.stderr) |
202 | 204 |
203 def ReadQueue(queue, out, out2): | 205 def ReadQueue(queue, out, out2): |
204 try: | 206 try: |
205 while True: | 207 while True: |
206 line = queue.get(False) | 208 line = queue.get(False) |
207 out.write(line) | 209 out.write(line) |
208 if out2 != None: | 210 if out2 != None: |
209 out2.write(line) | 211 out2.write(line) |
210 except Queue.Empty: | 212 except Queue.Empty: |
211 pass | 213 pass |
212 | 214 |
213 outBuf = StringIO.StringIO() | 215 outBuf = StringIO.StringIO() |
214 errorBuf = StringIO.StringIO() | 216 errorBuf = StringIO.StringIO() |
215 if input != None: | 217 if input: |
216 process.stdin.write(input) | 218 process.stdin.write(input) |
217 while True: | 219 while True: |
218 returncode = process.poll() | 220 returncode = process.poll() |
219 if returncode != None: | 221 if returncode != None: |
220 break | 222 break |
221 ReadQueue(errQueue, errorBuf, errStream) | 223 ReadQueue(errQueue, errorBuf, errStream) |
222 ReadQueue(outQueue, outBuf, outStream) | 224 ReadQueue(outQueue, outBuf, outStream) |
223 if pollFn != None and pollFn(): | 225 if pollFn != None and pollFn(): |
224 returncode = 0 | 226 returncode = 0 |
225 if killOnEarlyReturn: | 227 if killOnEarlyReturn: |
226 process.kill() | 228 process.kill() |
227 break | 229 break |
228 time.sleep(0.1) | 230 time.sleep(0.1) |
229 # Drain queue | 231 # Drain queue |
230 ReadQueue(errQueue, errorBuf, errStream) | 232 ReadQueue(errQueue, errorBuf, errStream) |
231 ReadQueue(outQueue, outBuf, outStream) | 233 ReadQueue(outQueue, outBuf, outStream) |
232 | 234 |
233 out = outBuf.getvalue(); | 235 out = outBuf.getvalue(); |
234 error = errorBuf.getvalue(); | 236 error = errorBuf.getvalue(); |
235 if returncode != 0: | 237 if returncode: |
236 if not isinstance(command, basestring): | 238 if not isinstance(command, basestring): |
237 command = ' '.join(command) | 239 command = ' '.join(command) |
238 if printErrorInfo: | 240 if printErrorInfo: |
239 sys.stderr.write("Command failed: '%s'\n" % command) | 241 sys.stderr.write("Command failed: '%s'\n" % command) |
240 sys.stderr.write(" stdout: '%s'\n" % out) | 242 sys.stderr.write(" stdout: '%s'\n" % out) |
241 sys.stderr.write(" stderr: '%s'\n" % error) | 243 sys.stderr.write(" stderr: '%s'\n" % error) |
242 sys.stderr.write(" returncode: %d\n" % returncode) | 244 sys.stderr.write(" returncode: %d\n" % returncode) |
243 raise Exception("Command failed: %s" % command) | 245 raise Error("Command failed: %s" % command) |
244 if debug: | 246 if debug: |
245 sys.stderr.write("output: %s\n" % out) | 247 sys.stderr.write("output: %s\n" % out) |
246 return out | 248 return out |
247 | 249 |
248 | 250 |
249 def Main(argv): | 251 def Main(argv): |
250 print "GuessOS() -> ", GuessOS() | 252 print "GuessOS() -> ", GuessOS() |
251 print "GuessArchitecture() -> ", GuessArchitecture() | 253 print "GuessArchitecture() -> ", GuessArchitecture() |
252 print "GuessCpus() -> ", GuessCpus() | 254 print "GuessCpus() -> ", GuessCpus() |
253 print "IsWindows() -> ", IsWindows() | 255 print "IsWindows() -> ", IsWindows() |
254 | 256 |
255 | 257 |
| 258 class Error(Exception): |
| 259 pass |
| 260 |
| 261 |
256 if __name__ == "__main__": | 262 if __name__ == "__main__": |
257 import sys | 263 import sys |
258 Main(sys.argv) | 264 Main(sys.argv) |
OLD | NEW |