forked from matryer/xbar-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.test.py
executable file
·257 lines (214 loc) · 9.52 KB
/
.test.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python
import re
import os
import subprocess
import urllib2
import argparse
import warnings
from distutils.spawn import find_executable
ignore_file_types = ['.md']
ignore_file_names = ['package.json', 'package-lock.json']
allowed_image_content_types = ['image/png', 'image/jpeg', 'image/gif']
required_metadata = ['author', 'author.github', 'title']
recommended_metadata = ['image', 'desc', 'version']
error_count = 0
def debug(s):
global args
if args.debug:
print "\033[1;44mDBG!\033[0;0m %s\n" % s
def passed(s):
global args
if args.verbose:
print "\033[1;42mPASS\033[0;0m %s\n" % s
def warn(s):
global args
if args.warn:
print "\033[1;43mWRN!\033[0;0m %s\n" % s
def error(s):
global error_count
error_count += 1
print "\033[1;41mERR!\033[0;0m %s\n" % s
class Language(object):
_languages = {}
def __init__(self, exts, shebang, linter, trim_shebang=False, full_options=[], pr_options=[]):
self.extensions = exts
self.shebang = shebang
self.cmd = linter
self.trim = trim_shebang
self.full = full_options
self.pr = pr_options
self.enabled = True
if not find_executable(self.cmd[0]):
error("Linter %s not present, skipping %s files" % (self.cmd[0], ', '.join(exts)))
self.enabled = False
@staticmethod
def registerLanguage(lang):
for extension in lang.extensions:
if extension in Language._languages:
Language._languages[extension].append(lang)
else:
Language._languages[extension] = [lang, ]
@staticmethod
def getLanguagesForFileExtension(ext):
if ext in Language._languages:
return Language._languages[ext]
else:
return None
def validShebang(self, bang):
return re.search(self.shebang, bang) is not None
def lint(self, file, is_pr):
if not self.enabled:
return None
if self.trim:
with open(file, 'r') as fp:
lines = fp.readlines()[1:]
with warnings.catch_warnings():
warnings.simplefilter("ignore")
tmpfile = os.tmpnam()
with open(tmpfile, 'w') as tp:
tp.writelines(lines)
file = tmpfile
command = list(self.cmd)
if is_pr and self.pr:
command.extend(self.pr)
elif not is_pr and self.full:
command.extend(self.full)
command.append(file)
return subprocess.check_output(command, stderr=subprocess.STDOUT)
Language.registerLanguage(Language(['.sh'], '(bash|ksh|zsh|sh|fish)$', ['shellcheck'], full_options=['-e', 'SC1117,SC2164,SC2183,SC2196,SC2197,SC2206,SC2207,SC2215,SC2219,SC2230,SC2236']))
Language.registerLanguage(Language(['.py', '.py2'], 'python(|2(\.\d+)?)$', ['python2', '-m', 'pyflakes']))
Language.registerLanguage(Language(['.py', '.py3'], 'python(|3(\.\d+)?)$', ['python3', '-m', 'pyflakes']))
Language.registerLanguage(Language(['.rb'], 'ruby$', ['rubocop', '-l'], full_options=['--except', 'Lint/RescueWithoutErrorClass']))
Language.registerLanguage(Language(['.js'], 'node$', ['jshint']))
Language.registerLanguage(Language(['.php'], 'php$', ['php', '-l']))
Language.registerLanguage(Language(['.pl'], 'perl( -[wW])?$', ['perl', '-MO=Lint']))
Language.registerLanguage(Language(['.swift'], 'swift$', ['xcrun', '-sdk', 'macosx', 'swiftc', '-o', '/dev/null']))
Language.registerLanguage(Language(['.lisp', '.clisp'], 'clisp$', ['clisp']))
Language.registerLanguage(Language(['.rkt'], 'racket$', ['raco', 'make']))
# go does not actually support shebang on line 1. gorun works around this, so we need to strip it before we lint
Language.registerLanguage(Language(['.go'], 'gorun$', ['golint', '-set_exit_status'], trim_shebang=True))
Language.registerLanguage(Language(['.lua'], 'lua$', ['luacheck']))
def check_file(file_full_path, pr=False):
file_short_name, file_extension = os.path.splitext(file_full_path)
candidates = Language.getLanguagesForFileExtension(file_extension)
if not candidates:
error("%s unrecognized file extension" % file_full_path)
return
else:
passed("%s has a recognized file extension" % file_full_path)
if not os.access(file_full_path, os.R_OK):
error("%s not readable" % file_full_path)
return
if not os.access(file_full_path, os.X_OK):
error("%s not executable" % file_full_path)
else:
passed("%s is executable" % file_full_path)
metadata = {}
linters = []
with open(file_full_path, "r") as fp:
first_line = fp.readline().strip()
for candidate in candidates:
if candidate.validShebang(first_line):
linters.append(candidate)
if not re.match(r'#! ?/', first_line):
error("'%s' does not look like a valid shebang" % first_line)
if not linters:
error("%s has incorrect shebang.\n Got %s\n Wanted %s" % (
file_full_path, first_line,
' or '.join(["'%s'" % candidate.shebang for candidate in candidates])))
else:
passed("%s has a good shebang (%s)" % (file_full_path, first_line))
for line in fp:
match = re.search("<bitbar.(?P<lho_tag>[^>]+)>(?P<value>[^<]+)</bitbar.(?P<rho_tag>[^>]+)>", line)
if match is not None:
if match.group('lho_tag') != match.group('rho_tag'):
error('%s includes mismatched metatags: %s' % (file_full_path, line))
else:
metadata[match.group('lho_tag')] = match.group('value')
for key in required_metadata:
if key not in metadata:
error('%s missing required metadata for %s' % (file_full_path, key))
else:
passed('%s has required metadata for %s (%s)' % (file_full_path, key, metadata[key]))
for key in recommended_metadata:
if key not in metadata:
warn('%s missing recommended metadata for %s' % (file_full_path, key))
else:
passed('%s has recommended metadata for %s (%s)' % (file_full_path, key, metadata[key]))
if metadata.get('image', False):
try:
response = urllib2.urlopen(metadata['image'])
response_content_type = response.info().getheader('Content-Type')
if response_content_type not in allowed_image_content_types:
error('%s image metadata has bad content type: %s' % (file_full_path, response_content_type))
else:
passed('%s image content type looks good: %s' % (file_full_path, response_content_type))
except Exception:
warn('%s cannot fetch image: %s' % (file_full_path, metadata['image']))
errors = []
for linter in linters:
try:
debug('running %s' % " ".join(linter.cmd))
linter.lint(file_full_path, pr)
except subprocess.CalledProcessError as cpe:
debug('%s failed linting with "%s"' % (file_full_path, " ".join(linter.cmd)))
errors.append({'linter': linter, 'output': cpe.output})
else:
errors = []
passed('%s linted successfully with "%s"' %
(file_full_path, " ".join(list(linter.cmd))))
break
for e in errors:
error('%s failed linting with "%s", please correct the following:' %
(file_full_path, " ".join(e['linter'].cmd)))
print e['output']
def boolean_string(string):
if string.lower() == "false":
return False
return True
parser = argparse.ArgumentParser()
parser.add_argument(
'--pr', action='store', nargs='?', const="True",
default=os.environ.get('TRAVIS_PULL_REQUEST', "False"),
type=boolean_string,
help='Run tests on changes from the root branch to HEAD. verbose is implied!')
parser.add_argument('--verbose', '-v', action='store_true', help='Turn on success and other non-critical messages')
parser.add_argument('--debug', action='store_true', help='Turn on debug messages')
parser.add_argument('--no-warn', action='store_false', dest='warn', help='Disable warnings', default=True)
parser.add_argument('files', nargs=argparse.REMAINDER)
args = parser.parse_args()
if args.pr:
output = subprocess.check_output(['git', 'diff', '--name-only', '--diff-filter=ACMR',
'origin/%s..HEAD' %
os.environ.get('TRAVIS_BRANCH', 'master')]).strip()
if not output:
warn('No changed files in this PR... weird...')
exit(0)
else:
args.files = output.split('\n')
args.verbose = True
elif not args.files:
for root, dirs, files_in_folder in os.walk("."):
for _file in files_in_folder:
args.files.append(os.path.join(root, _file).strip())
skip = [d for d in dirs if d.startswith(".")]
for d in skip:
debug('skipping directory %s' % d)
dirs.remove(d)
for _file in args.files:
file_name, file_ext = os.path.splitext(_file)
components = _file.split('/')
if components[0] == ".":
del components[0]
if any(s[0] == '.' for s in components):
debug('skipping file %s' % _file)
elif file_ext in ignore_file_types:
debug('ignoring file by type %s' % _file)
elif components[-1] in ignore_file_names:
debug('ignoring file by name %s' % _file)
else:
debug('checking file %s' % _file)
check_file(os.path.join(os.getcwd(), _file), args.pr)
if error_count > 0:
error('failed with %i errors' % error_count)
exit(1)