mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Merge branch 'master' of github.com:pre-commit/pre-commit
Conflicts: tests/git_test.py
This commit is contained in:
commit
3b0f03d0e0
11 changed files with 243 additions and 160 deletions
51
pre_commit/clientlib/validate_base.py
Normal file
51
pre_commit/clientlib/validate_base.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
|
||||
import jsonschema
|
||||
import jsonschema.exceptions
|
||||
import os.path
|
||||
import yaml
|
||||
|
||||
from pre_commit import git
|
||||
|
||||
|
||||
def get_validator(
|
||||
default_filename,
|
||||
json_schema,
|
||||
exception_type,
|
||||
additional_validation_strategy=lambda obj: None,
|
||||
):
|
||||
"""Returns a function which will validate a yaml file for correctness
|
||||
|
||||
Args:
|
||||
default_filename - Default filename to look for if none is specified
|
||||
json_schema - JSON schema to validate file with
|
||||
exception_type - Error type to raise on failure
|
||||
additional_validation_strategy - Strategy for additional validation of
|
||||
the object read from the file. The function should either raise
|
||||
exception_type on failure.
|
||||
"""
|
||||
|
||||
def validate(filename=None):
|
||||
filename = filename or os.path.join(git.get_root(), default_filename)
|
||||
|
||||
if not os.path.exists(filename):
|
||||
raise exception_type('File {0} does not exist'.format(filename))
|
||||
|
||||
file_contents = open(filename, 'r').read()
|
||||
|
||||
try:
|
||||
obj = yaml.load(file_contents)
|
||||
except Exception as e:
|
||||
raise exception_type(
|
||||
'File {0} is not a valid yaml file'.format(filename), e,
|
||||
)
|
||||
|
||||
try:
|
||||
jsonschema.validate(obj, json_schema)
|
||||
except jsonschema.exceptions.ValidationError as e:
|
||||
raise exception_type(
|
||||
'File {0} is not a valid file'.format(filename), e,
|
||||
)
|
||||
|
||||
additional_validation_strategy(obj)
|
||||
|
||||
return validate
|
||||
|
|
@ -2,47 +2,34 @@
|
|||
from __future__ import print_function
|
||||
|
||||
import argparse
|
||||
import jsonschema
|
||||
import jsonschema.exceptions
|
||||
import os.path
|
||||
import yaml
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit.clientlib.validate_base import get_validator
|
||||
|
||||
|
||||
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'],
|
||||
},
|
||||
'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']:
|
||||
def additional_manifest_check(obj):
|
||||
for hook_config in obj:
|
||||
language = hook_config.get('language')
|
||||
|
||||
if language is not None and not any(
|
||||
|
|
@ -57,41 +44,33 @@ def check_is_valid_manifest(file_contents):
|
|||
)
|
||||
|
||||
|
||||
validate_manifest = get_validator(
|
||||
C.MANIFEST_FILE,
|
||||
MANIFEST_JSON_SCHEMA,
|
||||
InvalidManifestError,
|
||||
additional_manifest_check,
|
||||
)
|
||||
|
||||
|
||||
def run(argv):
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
'--filename',
|
||||
required=False, default=None,
|
||||
'filename',
|
||||
nargs='?', 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))
|
||||
validate_manifest(args.filename)
|
||||
except InvalidManifestError 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]))
|
||||
return 1
|
||||
|
||||
return 0
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
PRE_COMMIT_FILE = '.pre-commit-config.yaml'
|
||||
CONFIG_FILE = '.pre-commit-config.yaml'
|
||||
|
||||
PRE_COMMIT_DIR = '.pre-commit-files'
|
||||
|
||||
|
|
|
|||
|
|
@ -54,7 +54,8 @@ class PreCommitProject(object):
|
|||
with self.in_checkout():
|
||||
if local.path('setup.py').exists():
|
||||
local['virtualenv']['py_env']()
|
||||
local['bash'][local['pip']['install', '.']]
|
||||
local['bash']['-c', 'source py_env/bin/activate && pip install .']()
|
||||
print local.cwd.getpath()
|
||||
|
||||
def create_repo_in_env(git_repo_path, sha):
|
||||
project = PreCommitProject(git_repo_path, sha)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue