-
Notifications
You must be signed in to change notification settings - Fork 0
/
norme
executable file
·414 lines (377 loc) · 13.7 KB
/
norme
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#!/usr/bin/python
#
# Made by [email protected]
# Version: 1.2.1
#
'''
An Epitech norme checker
Usage: python norme.py <files to scan> [-nocheat] [-verbose] [-score] [-libc]
-verbose: affiche les messages impossible d\'ouvrir
-nocheat: desactive la detection de la triche
-score: affiche le nombre de faute de norme
-libc: active la verification des fonctions de la libc
-malloc: desactive le controle du malloc
-printline: affiche la ligne provoquant une erreur
-return: active verifier le retour des fonctions (return ;)
-comment: ne pas verifier les commentaire
Non geree:
- Indentation
- +<escape>
- verification de la presence de gl_
_ macro multiligne
_ initialisation de variable a la declaration
_ separation initialisation de variable et code
A modifier:
Fonction checkant la protection contre la double inclusion des .h .
Appel ligne 163.
Bug:
Il est arrivee que le checker ne trouve aucune faute alors qu\'il en existe, si
ce bug vous arrive maillez moi.
'''
import sys,re,os,pwd
version = 0.5
class norme:
def __init__(self):
self.user = []
self.verbose = 0
self.cheat = 1
self.comment = 1
self.score = 1
self.note = 0
self.libc = 1
self.printline = 0
self.creturn = 1
def new_file(self):
self.nb_line = 1
self.nb_return = 0
self.nb_funcline = 0
self.nb_func = 0
self.sys_include = 0
self.double_inclusion = 0
self.is_func = 0
self.typedef = 0
if self.verbose == 1:
print "Scan",self.file
def check_header(self):
if (self.nb_line == 1):
if (self.line[:2] != '/*'):
self.print_error('header incorrect')
self.note += 20
elif (self.nb_line == 9):
if (self.line[:2] != '*/'):
self.print_error('header incorrect')
elif self.nb_line == 4 or self.nb_line == 7 or self.nb_line == 8:
if self.cheat:
p = re.compile('([\w-]* [\w-]*)$')
test = re.search(p, self.line)
if test:
if not test.group(1) in self.user:
self.print_error('login '+ test.group(1) +' incorrect')
print "Note: -42"
sys.exit()
elif (self.nb_line == 5):
if self.cheat:
p = re.compile('<(.*)@')
test = re.search(p, self.line)
if test:
if not test.group(1) in self.user:
self.print_error('login '+ test.group(1) +' incorrect')
print "Note: -42"
sys.exit()
else:
if (self.line[:2] != '**'):
self.print_error('header incorrect')
def check_virgule(self):
if is_commented(self.line) == False:
n = 0
quote = 0
while self.line[n] != '\n':
if self.line[n] == '\'' or self.line[n] == '"' and self.line[n - 1] != '\\':
if quote:
quote = 0
else:
quote = 1
if (self.line[n] == ';' or self.line[n] == ',') and quote == 0:
if self.line[n + 1] != ' ' and self.line[n + 1] != '\n':
self.note += 1
self.print_error('point-virgule ou virgule mal place')
n = n + 1
def check_nbchar(self):
line = self.line.replace('\t', ' ')
if (line[80:]):
self.note += 1
self.print_error('chaine de plus de 80 caracteres')
def check_return(self):
if (self.line[:1] == '\n'):
if (self.nb_return == 1):
self.note += 1
self.print_error('double retour a la ligne')
else:
self.nb_return = 1
else:
self.nb_return = 0
def check_nbline(self):
if self.file[-2:] == ".c":
if self.line[:1] == '}':
self.is_func = 0
self.nb_funcline = 0
if self.line[:1] == '{' and self.typedef == 0:
self.is_func = 1
self.nb_funcline = 0
self.nb_func = self.nb_func + 1
if self.nb_func == 6:
self.note += 1
self.print_error('plus de 5 fonctions dans le fichier')
else:
if self.nb_func >= 1 and self.is_func:
self.nb_funcline = self.nb_funcline + 1
if self.nb_funcline >= 26:
self.note += 1
self.print_error('fonction de plus de 25 lignes')
def check_cfunc(self):
if is_commented(self.line) == False:
p = re.compile('[ \t](if|else|return|while|for)(\()')
test = re.search(p, self.line)
if test:
self.note += 1
self.print_error('pas d\'espace apres mot clef')
def check_arg(self):
if self.line[-2:] == ")\n" and self.line[:1] != '\t' and self.line[:1] != ' ':
p = re.compile('(.*),(.*),(.*),(.*),(.*)\)$')
test = re.search(p, self.line)
if test:
self.note += 1
self.print_error('plus de 4 arguments passes en parametre')
def check_sys_include(self):
if self.line[:1] == "#" and self.line[-2:] == "\"\n":
self.sys_include = 1
else:
if self.line[:1] == "#" and self.line[-2:] == ">\n" and self.sys_include == 1:
self.note += 1
self.print_error('Header systeme mal placee')
def check_comment(self):
if self.is_func and self.comment:
p = re.compile('(//|/\*)')
test = re.search(p, self.line)
if test:
self.note += 1
self.print_error('Commentaires dans le code')
def check_malloc(self):
p = re.compile('[^x](malloc)(\()')
test = re.search(p, self.line)
if test and (self.file != "xmalloc.c"):
self.print_error('Malloc non verifiee')
self.note += 1
def check_operateur(self, op):
n = 0
quote = 0
while self.line[n] != '\n':
if self.line[n] == '\'' or self.line[n] == '"' and self.line[n - 1] != '\\':
if quote:
quote = 0
else:
quote = 1
if (self.line[n] == op) and quote == 0:
if self.line[n + 1] != ' ' and self.line[n + 1] != ';' and self.line[n + 1] != '=':
if self.line[n - 1] != op and self.line[n + 1] != op:
msg = 'Operateur %c mal place' % op
self.print_error(msg)
self.note += 1
n = n + 1
def check_typedef(self):
if self.line[:7] == "typedef":
self.typedef = 1
else:
self.typedef = 0
def check_regex(self, regex, msg):
p = re.compile(regex)
test = re.search(p, self.line)
if test:
self.note += 1
self.print_error(msg)
def check_line(self):
if is_commented(self.line) == False:
self.check_nbline() # DOIT TOUJORS ETRE EN PREMIER
self.check_sys_include()
self.check_virgule()
self.check_regex('[ \t]$', 'Espace en fin de ligne')
if self.creturn == 0:
self.check_regex('return( \(\)| ;|;)', 'Mauvais format de return')
if self.libc == 0:
self.check_regex('[^_](printf|atof|atoi|atol|strcmp|strlen|strcat|strncat|strncmp|strcpy|strncpy|fprintf|strstr|strtoc|sprintf|asprintf|perror|strtod|strtol|strtoul)(\()', \
'Fonction de la lib C')
self.check_nbchar()
self.check_cfunc()
self.check_arg()
self.check_comment()
self.check_return()
self.check_operateur('+')
self.check_operateur('|')
self.check_typedef() #DOIT TOUJOURS ETRE EN DERNIER
def print_error(self, msg):
print "Erreur dans",self.file,"a la ligne",self.nb_line,":",msg
if self.printline:
print self.line
def cant_open(self, file):
if (self.verbose or file == sys.argv[1]):
print "Impossible d'ouvrir",file
def scan_files(self, files):
for file_name in files:
self.file = file_name
self.new_file()
try:
fd = open(file_name, 'r')
except IOError:
self.cant_open(file)
else:
for self.line in fd.readlines():
if self.nb_line <= 9:
vartemp = 0
#self.check_header()
else:
self.check_line()
self.nb_line = self.nb_line + 1
fd.close()
def scandir(self, thedir):
try:
dir = os.listdir(thedir)
except:
self.cant_open(thedir)
else:
check_makefile(thedir)
for file in dir:
if (os.path.isdir(thedir + file)):
self.scandir(thedir + "/" + file + "/")
if file[-2:] == '.c' or file[-2:] == '.h':
self.file = file
self.new_file()
file = thedir + file
try:
fd = open(file, 'r')
except IOError:
self.cant_open(file)
else:
for self.line in fd.readlines():
if self.nb_line <= 9:
vartemp = 0
#self.check_header()
else:
self.check_line()
self.nb_line = self.nb_line + 1
fd.close()
# Trouve le login dans la variable $MOULINETTE_USER et va chercher
# le nom complet dans /afs/epitech.net/site/etc/passwd
# Si pas de variable $MOULINETTE_USER va chercher $USER
def get_user(self):
user_list = []
user = os.getenv('MOULINETTE_USER')
if user:
user_list = user.split(';')
if not user:
user_list.append(os.getenv('USER'))
for user_name in user_list:
f = open("/private/etc/passwd", 'r')
while 1:
line = f.readline()
if line:
line = line.split(':')
if user_name == line[0]:
break
if not line:
f.close()
return
f.close()
self.user.append(user_name)
self.user.append(line[4])
def check_makefile(thedir):
file = thedir + "Makefile"
if os.path.isfile(file):
try:
fd = open(file, 'r')
except IOError:
print "Impossible d'ouvrir le Makefile"
else:
buffer = fd.read()
p = re.compile('(-g|-pg|-lefence)')
test = re.search(p, buffer)
if test:
print "Options de debug dans le Makefile"
p = re.compile('(-Wall)')
test = re.search(p, buffer)
if not test:
print "-Wall n'est pas dans le Makefile"
p = re.compile('(-pedantic)')
test = re.search(p, buffer)
if not test:
print "-pedantic n'est pas dans le Makefile"
if buffer[:2] != "##":
print "Header du Makefile invalide"
fd.close()
def get_files(argv):
li = []
pattern = re.compile('[.]c$|[.]h$')
for arg in sys.argv:
test = re.search(pattern, arg)
if test:
li.append(arg)
return li
def is_commented(line):
if ((line[0] == '/' or line[0] == '*') and line[1] == '*'):
return True
return False
def check_version():
file = "/afs/epitech.net/users/prof/astek/public/norme_version"
if os.path.isfile(file):
try:
fd = open(file, 'r')
except IOError:
print "Impossible de verifier la version du script"
else:
buffer = fd.read()
if (version != float(buffer)):
print "Mauvaise version du script, verifiez sur le public astek"
def help():
print "Aide"
print "Moulinette version " + str(version)
print "Usage: norme.py <files_to_scan>"
print "-verbose: affiche les messages impossible d'ouvrir"
print "-nocheat: desactive la detection de la triche"
print "-score: affiche le nombre de faute de norme"
print "-libc: active la verification des fonctions de la libc"
print "-printline: affiche la ligne provoquant une erreur"
print "-return: active verifier le retour des fonctions (return ;)"
print "-comment: ne pas verifier les commentaires"
sys.exit()
def main():
if '-help' in sys.argv[1:]:
help()
check_version()
if len(sys.argv) == 1:
print "Usage: norme.py <files_to_scan>"
sys.exit()
moulin = norme()
files = get_files(sys.argv)
if '-verbose' in sys.argv[1:]:
moulin.verbose = 1
if '-comment' in sys.argv[1:]:
moulin.comment = 0
if '-nocheat' in sys.argv[1:]:
moulin.cheat = 0
if '-score' in sys.argv[1:]:
moulin.score = 1
if '-libc' in sys.argv[1:]:
moulin.libc = 0
if '-printline' in sys.argv[1:]:
moulin.printline = 1
if '-return' in sys.argv[1:]:
moulin.creturn = 0
if moulin.cheat == 1:
moulin.get_user()
try:
moulin.scan_files(files)
except NameError:
print "Usage: norme.py <files_to_scan>"
if moulin.score:
print "Note:",-moulin.note,
if __name__ == "__main__":
main()