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

comments on the code added

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: 4684
SVN UUID:     67feda4a-a26e-11df-9d6e-31afc202ad0c
parent 073d1695
......@@ -271,12 +271,14 @@ class Processor():
"""
Read the csback configuration file.
"""
# perform some checks
if not os.path.isfile(self.configfile):
raise Error("Given CONFIGFILE is not a regular file.")
if 0 == os.stat(self.configfile).st_size:
raise Error("Given CONFIGFILE is an empty file.")
if self.__verbose:
sys.stdout.write("csback2cron: Reading CONFIGFILE ... \n")
# read file
try:
self.config.read(self.configfile)
except ParsingError as err:
......@@ -304,7 +306,7 @@ class Processor():
raise Error("Argument error in [mail] section.")
else:
self.mail = {}
# fetch copies section
# fetch copies (keys) section
try:
copyKeys = self.config.get('copies', 'keys')
copyKeys = set(copyKeys.split(","))
......@@ -336,7 +338,7 @@ class Processor():
raise Error("{0}".format(err.message))
except NoSectionError:
self.copies = []
# fetch backups section
# fetch backups (keys) section
try:
backupKeys = self.config.get('backups', 'keys')
backupKeys = set(backupKeys.split(","))
......@@ -352,7 +354,8 @@ class Processor():
backup['targetdir'] = self.config.get(key, 'targetdir').strip()
backup['exclude'] = []
if self.config.has_option(key, 'exclude'):
backup['exclude'] = self.config.get(key, 'exclude',raw=1).split(", ")
backup['exclude'] =
self.config.get(key, 'exclude',raw=1).split(", ")
backup['daystart'] = False
if self.config.has_option(key, 'daystart'):
backup['daystart'] = self.config.getboolean(key, 'daystart')
......@@ -400,7 +403,7 @@ class Processor():
raise Error("{0}".format(err.message))
except NoSectionError:
self.backups = []
# fetch tests section
# fetch tests (keys) section
try:
testKeys = self.config.get('tests', 'keys')
testKeys = set(testKeys.split(","))
......@@ -493,6 +496,7 @@ class Processor():
if self.__verbose:
sys.stdout.write("csback2cron: " + self.crontabfile + " written.\n")
# global variable containing default values
DEFAULTS = {'logging': 'yes', 'recursive': 'yes', 'followlinks': 'no', \
'tolerant': 'no', 'test': 'yes', 'copy': 'no', 'hash': 'sha256'}
......@@ -527,10 +531,12 @@ def main(argv=None):
outputfile = args[0]
else:
raise Usage("Invalid arguments.")
# checks
# perform some checks
if not overwrite and os.path.isfile(outputfile):
raise Usage(outputfile +\
" already exists. Enable [-o] to overwrite file.")
# create a processor and read, write and convert the content of the
# configuration file using python's ConfigParser module
processor = Processor(inputfile, outputfile, verbose=verbose)
processor.read()
processor.convert()
......
......@@ -190,27 +190,33 @@ def main(argv=None):
# major part
logger.getLogger().debug("Collecting subdirectories ...")
# recursive
# checking includes subdirectories as well (recursive mode)
if not notRecursive:
# collect source directories
sourceDirs.extend( \
csfile.getSubDirectories(sourcepath, regexes, followlinks))
# special behaviour if the checksumfiles aren't located in the source
# directories
if sourceDirs[0] != inputDirs[0]:
inputDirs.extend( \
csfile.getSubDirectories(inputpath, regexes, followlinks))
if len(sourceDirs) != len(inputDirs):
raise Error(202, \
raise Error(205, \
"Directory structure of inputpath and sourcepath different.")
# create tuples of input directory path - source directory path
paths = list(zip(inputDirs, sourceDirs))
logger.getLogger().info("Start checking checksums ...")
# perform checks for all tuples generated above - using the csback pid
# lockfile module if desired
for path in paths:
logger.getLogger().info( \
"Check of files in '{0}' with checksumfile in '{1}'".format(path[1], \
path[0]))
if not csfile.hasCsFile(path[0]):
raise Error(213,"PATH does not contain a checksumfile.")
raise Error(219,"PATH does not contain a checksumfile.")
lock = pidlock.PidLocker(path[0])
checksumfile = csfile.CsFile(path[0], path[1], followlinks=followlinks)
if lock.lockValid():
......
......@@ -247,7 +247,7 @@ def main(argv=None):
if verbose or debugMode:
logger.addHandler(console)
# fetch arguments
# fetch remaining commandline arguments
if 1 == len(args):
srcpath = str(args[0]).rstrip(os.sep)+os.sep
else:
......@@ -258,24 +258,35 @@ def main(argv=None):
# major part
logger.getLogger().debug("Start collecting subdirectories ...")
dirs = [srcpath]
# if no target path for checksumfiles had been passed generate the and
# create the checksum files in the srcpath
if not targetSet:
targetDirectory = srcpath
targetdirs = dirs
# otherwise use the target directory path passed
else:
targetdirs = [targetDirectory]
if not os.path.isdir(targetDirectory):
raise Usage("Target directory not a valid directory.")
# recursive
# generate checksumfiles for subdirectories (recursive mode)
# so collect all available subdirectories here (excluding subdirectories)
if not notRecursive:
dirs.extend(csfile.getSubDirectories(srcpath, regexes, followlinks))
# if a target directory had been passed extend the list of
# target directories replacing the trunk of the source directory with the
# target directory. Remember that this is only valid for recursive mode
if targetSet:
targetdirs.extend([dir.replace(dirs[0],targetDirectory,1) \
for dir in dirs[1:]])
# create a list of tuples now so that each target directory has its
# corresponding source directory
pathes = list(zip(targetdirs, dirs))
logger.getLogger().debug("Directories arranged.")
logger.getLogger().info("Start updating checksumfile(s) ...")
# for all available pathes generate or rather update the checksumfile (after
# performing pid lockfile checkings)
for path in pathes:
logger.getLogger().info( \
"Updating checksumfile in '{0}' with files from '{1}' ...".format( \
......@@ -283,6 +294,9 @@ def main(argv=None):
lock = pidlock.PidLocker(path[0])
checksumfile = csfile.CsFile(path[0], path[1], hashfunc=hashfunc, \
timeDict=timeDict, followlinks=followlinks)
# lock directory currently working in (use pid locking module) if possible
# - else terminate raising an exception
if lock.lockValid():
raise pidlock.PidLockError(-1, \
"Directory '{0}' locked.".format(path[0]))
......
......@@ -192,8 +192,12 @@ def main(argv=None):
else:
raise Usage(193,"Invalid argument(s).")
# use deques so that only the last ten INFO and WARNING messages were
# printed to the mail
infoDeque = deque(maxlen=10)
warningDeque = deque(maxlen=10)
# instead of a deque use lists for ERROR and CRITICAL messages here to print
# the entire information to the mail
errorList = []
criticalList = []
# read logfiles
......@@ -212,9 +216,10 @@ def main(argv=None):
else:
logger.getLogger().info("Illegal line in logfile found.")
except IOError as err:
raise Error(215, "Error while reading logfiles.")
raise Error(219, "Error while reading logfiles.")
logger.getLogger().debug("Finished reading logfile(s).")
# if nagios mode had been enabled just print the status to stdout
if nagiosMode:
if len(criticalList):
sys.stdout.write("CSBACK CRITICAL\n")
......@@ -224,28 +229,29 @@ def main(argv=None):
sys.stdout.write("CSBACK WARNING\n")
else:
sys.stdout.write("CSBACK OK\n")
# generate the email using pythons smtplib
else:
logger.getLogger().debug("Checking email addresses.")
if 0 == len(sender):
raise Usage(230, "Email address of sender missing.")
raise Usage(236, "Email address of sender missing.")
if None == re.match( \
'^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$', sender):
raise Error(233, "Email address of sender not valid.")
raise Error(239, "Email address of sender not valid.")
if 0 == len(receivers):
raise Usage(235, "Email address of receiver missing.")
raise Usage(241, "Email address of receiver missing.")
for add in receivers:
if None == re.match( \
'^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$', add):
raise Error(239, "Email address of receiver not valid.")
raise Error(245, "Email address of receiver not valid.")
if 0 == len(host):
raise Usage(242, "Hostname missing.")
raise Usage(248, "Hostname missing.")
if 0 == len(username):
raise Usage(244, "Username missing.")
raise Usage(250, "Username missing.")
if 0 == len(password):
raise Usage(246, "Password missing.")
raise Usage(252, "Password missing.")
if -1 == port:
raise Usage(248, "Port missing.")
raise Usage(254, "Port missing.")
# prepare email
logger.getLogger().debug("Preparing email content ...")
msg = '============================\n'
......
......@@ -128,6 +128,9 @@ def main(argv=None):
if verbose or debugMode:
logger.addHandler(console)
if 0 == len(args):
raise Usage("Missing arguments.")
# fetch arguments
logger.getLogger().debug(
"Collecting command arguments from commandline ...")
......@@ -136,6 +139,10 @@ def main(argv=None):
commands.append(str(cmd).strip())
# major part
"""
Executes the program using os.system() and checks the exit status of the
executed program.
"""
logger.getLogger().debug("Start executing commands ...")
for cmd in commands:
exitstatus = os.system(cmd)
......
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