OLD | NEW |
(Empty) | |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import sys |
| 6 import platform |
| 7 |
| 8 from recipe_engine import recipe_api |
| 9 |
| 10 |
| 11 def norm_plat(plat): |
| 12 if plat.startswith('linux'): |
| 13 return 'linux' |
| 14 elif plat.startswith(('win', 'cygwin')): |
| 15 return 'win' |
| 16 elif plat.startswith(('darwin', 'mac')): |
| 17 return 'mac' |
| 18 else: # pragma: no cover |
| 19 raise ValueError('Don\'t understand platform "%s"' % plat) |
| 20 |
| 21 |
| 22 def norm_bits(arch): |
| 23 return 64 if '64' in str(arch) else 32 |
| 24 |
| 25 |
| 26 class PlatformApi(recipe_api.RecipeApi): |
| 27 """ |
| 28 Provides host-platform-detection properties. |
| 29 |
| 30 Mocks: |
| 31 name (str): A value equivalent to something that might be returned by |
| 32 sys.platform. |
| 33 bits (int): Either 32 or 64. |
| 34 """ |
| 35 |
| 36 def __init__(self, **kwargs): |
| 37 super(PlatformApi, self).__init__(**kwargs) |
| 38 self._name = norm_plat(sys.platform) |
| 39 |
| 40 self._arch = 'intel' |
| 41 self._bits = norm_bits(platform.machine()) |
| 42 |
| 43 if self._test_data.enabled: |
| 44 # Default to linux/64, unless test case says otherwise. |
| 45 self._name = norm_plat(self._test_data.get('name', 'linux')) |
| 46 self._bits = norm_bits(self._test_data.get('bits', 64)) |
| 47 else: # pragma: no cover |
| 48 # platform.machine is based on running kernel. It's possible to use 64-bit |
| 49 # kernel with 32-bit userland, e.g. to give linker slightly more memory. |
| 50 # Distinguish between different userland bitness by querying |
| 51 # the python binary. |
| 52 if (self._name == 'linux' and |
| 53 self._bits == 64 and |
| 54 platform.architecture()[0] == '32bit'): |
| 55 self._bits = 32 |
| 56 # On Mac, the inverse of the above is true: the kernel is 32-bit but the |
| 57 # CPU and userspace both are capable of running 64-bit programs. |
| 58 elif (self._name == 'mac' and |
| 59 self._bits == 32 and |
| 60 platform.architecture()[0] == '64bit'): |
| 61 self._bits = 64 |
| 62 |
| 63 @property |
| 64 def is_win(self): |
| 65 return self.name == 'win' |
| 66 |
| 67 @property |
| 68 def is_mac(self): |
| 69 return self.name == 'mac' |
| 70 |
| 71 @property |
| 72 def is_linux(self): |
| 73 return self.name == 'linux' |
| 74 |
| 75 @property |
| 76 def name(self): |
| 77 return self._name |
| 78 |
| 79 @property |
| 80 def bits(self): |
| 81 # The returned bitness corresponds to the userland. If anyone ever wants |
| 82 # to query for bitness of the kernel, another accessor should be added. |
| 83 return self._bits |
| 84 |
| 85 @property |
| 86 def arch(self): |
| 87 return self._arch |
| 88 |
| 89 @staticmethod |
| 90 def normalize_platform_name(platform): |
| 91 """One of python's sys.platform values -> 'win', 'linux' or 'mac'.""" |
| 92 return norm_plat(platform) # pragma: no cover |
OLD | NEW |