Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(374)

Unified Diff: recipe_modules/platform/api.py

Issue 2415793003: Setup basic Runtime with properties and platform.
Patch Set: Split out, more immutables, better utilization. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: recipe_modules/platform/api.py
diff --git a/recipe_modules/platform/api.py b/recipe_modules/platform/api.py
index ecd5f673cc345f4165edc7bc51bf8313e44e5bfa..9d9ec6b2dbdb2cb9fc00fe75aa9a9314df698bbd 100644
--- a/recipe_modules/platform/api.py
+++ b/recipe_modules/platform/api.py
@@ -2,27 +2,9 @@
# Use of this source code is governed under the Apache License, Version 2.0
# that can be found in the LICENSE file.
-import sys
-import platform
-
from recipe_engine import recipe_api
-def norm_plat(plat):
- if plat.startswith('linux'):
- return 'linux'
- elif plat.startswith(('win', 'cygwin')):
- return 'win'
- elif plat.startswith(('darwin', 'mac')):
- return 'mac'
- else: # pragma: no cover
- raise ValueError('Don\'t understand platform "%s"' % plat)
-
-
-def norm_bits(arch):
- return 64 if '64' in str(arch) else 32
-
-
class PlatformApi(recipe_api.RecipeApi):
"""
Provides host-platform-detection properties.
@@ -33,60 +15,47 @@ class PlatformApi(recipe_api.RecipeApi):
bits (int): Either 32 or 64.
"""
- def __init__(self, **kwargs):
- super(PlatformApi, self).__init__(**kwargs)
- self._name = norm_plat(sys.platform)
+ platform_client = recipe_api.RequireClient('platform')
- self._arch = 'intel'
- self._bits = norm_bits(platform.machine())
-
- if self._test_data.enabled:
- # Default to linux/64, unless test case says otherwise.
- self._name = norm_plat(self._test_data.get('name', 'linux'))
- self._bits = norm_bits(self._test_data.get('bits', 64))
- else: # pragma: no cover
- # platform.machine is based on running kernel. It's possible to use 64-bit
- # kernel with 32-bit userland, e.g. to give linker slightly more memory.
- # Distinguish between different userland bitness by querying
- # the python binary.
- if (self._name == 'linux' and
- self._bits == 64 and
- platform.architecture()[0] == '32bit'):
- self._bits = 32
- # On Mac, the inverse of the above is true: the kernel is 32-bit but the
- # CPU and userspace both are capable of running 64-bit programs.
- elif (self._name == 'mac' and
- self._bits == 32 and
- platform.architecture()[0] == '64bit'):
- self._bits = 64
+ def initialize(self):
+ # Pass-through platform constants.
+ self.WIN = self.platform_client.WIN
+ self.LINUX = self.platform_client.LINUX
+ self.MAC = self.platform_client.MAC
@property
def is_win(self):
- return self.name == 'win'
+ return self.name == self.WIN
@property
def is_mac(self):
- return self.name == 'mac'
+ return self.name == self.MAC
@property
def is_linux(self):
- return self.name == 'linux'
+ return self.name == self.LINUX
@property
def name(self):
- return self._name
+ if self._test_data.enabled:
+ # Default to linux, unless test case says otherwise.
+ return self.platform_client.norm_plat(
+ self._test_data.get('name', 'linux'))
+ return self.platform_client.name # pragma: no cover
@property
def bits(self):
# The returned bitness corresponds to the userland. If anyone ever wants
# to query for bitness of the kernel, another accessor should be added.
- return self._bits
+ if self._test_data.enabled:
+ # Default to 64-bit, unless test case says otherwise.
+ return self.platform_client.norm_bits(self._test_data.get('bits', 64))
+ return self.platform_client.bits # pragma: no cover
@property
def arch(self):
- return self._arch
+ return self.platform_client.arch
- @staticmethod
def normalize_platform_name(platform):
"""One of python's sys.platform values -> 'win', 'linux' or 'mac'."""
- return norm_plat(platform) # pragma: no cover
+ return self.platform_client.norm_plat(platform) # pragma: no cover

Powered by Google App Engine
This is Rietveld 408576698