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

Side by Side Diff: tools/utils.py

Issue 350483003: Build Tools Cleanup (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: more fixes as reviewed by ricow Created 6 years, 5 months 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
« no previous file with comments | « tools/testing/webdriver_test_setup.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 # Copyright (c) 2014, 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
11 import re 11 import re
12 import shutil 12 import shutil
13 import subprocess 13 import subprocess
14 import tempfile
14 import sys 15 import sys
15 import tempfile
16 16
17 class Version(object): 17 class Version(object):
18 def __init__(self, channel, major, minor, patch, prerelease, 18 def __init__(self, channel, major, minor, patch, prerelease,
19 prerelease_patch): 19 prerelease_patch):
20 self.channel = channel 20 self.channel = channel
21 self.major = major 21 self.major = major
22 self.minor = minor 22 self.minor = minor
23 self.patch = patch 23 self.patch = patch
24 self.prerelease = prerelease 24 self.prerelease = prerelease
25 self.prerelease_patch = prerelease_patch 25 self.prerelease_patch = prerelease_patch
26 26
27 # Try to guess the host operating system. 27 # Try to guess the host operating system.
28 def GuessOS(): 28 def GuessOS():
29 id = platform.system() 29 os_id = platform.system()
30 if id == "Linux": 30 if os_id == "Linux":
31 return "linux" 31 return "linux"
32 elif id == "Darwin": 32 elif os_id == "Darwin":
33 return "macos" 33 return "macos"
34 elif id == "Windows" or id == "Microsoft": 34 elif os_id == "Windows" or os_id == "Microsoft":
35 # On Windows Vista platform.system() can return "Microsoft" with some 35 # On Windows Vista platform.system() can return "Microsoft" with some
36 # versions of Python, see http://bugs.python.org/issue1082 for details. 36 # versions of Python, see http://bugs.python.org/issue1082 for details.
37 return "win32" 37 return "win32"
38 elif id == 'FreeBSD': 38 elif os_id == 'FreeBSD':
39 return 'freebsd' 39 return 'freebsd'
40 elif id == 'OpenBSD': 40 elif os_id == 'OpenBSD':
41 return 'openbsd' 41 return 'openbsd'
42 elif id == 'SunOS': 42 elif os_id == 'SunOS':
43 return 'solaris' 43 return 'solaris'
44 else: 44 else:
45 return None 45 return None
46 46
47 47
48 # Try to guess the host architecture. 48 # Try to guess the host architecture.
49 def GuessArchitecture(): 49 def GuessArchitecture():
50 id = platform.machine() 50 os_id = platform.machine()
51 if id.startswith('arm'): 51 if os_id.startswith('arm'):
52 return 'arm' 52 return 'arm'
53 elif id.startswith('aarch64'): 53 elif os_id.startswith('aarch64'):
54 return 'arm64' 54 return 'arm64'
55 elif id.startswith('mips'): 55 elif os_id.startswith('mips'):
56 return 'mips' 56 return 'mips'
57 elif (not id) or (not re.match('(x|i[3-6])86', id) is None): 57 elif (not os_id) or (not re.match('(x|i[3-6])86', os_id) is None):
58 return 'ia32' 58 return 'ia32'
59 elif id == 'i86pc': 59 elif os_id == 'i86pc':
60 return 'ia32' 60 return 'ia32'
61 elif '64' in id: 61 elif '64' in os_id:
62 return 'x64' 62 return 'x64'
63 else: 63 else:
64 guess_os = GuessOS() 64 guess_os = GuessOS()
65 print "Warning: Guessing architecture %s based on os %s\n" % (id, guess_os) 65 print "Warning: Guessing architecture %s based on os %s\n"\
66 % (os_id, guess_os)
66 if guess_os == 'win32': 67 if guess_os == 'win32':
67 return 'ia32' 68 return 'ia32'
68 return None 69 return None
69 70
70 71
71 # Try to guess the number of cpus on this machine. 72 # Try to guess the number of cpus on this machine.
72 def GuessCpus(): 73 def GuessCpus():
73 if os.path.exists("/proc/cpuinfo"): 74 if os.path.exists("/proc/cpuinfo"):
74 return int(commands.getoutput("grep -E '^processor' /proc/cpuinfo | wc -l")) 75 return int(commands.getoutput("grep -E '^processor' /proc/cpuinfo | wc -l"))
75 if os.path.exists("/usr/bin/hostinfo"): 76 if os.path.exists("/usr/bin/hostinfo"):
(...skipping 14 matching lines...) Expand all
90 wow6432Node = '' 91 wow6432Node = ''
91 return r'SOFTWARE\%s%s' % (wow6432Node, name) 92 return r'SOFTWARE\%s%s' % (wow6432Node, name)
92 93
93 # Try to guess Visual Studio location when buiding on Windows. 94 # Try to guess Visual Studio location when buiding on Windows.
94 def GuessVisualStudioPath(): 95 def GuessVisualStudioPath():
95 defaultPath = r"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7" \ 96 defaultPath = r"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7" \
96 r"\IDE" 97 r"\IDE"
97 defaultExecutable = "devenv.com" 98 defaultExecutable = "devenv.com"
98 99
99 if not IsWindows(): 100 if not IsWindows():
100 return (defaultPath, defaultExecutable) 101 return defaultPath, defaultExecutable
101 102
102 keyNamesAndExecutables = [ 103 keyNamesAndExecutables = [
103 # Pair for non-Express editions. 104 # Pair for non-Express editions.
104 (GetWindowsRegistryKeyName(r'Microsoft\VisualStudio'), 'devenv.com'), 105 (GetWindowsRegistryKeyName(r'Microsoft\VisualStudio'), 'devenv.com'),
105 # Pair for 2012 Express edition. 106 # Pair for 2012 Express edition.
106 (GetWindowsRegistryKeyName(r'Microsoft\VSWinExpress'), 'VSWinExpress.exe'), 107 (GetWindowsRegistryKeyName(r'Microsoft\VSWinExpress'), 'VSWinExpress.exe'),
107 # Pair for pre-2012 Express editions. 108 # Pair for pre-2012 Express editions.
108 (GetWindowsRegistryKeyName(r'Microsoft\VCExpress'), 'VCExpress.exe')] 109 (GetWindowsRegistryKeyName(r'Microsoft\VCExpress'), 'VCExpress.exe')]
109 110
110 bestGuess = (0.0, (defaultPath, defaultExecutable)) 111 bestGuess = (0.0, (defaultPath, defaultExecutable))
111 112
112 import _winreg 113 import _winreg
113 for (keyName, executable) in keyNamesAndExecutables: 114 for (keyName, executable) in keyNamesAndExecutables:
114 try: 115 try:
115 key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, keyName) 116 key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, keyName)
116 except WindowsError: 117 except WindowsError:
117 # Can't find this key - moving on the next one. 118 # Can't find this key - moving on the next one.
118 continue 119 continue
119 120
120 try: 121 try:
121 subkeyCounter = 0 122 subkeyCounter = 0
122 while True: 123 while True:
123 try: 124 try:
124 subkeyName = _winreg.EnumKey(key, subkeyCounter) 125 subkeyName = _winreg.EnumKey(key, subkeyCounter)
125 subkeyCounter = subkeyCounter + 1 126 subkeyCounter += 1
126 except WindowsError: 127 except WindowsError:
127 # Reached end of enumeration. Moving on the next key. 128 # Reached end of enumeration. Moving on the next key.
128 break 129 break
129 130
130 match = re.match(r'^\d+\.\d+$', subkeyName) 131 match = re.match(r'^\d+\.\d+$', subkeyName)
131 if match: 132 if match:
132 with _winreg.OpenKey(key, subkeyName) as subkey: 133 with _winreg.OpenKey(key, subkeyName) as subkey:
133 try: 134 try:
134 (installDir, registrytype) = _winreg.QueryValueEx(subkey, 135 (installDir, registrytype) = _winreg.QueryValueEx(subkey,
135 'InstallDir') 136 'InstallDir')
136 except WindowsError: 137 except WindowsError:
137 # Can't find value under the key - continue to the next key. 138 # Can't find value under the key - continue to the next key.
138 continue 139 continue
139 isExpress = executable != 'devenv.com' 140 isExpress = executable != 'devenv.com'
140 if not isExpress and subkeyName == '10.0': 141 if not isExpress and subkeyName == '10.0':
141 # Stop search since if we found non-Express VS2010 version 142 # Stop search since if we found non-Express VS2010 version
142 # installed, which is preferred version. 143 # installed, which is preferred version.
143 return (installDir, executable) 144 return installDir, executable
144 else: 145 else:
145 version = float(subkeyName) 146 version = float(subkeyName)
146 # We prefer higher version of Visual Studio and given equal 147 # We prefer higher version of Visual Studio and given equal
147 # version numbers we prefer non-Express edition. 148 # version numbers we prefer non-Express edition.
148 if version > bestGuess[0]: 149 if version > bestGuess[0]:
149 bestGuess = (version, (installDir, executable)) 150 bestGuess = (version, (installDir, executable))
150 finally: 151 finally:
151 _winreg.CloseKey(key) 152 _winreg.CloseKey(key)
152 return bestGuess[1] 153 return bestGuess[1]
153 154
(...skipping 11 matching lines...) Expand all
165 if '#' in line: 166 if '#' in line:
166 line = line[:line.find('#')] 167 line = line[:line.find('#')]
167 line = line.strip() 168 line = line.strip()
168 if len(line) == 0: 169 if len(line) == 0:
169 continue 170 continue
170 result.append(line) 171 result.append(line)
171 return result 172 return result
172 173
173 # Filters out all arguments until the next '--' argument 174 # Filters out all arguments until the next '--' argument
174 # occurs. 175 # occurs.
175 def ListArgCallback(option, opt_str, value, parser): 176 def ListArgCallback(option, value, parser):
176 if value is None: 177 if value is None:
177 value = [] 178 value = []
178 179
179 for arg in parser.rargs: 180 for arg in parser.rargs:
180 if arg[:2].startswith('--'): 181 if arg[:2].startswith('--'):
181 break 182 break
182 value.append(arg) 183 value.append(arg)
183 184
184 del parser.rargs[:len(value)] 185 del parser.rargs[:len(value)]
185 setattr(parser.values, option.dest, value) 186 setattr(parser.values, option.dest, value)
186 187
187 188
188 # Filters out all argument until the first non '-' or the 189 # Filters out all argument until the first non '-' or the
189 # '--' argument occurs. 190 # '--' argument occurs.
190 def ListDartArgCallback(option, opt_str, value, parser): 191 def ListDartArgCallback(option, value, parser):
191 if value is None: 192 if value is None:
192 value = [] 193 value = []
193 194
194 for arg in parser.rargs: 195 for arg in parser.rargs:
195 if arg[:2].startswith('--') or arg[0] != '-': 196 if arg[:2].startswith('--') or arg[0] != '-':
196 break 197 break
197 value.append(arg) 198 value.append(arg)
198 199
199 del parser.rargs[:len(value)] 200 del parser.rargs[:len(value)]
200 setattr(parser.values, option.dest, value) 201 setattr(parser.values, option.dest, value)
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 return '%s%s%s' % (GetBuildMode(mode), conf_os.title(), arch.upper()) 248 return '%s%s%s' % (GetBuildMode(mode), conf_os.title(), arch.upper())
248 else: 249 else:
249 # Ask for a cross build if the host and target architectures don't match. 250 # Ask for a cross build if the host and target architectures don't match.
250 host_arch = ARCH_GUESS 251 host_arch = ARCH_GUESS
251 cross_build = '' 252 cross_build = ''
252 if GetArchFamily(host_arch) != GetArchFamily(arch): 253 if GetArchFamily(host_arch) != GetArchFamily(arch):
253 print "GetBuildConf: Cross-build of %s on %s\n" % (arch, host_arch) 254 print "GetBuildConf: Cross-build of %s on %s\n" % (arch, host_arch)
254 cross_build = 'X' 255 cross_build = 'X'
255 return '%s%s%s' % (GetBuildMode(mode), cross_build, arch.upper()) 256 return '%s%s%s' % (GetBuildMode(mode), cross_build, arch.upper())
256 257
257 def GetBuildDir(host_os, target_os): 258 def GetBuildDir(host_os):
258 return BUILD_ROOT[host_os] 259 return BUILD_ROOT[host_os]
259 260
260 def GetBuildRoot(host_os, mode=None, arch=None, target_os=None): 261 def GetBuildRoot(host_os, mode=None, arch=None):
261 build_root = GetBuildDir(host_os, target_os) 262 build_root = GetBuildDir(host_os)
262 if mode: 263 if mode:
263 build_root = os.path.join(build_root, GetBuildConf(mode, arch)) 264 build_root = os.path.join(build_root, GetBuildConf(mode, arch))
264 return build_root 265 return build_root
265 266
266 def GetBaseDir(): 267 def GetBaseDir():
267 return BASE_DIR 268 return BASE_DIR
268 269
269 def GetShortVersion(): 270 def GetShortVersion():
270 version = ReadVersionFile() 271 version = ReadVersionFile()
271 return ('%s.%s.%s.%s.%s' % ( 272 return ('%s.%s.%s.%s.%s' % (
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 version = ReadVersionFile() 309 version = ReadVersionFile()
309 return version.channel 310 return version.channel
310 311
311 def GetUserName(): 312 def GetUserName():
312 key = 'USER' 313 key = 'USER'
313 if sys.platform == 'win32': 314 if sys.platform == 'win32':
314 key = 'USERNAME' 315 key = 'USERNAME'
315 return os.environ.get(key, '') 316 return os.environ.get(key, '')
316 317
317 def ReadVersionFile(): 318 def ReadVersionFile():
318 def match_against(pattern, content): 319 def match_against(pattern, file_content):
319 match = re.search(pattern, content, flags=re.MULTILINE) 320 match = re.search(pattern, file_content, flags=re.MULTILINE)
320 if match: 321 if match:
321 return match.group(1) 322 return match.group(1)
322 return None 323 return None
323 324
324 version_file = os.path.join(DART_DIR, 'tools', 'VERSION') 325 version_file = os.path.join(DART_DIR, 'tools', 'VERSION')
325 try: 326 try:
326 fd = open(version_file) 327 fd = open(version_file)
327 content = fd.read() 328 content = fd.read()
328 fd.close() 329 fd.close()
329 except: 330 except:
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 def ParseSvnInfoOutput(output): 404 def ParseSvnInfoOutput(output):
404 revision_match = re.search('Last Changed Rev: (\d+)', output) 405 revision_match = re.search('Last Changed Rev: (\d+)', output)
405 if revision_match: 406 if revision_match:
406 return revision_match.group(1) 407 return revision_match.group(1)
407 return None 408 return None
408 409
409 def RewritePathSeparator(path, workspace): 410 def RewritePathSeparator(path, workspace):
410 # Paths in test files are always specified using '/' 411 # Paths in test files are always specified using '/'
411 # as the path separator. Replace with the actual 412 # as the path separator. Replace with the actual
412 # path separator before use. 413 # path separator before use.
413 if ('/' in path): 414 if '/' in path:
414 split_path = path.split('/') 415 split_path = path.split('/')
415 path = os.sep.join(split_path) 416 path = os.sep.join(split_path)
416 path = os.path.join(workspace, path) 417 path = os.path.join(workspace, path)
417 if not os.path.exists(path): 418 if not os.path.exists(path):
418 raise Exception(path) 419 raise Exception(path)
419 return path 420 return path
420 421
421 422
422 def ParseTestOptions(pattern, source, workspace): 423 def ParseTestOptions(pattern, source, workspace):
423 match = pattern.search(source) 424 match = pattern.search(source)
424 if match: 425 if match:
425 return [RewritePathSeparator(o, workspace) for o in match.group(1).split(' ' )] 426 return [RewritePathSeparator(o, workspace) for o in match.group(1).split(' ' )]
426 else: 427 else:
427 return None 428 return None
428 429
429 430
430 def ParseTestOptionsMultiple(pattern, source, workspace): 431 def ParseTestOptionsMultiple(pattern, source, workspace):
431 matches = pattern.findall(source) 432 matches = pattern.findall(source)
432 if matches: 433 if matches:
433 result = [] 434 result = []
434 for match in matches: 435 for match in matches:
435 if len(match) > 0: 436 if len(match) > 0:
436 result.append( 437 result.append(
437 [RewritePathSeparator(o, workspace) for o in match.split(' ')]); 438 [RewritePathSeparator(o, workspace) for o in match.split(' ')])
438 else: 439 else:
439 result.append([]) 440 result.append([])
440 return result 441 return result
441 else: 442 else:
442 return None 443 return None
443 444
444 445
445 def ConfigureJava(): 446 def ConfigureJava():
446 java_home = '/usr/libexec/java_home' 447 java_home = '/usr/libexec/java_home'
447 if os.path.exists(java_home): 448 if os.path.exists(java_home):
448 proc = subprocess.Popen([java_home, '-v', '1.6+'], 449 proc = subprocess.Popen([java_home, '-v', '1.6+'],
449 stdout=subprocess.PIPE, 450 stdout=subprocess.PIPE,
450 stderr=subprocess.PIPE) 451 stderr=subprocess.PIPE)
451 (stdout, stderr) = proc.communicate() 452 (stdout, stderr) = proc.communicate()
452 if proc.wait() != 0: 453 if proc.wait() != 0:
453 return None 454 return None
454 new = stdout.strip() 455 new = stdout.strip()
455 current = os.getenv('JAVA_HOME', default=new) 456 current = os.getenv('JAVA_HOME', new)
456 if current != new: 457 if current != new:
457 sys.stderr.write('Please set JAVA_HOME to %s\n' % new) 458 sys.stderr.write('Please set JAVA_HOME to %s\n' % new)
458 os.putenv('JAVA_HOME', new) 459 os.putenv('JAVA_HOME', new)
459 460
460 461
461 def Daemonize(): 462 def Daemonize():
462 """ 463 """
463 Create a detached background process (daemon). Returns True for 464 Create a detached background process (daemon). Returns True for
464 the daemon, False for the parent process. 465 the daemon, False for the parent process.
465 See: http://www.faqs.org/faqs/unix-faq/programmer/faq/ 466 See: http://www.faqs.org/faqs/unix-faq/programmer/faq/
466 "1.7 How do I get my program to act like a daemon?" 467 "1.7 How do I get my program to act like a daemon?"
467 """ 468 """
468 if os.fork() > 0: 469 if os.fork() > 0:
469 return False 470 return False
470 os.setsid() 471 os.setsid()
471 if os.fork() > 0: 472 if os.fork() > 0:
472 os._exit(0) 473 exit(0)
473 raise 474 raise
474 return True 475 return True
475 476
476 477
477 def PrintError(str): 478 def PrintError(string):
478 """Writes and flushes a string to stderr.""" 479 """Writes and flushes a string to stderr."""
479 sys.stderr.write(str) 480 sys.stderr.write(string)
480 sys.stderr.write('\n') 481 sys.stderr.write('\n')
481 482
482 483
483 def CheckedUnlink(name): 484 def CheckedUnlink(name):
484 """Unlink a file without throwing an exception.""" 485 """Unlink a file without throwing an exception."""
485 try: 486 try:
486 os.unlink(name) 487 os.unlink(name)
487 except OSError, e: 488 except OSError, e:
488 PrintError("os.unlink() " + str(e)) 489 PrintError("os.unlink() " + str(e))
489 490
490 491
491 def Main(argv): 492 def Main():
492 print "GuessOS() -> ", GuessOS() 493 print "GuessOS() -> ", GuessOS()
493 print "GuessArchitecture() -> ", GuessArchitecture() 494 print "GuessArchitecture() -> ", GuessArchitecture()
494 print "GuessCpus() -> ", GuessCpus() 495 print "GuessCpus() -> ", GuessCpus()
495 print "IsWindows() -> ", IsWindows() 496 print "IsWindows() -> ", IsWindows()
496 print "GuessVisualStudioPath() -> ", GuessVisualStudioPath() 497 print "GuessVisualStudioPath() -> ", GuessVisualStudioPath()
497 498
498 499
499 class Error(Exception): 500 class Error(Exception):
500 pass 501 pass
501 502
(...skipping 27 matching lines...) Expand all
529 530
530 531
531 def ExecuteCommand(cmd): 532 def ExecuteCommand(cmd):
532 """Execute a command in a subprocess.""" 533 """Execute a command in a subprocess."""
533 print 'Executing: ' + ' '.join(cmd) 534 print 'Executing: ' + ' '.join(cmd)
534 pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, 535 pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
535 shell=IsWindows()) 536 shell=IsWindows())
536 output = pipe.communicate() 537 output = pipe.communicate()
537 if pipe.returncode != 0: 538 if pipe.returncode != 0:
538 raise Exception('Execution failed: ' + str(output)) 539 raise Exception('Execution failed: ' + str(output))
539 return (pipe.returncode, output) 540 return pipe.returncode, output
540 541
541 542
542 def DartBinary(): 543 def DartBinary():
543 tools_dir = os.path.dirname(os.path.realpath(__file__)) 544 tools_dir = os.path.dirname(os.path.realpath(__file__))
544 dart_binary_prefix = os.path.join(tools_dir, 'testing', 'bin') 545 dart_binary_prefix = os.path.join(tools_dir, 'testing', 'bin')
545 if IsWindows(): 546 if IsWindows():
546 return os.path.join(dart_binary_prefix, 'windows', 'dart.exe') 547 return os.path.join(dart_binary_prefix, 'windows', 'dart.exe')
547 else: 548 else:
548 arch = GuessArchitecture() 549 arch = GuessArchitecture()
549 system = GuessOS() 550 system = GuessOS()
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
583 print "Enter directory = ", self._working_directory 584 print "Enter directory = ", self._working_directory
584 os.chdir(self._working_directory) 585 os.chdir(self._working_directory)
585 586
586 def __exit__(self, *_): 587 def __exit__(self, *_):
587 print "Enter directory = ", self._old_cwd 588 print "Enter directory = ", self._old_cwd
588 os.chdir(self._old_cwd) 589 os.chdir(self._old_cwd)
589 590
590 591
591 if __name__ == "__main__": 592 if __name__ == "__main__":
592 import sys 593 import sys
593 Main(sys.argv) 594 Main()
OLDNEW
« no previous file with comments | « tools/testing/webdriver_test_setup.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698