diff --git a/LbPlatformUtils/inspect.py b/LbPlatformUtils/inspect.py index cbd138d61b4092876ebcace9491e8b21060f2eb2..5596a96fbc120170c7d995b0e2820193d6258c33 100644 --- a/LbPlatformUtils/inspect.py +++ b/LbPlatformUtils/inspect.py @@ -147,7 +147,21 @@ def _unknown_os(): return 'unknown' -os_id = globals().get('_%s_os' % platform.system(), _unknown_os) +_os_id_impl = globals().get('_%s_os' % platform.system(), _unknown_os) + +_force_host_os_warning_printed = False + + +def os_id(): + if 'force_host_os' in os.environ: + global _force_host_os_warning_printed + if not _force_host_os_warning_printed: + from logging import warning + warning('overriding host os detection (using %s)', + os.environ['force_host_os']) + _force_host_os_warning_printed = True + return os.environ['force_host_os'] + return _os_id_impl() def architecture(minimum=False): diff --git a/LbPlatformUtils/tests/test_detection.py b/LbPlatformUtils/tests/test_detection.py index 9e839ec34322041e16c7d09c67f2fd55822a4825..066608c6ead4d7d00b476a7f08514f7134d6f860 100644 --- a/LbPlatformUtils/tests/test_detection.py +++ b/LbPlatformUtils/tests/test_detection.py @@ -11,11 +11,16 @@ # or submit itself to any jurisdiction. # ############################################################################### import sys +import os def setup(): from LbPlatformUtils.tests import utils utils.setup_all() + if 'LbPlatformUtils.inspect' in sys.modules: + del sys.modules['LbPlatformUtils.inspect'] + if 'force_host_os' in os.environ: + del os.environ['force_host_os'] def teardown(): @@ -35,26 +40,39 @@ def test_os_id_function_selection(): platform._system = 'Linux' del sys.modules['LbPlatformUtils.inspect'] import LbPlatformUtils.inspect as PU - assert PU.os_id is PU._Linux_os + assert PU._os_id_impl is PU._Linux_os platform._system = 'Darwin' del sys.modules['LbPlatformUtils.inspect'] import LbPlatformUtils.inspect as PU - assert PU.os_id is PU._Darwin_os + assert PU._os_id_impl is PU._Darwin_os platform._system = 'Windows' del sys.modules['LbPlatformUtils.inspect'] import LbPlatformUtils.inspect as PU - assert PU.os_id is PU._Windows_os + assert PU._os_id_impl is PU._Windows_os platform._system = 'dummy' del sys.modules['LbPlatformUtils.inspect'] import LbPlatformUtils.inspect as PU - assert PU.os_id is PU._unknown_os + assert PU._os_id_impl is PU._unknown_os del sys.modules['LbPlatformUtils.inspect'] +def test_force_os_id(): + import LbPlatformUtils.inspect as PU + assert not PU._force_host_os_warning_printed + PU.os_id() + assert not PU._force_host_os_warning_printed + try: + os.environ['force_host_os'] = 'superOS' + assert PU.os_id() == 'superOS' + assert PU._force_host_os_warning_printed + finally: + del os.environ['force_host_os'] + + def test_parse_os_release(): import LbPlatformUtils.inspect as PU mappings = [ @@ -374,8 +392,7 @@ def test_dirac_platform(): # Check dirac_platform detection override open._overrides['/proc/cpuinfo'] = flags_for_arch('x86_64') - assert PU.can_run( - PU.dirac_platform(force_os='sl7'), 'x86_64-centos7') + assert PU.can_run(PU.dirac_platform(force_os='sl7'), 'x86_64-centos7') def test_compiler_id():