-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathmain.py
More file actions
121 lines (107 loc) · 3.66 KB
/
Copy pathmain.py
File metadata and controls
121 lines (107 loc) · 3.66 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from __future__ import annotations
import argparse
import json
from collections.abc import Sequence
from pre_commit_mirror_maker.make_repo import LIST_VERSIONS
from pre_commit_mirror_maker.make_repo import make_repo
def split_by_commas(maybe_s: str) -> tuple[str, ...]:
"""Split a string by commas, but allow escaped commas.
- If maybe_s is falsey, returns an empty tuple
- Ignore backslashed commas
"""
if not maybe_s:
return ()
parts: list[str] = []
split_by_backslash = maybe_s.split(r'\,')
for split_by_backslash_part in split_by_backslash:
splitby_comma = split_by_backslash_part.split(',')
if parts:
parts[-1] += ',' + splitby_comma[0]
else:
parts.append(splitby_comma[0])
parts.extend(splitby_comma[1:])
return tuple(parts)
def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument(
'repo_path',
help='Local path where the git repo is checked out.',
)
parser.add_argument(
'--language', required=True, choices=LIST_VERSIONS,
help='Which language to use.',
)
parser.add_argument(
'--package-name', required=True,
help='Package name as it appears on the remote package manager.',
)
parser.add_argument(
'--description', help='Hook description.', default='',
)
mutex = parser.add_mutually_exclusive_group(required=True)
mutex.add_argument(
'--files-regex', help='Files regex to use in hooks.yaml',
)
mutex.add_argument('--types', help='`identify` type to AND match')
mutex.add_argument(
'--types-or', action='append', help='`identify` type to OR match',
)
parser.add_argument(
'--id', help='Hook id, defaults to the entry point.',
)
parser.add_argument(
'--entry', help='Entry point, defaults to the package name.',
)
parser.add_argument(
'--args',
help=(
'Comma separated arguments for the hook. Escape commas in args '
'with a backslash (\\). '
r"For example: --args='-i,--ignore=E265\,E501' would give "
'you [-i, --ignore=E265,E501]'
),
)
parser.add_argument(
'--require-serial', action='store_true',
help='Set `require_serial: true` for the hook',
)
parser.add_argument(
'--pass-filenames', action=argparse.BooleanOptionalAction,
default=True,
help='Set `pass_filenames` for the hook',
)
args = parser.parse_args(argv)
minimum_pre_commit_version = '0'
if args.types_or:
match_key = 'types_or'
match_val = '[{}]'.format(', '.join(args.types_or))
minimum_pre_commit_version = '2.9.2'
elif args.types:
match_key = 'types'
match_val = f'[{args.types}]'
else:
match_key = 'files'
match_val = args.files_regex
hook_id = args.id or args.entry or args.package_name
if ' ' in hook_id:
raise SystemExit(
f'hook id should not contain spaces, perhaps specify --id?\n\n'
f'- id: {hook_id}',
)
make_repo(
args.repo_path,
name=args.package_name,
description=args.description,
language=args.language,
entry=args.entry or args.package_name,
id=hook_id,
match_key=match_key,
match_val=match_val,
args=json.dumps(split_by_commas(args.args)),
pass_filenames=json.dumps(args.pass_filenames),
require_serial=json.dumps(args.require_serial),
minimum_pre_commit_version=minimum_pre_commit_version,
)
return 0
if __name__ == '__main__':
raise SystemExit(main())