pre-commit-ex 4.5.1__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. pre_commit/__init__.py +0 -0
  2. pre_commit/__main__.py +7 -0
  3. pre_commit/all_languages.py +50 -0
  4. pre_commit/clientlib.py +551 -0
  5. pre_commit/color.py +109 -0
  6. pre_commit/commands/__init__.py +0 -0
  7. pre_commit/commands/autoupdate.py +215 -0
  8. pre_commit/commands/clean.py +16 -0
  9. pre_commit/commands/gc.py +98 -0
  10. pre_commit/commands/hazmat.py +95 -0
  11. pre_commit/commands/hook_impl.py +272 -0
  12. pre_commit/commands/init_templatedir.py +39 -0
  13. pre_commit/commands/install_uninstall.py +167 -0
  14. pre_commit/commands/migrate_config.py +135 -0
  15. pre_commit/commands/run.py +474 -0
  16. pre_commit/commands/sample_config.py +18 -0
  17. pre_commit/commands/try_repo.py +77 -0
  18. pre_commit/commands/validate_config.py +18 -0
  19. pre_commit/commands/validate_manifest.py +18 -0
  20. pre_commit/constants.py +13 -0
  21. pre_commit/envcontext.py +62 -0
  22. pre_commit/error_handler.py +81 -0
  23. pre_commit/errors.py +5 -0
  24. pre_commit/file_lock.py +75 -0
  25. pre_commit/git.py +245 -0
  26. pre_commit/hook.py +60 -0
  27. pre_commit/lang_base.py +196 -0
  28. pre_commit/languages/__init__.py +0 -0
  29. pre_commit/languages/conda.py +77 -0
  30. pre_commit/languages/coursier.py +76 -0
  31. pre_commit/languages/dart.py +97 -0
  32. pre_commit/languages/docker.py +181 -0
  33. pre_commit/languages/docker_image.py +32 -0
  34. pre_commit/languages/dotnet.py +111 -0
  35. pre_commit/languages/fail.py +27 -0
  36. pre_commit/languages/golang.py +161 -0
  37. pre_commit/languages/haskell.py +56 -0
  38. pre_commit/languages/julia.py +133 -0
  39. pre_commit/languages/lua.py +75 -0
  40. pre_commit/languages/node.py +110 -0
  41. pre_commit/languages/perl.py +50 -0
  42. pre_commit/languages/pygrep.py +133 -0
  43. pre_commit/languages/python.py +228 -0
  44. pre_commit/languages/r.py +278 -0
  45. pre_commit/languages/ruby.py +145 -0
  46. pre_commit/languages/rust.py +160 -0
  47. pre_commit/languages/swift.py +50 -0
  48. pre_commit/languages/unsupported.py +10 -0
  49. pre_commit/languages/unsupported_script.py +32 -0
  50. pre_commit/logging_handler.py +42 -0
  51. pre_commit/main.py +472 -0
  52. pre_commit/meta_hooks/__init__.py +0 -0
  53. pre_commit/meta_hooks/check_hooks_apply.py +43 -0
  54. pre_commit/meta_hooks/check_useless_excludes.py +83 -0
  55. pre_commit/meta_hooks/identity.py +17 -0
  56. pre_commit/output.py +33 -0
  57. pre_commit/parse_shebang.py +85 -0
  58. pre_commit/prefix.py +18 -0
  59. pre_commit/repository.py +237 -0
  60. pre_commit/resources/__init__.py +0 -0
  61. pre_commit/resources/empty_template_.npmignore +1 -0
  62. pre_commit/resources/empty_template_Cargo.toml +7 -0
  63. pre_commit/resources/empty_template_LICENSE.renv +7 -0
  64. pre_commit/resources/empty_template_Makefile.PL +6 -0
  65. pre_commit/resources/empty_template_activate.R +440 -0
  66. pre_commit/resources/empty_template_environment.yml +9 -0
  67. pre_commit/resources/empty_template_go.mod +1 -0
  68. pre_commit/resources/empty_template_main.go +3 -0
  69. pre_commit/resources/empty_template_main.rs +1 -0
  70. pre_commit/resources/empty_template_package.json +4 -0
  71. pre_commit/resources/empty_template_pre-commit-package-dev-1.rockspec +12 -0
  72. pre_commit/resources/empty_template_pre_commit_placeholder_package.gemspec +6 -0
  73. pre_commit/resources/empty_template_pubspec.yaml +4 -0
  74. pre_commit/resources/empty_template_renv.lock +20 -0
  75. pre_commit/resources/empty_template_setup.py +4 -0
  76. pre_commit/resources/hook-tmpl +20 -0
  77. pre_commit/resources/rbenv.tar.gz +0 -0
  78. pre_commit/resources/ruby-build.tar.gz +0 -0
  79. pre_commit/resources/ruby-download.tar.gz +0 -0
  80. pre_commit/staged_files_only.py +113 -0
  81. pre_commit/store.py +235 -0
  82. pre_commit/util.py +239 -0
  83. pre_commit/xargs.py +184 -0
  84. pre_commit/yaml.py +19 -0
  85. pre_commit/yaml_rewrite.py +52 -0
  86. pre_commit_ex-4.5.1.dist-info/METADATA +63 -0
  87. pre_commit_ex-4.5.1.dist-info/RECORD +91 -0
  88. pre_commit_ex-4.5.1.dist-info/WHEEL +6 -0
  89. pre_commit_ex-4.5.1.dist-info/entry_points.txt +2 -0
  90. pre_commit_ex-4.5.1.dist-info/licenses/LICENSE +19 -0
  91. pre_commit_ex-4.5.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,85 @@
