Commit 2b198722 authored by Daniel Armbruster's avatar Daniel Armbruster Committed by thomas.forbriger
Browse files

Implemented debug mode

This is a legacy commit from before 2015-05-18.
It may be incomplete as well as inconsistent.
See COPYING.legacy and README.history for details.


SVN Path:     http://gpitrsvn.gpi.uni-karlsruhe.de/repos/TFSoftware/trunk
SVN Revision: 4327
SVN UUID:     67feda4a-a26e-11df-9d6e-31afc202ad0c
parent 60f51c0f
......@@ -33,6 +33,8 @@
#
# REVISIONS and CHANGES
# 15/09/2011 V0.1 Daniel Armbruster
# 01/01/2012 V0.1.1 finished implementation
# 02/01/2012 V0.1.2 implemented debugMode
#
# =============================================================================
""" CsFile module to handle checksum files. """
......@@ -53,16 +55,27 @@ __copyright__ = "Copyright (c) 2012 by Daniel Armbruster"
# global variables
chunkSize = 131072 # 128kB
debugMode = False
# -----------------------------------------------------------------------------
class CsFileError(Exception):
def __init__(self, msg, line):
"""
Exception class of csfile module.
"""
def __init__(self, line, msg):
self.msg = msg
self.line = line
def display(self):
sys.stderr.write("csfile (ERROR): " + self.msg + "\n")
sys.stderr.write("csfile (ERROR): " + str(self.msg) + "\n")
sys.stderr.write("triggered in line: " + str(self.line) + "\n")
class CsReport(CsFileError):
"""
Auxiliary class for debugging.
"""
def display(self):
sys.stderr.write("csfile (REPORT): " + str(self.msg) + "\n")
sys.stderr.write("triggered in line: " + str(self.line) + "\n")
# -----------------------------------------------------------------------------
......@@ -83,8 +96,8 @@ class CsFile:
try:
csfile = open(path, 'w')
except IOError as err:
raise CsFileError("[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename, 87)
raise CsFileError(97, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename)
else:
csfile.close()
......@@ -94,16 +107,20 @@ class CsFile:
"""
path = self.__filepath+os.sep+self.__filename
try:
if debugMode:
CsReport(109, "Start reading checksumfile: "+path).display()
csfile = open(path)
self.__cslines = [CsLine(line.split()) for line in csfile \
if len(line.rstrip()) and line[0] != '#']
except IOError as err:
# Maybe better to create the file here and keep on going.
# Will be managed later during further dev.
raise CsFileError("[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename, 104)
raise CsFileError(116, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename)
else:
csfile.close()
if debugMode:
CsReport(121, "Finished reading checksumfile: "+path).display()
def write(self):
"""
......@@ -111,37 +128,48 @@ class CsFile:
"""
path = self.__filepath+os.sep+self.__filename
try:
if debugMode:
CsReport(130, "Start writing checksumfile: "+path).display()
csfile = open(path, 'w')
for csline in self.__cslines:
if debugMode:
CsReport(134, "Writing line: "+ str(csline)).display()
if isinstance(csline, CsLine):
csfile.write(str(csline) + '\n')
else:
raise CsFileError("Argument must be of type CsLine.", 116)
raise CsFileError(136, "Argument must be of type CsLine.")
except IOError as err:
raise CsFileError("[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename, 119)
raise CsFileError(138, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename)
else:
csfile.close()
if debugMode:
CsReport(145, "Finished writing checksumfile: "+path)
def append(self, cslines):
"""
Append checksum lines to the checksum file.
"""
path = self.__filepath+os.sep+self.__filename
# Maybe not necessary to raise an exception here
if 0 == len(cslines):
raise CsFileError("Invalid argument. Empty list.", 130)
if 0 == len(cslines) and debugMode:
CsReport(153, "Invalid argument. Empty list.").display()
try:
if debugMode:
CsReport(156, "Start appending to checksumfile: "+path).display()
csfile = open(path, 'a')
for csline in cslines:
if debugMode:
CsReport(160, "Writing line: "+str(csline)).display()
if isinstance(csline, CsLine):
csfile.write(str(csline) + '\n')
else:
raise CsFileError("Argument must be of type CsLine.", 137)
raise CsFileError(164, "Argument must be of type CsLine.")
except IOError as err:
raise CsFileError("[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename, 140)
raise CsFileError(166, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename)
else:
if debugMode:
CsReport(170, "Finished appending to checksumfile: "+path).display()
csfile.close()
def update(self, regexes=[]):
......@@ -150,16 +178,19 @@ class CsFile:
checksum file in current directory.
"""
if not isinstance(regexes, list):
raise CsFileError("Pass regular expressions in a list of strings", 150)
raise CsFileError(179, "Pass regular expressions in a list.")
if debugMode:
CsReport(181, "Updating checksumfile ...").display()
# fetch cslines in current csfile
self.read()
# IMPORTANT NOTE: Check again if it is more convenient to whole path or
if debugMode:
CsReport(185, "Fetching files not registered yet.").display()
registeredFiles = [csline.path.split(os.sep)[-1] \
for csline in self.__cslines]
# fetch files excluding regex matches
newFiles = list(set([file for file in os.listdir(self.__filepath) \
if os.path.isfile(file) for regex in regexes \
if None == re.search(regex,file)]))
if None == re.search(str(regex),file)]))
# exclude registered files
newFiles = list(set([file for file in newFiles \
for registeredFile in registeredFiles if registeredFile != file]))
......@@ -167,6 +198,8 @@ class CsFile:
csLines = [CsLine(self.__filepath+os.sep+file, self.__hashfunc) \
for file in newFiles]
for line in csLines:
if debugMode:
CsReport(200, "Calculating checksum of file: "+file).display()
line.generate(chunkSize)
self.append(csLines)
......@@ -177,14 +210,21 @@ class CsFile:
2, calculate checksum of file which is located in srcDir and check results
3. write the result to the checksum file
"""
if debugMode:
CsReport(212, "Start checking checksums.").display()
self.read()
if 0 == len(self.__cslines):
raise CsFileError("CSFILE does not contain any lines.", 179)
if 0 == len(self.__cslines) and debugMode:
CsReport(215, "CSFILE does not contain any lines.").display()
for csline in self.__cslines:
if not len(srcDir):
if debugMode:
CsReport(219, "Performing check of file with file itself.").display()
csline.check()
else:
filename = csline.path.split(os.sep)[-1]
if debugMode:
CsReport(219, "Performing check of file with source: "+ \
srcDir+os.sep+filename).display()
csline.check(srcDir+os.sep+filename)
self.write()
......@@ -239,8 +279,8 @@ class CsLine:
hasfunc.update(chunk)
self.checksum = hashfunc.hexdigest()
except IOError as err:
raise CsFileError("[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename, 240)
raise CsFileError(280, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename)
else:
file.close()
......@@ -274,13 +314,13 @@ class CsLine:
checksum = hashfunc.hexdigest()
except IOError as err:
# Maybe better to avoid Exception here and just put the status to notice
raise CsFileError("[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename, 275)
raise CsFileError(275, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename)
else:
file.close()
# checks
if not len(self.checksum):
raise CsFileError("No checksum calculated yet.", 280)
raise CsFileError(280, "Caclulation of checksum was not successful.")
if checksum == self.checksum:
self.statusLastCheck = 'ok'
else:
......@@ -298,6 +338,7 @@ class CsLine:
# Tests
if __name__ == '__main__':
try:
debugMode = True
file = CsFile('/home/daniel/')
file.read()
except CsFileError as err:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment