Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

miscellanous atomic nonaffective py2/py3 compatibilities #710

Merged
merged 22 commits into from
Aug 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/rez/bind/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import os.path
import os
import platform
import sys


def log(msg):
Expand All @@ -29,7 +30,7 @@ def make_dirs(*dirs):

def run_python_command(commands, exe=None):
py_cmd = "; ".join(commands)
args = [exe or "python", "-c", py_cmd]
args = [exe or sys.executable, "-c", py_cmd]
stdout, stderr, returncode = _run_command(args)
return (returncode == 0), stdout.strip(), stderr.strip()

Expand Down Expand Up @@ -67,7 +68,8 @@ def find_exe(name, filepath=None):
"""
if filepath:
if not os.path.exists(filepath):
open(filepath) # raise IOError
with open(filepath):
pass # raise IOError
elif not os.path.isfile(filepath):
raise RezBindError("not a file: %s" % filepath)
else:
Expand Down
6 changes: 3 additions & 3 deletions src/rez/cli/_bez.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def run():
"""
from __future__ import print_function

stream=open("%(buildfile)s")
env={}
exec stream in env
with open("%(buildfile)s") as stream:
exec(compile(stream.read(), stream.name, 'exec'), env)

buildfunc = env.get("build")
if not buildfunc:
Expand Down Expand Up @@ -78,7 +78,7 @@ def run():
fd.write(cli_code)

print("executing rezbuild.py...")
cmd = ["python", bezfile]
cmd = [sys.executable, bezfile]
p = subprocess.Popen(cmd)
p.wait()
tmpdir_manager.clear()
Expand Down
7 changes: 4 additions & 3 deletions src/rez/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import absolute_import
from rez import __version__
from rez.utils.data_utils import AttrDictWrapper, RO_AttrDictWrapper, \
convert_dicts, cached_property, cached_class_property, LazyAttributeMeta, \
Expand All @@ -10,6 +11,7 @@
from rez.system import system
from rez.vendor.schema.schema import Schema, SchemaError, And, Or, Use
from rez.vendor import yaml
from rez.vendor.six import six
from rez.vendor.yaml.error import YAMLError
from rez.backport.lru_cache import lru_cache
from contextlib import contextmanager
Expand Down Expand Up @@ -412,7 +414,7 @@ def _parse_env_var(self, value):
# Config
# -----------------------------------------------------------------------------

class Config(object):
class Config(six.with_metaclass(LazyAttributeMeta, object)):
"""Rez configuration settings.

You should call the `create_config` function, rather than constructing a
Expand All @@ -423,7 +425,6 @@ class Config(object):
files update the master configuration to create the final config. See the
comments at the top of 'rezconfig' for more details.
"""
__metaclass__ = LazyAttributeMeta
schema = config_schema
schema_error = ConfigurationError

