Add a main file that explore the given dir/files or pwd

This commit is contained in:
Némunaire 2011-11-13 13:35:50 +01:00
parent 95d423b7d5
commit 19221eba77
6 changed files with 79 additions and 79 deletions

0
__init__.py Normal file
View File

9
c.py
View File

@ -1,6 +1,7 @@
#!/usr/bin/python #!/usr/bin/python
import re import re
import os
from report import perr from report import perr
from common import checkTrailingWhitespace from common import checkTrailingWhitespace
from common import check80cols from common import check80cols
@ -583,8 +584,12 @@ def checkFile(path, opt):
current += 1 current += 1
cOnLine += 1 cOnLine += 1
#print("Ce fichier contient {0} lignes".format(numLign)) #print("{1} contient {0} lignes".format(numLign, os.path.basename(path)))
import sys import sys
checkFile(sys.argv[1], dict()) if __name__ == "__main__":
i = 1
while i < len(sys.argv):
checkFile(sys.argv[i], dict())
i += 1

2
common.py Normal file → Executable file
View File

@ -9,7 +9,7 @@ def checkTrailingWhitespace(path, line, num):
def check80cols(path, line, num): def check80cols(path, line, num):
if len(line) > 79: if len(line) > 79:
printError(path, "80 columns exceeded", num) perr(path, "80 columns exceeded", num)
def checkFile(path, opt): def checkFile(path, opt):

63
main.py Executable file
View File

@ -0,0 +1,63 @@
#!/usr/bin/python
import sys
import os
from report import perr
import c
import authors
import common
import moulinette
def checkDir(path, opt):
dirList = os.listdir(path)
filesList = []
for f in dirList:
basename = os.path.basename(f)
if os.path.isdir(f):
if basename == ".git" or basename == ".hg":
perr(path, "There is a {0} repository".format(basename), -1)
else:
if basename[0] == '.':
perr(path + '/' + basename, "Hidden directory", -1)
checkDir(path + '/' + f, opt)
else:
if basename[0] == '.':
perr(path + '/' + basename, "Hidden file", -1)
checkFile(path + '/' + f, opt)
def checkFile(path, opt):
if os.path.isdir(path):
checkDir(path, opt)
else:
baseName = os.path.basename(path)
if baseName.upper() == "AUTHORS":
authors.checkFile(path, opt)
if baseName[len(baseName)-1] == '~' or baseName[len(baseName)-1] == '#'\
or baseName[0] == '#':
perr(path, "This is a temporary file", -1)
iExt = baseName.rfind('.')
if iExt > 0:
ext = baseName[iExt:]
try:
if ext == ".c" or ext == ".h":
moulinette.checkCFile(path)
c.checkFile(path, opt)
elif ext == ".sh":
sh.checkFile(path, opt)
elif ext != ".o" and ext != ".so" and ext != ".a" and ext != ".in":
common.checkFile(path, opt)
except NameError as e:
print "Hmmm, something fail: {0}. Sorry, you may report this bug.".format(e)
except IndexError:
pass
if len(sys.argv) < 2:
checkFile(os.getcwd(), dict())
else:
i = 1
while i < len(sys.argv):
checkFile(sys.argv[i], dict())
i += 1

View File

