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 | |
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 | 19 |
63 @property | 20 @property |
64 def is_win(self): | 21 def is_win(self): |
65 return self.name == 'win' | 22 return self.name == 'win' |
66 | 23 |
67 @property | 24 @property |
68 def is_mac(self): | 25 def is_mac(self): |
69 return self.name == 'mac' | 26 return self.name == 'mac' |
70 | 27 |
71 @property | 28 @property |
72 def is_linux(self): | 29 def is_linux(self): |
73 return self.name == 'linux' | 30 return self.name == 'linux' |
iannucci
2016/10/15 00:39:45
these should probably pass-through too?
dnj
2016/10/15 00:59:27
Well, my thought is that at this point the strings
| |
74 | 31 |
75 @property | 32 @property |
76 def name(self): | 33 def name(self): |
77 return self._name | 34 if self._test_data.enabled: |
35 # Default to linux, unless test case says otherwise. | |
36 return self.platform_client.norm_plat( | |
37 self._test_data.get('name', 'linux')) | |
iannucci
2016/10/15 00:39:45
Hm... I think you need to move this up a level; if
dnj
2016/10/15 00:59:27
Yeah. I think that would be a good manifestation o
iannucci
2016/10/15 01:04:09
fair enough, but I wouldn't leave this gap open to
| |
38 return self.platform_client.name # pragma: no cover | |
78 | 39 |
79 @property | 40 @property |
80 def bits(self): | 41 def bits(self): |
81 # The returned bitness corresponds to the userland. If anyone ever wants | 42 # The returned bitness corresponds to the userland. If anyone ever wants |
82 # to query for bitness of the kernel, another accessor should be added. | 43 # to query for bitness of the kernel, another accessor should be added. |
83 return self._bits | 44 if self._test_data.enabled: |
45 # Default to 64-bit, unless test case says otherwise. | |
46 return self.platform_client.norm_bits(self._test_data.get('bits', 64)) | |
47 return self.platform_client.bits # pragma: no cover | |
84 | 48 |
85 @property | 49 @property |
86 def arch(self): | 50 def arch(self): |
87 return self._arch | 51 return self.platform_client.arch |
88 | 52 |
89 @staticmethod | |
90 def normalize_platform_name(platform): | 53 def normalize_platform_name(platform): |
91 """One of python's sys.platform values -> 'win', 'linux' or 'mac'.""" | 54 """One of python's sys.platform values -> 'win', 'linux' or 'mac'.""" |
92 return norm_plat(platform) # pragma: no cover | 55 return self.platform_client.norm_plat(platform) # pragma: no cover |
OLD | NEW |