forked from dxc-technology/ansible-scaleio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
travis_parse_yaml.py
64 lines (51 loc) · 1.82 KB
/
travis_parse_yaml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
import os
import fnmatch
import yaml
import linecache
import sys
from subprocess import Popen, PIPE
def execute_process(cmd_line):
try:
process = Popen(cmd_line, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
output, err = process.communicate()
return_code = process.returncode
return output, err, return_code
except Exception as e:
sys.exit(1)
def main():
argv = sys.argv[1:]
failure = False
for arg in argv:
cmd_line = "ansible-playbook --syntax-check %s" % arg
print "Running: %s " % cmd_line
output, err, return_code = execute_process(cmd_line)
print output, err
if return_code != 0:
failure = True
path = os.getcwd()
yamlfiles = [os.path.join(dirpath, f)
for dirpath, dirnames, files in os.walk(path)
for f in fnmatch.filter(files, '*.yml')]
for yml in yamlfiles:
caret = " " * 100
try:
stream = open(yml, 'r')
config = yaml.load(stream)
except yaml.YAMLError, exc:
failure = True
print "%s %s %s\n" % ("#"*10, exc.problem, "#"*10)
print "File: %s" % yml
if hasattr(exc, 'context_mark'):
if exc.context_mark is not None:
print "BEGIN: line: %d, column: %d" % (exc.context_mark.line,exc.context_mark.column)
if hasattr(exc, 'problem_mark'):
print "END: line:%d, column: %d\n" % (exc.problem_mark.line,exc.problem_mark.column)
print "%s" % linecache.getline(yml, exc.problem_mark.line+1)
print caret[:exc.problem_mark.column] + "^" + caret[exc.problem_mark.column+1:]
finally:
stream.close()
if failure:
sys.exit(1)
if __name__ == '__main__':
main()