/
clientlib.py
157 lines (123 loc) · 4.28 KB
/
clientlib.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from __future__ import absolute_import
from __future__ import unicode_literals
import argparse
import functools
from aspy.yaml import ordered_load
from identify.identify import ALL_TAGS
import pre_commit.constants as C
from pre_commit import schema
from pre_commit.errors import FatalError
from pre_commit.languages.all import all_languages
def check_language(v):
if v not in all_languages:
raise schema.ValidationError(
'Expected {} to be in {!r}'.format(v, all_languages),
)
def check_type_tag(tag):
if tag not in ALL_TAGS:
raise schema.ValidationError(
'Type tag {!r} is not recognized. '
'Try upgrading identify and pre-commit?'.format(tag),
)
def _make_argparser(filenames_help):
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*', help=filenames_help)
parser.add_argument('-V', '--version', action='version', version=C.VERSION)
return parser
MANIFEST_HOOK_DICT = schema.Map(
'Hook', 'id',
schema.Required('id', schema.check_string),
schema.Required('name', schema.check_string),
schema.Required('entry', schema.check_string),
schema.Required(
'language', schema.check_and(schema.check_string, check_language),
),
schema.Optional(
'files', schema.check_and(schema.check_string, schema.check_regex),
'',
),
schema.Optional(
'exclude',
schema.check_and(schema.check_string, schema.check_regex),
'^$',
),
schema.Optional('types', schema.check_array(check_type_tag), ['file']),
schema.Optional('exclude_types', schema.check_array(check_type_tag), []),
schema.Optional(
'additional_dependencies', schema.check_array(schema.check_string), [],
),
schema.Optional('args', schema.check_array(schema.check_string), []),
schema.Optional('always_run', schema.check_bool, False),
schema.Optional('pass_filenames', schema.check_bool, True),
schema.Optional('description', schema.check_string, ''),
schema.Optional('language_version', schema.check_string, 'default'),
schema.Optional('log_file', schema.check_string, ''),
schema.Optional('minimum_pre_commit_version', schema.check_string, '0'),
schema.Optional('stages', schema.check_array(schema.check_string), []),
)
MANIFEST_SCHEMA = schema.Array(MANIFEST_HOOK_DICT)
class InvalidManifestError(FatalError):
pass
load_manifest = functools.partial(
schema.load_from_filename,
schema=MANIFEST_SCHEMA,
load_strategy=ordered_load,
exc_tp=InvalidManifestError,
)
def validate_manifest_main(argv=None):
parser = _make_argparser('Manifest filenames.')
args = parser.parse_args(argv)
ret = 0
for filename in args.filenames:
try:
load_manifest(filename)
except InvalidManifestError as e:
print(e)
ret = 1
return ret
_LOCAL_SENTINEL = 'local'
CONFIG_HOOK_DICT = schema.Map(
'Hook', 'id',
schema.Required('id', schema.check_string),
# All keys in manifest hook dict are valid in a config hook dict, but
# are optional.
# No defaults are provided here as the config is merged on top of the
# manifest.
*[
schema.OptionalNoDefault(item.key, item.check_fn)
for item in MANIFEST_HOOK_DICT.items
if item.key != 'id'
]
)
CONFIG_REPO_DICT = schema.Map(
'Repository', 'repo',
schema.Required('repo', schema.check_string),
schema.RequiredRecurse('hooks', schema.Array(CONFIG_HOOK_DICT)),
schema.Conditional(
'sha', schema.check_string,
condition_key='repo', condition_value=schema.Not(_LOCAL_SENTINEL),
ensure_absent=True,
),
)
CONFIG_SCHEMA = schema.Array(CONFIG_REPO_DICT)
def is_local_repo(repo_entry):
return repo_entry['repo'] == _LOCAL_SENTINEL
class InvalidConfigError(FatalError):
pass
load_config = functools.partial(
schema.load_from_filename,
schema=CONFIG_SCHEMA,
load_strategy=ordered_load,
exc_tp=InvalidConfigError,
)
def validate_config_main(argv=None):
parser = _make_argparser('Config filenames.')
args = parser.parse_args(argv)
ret = 0
for filename in args.filenames:
try:
load_config(filename)
except InvalidConfigError as e:
print(e)
ret = 1
return ret