1 # Copyright (c) 2011 Google Inc. All rights reserved. 2 # 3 # Redistribution and use in source and binary forms, with or without 4 # modification, are permitted provided that the following conditions are 5 # met: 6 # 7 # * Redistributions of source code must retain the above copyright 8 # notice, this list of conditions and the following disclaimer. 9 # * Redistributions in binary form must reproduce the above 10 # copyright notice, this list of conditions and the following disclaimer 11 # in the documentation and/or other materials provided with the 12 # distribution. 13 # * Neither the name of Google Inc. nor the names of its 14 # contributors may be used to endorse or promote products derived from 15 # this software without specific prior written permission. 16 # 17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 import re 30 import sys 31 32 33 class PlatformInfo(object): 34 """This class provides a consistent (and mockable) interpretation of 35 system-specific values (like sys.platform and platform.mac_ver()) 36 to be used by the rest of the webkitpy code base. 37 38 Public (static) properties: 39 -- os_name 40 -- os_version 41 42 Note that 'future' is returned for os_version if the operating system is 43 newer than one known to the code. 44 """ 45 46 def __init__(self, sys_module, platform_module, executive): 47 self._executive = executive 48 self._platform_module = platform_module 49 self.os_name = self._determine_os_name(sys_module.platform) 50 if self.os_name == 'linux': 51 self.os_version = self._determine_linux_version() 52 if self.os_name == 'freebsd': 53 self.os_version = platform_module.release() 54 if self.os_name.startswith('mac'): 55 self.os_version = self._determine_mac_version(platform_module.mac_ver()[0]) 56 if self.os_name.startswith('win'): 57 self.os_version = self._determine_win_version(self._win_version_tuple(sys_module)) 58 self._is_cygwin = sys_module.platform == 'cygwin' 59 60 def is_mac(self): 61 return self.os_name == 'mac' 62 63 def is_win(self): 64 return self.os_name == 'win' 65 66 def is_cygwin(self): 67 return self._is_cygwin 68 69 def is_linux(self): 70 return self.os_name == 'linux' 71 72 def is_freebsd(self): 73 return self.os_name == 'freebsd' 74 75 def display_name(self): 76 # platform.platform() returns Darwin information for Mac, which is just confusing. 77 if self.is_mac(): 78 return "Mac OS X %s" % self._platform_module.mac_ver()[0] 79 80 # Returns strings like: 81 # Linux-2.6.18-194.3.1.el5-i686-with-redhat-5.5-Final 82 # Windows-2008ServerR2-6.1.7600 83 return self._platform_module.platform() 84 85 def total_bytes_memory(self): 86 if self.is_mac(): 87 return long(self._executive.run_command(["sysctl", "-n", "hw.memsize"])) 88 return None 89 90 def terminal_width(self): 91 """Returns sys.maxint if the width cannot be determined.""" 92 try: 93 if self.is_win(): 94 # From http://code.activestate.com/recipes/440694-determine-size-of-console-window-on-windows/ 95 from ctypes import windll, create_string_buffer 96 handle = windll.kernel32.GetStdHandle(-12) # -12 == stderr 97 console_screen_buffer_info = create_string_buffer(22) # 22 == sizeof(console_screen_buffer_info) 98 if windll.kernel32.GetConsoleScreenBufferInfo(handle, console_screen_buffer_info): 99 import struct 100 _, _, _, _, _, left, _, right, _, _, _ = struct.unpack("hhhhHhhhhhh", console_screen_buffer_info.raw) 101 # Note that we return 1 less than the width since writing into the rightmost column 102 # automatically performs a line feed. 103 return right - left 104 return sys.maxint 105 else: 106 import fcntl 107 import struct 108 import termios 109 packed = fcntl.ioctl(sys.stderr.fileno(), termios.TIOCGWINSZ, '\0' * 8) 110 _, columns, _, _ = struct.unpack('HHHH', packed) 111 return columns 112 except: 113 return sys.maxint 114 115 def _determine_os_name(self, sys_platform): 116 if sys_platform == 'darwin': 117 return 'mac' 118 if sys_platform.startswith('linux'): 119 return 'linux' 120 if sys_platform in ('win32', 'cygwin'): 121 return 'win' 122 if sys_platform.startswith('freebsd'): 123 return 'freebsd' 124 raise AssertionError('unrecognized platform string "%s"' % sys_platform) 125 126 def _determine_mac_version(self, mac_version_string): 127 release_version = mac_version_string.split('.')[1] 128 version_strings = { 129 '5': 'leopard', 130 '6': 'snowleopard', 131 '7': 'lion', 132 '8': 'mountainlion', 133 } 134 assert release_version >= min(version_strings.keys()) 135 return version_strings.get(release_version, 'future') 136 137 def _determine_linux_version(self): 138 # FIXME: we ignore whatever the real version is and pretend it's lucid for now. 139 return 'lucid' 140 141 def _determine_win_version(self, win_version_tuple): 142 if win_version_tuple[:3] == (6, 1, 7600): 143 return '7sp0' 144 if win_version_tuple[:2] == (6, 0): 145 return 'vista' 146 if win_version_tuple[:2] == (5, 1): 147 return 'xp' 148 assert win_version_tuple[0] > 6 or win_version_tuple[1] >= 1, 'Unrecognized Windows version tuple: "%s"' % (win_version_tuple,) 149 return 'future' 150 151 def _win_version_tuple(self, sys_module): 152 if hasattr(sys_module, 'getwindowsversion'): 153 return sys_module.getwindowsversion() 154 return self._win_version_tuple_from_cmd() 155 156 def _win_version_tuple_from_cmd(self): 157 # Note that this should only ever be called on windows, so this should always work. 158 ver_output = self._executive.run_command(['cmd', '/c', 'ver'], decode_output=False) 159 match_object = re.search(r'(?P<major>\d)\.(?P<minor>\d)\.(?P<build>\d+)', ver_output) 160 assert match_object, 'cmd returned an unexpected version string: ' + ver_output 161 return tuple(map(int, match_object.groups())) 162