aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside2/tests/registry/util.py
diff options
context:
space:
mode:
authorChristian Tismer <tismer@stackless.com>2019-08-10 18:09:17 +0200
committerChristian Tismer <tismer@stackless.com>2019-10-28 11:57:29 +0100
commitec1121a8768ccdba1788f09ad3147b36b73e5cc2 (patch)
tree90d16cf7e5a2f5dc7ee357db5fb9bce20f01b042 /sources/pyside2/tests/registry/util.py
parent804ea316c174ef5b18658a46d072eedbe03c534b (diff)
Make the function registry more usable in Python modules
It was impossible to use the registry in other functions without many dependencies. The numpy support made it desirable to write new test functions which lead to this refactoring. Task-number: PYSIDE-795 Change-Id: I60bd78a8eaec95dcb4840645c76629b1ef2b50f9 Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'sources/pyside2/tests/registry/util.py')
-rw-r--r--sources/pyside2/tests/registry/util.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/sources/pyside2/tests/registry/util.py b/sources/pyside2/tests/registry/util.py
index 3033608e6..3fcba921a 100644
--- a/sources/pyside2/tests/registry/util.py
+++ b/sources/pyside2/tests/registry/util.py
@@ -48,6 +48,7 @@ and eventually report them afterwards as error.
"""
import sys
+import os
import warnings
import re
from contextlib import contextmanager
@@ -97,4 +98,70 @@ def warn(message, category=None, stacklevel=2):
category = UserWarning
warnings.warn(message, category, stacklevel)
+
+# Python2 legacy: Correct 'linux2' to 'linux', recommended way.
+if sys.platform.startswith('linux'):
+ # We have to be more specific because we had differences between
+ # RHEL 6.6 and RHEL 7.4 .
+ # Note: The platform module is deprecated. We need to switch to the
+ # distro package, ASAP! The distro has been extracted from Python,
+ # because it changes more often than the Python version.
+ try:
+ import distro
+ except ImportError:
+ import platform as distro
+ platform_name = "".join(distro.linux_distribution()[:2]).lower()
+ # this currently happens on opensuse in 5.14:
+ if not platform_name:
+ # We intentionally crash when that last resort is also absent:
+ platform_name = os.environ["MACHTYPE"]
+ platform_name = re.sub('[^0-9a-z]', '_', platform_name)
+else:
+ platform_name = sys.platform
+# In the linux case, we need more information.
+
+is_py3 = sys.version_info[0] == 3
+is_ci = os.environ.get("QTEST_ENVIRONMENT", "") == "ci"
+
+def get_script_dir():
+ script_dir = os.path.normpath(os.path.dirname(__file__))
+ while "sources" not in os.listdir(script_dir):
+ script_dir = os.path.dirname(script_dir)
+ return script_dir
+
+def qt_version():
+ from PySide2.QtCore import __version__
+ return tuple(map(int, __version__.split(".")))
+
+# Format a registry file name for version.
+def _registry_filename(version, use_ci_module):
+ name = "exists_{}_{}_{}_{}{}.py".format(platform_name,
+ version[0], version[1], version[2], "_ci" if use_ci_module else "")
+ return os.path.join(os.path.dirname(__file__), name)
+
+# Return the expected registry file name.
+def get_refpath(use_ci_module=is_ci):
+ return _registry_filename(qt_version(), use_ci_module)
+
+# Return the registry file name, either that of the current
+# version or fall back to a previous patch release.
+def get_effective_refpath(use_ci_module=is_ci):
+ refpath = get_refpath(use_ci_module)
+ if os.path.exists(refpath):
+ return refpath
+ version = qt_version()
+ major, minor, patch = version[:3]
+ while patch >= 0:
+ file = _registry_filename((major, minor, patch), use_ci_module)
+ if os.path.exists(file):
+ return file
+ patch -= 1
+ return refpath
+
+# Import the CI version of the platform module
+def import_refmodule(use_ci_module=is_ci):
+ refpath = get_effective_refpath(use_ci_module)
+ modname = os.path.basename(os.path.splitext(refpath)[0])
+ return __import__(modname)
+
# eof