blob: 54edb75e495545b6ff95d843c98e204470bfacf4 [file] [log] [blame]
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +00001#!/usr/bin/python3
2import argparse
3import os
4import sys
5import subprocess
6import traceback
7import yaml
8
9run_pep8 = False
10try:
11 import pep8
12 run_pep8 = True
13except:
Antonio Terceiro1396fb12016-11-07 18:45:52 -020014 print("PEP8 is not available!")
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +000015
16
17def print_stderr(message):
18 sys.stderr.write(message)
19 sys.stderr.write("\n")
20
21
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +000022def publish_result(result_message_list, args):
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +000023 result_message = '\n'.join(result_message_list)
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +000024 try:
25 f = open(args.result_file, 'a')
26 f.write("\n\n")
27 f.write(result_message)
28 f.write("\n\n")
29 f.close()
30 except IOError as e:
31 print_stderr("Cannot write to result file: %s" % args.result_file)
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +000032 print_stderr(result_message)
33
34
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +000035def pep8_check(filepath, args):
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +000036 _fmt = "%(row)d:%(col)d: %(code)s %(text)s"
37 options = {
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +000038 'ignore': args.pep8_ignore,
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +000039 "show_source": True}
40 pep8_checker = pep8.StyleGuide(options)
41 fchecker = pep8_checker.checker_class(
42 filepath,
43 options=pep8_checker.options)
44 fchecker.check_all()
45 if fchecker.report.file_errors > 0:
46 result_message_list = []
47 result_message_list.append("* PEP8: [FAILED]: " + filepath)
48 fchecker.report.print_statistics()
49 for line_number, offset, code, text, doc in fchecker.report._deferred_print:
50 result_message_list.append(
51 _fmt % {
52 'path': filepath,
53 'row': fchecker.report.line_offset + line_number,
54 'col': offset + 1,
55 'code': code, 'text': text,
56 })
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +000057 publish_result(result_message_list, args)
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +000058 return 1
59 else:
60 message = "* PEP8: [PASSED]: " + filepath
61 print_stderr(message)
62 return 0
63
Milosz Wasilewski7ae041c2016-11-07 11:10:06 +000064
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +000065def metadata_check(filepath, args):
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +000066 if filepath.lower().endswith("yaml"):
67 with open(filepath, "r") as f:
68 result_message_list = []
69 y = yaml.load(f.read())
Milosz Wasilewski7ae041c2016-11-07 11:10:06 +000070 if 'metadata' not in y.keys():
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +000071 result_message_list.append("* METADATA [FAILED]: " + filepath)
72 result_message_list.append("\tmetadata section missing")
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +000073 publish_result(result_message_list, args)
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +000074 exit(1)
75 metadata_dict = y['metadata']
76 mandatory_keys = set([
77 'name',
78 'format',
79 'description',
80 'maintainer',
81 'os',
82 'devices'])
83 if not mandatory_keys.issubset(set(metadata_dict.keys())):
84 result_message_list.append("* METADATA [FAILED]: " + filepath)
Milosz Wasilewski7ae041c2016-11-07 11:10:06 +000085 result_message_list.append("\tmandatory keys missing: %s" %
86 mandatory_keys.difference(set(metadata_dict.keys())))
87 result_message_list.append("\tactual keys present: %s" %
88 metadata_dict.keys())
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +000089 publish_result(result_message_list, args)
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +000090 return 1
91 for key in mandatory_keys:
92 if len(metadata_dict[key]) == 0:
93 result_message_list.append("* METADATA [FAILED]: " + filepath)
94 result_message_list.append("\t%s has no content" % key)
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +000095 publish_result(result_message_list, args)
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +000096 return 1
97 result_message_list.append("* METADATA [PASSED]: " + filepath)
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +000098 publish_result(result_message_list, args)
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +000099 return 0
100
Milosz Wasilewski7ae041c2016-11-07 11:10:06 +0000101
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +0000102def validate_yaml(filename, args):
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000103 with open(filename, "r") as f:
104 try:
105 y = yaml.load(f.read())
106 message = "* YAMLVALID: [PASSED]: " + filename
107 print_stderr(message)
108 except:
109 message = "* YAMLVALID: [FAILED]: " + filename
110 result_message_list = []
111 result_message_list.append(message)
112 result_message_list.append("\n\n")
113 exc_type, exc_value, exc_traceback = sys.exc_info()
114 for line in traceback.format_exception_only(exc_type, exc_value):
115 result_message_list.append(' ' + line)
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +0000116 publish_result(result_message_list, args)
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000117 return 1
118 return 0
119
120
121def validate_shell(filename, ignore_options):
122 ignore_string = ""
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +0000123 if args.shellcheck_ignore is not None:
124 ignore_string = "-e %s" % " ".join(args.shellcheck_ignore)
Milosz Wasilewskiab7645a2016-11-07 10:45:34 +0000125 if len(ignore_string) < 4: # contains only "-e "
126 ignore_string = ""
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000127 cmd = 'shellcheck %s' % ignore_string
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +0000128 return validate_external(cmd, filename, "SHELLCHECK", args)
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000129
130
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +0000131def validate_php(filename, args):
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000132 cmd = 'php -l'
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +0000133 return validate_external(cmd, filename, "PHPLINT", args)
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000134
135
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +0000136def validate_external(cmd, filename, prefix, args):
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000137 final_cmd = "%s %s 2>&1" % (cmd, filename)
138 status, output = subprocess.getstatusoutput(final_cmd)
139 if status == 0:
140 message = '* %s: [PASSED]: %s' % (prefix, filename)
141 print_stderr(message)
142 else:
143 result_message_list = []
144 result_message_list.append('* %s: [FAILED]: %s' % (prefix, filename))
145 result_message_list.append('* %s: [OUTPUT]:' % prefix)
146 for line in output.splitlines():
147 result_message_list.append(' ' + line)
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +0000148 publish_result(result_message_list, args)
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000149 return 1
150 return 0
151
152
153def validate_file(args, path):
154 exitcode = 0
155 if path.endswith(".yaml"):
Milosz Wasilewskid0b78132016-11-22 19:21:14 +0000156 exitcode = validate_yaml(path, args)
157 if exitcode == 0:
158 # if yaml isn't valid there is no point in checking metadata
159 exitcode = metadata_check(path, args)
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000160 elif run_pep8 and path.endswith(".py"):
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +0000161 exitcode = pep8_check(path, args)
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000162 elif path.endswith(".php"):
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +0000163 exitcode = validate_php(path, args)
Milosz Wasilewskidb499392016-11-07 19:43:51 +0000164 elif path.endswith(".sh") or \
165 path.endswith("sh-test-lib") or \
166 path.endswith("android-test-lib"):
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +0000167 exitcode = validate_shell(path, args)
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000168 return exitcode
169
170
171def run_unit_tests(args, filelist=None):
172 exitcode = 0
173 if filelist is not None:
174 for filename in filelist:
Milosz Wasilewskidb499392016-11-07 19:43:51 +0000175 tmp_exitcode = validate_file(args, filename)
176 if tmp_exitcode != 0:
177 exitcode = 1
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000178 else:
179 for root, dirs, files in os.walk('.'):
180 if not root.startswith("./.git"):
181 for name in files:
Milosz Wasilewskidb499392016-11-07 19:43:51 +0000182 tmp_exitcode = validate_file(
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000183 args,
184 root + "/" + name)
Milosz Wasilewskidb499392016-11-07 19:43:51 +0000185 if tmp_exitcode != 0:
186 exitcode = 1
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000187 return exitcode
188
189
190def main(args):
191 exitcode = 0
192 if args.git_latest:
193 # check if git exists
194 git_status, git_result = subprocess.getstatusoutput(
195 "git show --name-only --format=''")
196 if git_status == 0:
197 filelist = git_result.split()
198 exitcode = run_unit_tests(args, filelist)
199 elif len(args.file_path) > 0:
200 exitcode = run_unit_tests(args, [args.file_path])
201 else:
202 exitcode = run_unit_tests(args)
203 exit(exitcode)
204
205
206if __name__ == '__main__':
207 parser = argparse.ArgumentParser()
208 parser.add_argument("-p",
Milosz Wasilewski7ae041c2016-11-07 11:10:06 +0000209 "--pep8-ignore",
210 nargs="*",
Milosz Wasilewskiab7645a2016-11-07 10:45:34 +0000211 default=["E501"],
Milosz Wasilewski7ae041c2016-11-07 11:10:06 +0000212 help="Space separated list of pep8 exclusions",
213 dest="pep8_ignore")
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000214 parser.add_argument("-s",
Milosz Wasilewski7ae041c2016-11-07 11:10:06 +0000215 "--shellcheck-ignore",
216 nargs="*",
217 help="Space separated list of shellcheck exclusions",
218 dest="shellcheck_ignore")
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000219 parser.add_argument("-g",
Milosz Wasilewski7ae041c2016-11-07 11:10:06 +0000220 "--git-latest",
221 action="store_true",
222 default=False,
223 help="If set, the script will try to evaluate files in last git \
224 commit instead of the whole repository",
225 dest="git_latest")
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000226 parser.add_argument("-f",
Milosz Wasilewski7ae041c2016-11-07 11:10:06 +0000227 "--file-path",
228 default="",
229 help="Path to the file that should be checked",
230 dest="file_path")
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +0000231 parser.add_argument("-r",
232 "--result-file",
233 default="build-error.txt",
234 help="Path to the file that contains results in case of failure",
235 dest="result_file")
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000236
237 args = parser.parse_args()
238 main(args)