markizano

yamllint

Feb 21st, 2017
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. '''
  4. Usage: yamllint /path/to/lint
  5.  
  6. Namely used for salt, but recursively checks a directory for files. For each file that ends with "sls" - check to ensure it can load using YAML. Print any files that have exceptions.
  7.  
  8. Great as a pre-commit hook to any github project working on YAML-based files.
  9. '''
  10.  
  11. import os
  12. import sys
  13. import yaml
  14.  
  15. def main(target):
  16.   if not os.path.isdir( target ):
  17.       print('Could not walk %s. Target is not a directory or doesnt exist.' % target)
  18.       return 1
  19.   result = 0
  20.   for rewt, dirs, files in os.walk( sys.argv[1] ):
  21.     for file in files:
  22.       if not file.endswith('sls'): continue
  23.       try:
  24.         with open(os.path.join(rewt, file)) as fd:
  25.           yaml.load(fd)
  26.       except Exception as e:
  27.         print("Problem with %(rewt)s/%(file)s: %(e)s" % locals())
  28.         result = 1
  29.   return result
  30.  
  31. sys.exit(main(sys.argv[1]))
Advertisement
Add Comment
Please, Sign In to add comment