forked from pre-commit/pre-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_manifest.py
More file actions
99 lines (81 loc) · 2.62 KB
/
validate_manifest.py
File metadata and controls
99 lines (81 loc) · 2.62 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
from __future__ import unicode_literals
from pre_commit.clientlib.validate_base import get_run_function
from pre_commit.clientlib.validate_base import get_validator
from pre_commit.clientlib.validate_base import is_regex_valid
from pre_commit.languages.all import all_languages
class InvalidManifestError(ValueError):
pass
MANIFEST_JSON_SCHEMA = {
'type': 'array',
'minItems': 1,
'items': {
'type': 'object',
'properties': {
'id': {'type': 'string'},
'name': {'type': 'string'},
'description': {'type': 'string', 'default': ''},
'entry': {'type': 'string'},
'exclude': {'type': 'string', 'default': '^$'},
'language': {'type': 'string'},
'language_version': {'type': 'string', 'default': 'default'},
'files': {'type': 'string'},
'stages': {
'type': 'array',
'default': [],
'items': {
'type': 'string',
},
},
'args': {
'type': 'array',
'default': [],
'items': {
'type': 'string',
},
},
'additional_dependencies': {
'type': 'array',
'items': {'type': 'string'},
},
},
'required': ['id', 'name', 'entry', 'language', 'files'],
},
}
def validate_languages(hook_config):
if hook_config['language'] not in all_languages:
raise InvalidManifestError(
'Expected language {0} for {1} to be one of {2!r}'.format(
hook_config['id'],
hook_config['language'],
all_languages,
)
)
def validate_files(hook_config):
if not is_regex_valid(hook_config['files']):
raise InvalidManifestError(
'Invalid files regex at {0}: {1}'.format(
hook_config['id'], hook_config['files'],
)
)
if not is_regex_valid(hook_config.get('exclude', '')):
raise InvalidManifestError(
'Invalid exclude regex at {0}: {1}'.format(
hook_config['id'], hook_config['exclude'],
)
)
def additional_manifest_check(obj):
for hook_config in obj:
validate_languages(hook_config)
validate_files(hook_config)
load_manifest = get_validator(
MANIFEST_JSON_SCHEMA,
InvalidManifestError,
additional_manifest_check,
)
run = get_run_function(
'Manifest filenames.',
load_manifest,
InvalidManifestError,
)
if __name__ == '__main__':
exit(run())