1
+ from __future__ import annotations
2
+
3
+ import os.path
4
+ from collections.abc import Mapping
5
+ from typing import NoReturn
6
+
7
+ from identify.identify import parse_shebang_from_file
8
+
9
+
10
+ class ExecutableNotFoundError(OSError):
11
+ def to_output(self) -> tuple[int, bytes, None]:
12
+ return (1, self.args[0].encode(), None)
13
+
14
+
15
+ def parse_filename(filename: str) -> tuple[str, ...]:
16
+ if not os.path.exists(filename):
17
+ return ()
18
+ else:
19
+ return parse_shebang_from_file(filename)
20
+
21
+
22
+ def find_executable(
23
+ exe: str, *, env: Mapping[str, str] | None = None,
24
+ ) -> str | None:
25
+ exe = os.path.normpath(exe)
26
+ if os.sep in exe:
27
+ return exe
28
+
29
+ environ = env if env is not None else os.environ
30
+
31
+ if 'PATHEXT' in environ:
32
+ exts = environ['PATHEXT'].split(os.pathsep)
33
+ possible_exe_names = tuple(f'{exe}{ext}' for ext in exts) + (exe,)
34
+ else:
35
+ possible_exe_names = (exe,)
36
+
37
+ for path in environ.get('PATH', '').split(os.pathsep):
38
+ for possible_exe_name in possible_exe_names:
39
+ joined = os.path.join(path, possible_exe_name)
40
+ if os.path.isfile(joined) and os.access(joined, os.X_OK):
41
+ return joined
42
+ else:
43
+ return None
44
+
45
+
46
+ def normexe(orig: str, *, env: Mapping[str, str] | None = None) -> str:
47
+ def _error(msg: str) -> NoReturn:
48
+ raise ExecutableNotFoundError(f'Executable `{orig}` {msg}')
49
+
50
+ if os.sep not in orig and (not os.altsep or os.altsep not in orig):
51
+ exe = find_executable(orig, env=env)
52
+ if exe is None:
53
+ _error('not found')
54
+ return exe
55
+ elif os.path.isdir(orig):
56
+ _error('is a directory')
57
+ elif not os.path.isfile(orig):
58
+ _error('not found')
59
+ elif not os.access(orig, os.X_OK): # pragma: win32 no cover
60
+ _error('is not executable')
61
+ else:
62
+ return orig
63
+
64
+
65
+ def normalize_cmd(
66
+ cmd: tuple[str, ...],
67
+ *,
68
+ env: Mapping[str, str] | None = None,
69
+ ) -> tuple[str, ...]:
70
+ """Fixes for the following issues on windows
71
+ - https://bugs.python.org/issue8557
72
+ - windows does not parse shebangs
73
+
74
+ This function also makes deep-path shebangs work just fine
75
+ """
76
+ # Use PATH to determine the executable
77
+ exe = normexe(cmd[0], env=env)
78
+
79
+ # Figure out the shebang from the resulting command
80
+ cmd = parse_filename(exe) + (exe,) + cmd[1:]
81
+
82
+ # This could have given us back another bare executable
83
+ exe = normexe(cmd[0], env=env)
84
+
85
+ return (exe,) + cmd[1:]
pre_commit/prefix.py ADDED
@@ -0,0 +1,18 @@
1
+ from __future__ import annotations
2
+
3
+ import os.path
4
+ from typing import NamedTuple
5
+
6
+
7
+ class Prefix(NamedTuple):
8
+ prefix_dir: str
9
+
10
+ def path(self, *parts: str) -> str:
11
+ return os.path.normpath(os.path.join(self.prefix_dir, *parts))
12
+
13
+ def exists(self, *parts: str) -> bool:
14
+ return os.path.exists(self.path(*parts))
15
+
16
+ def star(self, end: str) -> tuple[str, ...]:
17
+ paths = os.listdir(self.prefix_dir)
18
+ return tuple(path for path in paths if path.endswith(end))
@@ -0,0 +1,237 @@
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ import logging
5
+ import os
6
+ from collections.abc import Sequence
7
+ from typing import Any
8
+
9
+ import pre_commit.constants as C
10
+ from pre_commit.all_languages import languages
11
+ from pre_commit.clientlib import load_manifest
12
+ from pre_commit.clientlib import LOCAL
13
+ from pre_commit.clientlib import META
14
+ from pre_commit.hook import Hook
15
+ from pre_commit.lang_base import environment_dir
16
+ from pre_commit.prefix import Prefix
17
+ from pre_commit.store import Store
18
+ from pre_commit.util import clean_path_on_failure
19
+ from pre_commit.util import rmtree
20
+
21
+
22
+ logger = logging.getLogger('pre_commit')
23
+
24
+
25
+ def _state_filename_v1(venv: str) -> str:
26
+ return os.path.join(venv, '.install_state_v1')
27
+
28
+
29
+ def _state_filename_v2(venv: str) -> str:
30
+ return os.path.join(venv, '.install_state_v2')
31
+
32
+
33
+ def _state(additional_deps: Sequence[str]) -> object:
34
+ return {'additional_dependencies': additional_deps}
35
+
36
+
37
+ def _read_state(venv: str) -> object | None:
38
+ filename = _state_filename_v1(venv)
39
+ if not os.path.exists(filename):
40
+ return None
41
+ else:
42
+ with open(filename) as f:
43
+ return json.load(f)
44
+
45
+
46
+ def _hook_installed(hook: Hook) -> bool:
47
+ lang = languages[hook.language]
48
+ if lang.ENVIRONMENT_DIR is None:
49
+ return True
50
+
51
+ venv = environment_dir(
52
+ hook.prefix,
53
+ lang.ENVIRONMENT_DIR,
54
+ hook.language_version,
55
+ )
56
+ return (
57
+ (
58
+ os.path.exists(_state_filename_v2(venv)) or
59
+ _read_state(venv) == _state(hook.additional_dependencies)
60
+ ) and
61
+ not lang.health_check(hook.prefix, hook.language_version)
62
+ )
63
+
64
+
65
+ def _hook_install(hook: Hook) -> None:
66
+ logger.info(f'Installing environment for {hook.src}.')
67
+ logger.info('Once installed this environment will be reused.')
68
+ logger.info('This may take a few minutes...')
69
+
70
+ lang = languages[hook.language]
71
+ assert lang.ENVIRONMENT_DIR is not None
72
+
73
+ venv = environment_dir(
74
+ hook.prefix,
75
+ lang.ENVIRONMENT_DIR,
76
+ hook.language_version,
77
+ )
78
+
79
+ # There's potentially incomplete cleanup from previous runs
80
+ # Clean it up!
81
+ if os.path.exists(venv):
82
+ rmtree(venv)
83
+
84
+ with clean_path_on_failure(venv):
85
+ lang.install_environment(
86
+ hook.prefix, hook.language_version, hook.additional_dependencies,
87
+ )
88
+ health_error = lang.health_check(hook.prefix, hook.language_version)
89
+ if health_error:
90
+ raise AssertionError(
91
+ f'BUG: expected environment for {hook.language} to be healthy '
92
+ f'immediately after install, please open an issue describing '
93
+ f'your environment\n\n'
94
+ f'more info:\n\n{health_error}',
95
+ )
96
+
97
+ # TODO: remove v1 state writing, no longer needed after pre-commit 3.0
98
+ # Write our state to indicate we're installed
99
+ state_filename = _state_filename_v1(venv)
100
+ staging = f'{state_filename}staging'
101
+ with open(staging, 'w') as state_file:
102
+ state_file.write(json.dumps(_state(hook.additional_dependencies)))
103
+ # Move the file into place atomically to indicate we've installed
104
+ os.replace(staging, state_filename)
105
+
106
+ open(_state_filename_v2(venv), 'a+').close()
107
+
108
+
109
+ def _hook(
110
+ *hook_dicts: dict[str, Any],
111
+ root_config: dict[str, Any],
112
+ ) -> dict[str, Any]:
113
+ ret, rest = dict(hook_dicts[0]), hook_dicts[1:]
114
+ for dct in rest:
115
+ ret.update(dct)
116
+
117
+ lang = ret['language']
118
+ if ret['language_version'] == C.DEFAULT:
119
+ ret['language_version'] = root_config['default_language_version'][lang]
120
+ if ret['language_version'] == C.DEFAULT:
121
+ ret['language_version'] = languages[lang].get_default_version()
122
+
123
+ if not ret['stages']:
124
+ ret['stages'] = root_config['default_stages']
125
+
126
+ if languages[lang].ENVIRONMENT_DIR is None:
127
+ if ret['language_version'] != C.DEFAULT:
128
+ logger.error(
129
+ f'The hook `{ret["id"]}` specifies `language_version` but is '
130
+ f'using language `{lang}` which does not install an '
131
+ f'environment. '
132
+ f'Perhaps you meant to use a specific language?',
133
+ )
134
+ exit(1)
135
+ if ret['additional_dependencies']:
136
+ logger.error(
137
+ f'The hook `{ret["id"]}` specifies `additional_dependencies` '
138
+ f'but is using language `{lang}` which does not install an '
139
+ f'environment. '
140
+ f'Perhaps you meant to use a specific language?',
141
+ )
142
+ exit(1)
143
+
144
+ return ret
145
+
146
+
147
+ def _non_cloned_repository_hooks(
148
+ repo_config: dict[str, Any],
149
+ store: Store,
150
+ root_config: dict[str, Any],
151
+ ) -> tuple[Hook, ...]:
152
+ def _prefix(language_name: str, deps: Sequence[str]) -> Prefix:
153
+ language = languages[language_name]
154
+ # pygrep / script / system / docker_image do not have
155
+ # environments so they work out of the current directory
156
+ if language.ENVIRONMENT_DIR is None:
157
+ return Prefix(os.getcwd())
158
+ else:
159
+ return Prefix(store.make_local(deps))
160
+
161
+ return tuple(
162
+ Hook.create(
163
+ repo_config['repo'],
164
+ _prefix(hook['language'], hook['additional_dependencies']),
165
+ _hook(hook, root_config=root_config),
166
+ )
167
+ for hook in repo_config['hooks']
168
+ )
169
+
170
+
171
+ def _cloned_repository_hooks(
172
+ repo_config: dict[str, Any],
173
+ store: Store,
174
+ root_config: dict[str, Any],
175
+ ) -> tuple[Hook, ...]:
176
+ repo, rev = repo_config['repo'], repo_config['rev']
177
+ manifest_path = os.path.join(store.clone(repo, rev), C.MANIFEST_FILE)
178
+ by_id = {hook['id']: hook for hook in load_manifest(manifest_path)}
179
+
180
+ for hook in repo_config['hooks']:
181
+ if hook['id'] not in by_id:
182
+ logger.error(
183
+ f'`{hook["id"]}` is not present in repository {repo}. '
184
+ f'Typo? Perhaps it is introduced in a newer version? '
185
+ f'Often `pre-commit autoupdate` fixes this.',
186
+ )
187
+ exit(1)
188
+
189
+ hook_dcts = [
190
+ _hook(by_id[hook['id']], hook, root_config=root_config)
191
+ for hook in repo_config['hooks']
192
+ ]
193
+ return tuple(
194
+ Hook.create(
195
+ repo_config['repo'],
196
+ Prefix(store.clone(repo, rev, hook['additional_dependencies'])),
197
+ hook,
198
+ )
199
+ for hook in hook_dcts
200
+ )
201
+
202
+
203
+ def _repository_hooks(
204
+ repo_config: dict[str, Any],
205
+ store: Store,
206
+ root_config: dict[str, Any],
207
+ ) -> tuple[Hook, ...]:
208
+ if repo_config['repo'] in {LOCAL, META}:
209
+ return _non_cloned_repository_hooks(repo_config, store, root_config)
210
+ else:
211
+ return _cloned_repository_hooks(repo_config, store, root_config)
212
+
213
+
214
+ def install_hook_envs(hooks: Sequence[Hook], store: Store) -> None:
215
+ def _need_installed() -> list[Hook]:
216
+ seen: set[tuple[Prefix, str, str, tuple[str, ...]]] = set()
217
+ ret = []
218
+ for hook in hooks:
219
+ if hook.install_key not in seen and not _hook_installed(hook):
220
+ ret.append(hook)
221
+ seen.add(hook.install_key)
222
+ return ret
223
+
224
+ if not _need_installed():
225
+ return
226
+ with store.exclusive_lock():
227
+ # Another process may have already completed this work
228
+ for hook in _need_installed():
229
+ _hook_install(hook)
230
+
231
+
232
+ def all_hooks(root_config: dict[str, Any], store: Store) -> tuple[Hook, ...]:
233
+ return tuple(
234
+ hook
235
+ for repo in root_config['repos']
236
+ for hook in _repository_hooks(repo, store, root_config)
237
+ )
File without changes
@@ -0,0 +1,7 @@
1
+ [package]
2
+ name = "__fake_crate"
3
+ version = "0.0.0"
4
+
5
+ [[bin]]
6
+ name = "__fake_cmd"
7
+ path = "main.rs"
@@ -0,0 +1,7 @@
1
+ Copyright 2021 RStudio, PBC
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,6 @@
1
+ use ExtUtils::MakeMaker;
2
+
3
+ WriteMakefile(
4
+ NAME => "PreCommitPlaceholder",
5
+ VERSION => "0.0.1",
6
+ );