-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstylecheck.py
executable file
·193 lines (153 loc) · 6.37 KB
/
stylecheck.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
#!/usr/bin/env python
from __future__ import print_function
import os
import glob
import sys
if sys.version_info < (2,6):
print("must be run with at least Python 2.6", file=sys.stderr)
sys.exit(1)
try:
import argparse
except ImportError:
print("Could not find argparse module! This indicates you are running a version of Python "
"older than 2.7. Run `sudo easy_install argparse` to install it and try again.")
sys.exit(1)
parser = argparse.ArgumentParser(description="Verifies that files in the repository adhere to certain stylistic guidelines.")
parser.add_argument("--autofix", action="store_true", help="attempt to fix detected problems automatically")
# Absolute path to directory of Python script
scriptDirectory = os.path.dirname(os.path.realpath(__file__))
# Root directory of the Git repository
repositoryDirectory = os.path.realpath(os.getcwd())
args = parser.parse_args()
sourceHeader = ""
with open(os.path.join(scriptDirectory, "header.txt")) as sourceHeaderFile:
sourceHeader = sourceHeaderFile.read()
# File names and extensions to ignore
ignoredFiles = ["CMakeCache.txt"]
ignoredExtensions = ["nib", "plist", "strings"]
licenseIgnoredFiles = []
# http://stackoverflow.com/a/3002505/343845
def is_binary(filename):
"""Return true if the given filename is binary.
@raise EnvironmentError: if the file does not exist or cannot be accessed.
@attention: found @ http://bytes.com/topic/python/answers/21222-determine-file-type-binary-text on 6/08/2010
@author: Trent Mick <[email protected]>
@author: Jorge Orpinel <[email protected]>"""
fin = open(filename, 'rb')
try:
CHUNKSIZE = 1024
while 1:
chunk = fin.read(CHUNKSIZE)
if b'\0' in chunk: # found null byte
return True
if len(chunk) < CHUNKSIZE:
break # done
finally:
fin.close()
return False
# Converts an absolute path to a path relative to
# the root directory of the project's Git repository
def toRepositoryRelativePath(path, gitDirectory):
if path.startswith(gitDirectory):
return "." + path[len(gitDirectory):]
else:
raise Exception("Path given to toRepositoryRelativePath did not refer to a path within the repository")
# Finds problematic character sequences in the given
# string and returns an array of string messages noting
# the problems that were found
def findProblemSequences(wholeFile, line, file, lineNumber, totalLines):
problems = []
if lineNumber == 1 and line.startswith("\xEF\xBB\xBF"):
problems.append("File contains UTF-8 BOM; skipping additional checks for this file")
return problems, line
if lineNumber == 1 and ( \
(line.startswith(" ") and not os.path.basename(file) == "LICENSE") or \
(line.startswith("\t") and not os.path.basename(file) == ".gitignore") or \
line.startswith("\n")):
if args.autofix:
line = line.lstrip(" ")
else:
problems.append("Leading whitespace in file")
if lineNumber == totalLines and not line.endswith("\n"):
if args.autofix:
line = line + "\n"
else:
problems.append("File did not end with a newline")
if lineNumber == 1 and wholeFile.endswith("\n\n"):
problems.append("File contained multiple ending newlines")
if "\r" in line:
if args.autofix:
line = line.replace("\r", "")
else:
problems.append("Windows carriage return")
if "\t" in line and not os.path.basename(file) == ".gitmodules":
if args.autofix:
line = line.replace("\t", "")
else:
problems.append("Tab characters")
if line.endswith(" \n"):
if args.autofix:
line = line.rstrip(" \n") + "\n"
else:
problems.append("Trailing whitespace")
if lineNumber == 1 and \
(file.endswith(".h") or \
file.endswith(".c") or \
file.endswith(".cpp") or \
file.endswith(".m") or \
file.endswith(".mm")) and not os.path.basename(file) in licenseIgnoredFiles and not wholeFile.startswith(sourceHeader):
problems.append("C-language source code file does not contain standard header")
return problems, line
# Build a list of all files to check
def buildInspectionFileList(directory):
fileList = []
for root, subFolders, files in os.walk(directory):
if ".git" in subFolders:
subFolders.remove(".git")
if "firebreath" in subFolders:
subFolders.remove("firebreath")
ignoreDirectory = False
for file in files:
if file == ".stylecheckignore":
print("Ignoring directory " + root)
del subFolders[:]
ignoreDirectory = True
if ignoreDirectory:
continue
for file in files:
absFile = os.path.realpath(os.path.join(root, file))
# Only add the file if it's not in our ignore lists
_, ext = os.path.splitext(file)
if (not ext[1:] in ignoredExtensions) and (not file in ignoredFiles) and (not is_binary(absFile)):
fileList.append(absFile)
return fileList
def runComplianceCheck(directory):
hasProblems = False
fileList = buildInspectionFileList(directory)
# Go through each file for potential issues
for file in fileList:
with open(file, "r+") as f:
lines = f.readlines()
correctedLines = []
f.seek(0, 0)
# Check each line in the file for gremlins
for counter, line in enumerate(lines):
lineNumber = counter + 1
# If the line contained gremlins, show details
problems, line = findProblemSequences(''.join(lines), line, file, lineNumber, len(lines))
if problems:
hasProblems = True
print(toRepositoryRelativePath(file, directory) + ":" + str(lineNumber), file=sys.stderr)
for problem in problems:
print("\t" + problem, file=sys.stderr)
if args.autofix:
correctedLines.append(line)
for l in correctedLines:
f.write(l)
f.close()
return hasProblems
print("Checking code style...")
if not runComplianceCheck(repositoryDirectory):
print("No problems found!")
else:
sys.exit(1)