forked from matryer/xbar-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.test.py
executable file
·114 lines (98 loc) · 3.81 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
#!/usr/bin/env python
DEBUG = False
import re
import os
import sys
import subprocess
import urllib2
allowed_image_content_types = [ 'image/png', 'image/jpeg' ]
required_metadata = [ 'author', 'author.github', 'title', 'image' ]
required_shebangs = {
'.sh': '(bash|ksh|zsh|sh|fish)$',
'.py': 'python(|2|3)$',
'.rb': 'ruby$',
'.js': 'node$',
'.php': 'php$',
'.pl': 'perl$',
'.swift': 'swift$',
}
linter_command = {
'.sh': [ 'shellcheck' ],
'.py': [ 'pyflakes' ],
'.rb': [ 'rubocop', '-l' ],
'.js': [ 'jshint' ],
'.php': [ 'php', '-l' ],
'.pl': [ 'perl', '-MO=Lint'],
'.swift': [ 'xcrun', '-sdk', 'macosx', 'swiftc' ],
}
error_count = 0
def debug(s):
if DEBUG:
print "\033[1;44mDBG!\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
def check_file(file_full_path):
file_short_name, file_extension = os.path.splitext(file_full_path)
if not required_shebangs.get(file_extension, False):
error("%s unrecognized file extension" % file_full_path)
return
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)
metadata = {}
with open(file_full_path, "r") as fp:
first_line = fp.readline().strip()
shebang_re = required_shebangs.get(file_extension, '')
if first_line[0:3] != '#!/' or re.search(shebang_re, first_line) is None:
error("%s has incorrect shebang.\n Got %s\n Wanted %s" % (file_full_path, first_line, shebang_re))
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 metadata for %s' % (file_full_path, key))
if metadata.get('image', False):
try:
response = urllib2.urlopen(metadata['image'])
response_content_type = response.info().getheader('Content-Type')
if response_content_type in allowed_image_content_types:
error('%s has bad content type: %s' % (file_full_path, response_content_type))
except:
error('%s cannot fetch image' % file_full_path)
if linter_command.get(file_extension, False):
command = list(linter_command[file_extension])
command.append(file_full_path)
debug('running %s' % command)
lint_exit_code = subprocess.call(command)
if lint_exit_code > 0:
error('%s failed linting, see above errors' % file_full_path)
files = sys.argv[1:]
if len(files) == 0:
for root, dirs, files_in_folder in os.walk("."):
for file in files_in_folder:
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)
elif files[0] == '--pr':
output = subprocess.check_output('git diff --name-only upstream/master..HEAD | cat', shell=True)
files = output.strip().split('\n')
for file in files:
file_name, file_ext = os.path.splitext(file)
if any('.' in s for s in (file.split('/')[1:-1])) or file_ext == '.md':
debug('skipping file %s' % file)
else:
debug('checking file %s' % file)
check_file(os.path.join(os.getcwd(), file))
if error_count > 0:
error('failed with %i errors' % error_count)
exit(1)