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

Unified Diff: recipe_engine/util.py

Issue 2415793003: Setup basic Runtime with properties and platform.
Patch Set: 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_engine/util.py
diff --git a/recipe_engine/util.py b/recipe_engine/util.py
index 7fe739ee862c6d66a3ebfd833dd5e05049ddae29..75fd5f1428c3a28eeaa742eaab979f6df6aea1d3 100644
--- a/recipe_engine/util.py
+++ b/recipe_engine/util.py
@@ -2,11 +2,13 @@
# Use of this source code is governed under the Apache License, Version 2.0
# that can be found in the LICENSE file.
+import collections
import contextlib
import datetime
import functools
import logging
import os
+import platform
import sys
import traceback
import time
@@ -91,6 +93,60 @@ class OutputPlaceholder(Placeholder):
pass
+class Platform(collections.namedtuple('Platform', ('name', 'arch', 'bits'))):
+ """Normalized operating system platform information."""
+
+ @staticmethod
+ 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)
+
+ @staticmethod
+ def norm_bits(arch):
+ return 64 if '64' in str(arch) else 32
+
+ @classmethod
+ def probe(cls):
+ name = cls.norm_plat(sys.platform)
+ bits = cls.norm_bits(platform.machine())
+
+ # 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.
+ architecture = platform.architecture()[0]
+ if (name == 'linux' and
+ bits == 64 and
+ architecture == '32bit'):
+ 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 (name == 'mac' and
+ bits == 32 and
+ architecture == '64bit'):
+ bits = 64
+
+ return cls(name=name, arch='intel', bits=bits)
+
+ def is_win(self):
+ return self.name == 'win'
+
+ def is_mac(self):
+ return self.name == 'mac'
+
+ def is_linux(self):
+ return self.name == 'linux'
+
+ def is_posix(self):
+ return self.name in ('mac', 'linux')
+
+
def static_wraps(func):
wrapped_fn = func
if isinstance(func, staticmethod):

Powered by Google App Engine
This is Rietveld 408576698