Merge branch 'master' of github.com:pre-commit/pre-commit

This commit is contained in:
Ken Struys
2014-03-13 14:37:27 -07:00
12 changed files with 263 additions and 3 deletions

View File

View File

@@ -0,0 +1,97 @@
from __future__ import print_function
import argparse
import jsonschema
import jsonschema.exceptions
import os.path
import yaml
import pre_commit.constants as C
class InvalidManifestError(ValueError): pass
MANIFEST_JSON_SCHEMA = {
'type': 'object',
'properties': {
'hooks': {
'type': 'array',
'minItems': 1,
'items': {
'type': 'object',
'properties': {
'id': {'type': 'string'},
'name': {'type': 'string'},
'description': {'type': 'string'},
'entry': {'type': 'string'},
'language': {'type': 'string'},
'expected_return_value': {'type': 'number'},
},
'required': ['id', 'name', 'entry'],
},
},
},
'required': ['hooks'],
}
def check_is_valid_manifest(file_contents):
file_objects = yaml.load(file_contents)
jsonschema.validate(file_objects, MANIFEST_JSON_SCHEMA)
for hook_config in file_objects['hooks']:
language = hook_config.get('language')
if language is not None and not any(
language.startswith(lang) for lang in C.SUPPORTED_LANGUAGES
):
raise InvalidManifestError(
'Expected language {0} for {1} to start with one of {2!r}'.format(
hook_config['id'],
hook_config['language'],
C.SUPPORTED_LANGUAGES,
)
)
def run(argv):
parser = argparse.ArgumentParser()
parser.add_argument(
'--filename',
required=False, default=None,
help='Manifest filename. Defaults to {0} at root of git repo'.format(
C.MANIFEST_FILE,
)
)
args = parser.parse_args(argv)
if args.filename is None:
# TODO: filename = git.get_root() + C.MANIFEST_FILE
raise NotImplementedError
else:
filename = args.filename
if not os.path.exists(filename):
print('File {0} does not exist'.format(filename))
return 1
file_contents = open(filename, 'r').read()
try:
yaml.load(file_contents)
except Exception as e:
print('File {0} is not a valid yaml file'.format(filename))
print(str(e))
return 1
try:
check_is_valid_manifest(file_contents)
except (jsonschema.exceptions.ValidationError, InvalidManifestError) as e:
print('File {0} is not a valid manifest file'.format(filename))
print(str(e))
return 1
return 0

12
pre_commit/constants.py Normal file
View File

@@ -0,0 +1,12 @@
PRE_COMMIT_FILE = '.pre-commit-config.yaml'
PRE_COMMIT_DIR = '.pre-commit-files'
MANIFEST_FILE = 'manifest.yaml'
SUPPORTED_LANGUAGES = [
'python',
'ruby',
'node',
]

View File

@@ -0,0 +1,23 @@
import functools
import pre_commit.clientlib.validate_manifest
import pre_commit.run
def make_entry_point(entry_point_func):
"""Decorator which turns a function which takes sys.argv[1:] and returns
an integer into an argumentless function which returns an integer.
Args:
entry_point_func - A function which takes an array representing argv
"""
@functools.wraps(entry_point_func)
def func():
import sys
return entry_point_func(sys.argv[1:])
return func
pre_commit_func = make_entry_point(pre_commit.run.run)
validate_manifest_func = make_entry_point(pre_commit.clientlib.validate_manifest.run)

View File

@@ -34,8 +34,8 @@ def run(argv):
args = parser.parse_args(argv)
if args.install:
install()
return install()
elif args.uninstall:
uninstall()
return uninstall()
else:
run_hooks(args)
return run_hooks(args)