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

Avoid bug if empty file is given. Throw error message.

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: 4154
SVN UUID:     67feda4a-a26e-11df-9d6e-31afc202ad0c
parent d69ded4f
......@@ -31,8 +31,14 @@
# Copyright (c) 2011 by Daniel Armbruster
#
# REVISIONS and CHANGES
# 11/09/2011 V0.1 Daniel Armbruster
# 14/09/2011 V0.8 Now multiple exclude flags in CONFIGFILE are supported
# 11/09/2011 V0.1 Daniel Armbruster
# 14/09/2011 V0.8 Now multiple exclude flags in CONFIGFILE are supported
# 15/09/2011 V0.8.1 Added several improvements
#
# TODO
# 1) Implement a private function for Processor class to check crontab timestamp
# syntax.
# 2. Implement dynamic time for csbackmail.
#
# =============================================================================
......@@ -43,7 +49,7 @@ import pwd
import string
from datetime import datetime
version="cscron2back V0.8"
version="cscron2back V0.8.1"
license="GPL"
......@@ -51,13 +57,10 @@ class Error(Exception):
def __init__(self, msg):
self.msg = msg
def display(self):
sys.stderr.write("csback2cron (ERROR): " + self.msg)
sys.stderr.write("csback2cron (ERROR): " + self.msg + "\n")
# -----------------------------------------------------------------------------
class Usage(Error):
# check if constructor is necessary
def __init__(self, msg):
self.msg = msg
def display(self):
usage_text = version + "\nLicense:" + license + "\n" + """
Author: Daniel Armbruster
......@@ -98,15 +101,19 @@ class Processor():
def read(self):
try:
if not os.path.isfile(self.__configfile):
raise Error("Given CONFIGFILE is not a regular file.")
if os.stat(self.__configfile).st_size == 0:
raise Error("Given CONFIGFILE is an empty file.")
if self.__verbose:
print("csback2cron: Reading CONFIGFILE ... ")
configfile = open(self.__configfile)
try:
lines = [line.rstrip() for line in configfile \
if tmp_line != '' and tmp_line[0] != '#']
if line != '' and line[0] != '#']
finally:
configfile.close()
if len(lines) == 0:
if len(lines) < 2:
raise Error("CONFIGFILE not readable.")
# get mail= line
tmp = str(list(filter((lambda x: x[0:5] == "mail="), lines))[0])
......@@ -117,6 +124,10 @@ class Processor():
self.__sendmail = True
self.__addresses = tmp[5:]
# collect setting lines
if not os.path.isfile(self.__configfile):
raise Error("Given CONFIGFILE is not a regular file.")
if os.stat(self.__configfile).st_size == 0:
raise Error("Given CONFIGFILE is a empty file.")
if len(lines) == 0:
raise Error("No setting lines in CONFIGFILE given.")
for line in lines:
......@@ -129,6 +140,7 @@ class Processor():
self.__sLines.append(d)
except Error as err:
err.display()
sys.exit()
return 2
def write(self):
......@@ -214,7 +226,6 @@ class Processor():
__flags = ['-c', '-t', '-b', '-e']
__keylist = ['cronexpr', 'source', 'flags', 'target']
# REMEMBER: Not applying all tests yet!
#def __check_cronexpr(self, expr):
# -----------------------------------------------------------------------------
......@@ -243,13 +254,11 @@ def main(argv=None):
help()
sys.exit()
elif opt in ("-i", "--input"):
input = a
inputfile = a
else:
raise Usage("Unhandled option chosen.")
if len(args) == 0:
raise Usage("CRONTABFILENAME not given.")
if not os.path.isfile(inputfile):
raise Error("Given CONFIGFILE is not a regular file.")
if not overwrite and os.path.isfile(outputfile):
raise Usage(outputfile +\
" already exists. Enable [-o] to overwrite file.")
......@@ -259,9 +268,6 @@ def main(argv=None):
except Usage as err:
err.display()
return 2
except Error as err:
err.display()
return 2
# -----------------------------------------------------------------------------
if __name__ == "__main__":
......
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