Commit 17fd259c authored by Daniel Armbruster's avatar Daniel Armbruster Committed by thomas.forbriger
Browse files

checksumfile generation process improved

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: 4347
SVN UUID:     67feda4a-a26e-11df-9d6e-31afc202ad0c
parent 0be4a203
......@@ -100,10 +100,10 @@ def help():
-l|--logging Switch on logging to files. Logfiles will be located in
~/.csback/log/.
SOURCEPATH Optional sourcepath for comparison with files backed up in
PATH. PATH and its subdirectories (if option '-R' is not
set) must contain the csback checksumfile(s).
PATH. PATH must contain the csback checksumfile.
Note that the directory structure (if option '-R' is not
set) bellow SOURCEPATH must be equal to those in PATH.
set) bellow SOURCEPATH should be equal to those in PATH
because otherwise the files can not be found.
If SOURCEPATH is not passed a check of files located in
PATH with its checksumfiles will be performed.
PATH Path to perform check for.
......@@ -156,7 +156,7 @@ def main(argv=None):
elif opt in ("-l", "--logging"):
logger.configure()
else:
raise Usage(159,msg="Unhandled option chosen.")
raise Usage(159,"Unhandled option chosen.")
if verbose or debugMode:
logger.addHandler(console)
......@@ -191,12 +191,11 @@ def main(argv=None):
err.display()
return 2
except Error as err:
logger.getLogger().error("csbackchk.Error occured while checking files.")
logger.getLogger().error("message: %s [line %s]", err.msg, err.line)
err.display()
return 2
except csfile.CsFileError as err:
logger.getLogger().error( \
"csfile.CsFileError occured while checking files.")
logger.getLogger().error("message: %s [line %s]", err.msg, err.line)
err.display()
return 2
else:
......
......@@ -155,31 +155,25 @@ def main(argv=None):
# fetch arguments
if 1 == len(args):
inputpath = str(args[0]).rstrip(os.sep)+os.sep
srcpath = str(args[0]).rstrip(os.sep)+os.sep
else:
raise Usage("Invalid argument.")
if not targetSet:
targetDirectory = inputpath
targetDirectory = srcpath
if not os.path.isdir(targetDirectory):
raise Usage("Target directory not a valid directory.")
# major part
directories = [inputpath]
if not notRecursive:
logger.getLogger().info("Collecting subdirectories.")
directories.extend(csfile.getSubDirectories(inputpath, regexes, \
followLinks))
for dir in directories:
logger.getLogger().info( \
"Update files in directory: %s to checksumfile: %s%s", dir, \
targetDirectory, csfile.CsFile.filename)
csfile.CsFile(targetDirectory, srcpath=dir).update(regexes)
logger.getLogger().info("Start updating checksumfile in '%s'", srcpath)
csfile.CsFile(targetDirectory, srcpath=srcpath, recursive=not notRecursive, \
followLinks=followLinks).update(regexes)
except Usage as err:
err.display()
return 2
except csfile.CsFileError as err:
logger.getLogger().error("%s", err.msg)
err.display()
return 2
else:
......
......@@ -115,11 +115,14 @@ class CsFile:
A checksumfiles usually contains checksumlines (type CsLine) of files.
Generally this includes the files of the subdirectories in srcpath, too.
"""
def __init__(self, filedir, srcpath, hashfunc='sha256'):
def __init__(self, filedir, srcpath, hashfunc='sha256', recursive=True, \
followLinks=False):
self.filedir = filedir
self.__cslines = []
self.__hashfunc = hashfunc
self.srcpath = srcpath
self.recursive = recursive
self.followLinks = False
self.logger = logging.LoggerAdapter(logging.getLogger( \
csfileLoggerName+".CsFile"), csfileLogInfo)
......@@ -128,7 +131,7 @@ class CsFile:
Read a checksumfile.
"""
if not os.access(self.filedir, os.F_OK):
raise CsFileError(122, "Invalid directory path.")
raise CsFileError(134, "Invalid directory path.")
path = os.path.join(self.filedir, CsFile.filename)
# no checksumfile available -> create new file
if os.access(self.filedir, os.F_OK) and not os.path.isfile(path):
......@@ -136,7 +139,7 @@ class CsFile:
try:
csfile = open(path, 'w')
except IOError as err:
raise CsFileError(130, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
raise CsFileError(142, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename)
else:
csfile.close()
......@@ -148,7 +151,7 @@ class CsFile:
self.__cslines = [CsLine(line.split()) for line in csfile \
if len(line.rstrip()) and line[0] != '#']
except IOError as err:
raise CsFileError(142, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
raise CsFileError(154, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename)
else:
csfile.close()
......@@ -167,9 +170,9 @@ class CsFile:
if isinstance(csline, CsLine):
csfile.write(str(csline) + '\n')
else:
raise CsFileError(161, "Argument must be of type CsLine.")
raise CsFileError(173, "Argument must be of type CsLine.")
except IOError as err:
raise CsFileError(163, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
raise CsFileError(175, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename)
else:
csfile.close()
......@@ -182,21 +185,22 @@ class CsFile:
path = os.path.join(self.filedir, CsFile.filename)
if 0 == len(cslines):
self.logger.debug("Empty list passed. Nothing to append.")
try:
self.logger.debug("Start appending to checksumfile '%s'", path)
csfile = open(path, 'a')
for csline in cslines:
self.logger.debug("Writing line '%s'", str(csline))
if isinstance(csline, CsLine):
csfile.write(str(csline) + '\n')
else:
raise CsFileError(184, "Argument must be of type CsLine.")
except IOError as err:
raise CsFileError(186, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename)
else:
self.logger.debug("Finished appending to checksumfile '%s'", path)
csfile.close()
try:
self.logger.debug("Start appending to checksumfile '%s'", path)
csfile = open(path, 'a')
for csline in cslines:
self.logger.debug("Writing line '%s'", str(csline))
if isinstance(csline, CsLine):
csfile.write(str(csline) + '\n')
else:
raise CsFileError(197, "Argument must be of type CsLine.")
except IOError as err:
raise CsFileError(199, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename)
else:
self.logger.debug("Finished appending to checksumfile '%s'", path)
csfile.close()
def update(self, regexes=[]):
"""
......@@ -204,16 +208,25 @@ class CsFile:
checksum file in current directory.
"""
if not isinstance(regexes, list):
raise CsFileError(198, "Pass regular expressions in a list.")
raise CsFileError(211, "Pass regular expressions in a list.")
self.logger.debug("Updating checksumfile ...")
# fetch cslines in current csfile
self.read()
self.logger.debug("Fetching files not registered yet.")
registeredFiles = set(csline.path for csline in self.__cslines)
# fetch files (pathes)
newFiles = os.listdir(self.srcpath)
newFiles = set(os.path.join(self.srcpath, file) for file in newFiles \
if os.path.isfile(os.path.join(self.srcpath, file)))
newFiles = set()
if self.recursive:
self.logger.debug("Fetching files recursively.")
for root, dirs, files in os.walk(self.srcpath, \
followlinks=self.followLinks):
for file in files:
newFiles.add(os.path.join(root, file))
else:
self.logger.debug("Fetching files not recursively.")
newFiles = set(os.path.join(self.srcpath, file) \
for file in os.listdir(self.srcpath) \
if os.path.isfile(os.path.join(self.srcpath, file)))
# exclude files matching regexes
regexes.append(os.path.join(self.srcpath,CsFile.filename))
for regex in regexes:
......@@ -309,7 +322,7 @@ class CsLine:
self.dateLastCheck = ''
self.statusLastCheck = ''
else:
CsFileError(290, "Invalid argument(s).")
CsFileError(325, "Invalid argument(s).")
def generate(self, chunkSize):
"""
......@@ -328,7 +341,7 @@ class CsLine:
data = file.read(blockSize)
self.checksum = hashfunc.hexdigest()
except IOError as err:
raise CsFileError(309, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
raise CsFileError(344, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename)
else:
file.close()
......
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