Expand Down Expand Up @@ -827,7 +828,7 @@ def _load_config_py(filepath):
with open(filepath) as f:
try:
code = compile(f.read(), filepath, 'exec')
exec_(code, _globs_=globs)
exec_(code, globs)
except Exception as e:
raise ConfigurationError("Error loading configuration from %s: %s"
% (filepath, str(e)))
Expand Down
4 changes: 2 additions & 2 deletions src/rez/package_bind.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ def _bind_package(name, path=None, version_range=None, bind_args=None,
raise RezBindError("Bind module not found for '%s'" % name)

# load the bind module
stream = open(bindfile)
namespace = {}
exec stream in namespace
with open(bindfile) as stream:
exec(compile(stream.read(), stream.name, 'exec'), namespace)

# parse bind module params
bind_parser = argparse.ArgumentParser(prog="rez bind %s" % name,
Expand Down
4 changes: 2 additions & 2 deletions src/rez/package_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,13 @@ def _copy_variant_payload(src_variant, dest_pkg_repo, shallow=False,
if not variant_root:
raise PackageCopyError(
"Cannot copy source variant %s - it is a type of variant that "
"does not have a root.", src_variant.uri
"does not have a root." % src_variant.uri
)

if not os.path.isdir(variant_root):
raise PackageCopyError(
"Cannot copy source variant %s - its root does not appear to "
"be present on disk (%s).", src_variant.uri, variant_root
"be present on disk (%s)." % src_variant.uri, variant_root
)

dest_variant_name = overrides.get("name") or src_variant.name
Expand Down
9 changes: 4 additions & 5 deletions src/rez/package_resources_.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from rez.config import config, Config, create_config
from rez.vendor.version.version import Version
from rez.vendor.schema.schema import Schema, SchemaError, Optional, Or, And, Use
from rez.vendor.six import six

from textwrap import dedent
import os.path
Expand Down Expand Up @@ -406,8 +407,10 @@ def _convert_to_rex(self, commands):
else:
return commands

class _Metas(AttributeForwardMeta, LazyAttributeMeta):
pass

class VariantResourceHelper(VariantResource):
class VariantResourceHelper(six.with_metaclass(_Metas, VariantResource)):
"""Helper class for implementing variants that inherit properties from their
parent package.

Expand All @@ -416,10 +419,6 @@ class VariantResourceHelper(VariantResource):
exceptions - eg 'variants', 'requires'). This is a common enough pattern
that it's supplied here for other repository plugins to use.
"""
class _Metas(AttributeForwardMeta, LazyAttributeMeta):
pass

__metaclass__ = _Metas

# Note: lazy key validation doesn't happen in this class, it just fowards on
# attributes from the package. But LazyAttributeMeta does still use this
Expand Down
2 changes: 1 addition & 1 deletion src/rez/rex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ def compile_code(cls, code, filename=None, exec_namespace=None):
if isinstance(code, SourceCode):
code.exec_(globals_=exec_namespace)
else:
exec pyc in exec_namespace
exec(pyc, exec_namespace)
except RexError:
raise
except SourceCodeError as e:
Expand Down
5 changes: 3 additions & 2 deletions src/rez/serialise.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def _load_from_file__key(filepath, format_, update_data_callback):
callback_key = getattr(update_data_callback, "__name__", "None")

return str(("package_file", filepath, str(format_), callback_key,
st.st_ino, st.st_mtime))
int(st.st_ino), st.st_mtime))


@memcached(servers=config.memcached_uri if config.cache_package_files else None,
Expand Down Expand Up @@ -228,7 +228,8 @@ def _load_py(stream, filepath=None):
InvalidPackageError=InvalidPackageError)

try:
exec stream in g
with open(filepath, "rb") as f:
exec(compile(f.read(), filepath, 'exec'), g)
except Exception as e:
import traceback
frames = traceback.extract_tb(sys.exc_info()[2])
Expand Down
2 changes: 1 addition & 1 deletion src/rez/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1973,7 +1973,7 @@ def solve(self):
elif self.print_stats:
from pprint import pformat
data = {"solve_stats": self.solve_stats}
print(pformat(data), file=self.buf or sys.stdout)
print(pformat(data), file=(self.buf or sys.stdout))

@property
def solve_stats(self):
Expand Down
11 changes: 7 additions & 4 deletions src/rez/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from contextlib import contextmanager
import re
import sys
import warnings
from rez.vendor.six import six


@contextmanager
Expand All @@ -10,12 +12,13 @@ def with_noop():

def reraise(exc, new_exc_cls=None, format_str=None):
if new_exc_cls is None:
raise
six.reraise(*sys.exc_info())

if format_str is None:
format_str = "%s"
if format_str is not None:
warnings.warn("Argument `reraise.format_str` is deprecated")

raise new_exc_cls, format_str % exc, sys.exc_info()[2]
type_, value, traceback = sys.exc_info()
six.reraise(new_exc_cls, exc, traceback)


# Copyright 2013-2016 Allan Johns.
Expand Down
8 changes: 4 additions & 4 deletions src/rez/utils/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def copy_or_replace(src, dst):
'''
try:
shutil.copy(src, dst)
except (OSError, IOError), e:
except (OSError, IOError) as e:
# It's possible that the file existed, but was owned by someone
# else - in that situation, shutil.copy might then fail when it
# tries to copy perms.
Expand Down Expand Up @@ -387,18 +387,18 @@ def copy(srcname, dstname):
else:
copy(srcname, dstname)
# XXX What about devices, sockets etc.?
except (IOError, os.error), why:
except (IOError, os.error) as why:
errors.append((srcname, dstname, str(why)))
# catch the Error from the recursive copytree so that we can
# continue with other files
except shutil.Error, err:
except shutil.Error as err:
errors.extend(err.args[0])
try:
shutil.copystat(src, dst)
except shutil.WindowsError:
# can't copy file access times on Windows
pass
except OSError, why:
except OSError as why:
errors.extend((src, dst, str(why)))
if errors:
raise shutil.Error(errors)
Expand Down
7 changes: 3 additions & 4 deletions src/rez/utils/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@
from rez.exceptions import ResourceError
from rez.backport.lru_cache import lru_cache
from rez.utils.logging_ import print_debug
from rez.vendor.six import six


class Resource(object):
class Resource(six.with_metaclass(LazyAttributeMeta, object)):
"""Abstract base class for a data resource.

A resource is an object uniquely identified by a 'key' (the resource type),
Expand Down Expand Up @@ -70,7 +71,6 @@ class Resource(object):
schema_error (Exception): The exception type to raise on key
validation failure.
"""
__metaclass__ = LazyAttributeMeta
key = None
schema = None
schema_error = Exception
Expand Down Expand Up @@ -223,7 +223,7 @@ def _get_resource(self, resource_handle):
return resource_class(resource_handle.variables)


class ResourceWrapper(object):
class ResourceWrapper(six.with_metaclass(AttributeForwardMeta, object)):
"""An object that wraps a resource instance.

A resource wrapper is useful for two main reasons. First, we can wrap
Expand All @@ -241,7 +241,6 @@ class ResourceWrapper(object):
the resource that you want to expose in the wrapper. The `schema_keys`
function is provided to help get a list of keys from a resource schema.
"""
__metaclass__ = AttributeForwardMeta
keys = None

def __init__(self, resource):
Expand Down
2 changes: 1 addition & 1 deletion src/rez/utils/sourcecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def exec_(self, globals_={}):
pyc = self.compiled

try:
exec pyc in globals_
exec(pyc, globals_)
except Exception as e:
stack = traceback.format_exc()
raise SourceCodeExecError(
Expand Down
2 changes: 1 addition & 1 deletion src/rezplugins/build_process/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def _install_include_modules(self, install_path):
for name in self.package.includes:
filepath = os.path.join(definition_python_path, name) + ".py"

with open(filepath) as f:
with open(filepath, "rb") as f:
txt = f.read().strip()

uuid = sha1(txt).hexdigest()
Expand Down
2 changes: 1 addition & 1 deletion src/rezplugins/build_system/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def bind_cli(cls, parser, group):
before_args = set(x.dest for x in parser._actions)

try:
exec source in {"parser": group}
exec(source, {"parser": group})
except Exception as e:
print_warning("Error in ./parse_build_args.py: %s" % str(e))

Expand Down
6 changes: 3 additions & 3 deletions src/rezplugins/package_repository/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ def _uid(self):
t = ["filesystem", self.location]
if os.path.exists(self.location):
st = os.stat(self.location)
t.append(st.st_ino)
t.append(int(st.st_ino))
return tuple(t)

def get_package_family(self, name):
Expand Down Expand Up @@ -678,7 +678,7 @@ def get_package_payload_path(self, package_name, package_version=None):
def _get_family_dirs__key(self):
if os.path.isdir(self.location):
st = os.stat(self.location)
return str(("listdir", self.location, st.st_ino, st.st_mtime))
return str(("listdir", self.location, int(st.st_ino), st.st_mtime))
else:
return str(("listdir", self.location))

Expand All @@ -705,7 +705,7 @@ def _get_family_dirs(self):

def _get_version_dirs__key(self, root):
st = os.stat(root)
return str(("listdir", root, st.st_ino, st.st_mtime))
return str(("listdir", root, int(st.st_ino), st.st_mtime))

@memcached(servers=config.memcached_uri if config.cache_listdir else None,
min_compress_len=config.memcached_listdir_min_compress_len,
Expand Down
2 changes: 1 addition & 1 deletion src/rezplugins/release_vcs/svn.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_last_changed_revision(client, url):
if not svn_entries:
raise ReleaseVCSError("svn.info2() returned no results on url %s" % url)
return svn_entries[0][1].last_changed_rev
except pysvn.ClientError, ce:
except pysvn.ClientError as ce:
raise ReleaseVCSError("svn.info2() raised ClientError: %s" % ce)


Expand Down
4 changes: 2 additions & 2 deletions src/rezplugins/shell/sh.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ def get_startup_sequence(cls, rcfile, norc, stdin, command):

def _bind_interactive_rez(self):
if config.set_prompt and self.settings.prompt:
self._addline('if [ -z "$REZ_STORED_PROMPT" ]; then export REZ_STORED_PROMPT="$PS1"; fi')
self._addline(r'if [ -z "$REZ_STORED_PROMPT" ]; then export REZ_STORED_PROMPT="$PS1"; fi')
if config.prefix_prompt:
cmd = 'export PS1="%s $REZ_STORED_PROMPT"'
else:
cmd = 'export PS1="$REZ_STORED_PROMPT%s "'
self._addline(cmd % "\[\e[1m\]$REZ_ENV_PROMPT\[\e[0m\]")
self._addline(cmd % r"\[\e[1m\]$REZ_ENV_PROMPT\[\e[0m\]")

def setenv(self, key, value):
value = self.escape_string(value)
Expand Down