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

Continued development

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: 4325
SVN UUID:     67feda4a-a26e-11df-9d6e-31afc202ad0c
parent 2414eeb9
## @file csfile.py
# @brief Provide a module to read, write and treat with a csback chechsumfile.
# @brief Provide a module to read, write and treat with a csback
# checksumfiles.
#
# -----------------------------------------------------------------------------
#
......@@ -7,8 +8,8 @@
# @author Daniel Armbruster
# \date 15/09/2011
#
# Purpose: Provide a module to read, write and treat with a csback checksumfile.
# file.
# Purpose: Provide a module to read, write and treat with a csback
# checksumfiles.
#
# ----
# This file is part of csback.
......@@ -38,12 +39,18 @@ import os
import re
import sys
import hashlib
from datetime import datetime
__version__ = "V0.1"
__subversion__ = "Id: "
__subversion__ = "$Id$"
__license__ = "GPLv2"
__author__ = "Daniel Armbruster"
__copyright__ = "Copyright (c) 2011 by Daniel Armbruster"
__copyright__ = "Copyright (c) 2012 by Daniel Armbruster"
# -----------------------------------------------------------------------------
# global variables
chunkSize = 131072 # 128kB
# -----------------------------------------------------------------------------
class CsFileError(Exception):
......@@ -57,39 +64,78 @@ class CsFileError(Exception):
# -----------------------------------------------------------------------------
class CsFile:
def __init__(self, filename, hashfunc=hashlib.sha256):
self.__filename = filename
def __init__(self, filepath, hashfunc='sha256'):
self.__filepath = filepath
self.__filename = ".cs"
self.__cslines = []
self.__hashfunc = hashfunc
createFile()
def createFile(self):
path = self.__filepath+os.sep+self.__filename
if not os.path.isfile(path):
try:
csfile = open(path, 'w')
finally:
csfile.close()
def read(self):
if not os.path.isfile(self.__filename):
raise CsFileError("CSFILE is not a regular file.")
if 0 == os.stat(self.__filename).st_size:
raise CsFileError("CSFILE is an empty file.")
csfile = open(self.__filename)
path = self.__filepath+os.sep+self.__filename
if not os.path.isfile(path):
raise CsFileError("CSFILE does not exist.")
try:
csfile = open(path)
self.__cslines = [CsLine(line.split()) for line in csfile \
if len(line.rstrip()) and line[0] != '#']
finally:
csfile.close()
def write(self):
if 0 == len(self.__cslines):
raise CsFileError("CSFILE does not contain any lines.")
file = open(self.__filename, 'w')
for csline in self.__cslines:
file.write(str(csline) + '\n')
file.close()
def update(self, regex):
pass
def append(self, cslines):
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.")
try:
csfile = open(path, 'a')
for csline in cslines:
if isinstance(csline, CsLine):
csfile.write(str(csline) + '\n')
else:
raise CsFileError("Argument must be of type CsLine.")
except IOError as err:
raise CsFileError(err.msg)
finally:
csfile.close()
def checkLines(self):
def update(self, regexes=[]):
if not isinstance(regexes, list)
raise CsFileError("Pass regular expressions in a list of strings")
# fetch cslines in current csfile
read()
# IMPORTANT NOTE: Check again if it is more convenient to whole path or
sep = os.sep
registeredFiles = [csline.path.split(sep)[len(csline.path.split(sep))] \
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)]))
# exclude registered files
newFiles = list(set([file for file in newFiles \
for registeredFile in registeredFiles if registeredFile != file]))
# generate cslines of newFiles
csLines = [CsLine(self.__filepath+os.sep+file, self.__hashfunc) \
for file in newFiles]
for csline in csLines
csline.generate(chunkSize)
append(csLines)
def checkLines(self, srcDir):
if 0 == len(self.__cslines):
raise CsFileError("CSFILE does not contain any lines.")
for line in self.__cslines:
line.check()
# TODO: get here filename of file in checksum
line.check(srcDir)
def displayLines(self):
if 0 == len(self.__cslines):
......@@ -111,9 +157,58 @@ class CsFile:
# -----------------------------------------------------------------------------
class CsLine:
def __init__(self, file, hashfunc = hashlib.sha256):
def __init__(self, path, hashfunc='sha256'):
self.checksum = ''
self.path = path
self.hashfunc = hashfunc
self.creationDateFile = ''
self.creationLocationChecksum = ''
self.creationDateChecksum = ''
self.dateLastCheck = ''
self.statusLastCheck = ''
def __init__(self, argList):
if not isinstance(argList, list):
raise CsFileError("Invalid argument.")
self.checksum = argList[0]
self.path = argList[1]
self.hashfunc = argList[2]
self.creationDateFile = argList[3]
self.creationLocationChecksum = argList[4]
self.creationDateChecksum = argList[5]
self.dateLastCheck = argList[6]
self.statusLastCheck = argList[7]
def generate(self, chunkSize):
# generate checksum
try:
hashfunc = hashlib.new(self.hashfunc)
blockSize = chunkSize * hashfunc.block_size
with open(self.path, "rb") as file:
for chunk in iter(lambda: f.read(blockSize), ''):
hasfunc.update(chunk)
self.checksum = hashfunc.hexdigest()
except IOError as err:
raise CsFileError(err.msg)
# set remaining data
self.creationDateFile = \
datetime.fromtimestamp(os.path.getctime(self.path)).strftime( \
"%Y/%m/%d-%H:%M:%S")
self.creationLocationChecksum = os.uname()[1]
self.creationDateChecksum = datetime.now().strftime("%Y/%m/%d-%H:%M:%S")
self.dateLastCheck = self.creationDateChecksum
self.statusLastCheck = 'ok'
def check(self, src=''):
pass
def __str__(self):
return '{0} {1} {2} {3} {4} {5} {6} {7}'.format(self.checksum, self.path, \
self.hashfunc, self.creationDateFile, self.creationLocationChecksum, \
self.creationDateChecksum, self.dateLastCheck, self.statusLastCheck)
# -----------------------------------------------------------------------------
if __name__ == '__main__':
pass
......
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