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 | |
9 import os | |
8 import platform | 10 import platform |
11 import Queue | |
9 import re | 12 import re |
10 import os | 13 import StringIO |
11 import commands | 14 import subprocess |
15 import sys | |
16 import threading | |
17 import time | |
12 | 18 |
13 | 19 |
14 # Try to guess the host operating system. | 20 # Try to guess the host operating system. |
15 def GuessOS(): | 21 def GuessOS(): |
16 id = platform.system() | 22 id = platform.system() |
17 if id == "Linux": | 23 if id == "Linux": |
18 return "linux" | 24 return "linux" |
19 elif id == "Darwin": | 25 elif id == "Darwin": |
20 return "macos" | 26 return "macos" |
21 elif id == "Windows" or id == "Microsoft": | 27 elif id == "Windows" or id == "Microsoft": |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
121 | 127 |
122 def GetBuildMode(mode): | 128 def GetBuildMode(mode): |
123 global BUILD_MODES | 129 global BUILD_MODES |
124 return BUILD_MODES[mode] | 130 return BUILD_MODES[mode] |
125 | 131 |
126 | 132 |
127 def GetBuildConf(mode, arch): | 133 def GetBuildConf(mode, arch): |
128 return GetBuildMode(mode) + arch.upper() | 134 return GetBuildMode(mode) + arch.upper() |
129 | 135 |
130 | 136 |
131 def GetBuildRoot(target_os, mode=None, arch=None): | 137 def GetBuildRoot(host_os, mode=None, arch=None): |
132 global BUILD_ROOT | 138 global BUILD_ROOT |
133 if mode: | 139 if mode: |
134 return os.path.join(BUILD_ROOT[target_os], GetBuildConf(mode, arch)) | 140 return os.path.join(BUILD_ROOT[host_os], GetBuildConf(mode, arch)) |
135 else: | 141 else: |
136 return BUILD_ROOT[target_os] | 142 return BUILD_ROOT[host_os] |
143 | |
144 | |
145 def RunCommand(command, input=None, pollFn=None, outStream=None, errStream=None, | |
146 killOnEarlyReturn=True, verbose=False, debug=False, | |
147 printErrorInfo=False): | |
148 """ | |
149 Run a command, with optional input and polling function. | |
150 command is a list of the command and its arguments. | |
151 Input is an optional string of input to feed to the command, it should be | |
152 short enough to fit in an i/o pipe buffer. | |
153 The pollFn is called occasionally to check if the command should be finished | |
Emily Fortuna
2012/08/28 16:49:14
Comment formatting:
"""
Function Description.
Ar
jackpal
2012/08/28 20:15:20
Done.
| |
154 early. | |
155 If pollFn returns True then the command will finish early. | |
156 If outStream is not None, the stdout output of the command will be written to | |
157 outStream. | |
158 If errStream is not None, the stderr output of the command will be written to | |
159 errStream. | |
160 If pollFn returns True and killOnEarlyReturn is True, then the subprocess | |
161 will be killed, otherwise the subprocess will be detached. | |
162 If verbose is True, echos command arguments. | |
163 If debug is True, prints debugging information. | |
164 If printErrorInfo is True, prints error information when the command returns a | |
165 non-zero exit code. | |
166 Returns the output of the command. | |
167 Raises an exception if the command returns an error code. | |
168 """ | |
169 if verbose: | |
170 sys.stderr.write("command %s\n" % command) | |
171 stdin = None | |
172 if input is not None: | |
173 stdin = subprocess.PIPE | |
174 try: | |
175 process = subprocess.Popen(args=command, | |
176 stdin=stdin, | |
177 bufsize=1, | |
178 stdout=subprocess.PIPE, | |
179 stderr=subprocess.PIPE) | |
180 except Exception as e: | |
181 if not isinstance(command, basestring): | |
182 command = ' '.join(command) | |
183 if printErrorInfo: | |
184 sys.stderr.write("Command failed: '%s'\n" % command) | |
185 raise e | |
186 | |
187 def StartThread(out): | |
188 queue = Queue.Queue() | |
189 def EnqueueOutput(out, queue): | |
190 for line in iter(out.readline, b''): | |
191 queue.put(line) | |
192 out.close() | |
193 thread = threading.Thread(target=EnqueueOutput, args=(out, queue)) | |
194 thread.daemon = True | |
195 thread.start() | |
196 return queue | |
197 outQueue = StartThread(process.stdout) | |
198 errQueue = StartThread(process.stderr) | |
199 | |
200 def ReadQueue(queue, out, out2): | |
201 try: | |
202 while True: | |
203 line = queue.get(False) | |
204 out.write(line) | |
205 if out2 != None: | |
206 out2.write(line) | |
207 except Queue.Empty: | |
208 pass | |
209 | |
210 outBuf = StringIO.StringIO() | |
211 errorBuf = StringIO.StringIO() | |
212 if input != None: | |
213 process.stdin.write(input) | |
214 while True: | |
215 returncode = process.poll() | |
216 if returncode != None: | |
217 break | |
218 ReadQueue(errQueue, errorBuf, errStream) | |
219 ReadQueue(outQueue, outBuf, outStream) | |
220 if pollFn != None and pollFn(): | |
221 returncode = 0 | |
222 if killOnEarlyReturn: | |
223 process.kill() | |
224 break | |
225 time.sleep(0.1) | |
226 # Drain queue | |
227 ReadQueue(errQueue, errorBuf, errStream) | |
228 ReadQueue(outQueue, outBuf, outStream) | |
229 | |
230 out = outBuf.getvalue(); | |
231 error = errorBuf.getvalue(); | |
232 if returncode != 0: | |
233 if not isinstance(command, basestring): | |
234 command = ' '.join(command) | |
235 if printErrorInfo: | |
236 sys.stderr.write("Command failed: '%s'\n" % command) | |
237 sys.stderr.write(" stdout: '%s'\n" % out) | |
238 sys.stderr.write(" stderr: '%s'\n" % error) | |
239 sys.stderr.write(" returncode: %d\n" % returncode) | |
240 raise Exception("Command failed: %s" % command) | |
241 if debug: | |
242 sys.stderr.write("output: %s\n" % out) | |
243 return out | |
137 | 244 |
138 | 245 |
139 def Main(argv): | 246 def Main(argv): |
140 print "GuessOS() -> ", GuessOS() | 247 print "GuessOS() -> ", GuessOS() |
141 print "GuessArchitecture() -> ", GuessArchitecture() | 248 print "GuessArchitecture() -> ", GuessArchitecture() |
142 print "GuessCpus() -> ", GuessCpus() | 249 print "GuessCpus() -> ", GuessCpus() |
143 print "IsWindows() -> ", IsWindows() | 250 print "IsWindows() -> ", IsWindows() |
144 | 251 |
145 | 252 |
146 if __name__ == "__main__": | 253 if __name__ == "__main__": |
147 import sys | 254 import sys |
148 Main(sys.argv) | 255 Main(sys.argv) |
OLD | NEW |