summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbst-marge-bot <marge-bot@buildstream.build>2019-04-12 07:49:11 +0000
committerbst-marge-bot <marge-bot@buildstream.build>2019-04-12 07:49:11 +0000
commit4c391dd6bd549c349870758b14b84c8aef48953c (patch)
tree343e4ca19953dfdb1bbdb6832a6c0dcd593c0821
parent887461a584613c304064a636cbda8f4c863f35f9 (diff)
parent3ec65d9b4a21d5e503b7edfe9e691aa4e9e6714f (diff)
downloadbuildstream-4c391dd6bd549c349870758b14b84c8aef48953c.tar.gz
Merge branch 'aevri/platform_uname' into 'master'
use uname in a win32-compatible way See merge request BuildStream/buildstream!1262
-rw-r--r--buildstream/_options/optionos.py4
-rw-r--r--buildstream/_platform/platform.py9
-rw-r--r--tests/format/optionarch.py32
-rw-r--r--tests/format/optionos.py27
-rw-r--r--tests/testutils/__init__.py1
-rw-r--r--tests/testutils/platform.py49
6 files changed, 72 insertions, 50 deletions
diff --git a/buildstream/_options/optionos.py b/buildstream/_options/optionos.py
index e76cf177c..2d46b70ba 100644
--- a/buildstream/_options/optionos.py
+++ b/buildstream/_options/optionos.py
@@ -18,7 +18,7 @@
# Authors:
# Raoul Hidalgo Charman <raoul.hidalgocharman@codethink.co.uk>
-import os
+import platform
from .optionenum import OptionEnum
@@ -32,7 +32,7 @@ class OptionOS(OptionEnum):
super(OptionOS, self).load(node, allow_default_definition=False)
def load_default_value(self, node):
- return os.uname()[0]
+ return platform.uname().system
def resolve(self):
diff --git a/buildstream/_platform/platform.py b/buildstream/_platform/platform.py
index eef07812a..dba60ddca 100644
--- a/buildstream/_platform/platform.py
+++ b/buildstream/_platform/platform.py
@@ -18,6 +18,7 @@
# Tristan Maat <tristan.maat@codethink.co.uk>
import os
+import platform
import sys
import resource
@@ -75,7 +76,7 @@ class Platform():
@staticmethod
def get_host_os():
- return os.uname()[0]
+ return platform.uname().system
# canonicalize_arch():
#
@@ -84,6 +85,8 @@ class Platform():
#
@staticmethod
def canonicalize_arch(arch):
+ # Note that these are all expected to be lowercase, as we want a
+ # case-insensitive lookup. Windows can report its arch in ALLCAPS.
aliases = {
"aarch32": "aarch32",
"aarch64": "aarch64",
@@ -108,7 +111,7 @@ class Platform():
}
try:
- return aliases[arch.replace('_', '-')]
+ return aliases[arch.replace('_', '-').lower()]
except KeyError:
raise PlatformError("Unknown architecture: {}".format(arch))
@@ -122,7 +125,7 @@ class Platform():
@staticmethod
def get_host_arch():
# get the hardware identifier from uname
- uname_machine = os.uname()[4]
+ uname_machine = platform.uname().machine
return Platform.canonicalize_arch(uname_machine)
##################################################################
diff --git a/tests/format/optionarch.py b/tests/format/optionarch.py
index 86363b834..a7d38c8be 100644
--- a/tests/format/optionarch.py
+++ b/tests/format/optionarch.py
@@ -1,7 +1,6 @@
# Pylint doesn't play well with fixtures and dependency injection from pytest
# pylint: disable=redefined-outer-name
-from contextlib import contextmanager
import os
import pytest
@@ -10,29 +9,14 @@ from buildstream import _yaml
from buildstream._exceptions import ErrorDomain, LoadErrorReason
from buildstream.plugintestutils.runcli import cli # pylint: disable=unused-import
+from tests.testutils import override_platform_uname
+
# Project directory
DATA_DIR = os.path.dirname(os.path.realpath(__file__))
-# Context manager to override the reported value of `os.uname()`
-@contextmanager
-def override_uname_arch(name):
- orig_uname = os.uname
- orig_tuple = tuple(os.uname())
- override_result = (orig_tuple[0], orig_tuple[1],
- orig_tuple[2], orig_tuple[3],
- name)
-
- def override():
- return override_result
-
- os.uname = override
- yield
- os.uname = orig_uname
-
-
@pytest.mark.datafiles(DATA_DIR)
-@pytest.mark.parametrize("uname,value,expected", [
+@pytest.mark.parametrize("machine,value,expected", [
# Test explicitly provided arches
('arm', 'aarch32', 'Army'),
('arm', 'aarch64', 'Aarchy'),
@@ -46,8 +30,8 @@ def override_uname_arch(name):
('i386', 'aarch32', 'Army'),
('x86_64', 'aarch64', 'Aarchy'),
])
-def test_conditional(cli, datafiles, uname, value, expected):
- with override_uname_arch(uname):
+def test_conditional(cli, datafiles, machine, value, expected):
+ with override_platform_uname(machine=machine):
project = os.path.join(datafiles.dirname, datafiles.basename, 'option-arch')
bst_args = []
@@ -70,7 +54,7 @@ def test_conditional(cli, datafiles, uname, value, expected):
@pytest.mark.datafiles(DATA_DIR)
def test_unsupported_arch(cli, datafiles):
- with override_uname_arch("x86_64"):
+ with override_platform_uname(machine="x86_64"):
project = os.path.join(datafiles.dirname, datafiles.basename, 'option-arch')
result = cli.run(project=project, silent=True, args=[
'show',
@@ -85,7 +69,7 @@ def test_unsupported_arch(cli, datafiles):
@pytest.mark.datafiles(DATA_DIR)
def test_alias(cli, datafiles):
- with override_uname_arch("arm"):
+ with override_platform_uname(machine="arm"):
project = os.path.join(datafiles.dirname, datafiles.basename, 'option-arch-alias')
result = cli.run(project=project, silent=True, args=[
'show',
@@ -100,7 +84,7 @@ def test_alias(cli, datafiles):
@pytest.mark.datafiles(DATA_DIR)
def test_unknown_host_arch(cli, datafiles):
- with override_uname_arch("x86_128"):
+ with override_platform_uname(machine="x86_128"):
project = os.path.join(datafiles.dirname, datafiles.basename, 'option-arch')
result = cli.run(project=project, silent=True, args=[
'show',
diff --git a/tests/format/optionos.py b/tests/format/optionos.py
index 989989a99..6856fd69c 100644
--- a/tests/format/optionos.py
+++ b/tests/format/optionos.py
@@ -1,7 +1,6 @@
# Pylint doesn't play well with fixtures and dependency injection from pytest
# pylint: disable=redefined-outer-name
-from contextlib import contextmanager
import os
import pytest
@@ -10,27 +9,13 @@ from buildstream import _yaml
from buildstream._exceptions import ErrorDomain, LoadErrorReason
from buildstream.plugintestutils.runcli import cli # pylint: disable=unused-import
-DATA_DIR = os.path.dirname(os.path.realpath(__file__))
-
-
-@contextmanager
-def override_uname_os(name):
- orig_uname = os.uname
- orig_tuple = tuple(os.uname())
- override_result = (name, orig_tuple[1],
- orig_tuple[2], orig_tuple[3],
- orig_tuple[4])
+from tests.testutils import override_platform_uname
- def override():
- return override_result
-
- os.uname = override
- yield
- os.uname = orig_uname
+DATA_DIR = os.path.dirname(os.path.realpath(__file__))
@pytest.mark.datafiles(DATA_DIR)
-@pytest.mark.parametrize("uname,value,expected", [
+@pytest.mark.parametrize("system,value,expected", [
# Test explicitly provided arches
('Darwin', 'Linux', 'Linuxy'),
('SunOS', 'FreeBSD', 'FreeBSDy'),
@@ -44,8 +29,8 @@ def override_uname_os(name):
('AIX', 'Linux', 'Linuxy'),
('HaikuOS', 'SunOS', 'SunOSy'),
])
-def test_conditionals(cli, datafiles, uname, value, expected):
- with override_uname_os(uname):
+def test_conditionals(cli, datafiles, system, value, expected):
+ with override_platform_uname(system=system):
project = os.path.join(datafiles.dirname, datafiles.basename, 'option-os')
bst_args = []
@@ -68,7 +53,7 @@ def test_conditionals(cli, datafiles, uname, value, expected):
@pytest.mark.datafiles(DATA_DIR)
def test_unsupported_arch(cli, datafiles):
- with override_uname_os("AIX"):
+ with override_platform_uname(system="AIX"):
project = os.path.join(datafiles.dirname, datafiles.basename, 'option-os')
result = cli.run(project=project, silent=True, args=[
'show',
diff --git a/tests/testutils/__init__.py b/tests/testutils/__init__.py
index 67378c959..929ffc5f2 100644
--- a/tests/testutils/__init__.py
+++ b/tests/testutils/__init__.py
@@ -30,3 +30,4 @@ from .junction import generate_junction
from .runner_integration import wait_for_cache_granularity
from .python_repo import setup_pypi_repo
from .yaml import yaml_file_get_provenance
+from .platform import override_platform_uname
diff --git a/tests/testutils/platform.py b/tests/testutils/platform.py
new file mode 100644
index 000000000..f8faf286e
--- /dev/null
+++ b/tests/testutils/platform.py
@@ -0,0 +1,49 @@
+#
+# Copyright (C) 2019 Bloomberg Finance LP
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library. If not, see <http://www.gnu.org/licenses/>.
+#
+# Authors:
+# Angelos Evripiotis <jevripiotis@bloomberg.net>
+
+from contextlib import contextmanager
+import platform
+
+
+# override_platform_uname()
+#
+# Context manager to override the reported value of `platform.uname()`.
+#
+# Args:
+# system (str): Optional str to replace the 1st entry of uname with.
+# machine (str): Optional str to replace the 5th entry of uname with.
+#
+@contextmanager
+def override_platform_uname(*, system=None, machine=None):
+ orig_func = platform.uname
+ result = platform.uname()
+
+ if system is not None:
+ result = result._replace(system=system)
+ if machine is not None:
+ result = result._replace(machine=machine)
+
+ def override_func():
+ return result
+
+ platform.uname = override_func
+ try:
+ yield
+ finally:
+ platform.uname = orig_func