| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2010 Google Inc. All Rights Reserved. |
| 2 |
| 3 # This file contains a set of utilities functions used |
| 4 # by both SConstruct and other Python-based scripts. |
| 5 |
| 6 import commands |
| 7 import os |
| 8 import platform |
| 9 import re |
| 10 import subprocess |
| 11 |
| 12 class ChangedWorkingDirectory(object): |
| 13 def __init__(self, new_dir): |
| 14 self._new_dir = new_dir |
| 15 |
| 16 def __enter__(self): |
| 17 self._old_dir = os.getcwd() |
| 18 os.chdir(self._new_dir) |
| 19 return self._new_dir |
| 20 |
| 21 def __exit__(self, *_): |
| 22 os.chdir(self._old_dir) |
| 23 |
| 24 # Try to guess the host operating system. |
| 25 def guessOS(): |
| 26 id = platform.system() |
| 27 if id == "Linux": |
| 28 return "linux" |
| 29 elif id == "Darwin": |
| 30 return "mac" |
| 31 elif id == "Windows" or id == "Microsoft": |
| 32 # On Windows Vista platform.system() can return "Microsoft" with some |
| 33 # versions of Python, see http://bugs.python.org/issue1082 for details. |
| 34 return "win" |
| 35 else: |
| 36 return None |
| 37 |
| 38 |
| 39 # Try to guess the host architecture. |
| 40 def guessArchitecture(): |
| 41 id = platform.machine() |
| 42 if id.startswith('arm'): |
| 43 return 'arm' |
| 44 elif (not id) or (not re.match('(x|i[3-6])86', id) is None): |
| 45 return 'x86' |
| 46 elif id == 'i86pc': |
| 47 return 'x86' |
| 48 else: |
| 49 return None |
| 50 |
| 51 |
| 52 # Try to guess the number of cpus on this machine. |
| 53 def guessCpus(): |
| 54 if os.path.exists("/proc/cpuinfo"): |
| 55 return int(commands.getoutput("grep -E '^processor' /proc/cpuinfo | wc -l")) |
| 56 if os.path.exists("/usr/bin/hostinfo"): |
| 57 return int(commands.getoutput('/usr/bin/hostinfo | grep "processors are logi
cally available." | awk "{ print \$1 }"')) |
| 58 win_cpu_count = os.getenv("NUMBER_OF_PROCESSORS") |
| 59 if win_cpu_count: |
| 60 return int(win_cpu_count) |
| 61 return int(os.getenv("PARFAIT_NUMBER_OF_CORES", 2)) |
| 62 |
| 63 |
| 64 # Returns true if we're running under Windows. |
| 65 def isWindows(): |
| 66 return guessOS() == 'win32' |
| 67 |
| 68 # Reads a text file into an array of strings - one for each |
| 69 # line. Strips comments in the process. |
| 70 def readLinesFrom(name): |
| 71 result = [] |
| 72 for line in open(name): |
| 73 if '#' in line: |
| 74 line = line[:line.find('#')] |
| 75 line = line.strip() |
| 76 if len(line) == 0: |
| 77 continue |
| 78 result.append(line) |
| 79 return result |
| 80 |
| 81 def listArgCallback(option, opt_str, value, parser): |
| 82 if value is None: |
| 83 value = [] |
| 84 |
| 85 for arg in parser.rargs: |
| 86 if arg[:2].startswith('--'): |
| 87 break |
| 88 value.append(arg) |
| 89 |
| 90 del parser.rargs[:len(value)] |
| 91 setattr(parser.values, option.dest, value) |
| 92 |
| 93 |
| 94 def getCommandOutput(cmd): |
| 95 print cmd |
| 96 pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 97 output = pipe.communicate() |
| 98 if pipe.returncode == 0: |
| 99 return output[0] |
| 100 else: |
| 101 print output[1] |
| 102 raise Exception('Failed to run command. return code=%s' % pipe.returncode) |
| 103 |
| 104 def runCommand(cmd, env_update=None): |
| 105 if env_update is None: |
| 106 env_update = {} |
| 107 print 'Running: ' + ' '.join(["%s='%s'" % (k, v) for k, v in env_update.iterit
ems()]) + ' ' + ' '.join(cmd) |
| 108 env_copy = dict(os.environ.items()) |
| 109 env_copy.update(env_update) |
| 110 p = subprocess.Popen(cmd, env=env_copy) |
| 111 if p.wait() != 0: |
| 112 raise Exception('Failed to run command. return code=%s' % p.returncode) |
| 113 |
| 114 def main(argv): |
| 115 print "GuessOS() -> ", guessOS() |
| 116 print "GuessArchitecture() -> ", guessArchitecture() |
| 117 print "GuessCpus() -> ", guessCpus() |
| 118 print "IsWindows() -> ", isWindows() |
| 119 |
| 120 |
| 121 if __name__ == "__main__": |
| 122 import sys |
| 123 main(sys.argv) |
| OLD | NEW |