| OLD | NEW |
| 1 # Copyright 2013 The LUCI Authors. All rights reserved. | 1 # Copyright 2013 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
| 3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import sys | |
| 6 import platform | |
| 7 | |
| 8 from recipe_engine import recipe_api | 5 from recipe_engine import recipe_api |
| 9 | 6 |
| 10 | 7 |
| 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): | 8 class PlatformApi(recipe_api.RecipeApi): |
| 27 """ | 9 """ |
| 28 Provides host-platform-detection properties. | 10 Provides host-platform-detection properties. |
| 29 | 11 |
| 30 Mocks: | 12 Mocks: |
| 31 name (str): A value equivalent to something that might be returned by | 13 name (str): A value equivalent to something that might be returned by |
| 32 sys.platform. | 14 sys.platform. |
| 33 bits (int): Either 32 or 64. | 15 bits (int): Either 32 or 64. |
| 34 """ | 16 """ |
| 35 | 17 |
| 36 def __init__(self, **kwargs): | 18 platform_client = recipe_api.RequireClient('platform') |
| 37 super(PlatformApi, self).__init__(**kwargs) | |
| 38 self._name = norm_plat(sys.platform) | |
| 39 | 19 |
| 40 self._arch = 'intel' | 20 def initialize(self): |
| 41 self._bits = norm_bits(platform.machine()) | 21 # Pass-through platform constants. |
| 42 | 22 self.WIN = self.platform_client.WIN |
| 43 if self._test_data.enabled: | 23 self.LINUX = self.platform_client.LINUX |
| 44 # Default to linux/64, unless test case says otherwise. | 24 self.MAC = self.platform_client.MAC |
| 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 | 25 |
| 63 @property | 26 @property |
| 64 def is_win(self): | 27 def is_win(self): |
| 65 return self.name == 'win' | 28 return self.name == self.WIN |
| 66 | 29 |
| 67 @property | 30 @property |
| 68 def is_mac(self): | 31 def is_mac(self): |
| 69 return self.name == 'mac' | 32 return self.name == self.MAC |
| 70 | 33 |
| 71 @property | 34 @property |
| 72 def is_linux(self): | 35 def is_linux(self): |
| 73 return self.name == 'linux' | 36 return self.name == self.LINUX |
| 74 | 37 |
| 75 @property | 38 @property |
| 76 def name(self): | 39 def name(self): |
| 77 return self._name | 40 if self._test_data.enabled: |
| 41 # Default to linux, unless test case says otherwise. |
| 42 return self.platform_client.norm_plat( |
| 43 self._test_data.get('name', 'linux')) |
| 44 return self.platform_client.name # pragma: no cover |
| 78 | 45 |
| 79 @property | 46 @property |
| 80 def bits(self): | 47 def bits(self): |
| 81 # The returned bitness corresponds to the userland. If anyone ever wants | 48 # The returned bitness corresponds to the userland. If anyone ever wants |
| 82 # to query for bitness of the kernel, another accessor should be added. | 49 # to query for bitness of the kernel, another accessor should be added. |
| 83 return self._bits | 50 if self._test_data.enabled: |
| 51 # Default to 64-bit, unless test case says otherwise. |
| 52 return self.platform_client.norm_bits(self._test_data.get('bits', 64)) |
| 53 return self.platform_client.bits # pragma: no cover |
| 84 | 54 |
| 85 @property | 55 @property |
| 86 def arch(self): | 56 def arch(self): |
| 87 return self._arch | 57 return self.platform_client.arch |
| 88 | 58 |
| 89 @staticmethod | |
| 90 def normalize_platform_name(platform): | 59 def normalize_platform_name(platform): |
| 91 """One of python's sys.platform values -> 'win', 'linux' or 'mac'.""" | 60 """One of python's sys.platform values -> 'win', 'linux' or 'mac'.""" |
| 92 return norm_plat(platform) # pragma: no cover | 61 return self.platform_client.norm_plat(platform) # pragma: no cover |
| OLD | NEW |