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 @@ ...@@ -33,6 +33,8 @@
# #
# REVISIONS and CHANGES # REVISIONS and CHANGES
# 15/09/2011 V0.1 Daniel Armbruster # 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. """ """ CsFile module to handle checksum files. """
...@@ -53,16 +55,27 @@ __copyright__ = "Copyright (c) 2012 by Daniel Armbruster" ...@@ -53,16 +55,27 @@ __copyright__ = "Copyright (c) 2012 by Daniel Armbruster"
# global variables # global variables
chunkSize = 131072 # 128kB chunkSize = 131072 # 128kB
debugMode = False
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
class CsFileError(Exception): class CsFileError(Exception):
"""
def __init__(self, msg, line): Exception class of csfile module.
"""
def __init__(self, line, msg):
self.msg = msg self.msg = msg
self.line = line self.line = line
def display(self): 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") sys.stderr.write("triggered in line: " + str(self.line) + "\n")
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
...@@ -83,8 +96,8 @@ class CsFile: ...@@ -83,8 +96,8 @@ class CsFile:
try: try:
csfile = open(path, 'w') csfile = open(path, 'w')
except IOError as err: except IOError as err:
raise CsFileError("[Errno "+str(err.errno)+"] "+err.strerror+": " \ raise CsFileError(97, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename, 87) +err.filename)
else: else:
csfile.close() csfile.close()
...@@ -94,16 +107,20 @@ class CsFile: ...@@ -94,16 +107,20 @@ class CsFile:
""" """
path = self.__filepath+os.sep+self.__filename path = self.__filepath+os.sep+self.__filename
try: try:
if debugMode:
CsReport(109, "Start reading checksumfile: "+path).display()
csfile = open(path) csfile = open(path)
self.__cslines = [CsLine(line.split()) for line in csfile \ self.__cslines = [CsLine(line.split()) for line in csfile \
if len(line.rstrip()) and line[0] != '#'] if len(line.rstrip()) and line[0] != '#']
except IOError as err: except IOError as err:
# Maybe better to create the file here and keep on going. # Maybe better to create the file here and keep on going.
# Will be managed later during further dev. # Will be managed later during further dev.
raise CsFileError("[Errno "+str(err.errno)+"] "+err.strerror+": " \ raise CsFileError(116, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename, 104) +err.filename)
else: else:
csfile.close() csfile.close()
if debugMode:
CsReport(121, "Finished reading checksumfile: "+path).display()
def write(self): def write(self):
""" """
...@@ -111,37 +128,48 @@ class CsFile: ...@@ -111,37 +128,48 @@ class CsFile:
""" """
path = self.__filepath+os.sep+self.__filename path = self.__filepath+os.sep+self.__filename
try: try:
if debugMode:
CsReport(130, "Start writing checksumfile: "+path).display()
csfile = open(path, 'w') csfile = open(path, 'w')
for csline in self.__cslines: for csline in self.__cslines:
if debugMode:
CsReport(134, "Writing line: "+ str(csline)).display()
if isinstance(csline, CsLine): if isinstance(csline, CsLine):
csfile.write(str(csline) + '\n') csfile.write(str(csline) + '\n')
else: else:
raise CsFileError("Argument must be of type CsLine.", 116) raise CsFileError(136, "Argument must be of type CsLine.")
except IOError as err: except IOError as err:
raise CsFileError("[Errno "+str(err.errno)+"] "+err.strerror+": " \ raise CsFileError(138, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename, 119) +err.filename)
else: else:
csfile.close() csfile.close()
if debugMode:
CsReport(145, "Finished writing checksumfile: "+path)
def append(self, cslines): def append(self, cslines):
""" """
Append checksum lines to the checksum file. Append checksum lines to the checksum file.
""" """
path = self.__filepath+os.sep+self.__filename path = self.__filepath+os.sep+self.__filename
# Maybe not necessary to raise an exception here if 0 == len(cslines) and debugMode:
if 0 == len(cslines): CsReport(153, "Invalid argument. Empty list.").display()
raise CsFileError("Invalid argument. Empty list.", 130)
try: try:
if debugMode:
CsReport(156, "Start appending to checksumfile: "+path).display()
csfile = open(path, 'a') csfile = open(path, 'a')
for csline in cslines: for csline in cslines:
if debugMode:
CsReport(160, "Writing line: "+str(csline)).display()
if isinstance(csline, CsLine): if isinstance(csline, CsLine):
csfile.write(str(csline) + '\n') csfile.write(str(csline) + '\n')
else: else:
raise CsFileError("Argument must be of type CsLine.", 137) raise CsFileError(164, "Argument must be of type CsLine.")
except IOError as err: except IOError as err:
raise CsFileError("[Errno "+str(err.errno)+"] "+err.strerror+": " \ raise CsFileError(166, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename, 140) +err.filename)
else: else:
if debugMode:
CsReport(170, "Finished appending to checksumfile: "+path).display()
csfile.close() csfile.close()
def update(self, regexes=[]): def update(self, regexes=[]):
...@@ -150,16 +178,19 @@ class CsFile: ...@@ -150,16 +178,19 @@ class CsFile:
checksum file in current directory. checksum file in current directory.
""" """
if not isinstance(regexes, list): 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 # fetch cslines in current csfile
self.read() 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] \ registeredFiles = [csline.path.split(os.sep)[-1] \
for csline in self.__cslines] for csline in self.__cslines]
# fetch files excluding regex matches # fetch files excluding regex matches
newFiles = list(set([file for file in os.listdir(self.__filepath) \ newFiles = list(set([file for file in os.listdir(self.__filepath) \
if os.path.isfile(file) for regex in regexes \ 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 # exclude registered files
newFiles = list(set([file for file in newFiles \ newFiles = list(set([file for file in newFiles \
for registeredFile in registeredFiles if registeredFile != file])) for registeredFile in registeredFiles if registeredFile != file]))
...@@ -167,6 +198,8 @@ class CsFile: ...@@ -167,6 +198,8 @@ class CsFile:
csLines = [CsLine(self.__filepath+os.sep+file, self.__hashfunc) \ csLines = [CsLine(self.__filepath+os.sep+file, self.__hashfunc) \
for file in newFiles] for file in newFiles]
for line in csLines: for line in csLines:
if debugMode:
CsReport(200, "Calculating checksum of file: "+file).display()
line.generate(chunkSize) line.generate(chunkSize)
self.append(csLines) self.append(csLines)
...@@ -177,14 +210,21 @@ class CsFile: ...@@ -177,14 +210,21 @@ class CsFile:
2, calculate checksum of file which is located in srcDir and check results 2, calculate checksum of file which is located in srcDir and check results
3. write the result to the checksum file 3. write the result to the checksum file
""" """
if debugMode:
CsReport(212, "Start checking checksums.").display()
self.read() self.read()
if 0 == len(self.__cslines): if 0 == len(self.__cslines) and debugMode:
raise CsFileError("CSFILE does not contain any lines.", 179) CsReport(215, "CSFILE does not contain any lines.").display()
for csline in self.__cslines: for csline in self.__cslines:
if not len(srcDir): if not len(srcDir):
if debugMode:
CsReport(219, "Performing check of file with file itself.").display()
csline.check() csline.check()
else: else:
filename = csline.path.split(os.sep)[-1] 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) csline.check(srcDir+os.sep+filename)
self.write() self.write()
...@@ -239,8 +279,8 @@ class CsLine: ...@@ -239,8 +279,8 @@ class CsLine:
hasfunc.update(chunk) hasfunc.update(chunk)
self.checksum = hashfunc.hexdigest() self.checksum = hashfunc.hexdigest()
except IOError as err: except IOError as err:
raise CsFileError("[Errno "+str(err.errno)+"] "+err.strerror+": " \ raise CsFileError(280, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename, 240) +err.filename)
else: else:
file.close() file.close()
...@@ -274,13 +314,13 @@ class CsLine: ...@@ -274,13 +314,13 @@ class CsLine:
checksum = hashfunc.hexdigest() checksum = hashfunc.hexdigest()
except IOError as err: except IOError as err:
# Maybe better to avoid Exception here and just put the status to notice # Maybe better to avoid Exception here and just put the status to notice
raise CsFileError("[Errno "+str(err.errno)+"] "+err.strerror+": " \ raise CsFileError(275, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename, 275) +err.filename)
else: else:
file.close() file.close()
# checks # checks
if not len(self.checksum): 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: if checksum == self.checksum:
self.statusLastCheck = 'ok' self.statusLastCheck = 'ok'
else: else:
...@@ -298,6 +338,7 @@ class CsLine: ...@@ -298,6 +338,7 @@ class CsLine:
# Tests # Tests
if __name__ == '__main__': if __name__ == '__main__':
try: try:
debugMode = True
file = CsFile('/home/daniel/') file = CsFile('/home/daniel/')
file.read() file.read()
except CsFileError as err: except CsFileError as err:
......
Supports Markdown
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