22from __future__ import print_function
33
44import argparse
5- import jsonschema
6- import jsonschema .exceptions
7- import os .path
8- import yaml
95
106import pre_commit .constants as C
7+ from pre_commit .clientlib .validate_base import get_validator
118
129
1310class InvalidManifestError (ValueError ): pass
1411
1512
1613MANIFEST_JSON_SCHEMA = {
17- 'type' : 'object' ,
18- 'properties' : {
19- 'hooks' : {
20- 'type' : 'array' ,
21- 'minItems' : 1 ,
22- 'items' : {
23- 'type' : 'object' ,
24- 'properties' : {
25- 'id' : {'type' : 'string' },
26- 'name' : {'type' : 'string' },
27- 'description' : {'type' : 'string' },
28- 'entry' : {'type' : 'string' },
29- 'language' : {'type' : 'string' },
30- 'expected_return_value' : {'type' : 'number' },
31- },
32- 'required' : ['id' , 'name' , 'entry' ],
33- },
14+ 'type' : 'array' ,
15+ 'minItems' : 1 ,
16+ 'items' : {
17+ 'type' : 'object' ,
18+ 'properties' : {
19+ 'id' : {'type' : 'string' },
20+ 'name' : {'type' : 'string' },
21+ 'description' : {'type' : 'string' },
22+ 'entry' : {'type' : 'string' },
23+ 'language' : {'type' : 'string' },
24+ 'expected_return_value' : {'type' : 'number' },
3425 },
26+ 'required' : ['id' , 'name' , 'entry' ],
3527 },
36- 'required' : ['hooks' ],
3728}
3829
3930
40- def check_is_valid_manifest (file_contents ):
41- file_objects = yaml .load (file_contents )
42-
43- jsonschema .validate (file_objects , MANIFEST_JSON_SCHEMA )
44-
45- for hook_config in file_objects ['hooks' ]:
31+ def additional_manifest_check (obj ):
32+ for hook_config in obj :
4633 language = hook_config .get ('language' )
4734
4835 if language is not None and not any (
@@ -57,41 +44,33 @@ def check_is_valid_manifest(file_contents):
5744 )
5845
5946
47+ validate_manifest = get_validator (
48+ C .MANIFEST_FILE ,
49+ MANIFEST_JSON_SCHEMA ,
50+ InvalidManifestError ,
51+ additional_manifest_check ,
52+ )
53+
54+
6055def run (argv ):
6156 parser = argparse .ArgumentParser ()
6257 parser .add_argument (
63- '-- filename' ,
64- required = False , default = None ,
58+ 'filename' ,
59+ nargs = '?' , default = None ,
6560 help = 'Manifest filename. Defaults to {0} at root of git repo' .format (
6661 C .MANIFEST_FILE ,
6762 )
6863 )
6964 args = parser .parse_args (argv )
7065
71- if args .filename is None :
72- # TODO: filename = git.get_root() + C.MANIFEST_FILE
73- raise NotImplementedError
74- else :
75- filename = args .filename
76-
77- if not os .path .exists (filename ):
78- print ('File {0} does not exist' .format (filename ))
79- return 1
80-
81- file_contents = open (filename , 'r' ).read ()
82-
83- try :
84- yaml .load (file_contents )
85- except Exception as e :
86- print ('File {0} is not a valid yaml file' .format (filename ))
87- print (str (e ))
88- return 1
89-
9066 try :
91- check_is_valid_manifest (file_contents )
92- except (jsonschema .exceptions .ValidationError , InvalidManifestError ) as e :
93- print ('File {0} is not a valid manifest file' .format (filename ))
94- print (str (e ))
67+ validate_manifest (args .filename )
68+ except InvalidManifestError as e :
69+ print (e .args [0 ])
70+ # If we have more than one exception argument print the stringified
71+ # version
72+ if len (e .args ) > 1 :
73+ print (str (e .args [1 ]))
9574 return 1
9675
9776 return 0
0 commit comments