mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-12 05:54:18 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
72ad6dc953
commit
f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions
|
|
@ -3,17 +3,20 @@
|
|||
Miscellaneous utility functions -- anything that doesn't fit into
|
||||
one of the other *util.py modules.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
import os
|
||||
import re
|
||||
import importlib.util
|
||||
import string
|
||||
import sys
|
||||
from distutils.errors import DistutilsPlatformError
|
||||
from distutils.dep_util import newer
|
||||
from distutils.spawn import spawn
|
||||
|
||||
from distutils import log
|
||||
from distutils.dep_util import newer
|
||||
from distutils.errors import DistutilsByteCompileError
|
||||
from distutils.errors import DistutilsPlatformError
|
||||
from distutils.spawn import spawn
|
||||
|
||||
from .py35compat import _optim_args_from_interpreter_flags
|
||||
|
||||
|
||||
|
|
@ -47,10 +50,10 @@ def get_host_platform():
|
|||
return sys.platform
|
||||
|
||||
# Set for cross builds explicitly
|
||||
if "_PYTHON_HOST_PLATFORM" in os.environ:
|
||||
return os.environ["_PYTHON_HOST_PLATFORM"]
|
||||
if '_PYTHON_HOST_PLATFORM' in os.environ:
|
||||
return os.environ['_PYTHON_HOST_PLATFORM']
|
||||
|
||||
if os.name != "posix" or not hasattr(os, 'uname'):
|
||||
if os.name != 'posix' or not hasattr(os, 'uname'):
|
||||
# XXX what about the architecture? NT is Intel or Alpha,
|
||||
# Mac OS is M68k or PPC, etc.
|
||||
return sys.platform
|
||||
|
|
@ -65,44 +68,47 @@ def get_host_platform():
|
|||
machine = machine.replace(' ', '_')
|
||||
machine = machine.replace('/', '-')
|
||||
|
||||
if osname[:5] == "linux":
|
||||
if osname[:5] == 'linux':
|
||||
# At least on Linux/Intel, 'machine' is the processor --
|
||||
# i386, etc.
|
||||
# XXX what about Alpha, SPARC, etc?
|
||||
return "%s-%s" % (osname, machine)
|
||||
elif osname[:5] == "sunos":
|
||||
if release[0] >= "5": # SunOS 5 == Solaris 2
|
||||
osname = "solaris"
|
||||
release = "%d.%s" % (int(release[0]) - 3, release[2:])
|
||||
return '{}-{}'.format(osname, machine)
|
||||
elif osname[:5] == 'sunos':
|
||||
if release[0] >= '5': # SunOS 5 == Solaris 2
|
||||
osname = 'solaris'
|
||||
release = '%d.%s' % (int(release[0]) - 3, release[2:])
|
||||
# We can't use "platform.architecture()[0]" because a
|
||||
# bootstrap problem. We use a dict to get an error
|
||||
# if some suspicious happens.
|
||||
bitness = {2147483647:"32bit", 9223372036854775807:"64bit"}
|
||||
machine += ".%s" % bitness[sys.maxsize]
|
||||
bitness = {2147483647: '32bit', 9223372036854775807: '64bit'}
|
||||
machine += '.%s' % bitness[sys.maxsize]
|
||||
# fall through to standard osname-release-machine representation
|
||||
elif osname[:3] == "aix":
|
||||
elif osname[:3] == 'aix':
|
||||
from .py38compat import aix_platform
|
||||
return aix_platform(osname, version, release)
|
||||
elif osname[:6] == "cygwin":
|
||||
osname = "cygwin"
|
||||
rel_re = re.compile (r'[\d.]+', re.ASCII)
|
||||
elif osname[:6] == 'cygwin':
|
||||
osname = 'cygwin'
|
||||
rel_re = re.compile(r'[\d.]+', re.ASCII)
|
||||
m = rel_re.match(release)
|
||||
if m:
|
||||
release = m.group()
|
||||
elif osname[:6] == "darwin":
|
||||
import _osx_support, distutils.sysconfig
|
||||
elif osname[:6] == 'darwin':
|
||||
import _osx_support
|
||||
import distutils.sysconfig
|
||||
osname, release, machine = _osx_support.get_platform_osx(
|
||||
distutils.sysconfig.get_config_vars(),
|
||||
osname, release, machine)
|
||||
distutils.sysconfig.get_config_vars(),
|
||||
osname, release, machine,
|
||||
)
|
||||
|
||||
return '{}-{}-{}'.format(osname, release, machine)
|
||||
|
||||
return "%s-%s-%s" % (osname, release, machine)
|
||||
|
||||
def get_platform():
|
||||
if os.name == 'nt':
|
||||
TARGET_TO_PLAT = {
|
||||
'x86' : 'win32',
|
||||
'x64' : 'win-amd64',
|
||||
'arm' : 'win-arm32',
|
||||
'x86': 'win32',
|
||||
'x64': 'win-amd64',
|
||||
'arm': 'win-arm32',
|
||||
'arm64': 'win-arm64',
|
||||
}
|
||||
return TARGET_TO_PLAT.get(os.environ.get('VSCMD_ARG_TGT_ARCH')) or get_host_platform()
|
||||
|
|
@ -111,14 +117,16 @@ def get_platform():
|
|||
|
||||
|
||||
if sys.platform == 'darwin':
|
||||
_syscfg_macosx_ver = None # cache the version pulled from sysconfig
|
||||
_syscfg_macosx_ver = None # cache the version pulled from sysconfig
|
||||
MACOSX_VERSION_VAR = 'MACOSX_DEPLOYMENT_TARGET'
|
||||
|
||||
|
||||
def _clear_cached_macosx_ver():
|
||||
"""For testing only. Do not call."""
|
||||
global _syscfg_macosx_ver
|
||||
_syscfg_macosx_ver = None
|
||||
|
||||
|
||||
def get_macosx_target_ver_from_syscfg():
|
||||
"""Get the version of macOS latched in the Python interpreter configuration.
|
||||
Returns the version as a string or None if can't obtain one. Cached."""
|
||||
|
|
@ -130,6 +138,7 @@ def get_macosx_target_ver_from_syscfg():
|
|||
_syscfg_macosx_ver = ver
|
||||
return _syscfg_macosx_ver
|
||||
|
||||
|
||||
def get_macosx_target_ver():
|
||||
"""Return the version of macOS for which we are building.
|
||||
|
||||
|
|
@ -148,11 +157,13 @@ def get_macosx_target_ver():
|
|||
# values, specifically LDSHARED which can use
|
||||
# '-undefined dynamic_lookup' which only works on >= 10.3.
|
||||
if syscfg_ver and split_version(syscfg_ver) >= [10, 3] and \
|
||||
split_version(env_ver) < [10, 3]:
|
||||
my_msg = ('$' + MACOSX_VERSION_VAR + ' mismatch: '
|
||||
'now "%s" but "%s" during configure; '
|
||||
'must use 10.3 or later'
|
||||
% (env_ver, syscfg_ver))
|
||||
split_version(env_ver) < [10, 3]:
|
||||
my_msg = (
|
||||
'$' + MACOSX_VERSION_VAR + ' mismatch: '
|
||||
'now "%s" but "%s" during configure; '
|
||||
'must use 10.3 or later'
|
||||
% (env_ver, syscfg_ver)
|
||||
)
|
||||
raise DistutilsPlatformError(my_msg)
|
||||
return env_ver
|
||||
return syscfg_ver
|
||||
|
|
@ -163,7 +174,7 @@ def split_version(s):
|
|||
return [int(n) for n in s.split('.')]
|
||||
|
||||
|
||||
def convert_path (pathname):
|
||||
def convert_path(pathname):
|
||||
"""Return 'pathname' as a name that will work on the native filesystem,
|
||||
i.e. split it on '/' and put it back together again using the current
|
||||
directory separator. Needed because filenames in the setup script are
|
||||
|
|
@ -191,7 +202,7 @@ def convert_path (pathname):
|
|||
# convert_path ()
|
||||
|
||||
|
||||
def change_root (new_root, pathname):
|
||||
def change_root(new_root, pathname):
|
||||
"""Return 'pathname' with 'new_root' prepended. If 'pathname' is
|
||||
relative, this is equivalent to "os.path.join(new_root,pathname)".
|
||||
Otherwise, it requires making 'pathname' relative and then joining the
|
||||
|
|
@ -214,7 +225,9 @@ def change_root (new_root, pathname):
|
|||
|
||||
|
||||
_environ_checked = 0
|
||||
def check_environ ():
|
||||
|
||||
|
||||
def check_environ():
|
||||
"""Ensure that 'os.environ' has all the environment variables we
|
||||
guarantee that users can use in config files, command-line options,
|
||||
etc. Currently this includes:
|
||||
|
|
@ -241,7 +254,7 @@ def check_environ ():
|
|||
_environ_checked = 1
|
||||
|
||||
|
||||
def subst_vars (s, local_vars):
|
||||
def subst_vars(s, local_vars):
|
||||
"""Perform shell/Perl-style variable substitution on 'string'. Every
|
||||
occurrence of '$' followed by a name is considered a variable, and
|
||||
variable is substituted by the value found in the 'local_vars'
|
||||
|
|
@ -251,7 +264,8 @@ def subst_vars (s, local_vars):
|
|||
variables not found in either 'local_vars' or 'os.environ'.
|
||||
"""
|
||||
check_environ()
|
||||
def _subst (match, local_vars=local_vars):
|
||||
|
||||
def _subst(match, local_vars=local_vars):
|
||||
var_name = match.group(1)
|
||||
if var_name in local_vars:
|
||||
return str(local_vars[var_name])
|
||||
|
|
@ -266,7 +280,7 @@ def subst_vars (s, local_vars):
|
|||
# subst_vars ()
|
||||
|
||||
|
||||
def grok_environment_error (exc, prefix="error: "):
|
||||
def grok_environment_error(exc, prefix='error: '):
|
||||
# Function kept for backward compatibility.
|
||||
# Used to try clever things with EnvironmentErrors,
|
||||
# but nowadays str(exception) produces good messages.
|
||||
|
|
@ -275,13 +289,16 @@ def grok_environment_error (exc, prefix="error: "):
|
|||
|
||||
# Needed by 'split_quoted()'
|
||||
_wordchars_re = _squote_re = _dquote_re = None
|
||||
|
||||
|
||||
def _init_regex():
|
||||
global _wordchars_re, _squote_re, _dquote_re
|
||||
_wordchars_re = re.compile(r'[^\\\'\"%s ]*' % string.whitespace)
|
||||
_squote_re = re.compile(r"'(?:[^'\\]|\\.)*'")
|
||||
_dquote_re = re.compile(r'"(?:[^"\\]|\\.)*"')
|
||||
|
||||
def split_quoted (s):
|
||||
|
||||
def split_quoted(s):
|
||||
"""Split a string up according to Unix shell-like rules for quotes and
|
||||
backslashes. In short: words are delimited by spaces, as long as those
|
||||
spaces are not escaped by a backslash, or inside a quoted string.
|
||||
|
|
@ -295,7 +312,8 @@ def split_quoted (s):
|
|||
# This is a nice algorithm for splitting up a single string, since it
|
||||
# doesn't require character-by-character examination. It was a little
|
||||
# bit of a brain-bender to get it working right, though...
|
||||
if _wordchars_re is None: _init_regex()
|
||||
if _wordchars_re is None:
|
||||
_init_regex()
|
||||
|
||||
s = s.strip()
|
||||
words = []
|
||||
|
|
@ -308,15 +326,15 @@ def split_quoted (s):
|
|||
words.append(s[:end])
|
||||
break
|
||||
|
||||
if s[end] in string.whitespace: # unescaped, unquoted whitespace: now
|
||||
if s[end] in string.whitespace: # unescaped, unquoted whitespace: now
|
||||
words.append(s[:end]) # we definitely have a word delimiter
|
||||
s = s[end:].lstrip()
|
||||
pos = 0
|
||||
|
||||
elif s[end] == '\\': # preserve whatever is being escaped;
|
||||
# will become part of the current word
|
||||
s = s[:end] + s[end+1:]
|
||||
pos = end+1
|
||||
# will become part of the current word
|
||||
s = s[:end] + s[end + 1:]
|
||||
pos = end + 1
|
||||
|
||||
else:
|
||||
if s[end] == "'": # slurp singly-quoted string
|
||||
|
|
@ -327,10 +345,10 @@ def split_quoted (s):
|
|||
raise RuntimeError("this can't happen (bad char '%c')" % s[end])
|
||||
|
||||
if m is None:
|
||||
raise ValueError("bad string (mismatched %s quotes?)" % s[end])
|
||||
raise ValueError('bad string (mismatched %s quotes?)' % s[end])
|
||||
|
||||
(beg, end) = m.span()
|
||||
s = s[:beg] + s[beg+1:end-1] + s[end:]
|
||||
s = s[:beg] + s[beg + 1:end - 1] + s[end:]
|
||||
pos = m.end() - 2
|
||||
|
||||
if pos >= len(s):
|
||||
|
|
@ -342,7 +360,7 @@ def split_quoted (s):
|
|||
# split_quoted ()
|
||||
|
||||
|
||||
def execute (func, args, msg=None, verbose=0, dry_run=0):
|
||||
def execute(func, args, msg=None, verbose=0, dry_run=0):
|
||||
"""Perform some action that affects the outside world (eg. by
|
||||
writing to the filesystem). Such actions are special because they
|
||||
are disabled by the 'dry_run' flag. This method takes care of all
|
||||
|
|
@ -352,7 +370,7 @@ def execute (func, args, msg=None, verbose=0, dry_run=0):
|
|||
print.
|
||||
"""
|
||||
if msg is None:
|
||||
msg = "%s%r" % (func.__name__, args)
|
||||
msg = '{}{!r}'.format(func.__name__, args)
|
||||
if msg[-2:] == ',)': # correct for singleton tuple
|
||||
msg = msg[0:-2] + ')'
|
||||
|
||||
|
|
@ -361,7 +379,7 @@ def execute (func, args, msg=None, verbose=0, dry_run=0):
|
|||
func(*args)
|
||||
|
||||
|
||||
def strtobool (val):
|
||||
def strtobool(val):
|
||||
"""Convert a string representation of truth to true (1) or false (0).
|
||||
|
||||
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
|
||||
|
|
@ -374,14 +392,16 @@ def strtobool (val):
|
|||
elif val in ('n', 'no', 'f', 'false', 'off', '0'):
|
||||
return 0
|
||||
else:
|
||||
raise ValueError("invalid truth value %r" % (val,))
|
||||
raise ValueError('invalid truth value {!r}'.format(val))
|
||||
|
||||
|
||||
def byte_compile (py_files,
|
||||
optimize=0, force=0,
|
||||
prefix=None, base_dir=None,
|
||||
verbose=1, dry_run=0,
|
||||
direct=None):
|
||||
def byte_compile(
|
||||
py_files,
|
||||
optimize=0, force=0,
|
||||
prefix=None, base_dir=None,
|
||||
verbose=1, dry_run=0,
|
||||
direct=None,
|
||||
):
|
||||
"""Byte-compile a collection of Python source files to .pyc
|
||||
files in a __pycache__ subdirectory. 'py_files' is a list
|
||||
of files to compile; any files that don't end in ".py" are silently
|
||||
|
|
@ -437,16 +457,16 @@ def byte_compile (py_files,
|
|||
if not direct:
|
||||
try:
|
||||
from tempfile import mkstemp
|
||||
(script_fd, script_name) = mkstemp(".py")
|
||||
(script_fd, script_name) = mkstemp('.py')
|
||||
except ImportError:
|
||||
from tempfile import mktemp
|
||||
(script_fd, script_name) = None, mktemp(".py")
|
||||
(script_fd, script_name) = None, mktemp('.py')
|
||||
log.info("writing byte-compilation script '%s'", script_name)
|
||||
if not dry_run:
|
||||
if script_fd is not None:
|
||||
script = os.fdopen(script_fd, "w")
|
||||
script = os.fdopen(script_fd, 'w')
|
||||
else:
|
||||
script = open(script_name, "w")
|
||||
script = open(script_name, 'w')
|
||||
|
||||
with script:
|
||||
script.write("""\
|
||||
|
|
@ -468,20 +488,24 @@ files = [
|
|||
#if prefix:
|
||||
# prefix = os.path.abspath(prefix)
|
||||
|
||||
script.write(",\n".join(map(repr, py_files)) + "]\n")
|
||||
script.write("""
|
||||
byte_compile(files, optimize=%r, force=%r,
|
||||
prefix=%r, base_dir=%r,
|
||||
verbose=%r, dry_run=0,
|
||||
script.write(',\n'.join(map(repr, py_files)) + ']\n')
|
||||
script.write(
|
||||
"""
|
||||
byte_compile(files, optimize={!r}, force={!r},
|
||||
prefix={!r}, base_dir={!r},
|
||||
verbose={!r}, dry_run=0,
|
||||
direct=1)
|
||||
""" % (optimize, force, prefix, base_dir, verbose))
|
||||
""".format(optimize, force, prefix, base_dir, verbose),
|
||||
)
|
||||
|
||||
cmd = [sys.executable]
|
||||
cmd.extend(_optim_args_from_interpreter_flags())
|
||||
cmd.append(script_name)
|
||||
spawn(cmd, dry_run=dry_run)
|
||||
execute(os.remove, (script_name,), "removing %s" % script_name,
|
||||
dry_run=dry_run)
|
||||
execute(
|
||||
os.remove, (script_name,), 'removing %s' % script_name,
|
||||
dry_run=dry_run,
|
||||
)
|
||||
|
||||
# "Direct" byte-compilation: use the py_compile module to compile
|
||||
# right here, right now. Note that the script generated in indirect
|
||||
|
|
@ -491,7 +515,7 @@ byte_compile(files, optimize=%r, force=%r,
|
|||
from py_compile import compile
|
||||
|
||||
for file in py_files:
|
||||
if file[-3:] != ".py":
|
||||
if file[-3:] != '.py':
|
||||
# This lets us be lazy and not filter filenames in
|
||||
# the "install_lib" command.
|
||||
continue
|
||||
|
|
@ -502,14 +526,17 @@ byte_compile(files, optimize=%r, force=%r,
|
|||
if optimize >= 0:
|
||||
opt = '' if optimize == 0 else optimize
|
||||
cfile = importlib.util.cache_from_source(
|
||||
file, optimization=opt)
|
||||
file, optimization=opt,
|
||||
)
|
||||
else:
|
||||
cfile = importlib.util.cache_from_source(file)
|
||||
dfile = file
|
||||
if prefix:
|
||||
if file[:len(prefix)] != prefix:
|
||||
raise ValueError("invalid prefix: filename %r doesn't start with %r"
|
||||
% (file, prefix))
|
||||
raise ValueError(
|
||||
"invalid prefix: filename %r doesn't start with %r"
|
||||
% (file, prefix),
|
||||
)
|
||||
dfile = dfile[len(prefix):]
|
||||
if base_dir:
|
||||
dfile = os.path.join(base_dir, dfile)
|
||||
|
|
@ -517,16 +544,19 @@ byte_compile(files, optimize=%r, force=%r,
|
|||
cfile_base = os.path.basename(cfile)
|
||||
if direct:
|
||||
if force or newer(file, cfile):
|
||||
log.info("byte-compiling %s to %s", file, cfile_base)
|
||||
log.info('byte-compiling %s to %s', file, cfile_base)
|
||||
if not dry_run:
|
||||
compile(file, cfile, dfile)
|
||||
else:
|
||||
log.debug("skipping byte-compilation of %s to %s",
|
||||
file, cfile_base)
|
||||
log.debug(
|
||||
'skipping byte-compilation of %s to %s',
|
||||
file, cfile_base,
|
||||
)
|
||||
|
||||
# byte_compile ()
|
||||
|
||||
def rfc822_escape (header):
|
||||
|
||||
def rfc822_escape(header):
|
||||
"""Return a version of the string escaped for inclusion in an
|
||||
RFC-822 header, by ensuring there are 8 spaces space after each newline.
|
||||
"""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue