blob: cc117dfa73c575a88cdb0f460a7fe42efb23e72a [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 Wasilewski65d6f0e2016-11-08 13:54:59 +0000156 exitcode = exitcode + validate_yaml(path, args)
157 exitcode = exitcode + metadata_check(path, args)
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000158 elif run_pep8 and path.endswith(".py"):
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +0000159 exitcode = pep8_check(path, args)
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000160 elif path.endswith(".php"):
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +0000161 exitcode = validate_php(path, args)
Milosz Wasilewskidb499392016-11-07 19:43:51 +0000162 elif path.endswith(".sh") or \
163 path.endswith("sh-test-lib") or \
164 path.endswith("android-test-lib"):
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +0000165 exitcode = validate_shell(path, args)
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000166 return exitcode
167
168
169def run_unit_tests(args, filelist=None):
170 exitcode = 0
171 if filelist is not None:
172 for filename in filelist:
Milosz Wasilewskidb499392016-11-07 19:43:51 +0000173 tmp_exitcode = validate_file(args, filename)
174 if tmp_exitcode != 0:
175 exitcode = 1
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000176 else:
177 for root, dirs, files in os.walk('.'):
178 if not root.startswith("./.git"):
179 for name in files:
Milosz Wasilewskidb499392016-11-07 19:43:51 +0000180 tmp_exitcode = validate_file(
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000181 args,
182 root + "/" + name)
Milosz Wasilewskidb499392016-11-07 19:43:51 +0000183 if tmp_exitcode != 0:
184 exitcode = 1
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000185 return exitcode
186
187
188def main(args):
189 exitcode = 0
190 if args.git_latest:
191 # check if git exists
192 git_status, git_result = subprocess.getstatusoutput(
193 "git show --name-only --format=''")
194 if git_status == 0:
195 filelist = git_result.split()
196 exitcode = run_unit_tests(args, filelist)
197 elif len(args.file_path) > 0:
198 exitcode = run_unit_tests(args, [args.file_path])
199 else:
200 exitcode = run_unit_tests(args)
201 exit(exitcode)
202
203
204if __name__ == '__main__':
205 parser = argparse.ArgumentParser()
206 parser.add_argument("-p",
Milosz Wasilewski7ae041c2016-11-07 11:10:06 +0000207 "--pep8-ignore",
208 nargs="*",
Milosz Wasilewskiab7645a2016-11-07 10:45:34 +0000209 default=["E501"],
Milosz Wasilewski7ae041c2016-11-07 11:10:06 +0000210 help="Space separated list of pep8 exclusions",
211 dest="pep8_ignore")
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000212 parser.add_argument("-s",
Milosz Wasilewski7ae041c2016-11-07 11:10:06 +0000213 "--shellcheck-ignore",
214 nargs="*",
215 help="Space separated list of shellcheck exclusions",
216 dest="shellcheck_ignore")
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000217 parser.add_argument("-g",
Milosz Wasilewski7ae041c2016-11-07 11:10:06 +0000218 "--git-latest",
219 action="store_true",
220 default=False,
221 help="If set, the script will try to evaluate files in last git \
222 commit instead of the whole repository",
223 dest="git_latest")
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000224 parser.add_argument("-f",
Milosz Wasilewski7ae041c2016-11-07 11:10:06 +0000225 "--file-path",
226 default="",
227 help="Path to the file that should be checked",
228 dest="file_path")
Milosz Wasilewski65d6f0e2016-11-08 13:54:59 +0000229 parser.add_argument("-r",
230 "--result-file",
231 default="build-error.txt",
232 help="Path to the file that contains results in case of failure",
233 dest="result_file")
Milosz Wasilewskidbf52aa2016-11-01 17:51:26 +0000234
235 args = parser.parse_args()
236 main(args)