This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
forked from pre-commit/pre-commit.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_all_hooks.py
54 lines (42 loc) · 1.62 KB
/
make_all_hooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from __future__ import annotations
import functools
import json
import multiprocessing
import os.path
import subprocess
import tempfile
from typing import Any
import yaml
from pre_commit.clientlib import load_manifest
Loader = getattr(yaml, 'CSafeLoader', yaml.SafeLoader)
fast_load = functools.partial(yaml.load, Loader=Loader)
def get_manifest(repo_path: str) -> tuple[bool, str, list[dict[str, Any]]]:
print(f'*** {repo_path}')
with tempfile.TemporaryDirectory() as directory:
repo_dir = os.path.join(directory, 'repo')
cmd = ('git', 'clone', '--depth', '1', '-q', repo_path, repo_dir)
subprocess.check_call(cmd)
manifest_path = os.path.join(repo_dir, '.pre-commit-hooks.yaml')
# Validate the manifest just to make sure it's ok.
manifest = load_manifest(manifest_path)
# hooks should not set debugging `verbose: true` flag
for hook in manifest:
if hook['verbose']:
print(f'{repo_path} ({hook["id"]}) sets `verbose: true`')
return False, repo_path, []
with open(manifest_path) as f:
return True, repo_path, fast_load(f)
def main() -> int:
with open('all-repos.yaml') as f:
repos = fast_load(f)
hooks_json = {}
with multiprocessing.Pool(4) as pool:
for ok, path, manifest in pool.map(get_manifest, repos):
if not ok:
return 1
hooks_json[path] = manifest
with open('all-hooks.json', 'w') as hooks_json_file:
json.dump(hooks_json, hooks_json_file, indent=4)
return 0
if __name__ == '__main__':
raise SystemExit(main())