Files
pre-commit/pre_commit/clientlib/validate_config.py
2014-03-23 16:22:24 -07:00

98 lines
2.4 KiB
Python

from __future__ import print_function
import argparse
import re
import sys
import pre_commit.constants as C
from pre_commit.clientlib.validate_base import get_validator
from pre_commit.util import entry
class InvalidConfigError(ValueError): pass
CONFIG_JSON_SCHEMA = {
'type': 'array',
'minItems': 1,
'items': {
'type': 'object',
'properties': {
'repo': {'type': 'string'},
'sha': {'type': 'string'},
'hooks': {
'type': 'array',
'minItems': 1,
'items': {
'type': 'object',
'properties': {
'id': {'type': 'string'},
'files': {'type': 'string'},
'args': {
'type': 'array',
'minItems': 1,
'items': {'type': 'string'},
},
},
'required': ['id', 'files'],
}
}
},
'required': ['repo', 'sha', 'hooks'],
}
}
def validate_config_extra(config):
for repo in config:
for hook in repo['hooks']:
try:
re.compile(hook['files'])
except re.error:
raise InvalidConfigError(
'Invalid file regex at {0}, {1}: {2}'.format(
repo['repo'], hook['id'], hook['files'],
)
)
load_config = get_validator(
C.CONFIG_FILE,
CONFIG_JSON_SCHEMA,
InvalidConfigError,
additional_validation_strategy=validate_config_extra,
)
@entry
def run(argv):
parser = argparse.ArgumentParser()
parser.add_argument(
'filenames',
nargs='*', default=None,
help='Config filenames. Defaults to {0} at root of git repo'.format(
C.CONFIG_FILE,
)
)
args = parser.parse_args(argv)
filenames = args.filenames or [C.CONFIG_FILE]
retval = 0
for filename in filenames:
try:
load_config(filename)
except InvalidConfigError as e:
print(e.args[0])
# If we have more than one exception argument print the stringified
# version
if len(e.args) > 1:
print(str(e.args[1]))
retval = 1
return retval
if __name__ == '__main__':
sys.exit(run())