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

implementation finished - not tested yetx

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: 4360
SVN UUID:     67feda4a-a26e-11df-9d6e-31afc202ad0c
parent bdf119a9
......@@ -45,7 +45,6 @@
import sys
import getopt
import os
import pwd
from datetime import datetime
if sys.version_info == (2,)
from ConfigParser import ConfigParser
......@@ -105,8 +104,11 @@ def help():
~/.csback/csbackrc as default path to the configuration
file.
-o/--overwrite Overwrite already existing crontab.
<CRONTABFILENAME> Outputfilename of the generated crontab.\n"""
print(help_text)
<CRONTABFILENAME> Outputfilename of the generated crontab.
-------------------------------------------------------------------------------
Notice that csback2cron does not check any logical values e.g. pathes and/or
cronexpressions.\n"""
sys.stdout.write(help_text)
# -----------------------------------------------------------------------------
class Converter():
......@@ -201,11 +203,14 @@ class BackupConverter(Converter):
'tolerant': self.sectionDict['tolerant']})
testConv.addCronExpr = False
testConv.convert()
self.line += ' '+str(testConv)
self.line += '; '+str(testConv)
# -----------------------------------------------------------------------------
class Processor():
"""
Processing class which is responsible for the processing of the csbackrc
configuration file conversion.
"""
def __init__(self, configfile, crontabfile, verbose=False):
self.configfile = configfile
self.crontabfile = crontabfile
......@@ -214,8 +219,12 @@ class Processor():
self.mail = {}
self.backups = []
self.tests = []
self.result = []
def read(self):
"""
Read the csback configuration file.
"""
if not os.path.isfile(self.__configfile):
raise Error("Given CONFIGFILE is not a regular file.")
if os.stat(self.__configfile).st_size == 0:
......@@ -323,95 +332,53 @@ class Processor():
"Key 'keys' in configfile section [tests] not defined.")
except NoSectionError:
self.tests = {}
if self.__verbose:
sys.stdout.write("csback2cron: Finished reading CONFIGFILE.")
def convert(self):
"""
Convert the csback configuration file to a crontab file.
"""
if self.__verbose:
sys.stdout.write("csback2cron: Conversion ... ")
# convert backup sections
if len(self.backups) and self.__verbose:
sys.stdout.write("csback2cron: Converting backup sections ...")
for backup in self.backups:
backupConv = BackupConverter(backup)
backupConv.convert()
self.result.append(str(backupConv)+"\n")
# convert test sections
if len(self.tests) and self.__verbose:
sys.stdout.write("csback2cron: Converting test sections ...")
for test in self.tests:
testConv = TestConverter(test)
testConv.convert()
self.result.append(str(testConv)+"\n")
# convert mail section
if len(self.mail) and self.__verbose:
sys.stdout.write("csback2cron: Converting mail section ...")
if len(self.mail):
mailConv = MailConverter(self.mail)
mailConv.convert()
self.result.append(str(mailConv)+"\n")
def write(self):
"""
Write a csback crontab file.
"""
# header lines
output = ['# This is <' + self.__crontabfile + '>\n',
output = ['# This is <' + self.crontabfile + '>\n',
'# Generated with csback2cron.\n',
'# '+datetime.now().strftime("%Y-%m-%d %H:%M:%S")+"\n"]
'# '+datetime.now().strftime("%Y-%m-%d %H:%M:%S")+'\n',
'# -------------------------------------------------------------\n']
output.extend(self.result)
if self.__verbose:
print("csback2cron: Conversion ... ")
output.extend(self.__convert())
if self.__verbose:
print("csback2cron: Writing " + self.__crontabfile + " ...")
sys.stdout.write("csback2cron: Writing " + self.__crontabfile + " ...")
crontabfile = open(self.__crontabfile, 'w')
crontabfile.writelines(output)
crontabfile.close()
def __convert(self):
res = []
for line_dict in self.__sLines:
# check flags
flags = dict.fromkeys(self.__flags, False)
if self.__keylist[2] in line_dict:
flags = self.__chkflags(line_dict[self.__keylist[2]])
s = line_dict[self.__keylist[0]] + " "
# handle exclude flags
if flags[self.__flags[3]]:
e_flags = flags[self.__flags[3]].split()
else:
e_flags = []
if self.__keylist[3] in line_dict:
# backup is desired - use rsync
s += "rsync"
for f in e_flags:
s += " --exclude '" + f + "'"
s += " " + line_dict[self.__keylist[1]] + " " + \
line_dict[self.__keylist[3]] + "; csbackgen"
for f in e_flags:
s += " " + self.__flags[3] + " '" + f + "'"
s += " " + line_dict[self.__keylist[3]]
# check of integrity is desired
if not flags[self.__flags[2]]:
s += "; csbackchk"
if flags[self.__flags[1]]:
s += " " + __flags[1]
for f in e_flags:
s += " " + self.__flags[3] + " '" + f + "'"
s += " " + line_dict[self.__keylist[3]]
if flags[self.__flags[0]]:
s += " " + line_dict[self.__keylist[1]]
s += "\n"
else:
# test only
s += 'csbackchk'
if flags[self.__flags[1]]:
s += " " + self.__flags[1]
for f in e_flags:
s += " " + self.__flags[3] + " '" + f + "'"
s += " " + line_dict[self.__keylist[1]] + "\n"
res.append(s)
# email report
if self.__sendmail:
# TODO: variable time
s = "0 12 * * * csbackmail " + self.__addresses + "\n"
res.append(s)
return res
def __chkflags(self, flags):
res = dict.fromkeys(self.__flags, False)
res[self.__flags[3]] = ''
l = flags.split()
# get only True/False flags
for f in self.__flags[:3]:
if f in l:
res[f] = True
# handle exclude flags
for e_flag in l:
if e_flag == self.__flags[3] and \
l[l.index(e_flag)+1] not in self.__flags:
res[self.__flags[3]] += l[l.index(self.__flags[3])+1] + " "
l[l.index(self.__flags[3])] = ''
res[self.__flags[3]] = res[self.__flags[3]].rstrip()
return res
DEFAULTS = {'logging': True, 'recursive': True, 'followlinks': False, \
'tolerant': False, 'test': True, 'exclude' : ''}
......@@ -429,7 +396,7 @@ def main(argv=None):
inputfile = DEFAULTPATH
verbose = False
overwrite = False
for opt, a in opts:
for opt, arg in opts:
if opt in ("-v", "--verbose"):
verbose = True
elif opt in ("-o", "--overwrite"):
......@@ -438,20 +405,21 @@ def main(argv=None):
help()
sys.exit()
elif opt in ("-i", "--input"):
inputfile = a
inputfile = arg
else:
raise Usage("Unhandled option chosen.")
if len(args) == 1:
outputfile = args[0]
else:
raise Usage("Invalid arguments")
raise Usage("Invalid arguments.")
# checks
if not overwrite and os.path.isfile(outputfile):
raise Usage(outputfile +\
" already exists. Enable [-o] to overwrite file.")
process = Processor(inputfile, outputfile, verbose)
process.read()
processor = Processor(inputfile, outputfile, verbose=verbose)
processor.read()
processor.convert()
process.write()
except Usage as err:
err.display()
......
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