@ -3,61 +3,10 @@ import signal
import sys import sys
import os import os
import re import re
from report import perr
if len(sys.argv) < 2:
print("Usage:\t{0} FILE...".format(sys.argv[0]))
sys.exit(1)
def printError(file, type, line = -1): def printError(file, type, line = -1):
if line >= 0: perr(file, type, line)
print("{0} in {1}:{2}".format(type, file, line))
else:
print("{0} for {1}".format(type, file))
def checkFile(path):
if os.path.isdir(path):
checkDir(path)
else:
baseName = os.path.basename(path)
if baseName.upper() == "AUTHORS":
if baseName != "AUTHORS":
printError(path, "Bad filename")
checkAuthors(path)
if baseName[len(baseName)-1] == '~' or baseName[len(baseName)-1] == '#'\
or baseName[0] == '#':
printError(path, "This is a temporary file")
iExt = baseName.rfind('.')
if iExt > 0:
ext = baseName[iExt:]
if ext == ".c":
checkCFile(path)
elif ext == ".h":
checkHFile(path)
elif ext == ".sh":
checkSHFile(path)
else:
checkOtherFile(path)
def checkDir(path):
dirList = os.listdir(path)
filesList = []
for f in dirList:
basename = os.path.basename(f)
if os.path.isdir(f):
if basename == ".git" or basename == ".hg":
printError(path, "There is a {0} repository".format(basename))
else:
if basename[0] == '.':
printError(path + '/' + basename, "Hidden directory")
checkDir(path + '/' + f)
else:
if basename[0] == '.':
printError(path + '/' + basename, "Hidden file")
checkFile(path + '/' + f)
def checkCComment(path, line, num): def checkCComment(path, line, num):
txt = line.strip() txt = line.strip()
@ -84,18 +33,6 @@ def checkCLang(path, line, num):
elif re.match("^# *(else|endif)", txt) is not None and \ elif re.match("^# *(else|endif)", txt) is not None and \
re.match("^# *(else|endif) +/", txt) is None: re.match("^# *(else|endif) +/", txt) is None:
printError(path, "Preprocesor condition without comment describing the corresponding condition", num) printError(path, "Preprocesor condition without comment describing the corresponding condition", num)
elif re.match("^(typedef +)?struct", txt) is not None and \
re.match("^(typedef +)?struct +s_", txt) is None:
printError(path, "New struct without s_ name", num)
elif re.match("^(typedef +)?union", txt) is not None and \
re.match("^(typedef +)?union +u_", txt) is None:
printError(path, "New union without u_ name", num)
elif re.match("^(typedef +)?enum", txt) is not None and \
re.match("^(typedef +)?enum +e_", txt) is None:
printError(path, "New struct without s_ name", num)
elif re.match("^typedef *(^(union|struct|enum))", txt) is not None and \
re.match("^typedef +(unsigned )?[a-z_\*] +t_", txt) is None:
printError(path, "New type without t_ name", num)
elif re.match("^(if|else|elseif|for|while|do|typedef|struct|return|sizeof)", txt) is not None and \ elif re.match("^(if|else|elseif|for|while|do|typedef|struct|return|sizeof)", txt) is not None and \
re.match("^(if|else|elseif|for|while|do|typedef|struct|return|sizeof)(uble| |$)", txt) is None: re.match("^(if|else|elseif|for|while|do|typedef|struct|return|sizeof)(uble| |$)", txt) is None:
printError(path, "Forgotten space after keyword", num) printError(path, "Forgotten space after keyword", num)
@ -103,7 +40,7 @@ def checkCLang(path, line, num):
re.match("^(if|else|elseif|for|while|do|typedef|struct).*\{", txt) is not None: re.match("^(if|else|elseif|for|while|do|typedef|struct).*\{", txt) is not None:
printError(path, "{ on the same line of instruction", num) printError(path, "{ on the same line of instruction", num)
elif re.match("(^/\*|\*/$)", txt) is not None and \ elif re.match("(^/\*|\*/$)", txt) is not None and \
re.match("^(/\*|\*/)$", txt) is None: re.match("^(/\*\*?|\*/)$", txt) is None:
printError(path, "First and last big comment lines aren't empty", num) printError(path, "First and last big comment lines aren't empty", num)
elif re.match("/\*$", txt) is not None: elif re.match("/\*$", txt) is not None:
return 1 return 1
@ -142,19 +79,14 @@ def checkCFile(path):
num += 1 num += 1
line = line.replace(" ", " ") line = line.replace(" ", " ")
check80cols(path, line, num)
checkTrailingWhitespace(path, line, num)
if typeBlock == 1: if typeBlock == 1:
typeBlock = checkCComment(path, line, num) typeBlock = checkCComment(path, line, num)
else: else:
typeBlock = checkCLang(path, line, num) typeBlock = checkCLang(path, line, num)
#Define some constants if __name__ == "__main__":
i = 1 i = 1
while i < len(sys.argv): while i < len(sys.argv):
checkFile(sys.argv[i]) checkCFile(sys.argv[i])
i += 1 i += 1

0
report.py Normal file → Executable file
View File