This repository was archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 401
/
Copy pathcheckin_test.py
121 lines (103 loc) · 3.26 KB
/
checkin_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
#!/usr/bin/python
# Copyright (C) 2001, 2002, 2003 Vladimir Prus. Permission to copy, use, modify, sell and
# distribute this software is granted, provided this copyright notice appears
# in all copies and modified versions are clearly marked as such. This software
# is provided "as is" without express or implied warranty, and with no claim as
# to is suitability for any purpose.
import os;
import re;
import string;
import getopt;
import sys;
import string;
no_tabs = 0
no_endings = 0
no_trailing = 0
no_last = 0
def get_files_list(current_path=""):
try:
entries_file = open("CVS/Entries")
except IOError:
pass
result = []
entries = parse_entries_file(entries_file)
for e in entries:
if e[1] == "file":
result.append(current_path + e[0])
elif e[1] == "dir":
cwd = os.path.abspath(os.getcwd())
try:
os.chdir(e[0])
result.extend(get_files_list(current_path + e[0] + "/"))
os.chdir(cwd)
except OSError:
pass
else:
assert(0)
def is_text(f):
i = string.rfind(f, ".")
return i != -1 and f[i:] in [".cpp", ".hpp", ".py", ".jam"]
return filter(is_text, result)
entries_re = re.compile("(.*?)/(.+?)/");
def parse_entries_file(entries_file):
def mk(x):
m = entries_re.match(x)
if m is None:
return None
if m.group(1) == "":
type = "file"
elif m.group(1) == "D":
type = "dir"
else:
print x
print m.group(1)
assert(0)
return (m.group(2), type)
return filter(lambda x: x, map(mk, entries_file.readlines()))
check_file_re = re.compile("(?s).+[ \t\r]+\n")
def check_file(name):
complaints = []
f = open(name, "rb")
s = f.read()
if not no_tabs and '\t' in s:
complaints.append("tabs")
if not no_endings and '\r' in s:
complaints.append("line endings")
if not no_trailing and check_file_re.match(s) is not None:
complaints.append("trailing spaces")
if not no_last and len(s) != 0 and s[-1] != '\n':
complaints.append("last line")
f.close()
if len(complaints) != 0:
return name + " : " + string.join(complaints, ",")
else:
return None
def main():
global no_tabs, no_endings, no_trailing, no_last
try:
opts, args = getopt.getopt(sys.argv[1:], "", ["no-tabs", "no-endings", "no-trailing", "no-last"])
except getopt.error, x:
print x
print "usage: checkin_test.py [--no-tabs] [--no-endings] [--no-trailing] [--no-last]"
sys.exit(1)
for i in opts:
if i[0] == "--no-tabs":
no_tabs = 1
elif i[0] == "--no-endings":
no_endings = 1
elif i[0] == "--no-trailing":
no_trailing = 1
elif i[0] == "--no-last":
no_last = 1
else:
assert(0)
files = get_files_list()
complaints = filter(lambda x: x, map(check_file, files))
if len(complaints) != 0:
print "There are problem, see 'checking_problems' file for details."
f = open("checkin_problems", "w")
for c in complaints:
f.write(c + "\n")
else:
print "Ok."
main()