From 584df69c3724c74caf6d13190ff0165b5f1ad6af Mon Sep 17 00:00:00 2001 From: wwn <1506514799@qq.com> Date: Wed, 6 Mar 2024 16:06:53 +0800 Subject: [PATCH 1/7] remove py23 module Signed-off-by: wwn <1506514799@qq.com> --- src/rez/cli/forward.py | 4 +- src/rez/serialise.py | 4 +- .../{test_utils_py23.py => test_util.py} | 12 ++-- src/rez/util.py | 34 +++++++++ src/rez/utils/memcached.py | 4 +- src/rez/utils/py23.py | 71 ------------------- src/rez/utils/sourcecode.py | 4 +- 7 files changed, 46 insertions(+), 87 deletions(-) rename src/rez/tests/{test_utils_py23.py => test_util.py} (83%) delete mode 100644 src/rez/utils/py23.py diff --git a/src/rez/cli/forward.py b/src/rez/cli/forward.py index be725b153..97b9962ae 100644 --- a/src/rez/cli/forward.py +++ b/src/rez/cli/forward.py @@ -19,7 +19,7 @@ def command(opts, parser, extra_arg_groups=None): from rez.exceptions import RezSystemError from rez.vendor import yaml from rez.vendor.yaml.error import YAMLError - from rez.utils import py23 + from rez.util import get_function_arg_names import os.path # we don't usually want warnings printed in a wrapped tool. But in cases @@ -63,7 +63,7 @@ def command(opts, parser, extra_arg_groups=None): module = plugin_manager.get_plugin_module(plugin_type, plugin_name) target_func = getattr(module, func_name) - func_args = py23.get_function_arg_names(target_func) + func_args = get_function_arg_names(target_func) if "_script" in func_args: kwargs["_script"] = yaml_file diff --git a/src/rez/serialise.py b/src/rez/serialise.py index da46b78b5..fe82421ea 100644 --- a/src/rez/serialise.py +++ b/src/rez/serialise.py @@ -23,7 +23,7 @@ from rez.exceptions import ResourceError, InvalidPackageError from rez.utils.memcached import memcached from rez.utils.execution import add_sys_paths -from rez.utils import py23 +from rez.util import get_function_arg_names from rez.config import config from rez.vendor.atomicwrites import atomic_write from rez.vendor import yaml @@ -332,7 +332,7 @@ def _process(value): fn.__globals__.update(get_objects()) # execute the function - args = py23.get_function_arg_names(func) + args = get_function_arg_names(func) if len(args) not in (0, 1): raise ResourceError("@early decorated function must " diff --git a/src/rez/tests/test_utils_py23.py b/src/rez/tests/test_util.py similarity index 83% rename from src/rez/tests/test_utils_py23.py rename to src/rez/tests/test_util.py index 96b2c04de..16391e2d9 100644 --- a/src/rez/tests/test_utils_py23.py +++ b/src/rez/tests/test_util.py @@ -3,14 +3,13 @@ """ -unit tests for 'utils.py23' module +unit tests for 'util' module """ import os -import sys import tempfile - +import sys from rez.tests.util import TestBase -from rez.utils import py23 +from rez.util import load_module_from_file class TestLoadModuleFromFile(TestBase): @@ -27,8 +26,5 @@ def test_load_module(self): with open(os.path.join(tmpdir, filename), 'w') as fd: fd.write('') - py23.load_module_from_file( - module, - os.path.join(tmpdir, filename) - ) + load_module_from_file(module, os.path.join(tmpdir, filename)) self.assertEqual(sys.modules.get(module), None, msg='Module was found in sys.modules') diff --git a/src/rez/util.py b/src/rez/util.py index 969bbf30d..5f10b4735 100644 --- a/src/rez/util.py +++ b/src/rez/util.py @@ -11,6 +11,8 @@ import os import os.path import re +import inspect + from rez.exceptions import RezError from rez.vendor.progress.bar import Bar @@ -156,3 +158,35 @@ def is_non_string_iterable(arg): isinstance(arg, collections.abc.Iterable) and not isinstance(arg, str) ) + + + +def get_function_arg_names(func): + """Get names of a function's args. + + Gives full list of positional and keyword-only (py3 only) args. + """ + spec = inspect.getfullargspec(func) + return spec.args + spec.kwonlyargs + + + +def load_module_from_file(name, filepath): + """Load a python module from a sourcefile. + + Args: + name (str): Module name. + filepath (str): Python sourcefile. + + Returns: + `module`: Loaded module. + """ + # The below code will import the module _without_ adding it to + # sys.modules. We want this otherwise we can't import multiple + # versions of the same module + # See: https://github.com/AcademySoftwareFoundation/rez/issues/1483 + import importlib.util + spec = importlib.util.spec_from_file_location(name, filepath) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module diff --git a/src/rez/utils/memcached.py b/src/rez/utils/memcached.py index ab0945cc9..000d4d262 100644 --- a/src/rez/utils/memcached.py +++ b/src/rez/utils/memcached.py @@ -5,7 +5,7 @@ from rez.config import config from rez.vendor.memcache.memcache import Client as Client_, \ SERVER_MAX_KEY_LENGTH, __version__ as memcache_client_version -from rez.utils import py23 +from rez.util import get_function_arg_names from threading import local from contextlib import contextmanager from functools import update_wrapper @@ -318,7 +318,7 @@ def _listdir(path): """ def default_key(func, *nargs, **kwargs): parts = [func.__module__] - argnames = py23.get_function_arg_names(func) + argnames = get_function_arg_names(func) if argnames: if argnames[0] == "cls": diff --git a/src/rez/utils/py23.py b/src/rez/utils/py23.py deleted file mode 100644 index 8eee97f81..000000000 --- a/src/rez/utils/py23.py +++ /dev/null @@ -1,71 +0,0 @@ -# SPDX-License-Identifier: Apache-2.0 -# Copyright Contributors to the Rez Project - - -""" -Custom py2/3 interoperability code. - -Put any code here that deals with py2/3 interoperability, beyond simple cases -that use (for eg) the six module. -""" -import sys - -from rez.vendor.six import six - -try: - from html import escape # noqa: F401 -except ImportError: - # Python 2 - from cgi import escape # noqa: F401 - -try: - from shlex import quote # noqa: F401 -except ImportError: - # Python 2 - from pipes import quote # noqa: F401 - - -def get_function_arg_names(func): - """Get names of a function's args. - - Gives full list of positional and keyword-only (py3 only) args. - """ - import inspect - - if hasattr(inspect, "getfullargspec"): - spec = inspect.getfullargspec(func) - return spec.args + spec.kwonlyargs - else: - return inspect.getargspec(func).args - - -def load_module_from_file(name, filepath): - """Load a python module from a sourcefile. - - Args: - name (str): Module name. - filepath (str): Python sourcefile. - - Returns: - `module`: Loaded module. - """ - if six.PY2: - import imp - with open(filepath) as f: - module = imp.load_source(name, filepath, f) - # Keep the module out of sys.modules. See comment in the `else:` - # for more info - if name in sys.modules: - del sys.modules[name] - return module - - else: - # The below code will import the module _without_ adding it to - # sys.modules. We want this otherwise we can't import multiple - # versions of the same module - # See: https://github.com/AcademySoftwareFoundation/rez/issues/1483 - import importlib.util - spec = importlib.util.spec_from_file_location(name, filepath) - module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(module) - return module diff --git a/src/rez/utils/sourcecode.py b/src/rez/utils/sourcecode.py index 42c7ed63d..ff835e6ac 100644 --- a/src/rez/utils/sourcecode.py +++ b/src/rez/utils/sourcecode.py @@ -5,7 +5,7 @@ from rez.utils.formatting import indent from rez.utils.data_utils import cached_property from rez.utils.logging_ import print_debug -from rez.utils import py23 +from rez.util import load_module_from_file from inspect import getsourcelines from textwrap import dedent from glob import glob @@ -351,7 +351,7 @@ def load_module(self, name, package): if config.debug("file_loads"): print_debug("Loading include sourcefile: %s" % filepath) - module = py23.load_module_from_file(name, filepath) + module = load_module_from_file(name, filepath) self.modules[hash_str] = module return module From 8309a4e64e0a055856ff1df72548ffe519b0e025 Mon Sep 17 00:00:00 2001 From: wwn <1506514799@qq.com> Date: Wed, 6 Mar 2024 16:11:47 +0800 Subject: [PATCH 2/7] remove __nonzero__ Signed-off-by: wwn <1506514799@qq.com> --- src/rez/package_filter.py | 8 ++------ src/rez/rex.py | 4 +--- src/rez/solver.py | 4 +--- src/rez/utils/logging_.py | 4 +--- src/rez/utils/platform_.py | 6 ------ src/rez/version/_version.py | 4 +--- 6 files changed, 6 insertions(+), 24 deletions(-) diff --git a/src/rez/package_filter.py b/src/rez/package_filter.py index b6b60f296..70014e400 100644 --- a/src/rez/package_filter.py +++ b/src/rez/package_filter.py @@ -157,11 +157,9 @@ def __and__(self, other): result.add_inclusion(rule) return result - def __nonzero__(self): + def __bool__(self): return bool(self._excludes) - __bool__ = __nonzero__ # py3 compat - @cached_property def cost(self): """Get the approximate cost of this filter. @@ -304,11 +302,9 @@ def to_pod(self): data.append(f.to_pod()) return data - def __nonzero__(self): + def __bool__(self): return any(self.filters) - __bool__ = __nonzero__ # py3 compat - def __str__(self): filters = sorted(self.filters, key=lambda x: (x.cost, str(x))) return str(tuple(filters)) diff --git a/src/rez/rex.py b/src/rez/rex.py index 6086db5b4..1606a12d1 100644 --- a/src/rez/rex.py +++ b/src/rez/rex.py @@ -1180,14 +1180,12 @@ def __repr__(self): return '%s(%r, %r)' % (self.__class__.__name__, self._name, self.value()) - def __nonzero__(self): + def __bool__(self): try: return bool(self.value()) except RexUndefinedVariableError: return False - __bool__ = __nonzero__ # py3 compat - def __eq__(self, value): if isinstance(value, EnvironmentVariable): value = value.value() diff --git a/src/rez/solver.py b/src/rez/solver.py index f7652cf5d..7fc8ee59a 100644 --- a/src/rez/solver.py +++ b/src/rez/solver.py @@ -134,11 +134,9 @@ def br(self): def pr(self, txt='', *args): print(txt % args, file=self.buf) - def __nonzero__(self): + def __bool__(self): return self.verbosity > 0 - __bool__ = __nonzero__ # py3 compat - class SolverState(object): """Represent the current state of the solver instance for use with a diff --git a/src/rez/utils/logging_.py b/src/rez/utils/logging_.py index 63a33e131..76b71601b 100644 --- a/src/rez/utils/logging_.py +++ b/src/rez/utils/logging_.py @@ -62,11 +62,9 @@ def __call__(self, msg, *nargs): msg = msg % nargs self.printer_function(msg) - def __nonzero__(self): + def __bool__(self): return bool(self.printer_function) - __bool__ = __nonzero__ # py3 compat - @contextmanager def log_duration(printer, msg): diff --git a/src/rez/utils/platform_.py b/src/rez/utils/platform_.py index bf66c30c0..9ce8e0027 100644 --- a/src/rez/utils/platform_.py +++ b/src/rez/utils/platform_.py @@ -466,12 +466,6 @@ class WindowsPlatform(Platform): name = "windows" def _arch(self): - # http://stackoverflow.com/questions/7164843/in-python-how-do-you-determine-whether-the-kernel-is-running-in-32-bit-or-64-bi - if os.name == 'nt' and sys.version_info[:2] < (2, 7): - arch = os.environ.get("PROCESSOR_ARCHITEW6432", - os.environ.get('PROCESSOR_ARCHITECTURE')) - if arch: - return arch return super(WindowsPlatform, self)._arch() def _os(self): diff --git a/src/rez/version/_version.py b/src/rez/version/_version.py index 0dc702e54..2114f5bf5 100644 --- a/src/rez/version/_version.py +++ b/src/rez/version/_version.py @@ -390,12 +390,10 @@ def __getitem__(self, index): except IndexError: raise IndexError("version token index out of range") - def __nonzero__(self): + def __bool__(self): """The empty version equates to False.""" return bool(self.tokens) - __bool__ = __nonzero__ # py3 compat - def __eq__(self, other): return isinstance(other, Version) and self.tokens == other.tokens From e4c918d0f73f95923bbf170a7988d42d0496726d Mon Sep 17 00:00:00 2001 From: wwn <1506514799@qq.com> Date: Wed, 6 Mar 2024 16:18:33 +0800 Subject: [PATCH 3/7] remove package cache py version less 3.4 condition Signed-off-by: wwn <1506514799@qq.com> --- src/rez/package_cache.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/rez/package_cache.py b/src/rez/package_cache.py index e17b2c782..086000e29 100644 --- a/src/rez/package_cache.py +++ b/src/rez/package_cache.py @@ -185,16 +185,9 @@ def add_variant(self, variant, force=False): "Not cached - package is local: %s" % package.uri ) - # Package is already on same disk device as package cache. Note that - # this check is skipped on Windows + Py<3.4, as os.stat does not - # support device identification. - # - dev_stat_not_supported = ( - platform.system() == "Windows" - and sys.version_info[:2] < (3, 4) - ) + # Package is already on same disk device as package cache - if not config.package_cache_same_device and not dev_stat_not_supported: + if not config.package_cache_same_device: st_pkgcache = os.stat(self.path) st_variant = os.stat(variant_root) if st_pkgcache.st_dev == st_variant.st_dev: From 7931ea3c87af5d2bac263a62f3167af53ee41d2c Mon Sep 17 00:00:00 2001 From: wwn <1506514799@qq.com> Date: Fri, 8 Mar 2024 09:53:01 +0800 Subject: [PATCH 4/7] fix flake8 error Signed-off-by: wwn <1506514799@qq.com> --- src/rez/utils/platform_.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/rez/utils/platform_.py b/src/rez/utils/platform_.py index 9ce8e0027..8ed124a54 100644 --- a/src/rez/utils/platform_.py +++ b/src/rez/utils/platform_.py @@ -3,7 +3,6 @@ import platform -import sys import os import os.path import re From e7033b348750b5075715aeae88b903f332482487 Mon Sep 17 00:00:00 2001 From: wwn <1506514799@qq.com> Date: Fri, 8 Mar 2024 09:53:59 +0800 Subject: [PATCH 5/7] fix typo Signed-off-by: wwn <1506514799@qq.com> --- src/rez/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rez/util.py b/src/rez/util.py index 5f10b4735..9b2296c72 100644 --- a/src/rez/util.py +++ b/src/rez/util.py @@ -164,7 +164,7 @@ def is_non_string_iterable(arg): def get_function_arg_names(func): """Get names of a function's args. - Gives full list of positional and keyword-only (py3 only) args. + Gives full list of positional and keyword-only args. """ spec = inspect.getfullargspec(func) return spec.args + spec.kwonlyargs From ddaaa35f7804e6ba05b657653fcad6df5867d0a7 Mon Sep 17 00:00:00 2001 From: wwn <1506514799@qq.com> Date: Fri, 8 Mar 2024 09:59:18 +0800 Subject: [PATCH 6/7] remove plateform _arch methord Signed-off-by: wwn <1506514799@qq.com> --- src/rez/utils/platform_.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/rez/utils/platform_.py b/src/rez/utils/platform_.py index 8ed124a54..f0901a1ca 100644 --- a/src/rez/utils/platform_.py +++ b/src/rez/utils/platform_.py @@ -464,9 +464,6 @@ def _difftool(self): class WindowsPlatform(Platform): name = "windows" - def _arch(self): - return super(WindowsPlatform, self)._arch() - def _os(self): release, version, csd, ptype = platform.win32_ver() toks = [] From 9a21f645b23bf7d6b7dc529efd6b01855d913a81 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Morin <38703886+JeanChristopheMorinPerso@users.noreply.github.com> Date: Thu, 7 Mar 2024 21:57:57 -0500 Subject: [PATCH 7/7] Remove whitespaces Signed-off-by: Jean-Christophe Morin <38703886+JeanChristopheMorinPerso@users.noreply.github.com> --- src/rez/util.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/rez/util.py b/src/rez/util.py index 9b2296c72..cfdbe6174 100644 --- a/src/rez/util.py +++ b/src/rez/util.py @@ -160,7 +160,6 @@ def is_non_string_iterable(arg): ) - def get_function_arg_names(func): """Get names of a function's args. @@ -170,7 +169,6 @@ def get_function_arg_names(func): return spec.args + spec.kwonlyargs - def load_module_from_file(name, filepath): """Load a python module from a sourcefile.