Starting the newer version ; currently, preprocessor directives are managed

This commit is contained in:
Némunaire 2011-10-31 02:39:04 +00:00
parent 28f6dfa2aa
commit 98095ca4ed
5 changed files with 182 additions and 52 deletions

27
authors.py Executable file
View File

@ -0,0 +1,27 @@
#!/usr/bin/python
from report import perr
import re
def checkFile(path, opt):
num = 0
with open(path, "r") as fp:
content = fp.read()
if len(content) < 1 or content[len(content)-1] != '\n':
perr(path, "No empty line at the end of file", -1)
if 'authors-prefix' in opt:
prefix = opt['authors-prefix'].replace("*", "\*")
else:
prefix = "\*"
lines = content.split('\n')
for line in lines:
num += 1
if len(line) <= 0 and num != len(lines):
perr(path, "Empty line", num)
if re.match("^(.{" + "{0}".format(len(prefix.replace("\\", ""))) + "} [a-z][a-z-]{,5}_[a-z0-9])?$", line) is None:
perr(path, "Bad author format", num)
elif re.match("^(" + prefix + " [a-z][a-z-]{,5}_[a-z0-9])?$", line) is None:
perr(path, "Bad line prefix (expected: {0})".format(prefix.replace("\\", "")), num)

123
c.py Executable file
View File

@ -0,0 +1,123 @@
#!/usr/bin/python
import re
from report import perr
from common import checkTrailingWhitespace
from common import check80cols
def validateString(path, content, start, lign):
current = start + 1
contentSize = len(content)
while current < contentSize and (content[current] != '"' or content[current - 1] == '\\'):
if content[current] == '\n':
perr(path, "Wrap in string", lign)
current += 1
return (current - start)
def validatePreprocDirective(path, content, start, start_lign, states):
lign = start_lign
current = start
contentSize = len(content)
cOnLine = 1
eolAlign = -1 #Alignement de la fin de ligne
state = 0
directive = "" #The name of the directive
content = "" #The rest of the line
ident = 0;
while current < contentSize and (content[current] != '\n' or content[current - 1] == '\\'):
current += 1
if content[current] == '\n' and content[current - 1] == '\\':
print "new line"
lign += 1
cOnLine = 0
if content[current - 1] == '\t':
cOnLine += 7
else:
cOnLine += 1
#Check indentation
if state == 0:
if content[current] != ' ' and content[current] != '\t':
state += 1
ident = current - start - 1
#Wrap
if content[current] == '\\':
print '\\'
if eolAlign <= 0:
eolAlign = cOnLine
elif cOnLine != eolAlign:
perr(path, "Bad endline", lign)
print '\\'
#Spaces state
if state == 2 or state == 4:
if content[current] != ' ' and content[current] != '\t':
state += 1
#Searching the directive name
if state == 1:
if content[current] != ' ' and content[current] != '\t':
directive += content[current]
else:
state += 1
if directive.find("endif") == 0:
states['preproc-ident'] -= 1
if ident != states['preproc-ident']:
perr(path, "Bad preprocessor indentation (expected: {0}, real: {1})".format(states['preproc-ident'], (ident)), lign)
if directive.find("if") == 0:
states['preproc-ident'] += 1
eaten = { 'chars': current - start - 1, 'lines': lign, 'col': cOnLine }
return eaten
def checkFile(path, opt):
numLign = 1
with open(path, "r") as fp:
content = fp.read()
current = 0;
cOnLine = 0
last_nblank = 0
contentSize = len(content)
states = {
'preproc-ident': 0 #Niveau d'identation dans le preprocesseur
}
while current < contentSize:
if content[current] == '\n':
numLign += 1
last_nblank = 0
cOnLine = -1
#Preprocessor line
if last_nblank == cOnLine and content[current] == '#':
if cOnLine != 0:
perr(path, "Preprocessor directive mark not on the first column", numLign)
eaten = validatePreprocDirective(path, content, current, numLign, states)
current += eaten['chars']
cOnLine += eaten['col']
numLign = eaten['lines']
#String
elif content[current] == '"':
eatenChar = validateString(path, content, current, numLign)
current += eatenChar
cOnLine += eatenChar
if last_nblank == cOnLine and content[current] == ' ' or content[current] == '\t':
last_nblank += 1
current += 1
cOnLine += 1
print("Ce fichier contient {0} lignes".format(numLign + 1))
import sys
checkFile(sys.argv[1], dict())

24
common.py Normal file
View File

@ -0,0 +1,24 @@
#!/usr/bin/python
import re
from report import perr
def checkTrailingWhitespace(path, line, num):
if len(line) > 1 and (line[len(line)-1] == ' ' or line[len(line)-1] == '\t'):
perr(path, "Trailing whitespace", num)
def check80cols(path, line, num):
if len(line) > 79:
printError(path, "80 columns exceeded", num)
def checkFile(path, opt):
num = 0
with open(path, "r") as fp:
lines = fp.read().split('\n')
for line in lines:
num += 1
line = line.replace(" ", " ")
checkTrailingWhitespace(path, line, num)
check80cols(path, line, num)

View File

@ -4,13 +4,6 @@ import sys
import os
import re
def onSignal(signal, frame):
print("Bye!")
sys.exit(0)
signal.signal(signal.SIGINT, onSignal)
if len(sys.argv) < 2:
print("Usage:\t{0} FILE...".format(sys.argv[0]))
sys.exit(1)
@ -66,14 +59,6 @@ def checkDir(path):
printError(path + '/' + basename, "Hidden file")
checkFile(path + '/' + f)
def check80cols(path, line, num):
if len(line) > 79:
printError(path, "80 columns exceeded", num)
def checkTrailingWhitespace(path, line, num):
if len(line) > 1 and (line[len(line)-1] == ' ' or line[len(line)-1] == '\t'):
printError(path, "Trailing whitespace", num)
def checkCComment(path, line, num):
txt = line.strip()
if re.match("\*/", txt) is not None:
@ -164,43 +149,6 @@ def checkCFile(path):
else:
typeBlock = checkCLang(path, line, num)
def checkSHFile(path):
num = 0
with open(path, "r") as fp:
lines = fp.read().split('\n')
for line in lines:
num += 1
line = line.replace(" ", " ")
check80cols(path, line, num)
checkTrailingWhitespace(path, line, num)
def checkOtherFile(path):
num = 0
with open(path, "r") as fp:
lines = fp.read().split('\n')
for line in lines:
num += 1
line = line.replace(" ", " ")
checkTrailingWhitespace(path, line, num)
def checkAuthors(path):
num = 0
with open(path, "r") as fp:
fullContent = fp.read()
if fullContent < 1 or fullContent[len(fullContent)-1] != '\n':
printError(path, "No empty line at the end of file")
lines = fullContent.split('\n')
for line in lines:
num += 1
checkTrailingWhitespace(path, line, num)
if re.match("^(\* [a-z][a-z-]{,5}_[a-z0-9])?$", line) is None:
printError(path, "Bad author format", num)
#Define some constants

8
report.py Normal file
View File

@ -0,0 +1,8 @@
#!/usr/bin/python
def perr(path, descr, line):
if line >= 0:
print("\033[3m{0}\033[m in `{1}' on line {2}".format(descr, path, line))
else:
print("\033[3m{0}\033[m in `{1}'".format(descr, path))