diff options
Diffstat (limited to '')
-rw-r--r-- | pre_commit/clientlib.py | 47 | ||||
-rw-r--r-- | pre_commit/commands/install_uninstall.py | 32 | ||||
-rw-r--r-- | pre_commit/commands/run.py | 3 | ||||
-rw-r--r-- | pre_commit/error_handler.py | 5 | ||||
-rw-r--r-- | pre_commit/languages/all.py | 2 | ||||
-rw-r--r-- | pre_commit/languages/conda.py | 15 | ||||
-rw-r--r-- | pre_commit/languages/golang.py | 6 | ||||
-rw-r--r-- | pre_commit/languages/lua.py | 90 | ||||
-rw-r--r-- | pre_commit/languages/r.py | 11 | ||||
-rw-r--r-- | pre_commit/resources/empty_template_pre-commit-package-dev-1.rockspec | 12 | ||||
-rw-r--r-- | pre_commit/resources/rbenv.tar.gz | bin | 32678 -> 32551 bytes | |||
-rw-r--r-- | pre_commit/resources/ruby-build.tar.gz | bin | 68689 -> 71151 bytes | |||
-rw-r--r-- | pre_commit/store.py | 3 |
13 files changed, 168 insertions, 58 deletions
diff --git a/pre_commit/clientlib.py b/pre_commit/clientlib.py index 6377a8b..47ebd54 100644 --- a/pre_commit/clientlib.py +++ b/pre_commit/clientlib.py @@ -144,18 +144,13 @@ class OptionalSensibleRegexAtHook(cfgv.OptionalNoDefault): f"regex, not a glob -- matching '/*' probably isn't what you " f'want here', ) - if r'[\/]' in dct.get(self.key, ''): - logger.warning( - fr'pre-commit normalizes slashes in the {self.key!r} field ' - fr'in hook {dct.get("id")!r} to forward slashes, so you ' - fr'can use / instead of [\/]', - ) - if r'[/\\]' in dct.get(self.key, ''): - logger.warning( - fr'pre-commit normalizes slashes in the {self.key!r} field ' - fr'in hook {dct.get("id")!r} to forward slashes, so you ' - fr'can use / instead of [/\\]', - ) + for fwd_slash_re in (r'[\\/]', r'[\/]', r'[/\\]'): + if fwd_slash_re in dct.get(self.key, ''): + logger.warning( + fr'pre-commit normalizes slashes in the {self.key!r} ' + fr'field in hook {dct.get("id")!r} to forward slashes, ' + fr'so you can use / instead of {fwd_slash_re}', + ) class OptionalSensibleRegexAtTop(cfgv.OptionalNoDefault): @@ -167,18 +162,13 @@ class OptionalSensibleRegexAtTop(cfgv.OptionalNoDefault): f'The top-level {self.key!r} field is a regex, not a glob -- ' f"matching '/*' probably isn't what you want here", ) - if r'[\/]' in dct.get(self.key, ''): - logger.warning( - fr'pre-commit normalizes the slashes in the top-level ' - fr'{self.key!r} field to forward slashes, so you can use / ' - fr'instead of [\/]', - ) - if r'[/\\]' in dct.get(self.key, ''): - logger.warning( - fr'pre-commit normalizes the slashes in the top-level ' - fr'{self.key!r} field to forward slashes, so you can use / ' - fr'instead of [/\\]', - ) + for fwd_slash_re in (r'[\\/]', r'[\/]', r'[/\\]'): + if fwd_slash_re in dct.get(self.key, ''): + logger.warning( + fr'pre-commit normalizes the slashes in the top-level ' + fr'{self.key!r} field to forward slashes, so you ' + fr'can use / instead of {fwd_slash_re}', + ) class MigrateShaToRev: @@ -261,12 +251,21 @@ _meta = ( ), ) + +class NotAllowed(cfgv.OptionalNoDefault): + def check(self, dct: Dict[str, Any]) -> None: + if self.key in dct: + raise cfgv.ValidationError(f'{self.key!r} cannot be overridden') + + META_HOOK_DICT = cfgv.Map( 'Hook', 'id', cfgv.Required('id', cfgv.check_string), cfgv.Required('id', cfgv.check_one_of(tuple(k for k, _ in _meta))), # language must be system cfgv.Optional('language', cfgv.check_one_of({'system'}), 'system'), + # entry cannot be overridden + NotAllowed('entry', cfgv.check_any), *( # default to the hook definition for the meta hooks cfgv.ConditionalOptional(key, cfgv.check_any, value, 'id', hook_id) diff --git a/pre_commit/commands/install_uninstall.py b/pre_commit/commands/install_uninstall.py index 7974423..50c6443 100644 --- a/pre_commit/commands/install_uninstall.py +++ b/pre_commit/commands/install_uninstall.py @@ -1,4 +1,3 @@ -import itertools import logging import os.path import shlex @@ -31,10 +30,6 @@ PRIOR_HASHES = ( CURRENT_HASH = b'138fd403232d2ddd5efb44317e38bf03' TEMPLATE_START = '# start templated\n' TEMPLATE_END = '# end templated\n' -# Homebrew/homebrew-core#35825: be more timid about appropriate `PATH` -# #1312 os.defpath is too restrictive on BSD -POSIX_SEARCH_PATH = ('/usr/local/bin', '/usr/bin', '/bin') -SYS_EXE = os.path.basename(os.path.realpath(sys.executable)) def _hook_paths( @@ -54,26 +49,6 @@ def is_our_script(filename: str) -> bool: return any(h in contents for h in (CURRENT_HASH,) + PRIOR_HASHES) -def shebang() -> str: - if sys.platform == 'win32': - py, _ = os.path.splitext(SYS_EXE) - else: - exe_choices = [ - f'python{sys.version_info[0]}.{sys.version_info[1]}', - f'python{sys.version_info[0]}', - ] - # avoid searching for bare `python` as it's likely to be python 2 - if SYS_EXE != 'python': - exe_choices.append(SYS_EXE) - for path, exe in itertools.product(POSIX_SEARCH_PATH, exe_choices): - if os.access(os.path.join(path, exe), os.X_OK): - py = exe - break - else: - py = SYS_EXE - return f'#!/usr/bin/env {py}' - - def _install_hook_script( config_file: str, hook_type: str, @@ -107,6 +82,13 @@ def _install_hook_script( before, rest = contents.split(TEMPLATE_START) _, after = rest.split(TEMPLATE_END) + # on windows always use `/bin/sh` since `bash` might not be on PATH + # though we use bash-specific features `sh` on windows is actually + # bash in "POSIXLY_CORRECT" mode which still supports the features we + # use: subshells / arrays + if sys.platform == 'win32': # pragma: win32 cover + hook_file.write('#!/bin/sh\n') + hook_file.write(before + TEMPLATE_START) hook_file.write(f'INSTALL_PYTHON={shlex.quote(sys.executable)}\n') # TODO: python3.8+: shlex.join diff --git a/pre_commit/commands/run.py b/pre_commit/commands/run.py index 2714faf..f8ced0f 100644 --- a/pre_commit/commands/run.py +++ b/pre_commit/commands/run.py @@ -275,7 +275,6 @@ def _run_hooks( hooks: Sequence[Hook], skips: Set[str], args: argparse.Namespace, - environ: MutableMapping[str, str], ) -> int: """Actually run the hooks.""" cols = _compute_cols(hooks) @@ -416,7 +415,7 @@ def run( to_install = [hook for hook in hooks if hook.id not in skips] install_hook_envs(to_install, store) - return _run_hooks(config, hooks, skips, args, environ) + return _run_hooks(config, hooks, skips, args) # https://github.com/python/mypy/issues/7726 raise AssertionError('unreachable') diff --git a/pre_commit/error_handler.py b/pre_commit/error_handler.py index 023dd35..7e74b95 100644 --- a/pre_commit/error_handler.py +++ b/pre_commit/error_handler.py @@ -9,6 +9,7 @@ import pre_commit.constants as C from pre_commit import output from pre_commit.errors import FatalError from pre_commit.store import Store +from pre_commit.util import cmd_output_b from pre_commit.util import force_bytes @@ -21,6 +22,9 @@ def _log_and_exit( error_msg = f'{msg}: {type(exc).__name__}: '.encode() + force_bytes(exc) output.write_line_b(error_msg) + _, git_version_b, _ = cmd_output_b('git', '--version', retcode=None) + git_version = git_version_b.decode(errors='backslashreplace').rstrip() + storedir = Store().directory log_path = os.path.join(storedir, 'pre-commit.log') with contextlib.ExitStack() as ctx: @@ -38,6 +42,7 @@ def _log_and_exit( _log_line() _log_line('```') _log_line(f'pre-commit version: {C.VERSION}') + _log_line(f'git --version: {git_version}') _log_line('sys.version:') for line in sys.version.splitlines(): _log_line(f' {line}') diff --git a/pre_commit/languages/all.py b/pre_commit/languages/all.py index d8a364c..0bcedd6 100644 --- a/pre_commit/languages/all.py +++ b/pre_commit/languages/all.py @@ -13,6 +13,7 @@ from pre_commit.languages import docker_image from pre_commit.languages import dotnet from pre_commit.languages import fail from pre_commit.languages import golang +from pre_commit.languages import lua from pre_commit.languages import node from pre_commit.languages import perl from pre_commit.languages import pygrep @@ -51,6 +52,7 @@ languages = { 'dotnet': Language(name='dotnet', ENVIRONMENT_DIR=dotnet.ENVIRONMENT_DIR, get_default_version=dotnet.get_default_version, healthy=dotnet.healthy, install_environment=dotnet.install_environment, run_hook=dotnet.run_hook), # noqa: E501 'fail': Language(name='fail', ENVIRONMENT_DIR=fail.ENVIRONMENT_DIR, get_default_version=fail.get_default_version, healthy=fail.healthy, install_environment=fail.install_environment, run_hook=fail.run_hook), # noqa: E501 'golang': Language(name='golang', ENVIRONMENT_DIR=golang.ENVIRONMENT_DIR, get_default_version=golang.get_default_version, healthy=golang.healthy, install_environment=golang.install_environment, run_hook=golang.run_hook), # noqa: E501 + 'lua': Language(name='lua', ENVIRONMENT_DIR=lua.ENVIRONMENT_DIR, get_default_version=lua.get_default_version, healthy=lua.healthy, install_environment=lua.install_environment, run_hook=lua.run_hook), # noqa: E501 'node': Language(name='node', ENVIRONMENT_DIR=node.ENVIRONMENT_DIR, get_default_version=node.get_default_version, healthy=node.healthy, install_environment=node.install_environment, run_hook=node.run_hook), # noqa: E501 'perl': Language(name='perl', ENVIRONMENT_DIR=perl.ENVIRONMENT_DIR, get_default_version=perl.get_default_version, healthy=perl.healthy, install_environment=perl.install_environment, run_hook=perl.run_hook), # noqa: E501 'pygrep': Language(name='pygrep', ENVIRONMENT_DIR=pygrep.ENVIRONMENT_DIR, get_default_version=pygrep.get_default_version, healthy=pygrep.healthy, install_environment=pygrep.install_environment, run_hook=pygrep.run_hook), # noqa: E501 diff --git a/pre_commit/languages/conda.py b/pre_commit/languages/conda.py index d634e49..97e2f69 100644 --- a/pre_commit/languages/conda.py +++ b/pre_commit/languages/conda.py @@ -50,6 +50,15 @@ def in_env( yield +def _conda_exe() -> str: + if os.environ.get('PRE_COMMIT_USE_MICROMAMBA'): + return 'micromamba' + elif os.environ.get('PRE_COMMIT_USE_MAMBA'): + return 'mamba' + else: + return 'conda' + + def install_environment( prefix: Prefix, version: str, @@ -58,15 +67,17 @@ def install_environment( helpers.assert_version_default('conda', version) directory = helpers.environment_dir(ENVIRONMENT_DIR, version) + conda_exe = _conda_exe() + env_dir = prefix.path(directory) with clean_path_on_failure(env_dir): cmd_output_b( - 'conda', 'env', 'create', '-p', env_dir, '--file', + conda_exe, 'env', 'create', '-p', env_dir, '--file', 'environment.yml', cwd=prefix.prefix_dir, ) if additional_dependencies: cmd_output_b( - 'conda', 'install', '-p', env_dir, *additional_dependencies, + conda_exe, 'install', '-p', env_dir, *additional_dependencies, cwd=prefix.prefix_dir, ) diff --git a/pre_commit/languages/golang.py b/pre_commit/languages/golang.py index d6165d9..10ebc62 100644 --- a/pre_commit/languages/golang.py +++ b/pre_commit/languages/golang.py @@ -79,9 +79,11 @@ def install_environment( gopath = directory env = dict(os.environ, GOPATH=gopath) env.pop('GOBIN', None) - cmd_output_b('go', 'get', './...', cwd=repo_src_dir, env=env) + cmd_output_b('go', 'install', './...', cwd=repo_src_dir, env=env) for dependency in additional_dependencies: - cmd_output_b('go', 'get', dependency, cwd=repo_src_dir, env=env) + cmd_output_b( + 'go', 'install', dependency, cwd=repo_src_dir, env=env, + ) # Same some disk space, we don't need these after installation rmtree(prefix.path(directory, 'src')) pkgdir = prefix.path(directory, 'pkg') diff --git a/pre_commit/languages/lua.py b/pre_commit/languages/lua.py new file mode 100644 index 0000000..f699937 --- /dev/null +++ b/pre_commit/languages/lua.py @@ -0,0 +1,90 @@ +import contextlib +import os +import sys +from typing import Generator +from typing import Sequence +from typing import Tuple + +import pre_commit.constants as C +from pre_commit.envcontext import envcontext +from pre_commit.envcontext import PatchesT +from pre_commit.envcontext import Var +from pre_commit.hook import Hook +from pre_commit.languages import helpers +from pre_commit.prefix import Prefix +from pre_commit.util import clean_path_on_failure +from pre_commit.util import cmd_output + +ENVIRONMENT_DIR = 'lua_env' +get_default_version = helpers.basic_get_default_version +healthy = helpers.basic_healthy + + +def _get_lua_version() -> str: # pragma: win32 no cover + """Get the Lua version used in file paths.""" + _, stdout, _ = cmd_output('luarocks', 'config', '--lua-ver') + return stdout.strip() + + +def get_env_patch(d: str) -> PatchesT: # pragma: win32 no cover + version = _get_lua_version() + so_ext = 'dll' if sys.platform == 'win32' else 'so' + return ( + ('PATH', (os.path.join(d, 'bin'), os.pathsep, Var('PATH'))), + ( + 'LUA_PATH', ( + os.path.join(d, 'share', 'lua', version, '?.lua;'), + os.path.join(d, 'share', 'lua', version, '?', 'init.lua;;'), + ), + ), + ( + 'LUA_CPATH', + (os.path.join(d, 'lib', 'lua', version, f'?.{so_ext};;'),), + ), + ) + + +def _envdir(prefix: Prefix) -> str: # pragma: win32 no cover + directory = helpers.environment_dir(ENVIRONMENT_DIR, C.DEFAULT) + return prefix.path(directory) + + +@contextlib.contextmanager # pragma: win32 no cover +def in_env(prefix: Prefix) -> Generator[None, None, None]: + with envcontext(get_env_patch(_envdir(prefix))): + yield + + +def install_environment( + prefix: Prefix, + version: str, + additional_dependencies: Sequence[str], +) -> None: # pragma: win32 no cover + helpers.assert_version_default('lua', version) + + envdir = _envdir(prefix) + with clean_path_on_failure(envdir): + with in_env(prefix): + # luarocks doesn't bootstrap a tree prior to installing + # so ensure the directory exists. + os.makedirs(envdir, exist_ok=True) + + # Older luarocks (e.g., 2.4.2) expect the rockspec as an arg + for rockspec in prefix.star('.rockspec'): + make_cmd = ('luarocks', '--tree', envdir, 'make', rockspec) + helpers.run_setup_cmd(prefix, make_cmd) + + # luarocks can't install multiple packages at once + # so install them individually. + for dependency in additional_dependencies: + cmd = ('luarocks', '--tree', envdir, 'install', dependency) + helpers.run_setup_cmd(prefix, cmd) + + +def run_hook( + hook: Hook, + file_args: Sequence[str], + color: bool, +) -> Tuple[int, bytes]: # pragma: win32 no cover + with in_env(hook.prefix): + return helpers.run_xargs(hook, hook.cmd, file_args, color=color) diff --git a/pre_commit/languages/r.py b/pre_commit/languages/r.py index d573775..e034e39 100644 --- a/pre_commit/languages/r.py +++ b/pre_commit/languages/r.py @@ -8,6 +8,7 @@ from typing import Tuple from pre_commit.envcontext import envcontext from pre_commit.envcontext import PatchesT +from pre_commit.envcontext import UNSET from pre_commit.hook import Hook from pre_commit.languages import helpers from pre_commit.prefix import Prefix @@ -23,6 +24,7 @@ healthy = helpers.basic_healthy def get_env_patch(venv: str) -> PatchesT: return ( ('R_PROFILE_USER', os.path.join(venv, 'activate.R')), + ('RENV_PROJECT', UNSET), ) @@ -55,6 +57,10 @@ def _prefix_if_non_local_file_entry( return (path,) +def _rscript_exec() -> str: + return os.path.join(os.getenv('R_HOME', ''), 'Rscript') + + def _entry_validate(entry: Sequence[str]) -> None: """ Allowed entries: @@ -95,8 +101,9 @@ def install_environment( os.makedirs(env_dir, exist_ok=True) shutil.copy(prefix.path('renv.lock'), env_dir) shutil.copytree(prefix.path('renv'), os.path.join(env_dir, 'renv')) + cmd_output_b( - 'Rscript', '--vanilla', '-e', + _rscript_exec(), '--vanilla', '-e', f"""\ prefix_dir <- {prefix.prefix_dir!r} options( @@ -130,7 +137,7 @@ def install_environment( if additional_dependencies: with in_env(prefix, version): cmd_output_b( - 'Rscript', *RSCRIPT_OPTS, '-e', + _rscript_exec(), *RSCRIPT_OPTS, '-e', 'renv::install(commandArgs(trailingOnly = TRUE))', *additional_dependencies, cwd=env_dir, diff --git a/pre_commit/resources/empty_template_pre-commit-package-dev-1.rockspec b/pre_commit/resources/empty_template_pre-commit-package-dev-1.rockspec new file mode 100644 index 0000000..f063c8e --- /dev/null +++ b/pre_commit/resources/empty_template_pre-commit-package-dev-1.rockspec @@ -0,0 +1,12 @@ +package = "pre-commit-package" +version = "dev-1" + +source = { + url = "git+ssh://git@github.com/pre-commit/pre-commit.git" +} +description = {} +dependencies = {} +build = { + type = "builtin", + modules = {}, +} diff --git a/pre_commit/resources/rbenv.tar.gz b/pre_commit/resources/rbenv.tar.gz Binary files differindex 98b7e0f..da2514e 100644 --- a/pre_commit/resources/rbenv.tar.gz +++ b/pre_commit/resources/rbenv.tar.gz diff --git a/pre_commit/resources/ruby-build.tar.gz b/pre_commit/resources/ruby-build.tar.gz Binary files differindex afec5db..01867be 100644 --- a/pre_commit/resources/ruby-build.tar.gz +++ b/pre_commit/resources/ruby-build.tar.gz diff --git a/pre_commit/store.py b/pre_commit/store.py index fc3bc51..27d8553 100644 --- a/pre_commit/store.py +++ b/pre_commit/store.py @@ -188,7 +188,8 @@ class Store: LOCAL_RESOURCES = ( 'Cargo.toml', 'main.go', 'go.mod', 'main.rs', '.npmignore', - 'package.json', 'pre_commit_placeholder_package.gemspec', 'setup.py', + 'package.json', 'pre-commit-package-dev-1.rockspec', + 'pre_commit_placeholder_package.gemspec', 'setup.py', 'environment.yml', 'Makefile.PL', 'pubspec.yaml', 'renv.lock', 'renv/activate.R', 'renv/LICENSE.renv', ) |