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 |
| 151 Args: |
| 152 command: list of the command and its arguments. |
| 153 input: optional string of input to feed to the command, it should be |
| 154 short enough to fit in an i/o pipe buffer. |
| 155 pollFn: if present will be called occasionally to check if the command |
| 156 should be finished early. If pollFn() returns true then the command |
| 157 will finish early. |
| 158 outStream: if present, the stdout output of the command will be written to |
| 159 outStream. |
| 160 errStream: if present, the stderr output of the command will be written to |
| 161 errStream. |
| 162 killOnEarlyReturn: if true and pollFn returns true, then the subprocess will |
| 163 be killed, otherwise the subprocess will be detached. |
| 164 verbose: if true, the command is echoed to stderr. |
| 165 debug: if true, prints debugging information to stderr. |
| 166 printErrorInfo: if true, prints error information when the subprocess |
| 167 returns a non-zero exit code. |
| 168 Returns: the output of the subprocess. |
| 169 |
| 170 Raises an exception if the subprocess returns an error code. |
| 171 """ |
| 172 if verbose: |
| 173 sys.stderr.write("command %s\n" % command) |
| 174 stdin = None |
| 175 if input is not None: |
| 176 stdin = subprocess.PIPE |
| 177 try: |
| 178 process = subprocess.Popen(args=command, |
| 179 stdin=stdin, |
| 180 bufsize=1, |
| 181 stdout=subprocess.PIPE, |
| 182 stderr=subprocess.PIPE) |
| 183 except Exception as e: |
| 184 if not isinstance(command, basestring): |
| 185 command = ' '.join(command) |
| 186 if printErrorInfo: |
| 187 sys.stderr.write("Command failed: '%s'\n" % command) |
| 188 raise e |
| 189 |
| 190 def StartThread(out): |
| 191 queue = Queue.Queue() |
| 192 def EnqueueOutput(out, queue): |
| 193 for line in iter(out.readline, b''): |
| 194 queue.put(line) |
| 195 out.close() |
| 196 thread = threading.Thread(target=EnqueueOutput, args=(out, queue)) |
| 197 thread.daemon = True |
| 198 thread.start() |
| 199 return queue |
| 200 outQueue = StartThread(process.stdout) |
| 201 errQueue = StartThread(process.stderr) |
| 202 |
| 203 def ReadQueue(queue, out, out2): |
| 204 try: |
| 205 while True: |
| 206 line = queue.get(False) |
| 207 out.write(line) |
| 208 if out2 != None: |
| 209 out2.write(line) |
| 210 except Queue.Empty: |
| 211 pass |
| 212 |
| 213 outBuf = StringIO.StringIO() |
| 214 errorBuf = StringIO.StringIO() |
| 215 if input != None: |
| 216 process.stdin.write(input) |
| 217 while True: |
| 218 returncode = process.poll() |
| 219 if returncode != None: |
| 220 break |
| 221 ReadQueue(errQueue, errorBuf, errStream) |
| 222 ReadQueue(outQueue, outBuf, outStream) |
| 223 if pollFn != None and pollFn(): |
| 224 returncode = 0 |
| 225 if killOnEarlyReturn: |
| 226 process.kill() |
| 227 break |
| 228 time.sleep(0.1) |
| 229 # Drain queue |
| 230 ReadQueue(errQueue, errorBuf, errStream) |
| 231 ReadQueue(outQueue, outBuf, outStream) |
| 232 |
| 233 out = outBuf.getvalue(); |
| 234 error = errorBuf.getvalue(); |
| 235 if returncode != 0: |
| 236 if not isinstance(command, basestring): |
| 237 command = ' '.join(command) |
| 238 if printErrorInfo: |
| 239 sys.stderr.write("Command failed: '%s'\n" % command) |
| 240 sys.stderr.write(" stdout: '%s'\n" % out) |
| 241 sys.stderr.write(" stderr: '%s'\n" % error) |
| 242 sys.stderr.write(" returncode: %d\n" % returncode) |
| 243 raise Exception("Command failed: %s" % command) |
| 244 if debug: |
| 245 sys.stderr.write("output: %s\n" % out) |
| 246 return out |
137 | 247 |
138 | 248 |
139 def Main(argv): | 249 def Main(argv): |
140 print "GuessOS() -> ", GuessOS() | 250 print "GuessOS() -> ", GuessOS() |
141 print "GuessArchitecture() -> ", GuessArchitecture() | 251 print "GuessArchitecture() -> ", GuessArchitecture() |
142 print "GuessCpus() -> ", GuessCpus() | 252 print "GuessCpus() -> ", GuessCpus() |
143 print "IsWindows() -> ", IsWindows() | 253 print "IsWindows() -> ", IsWindows() |
144 | 254 |
145 | 255 |
146 if __name__ == "__main__": | 256 if __name__ == "__main__": |
147 import sys | 257 import sys |
148 Main(sys.argv) | 258 Main(sys.argv) |
OLD | NEW |