From 1f30ac8dd48cb50fd34d1fe73b73860ff8b39901 Mon Sep 17 00:00:00 2001 From: Konstantin Stepanov Date: Fri, 17 Dec 2010 14:40:08 +0200 Subject: [PATCH] dbuscrontab: refactored error handling --- dbuscrontab.py | 100 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 54 insertions(+), 46 deletions(-) diff --git a/dbuscrontab.py b/dbuscrontab.py index 7b9bf17..4f61a3f 100755 --- a/dbuscrontab.py +++ b/dbuscrontab.py @@ -30,8 +30,12 @@ def get_dbuscron_pid(): def check_syntax(filename): parser = CrontabParser(filename) - for rule, command in parser: - pass + try: + for rule, command in parser: + pass + except CrontabParserError, e: + print e.message + raise SystemError("File %s has syntax errors." % (filename)) if __name__ == '__main__': @@ -40,52 +44,52 @@ if __name__ == '__main__': except IndexError: action = None - if action == '-e': - # 1. create temporary config file copy - temp_file = create_temp_file(conffile) - mod_time = os.path.getmtime(temp_file) - - # 2. run system editor on this file - run_system_editor(temp_file) - - # 3. check if this file is changed - if os.path.getmtime(temp_file) <= mod_time: - print 'File was not changed.' - sys.exit(2) - - # TODO: 4. check this file's syntax - try: - check_syntax(temp_file) - except CrontabParserError, e: - print e.message - print 'File has syntax errors, aborting.' - sys.exit(3) - - # 5. replace system wide config file with new one - shutil.move(temp_file, conffile) - - # 6. send sighup to dbuscron daemon - pid = get_dbuscron_pid() - os.kill(pid, signal.SIGHUP) - print "Everything's OK, SIGHUP to dbuscron is sent." - - elif action == '-l': - f = open(conffile, 'r') - for l in f: - print l.strip() - f.close() + try: + if action == '-e': + + # 1. create temporary config file copy + temp_file = create_temp_file(conffile) + mod_time = os.path.getmtime(temp_file) + + try: + # 2. run system editor on this file + run_system_editor(temp_file) + + # 3. check if this file is changed + if os.path.getmtime(temp_file) <= mod_time: + print 'File was not changed.' + sys.exit(2) + + # 4. check this file's syntax + check_syntax(temp_file) - elif action == '-k': - try: + # 5. replace system wide config file with new one + shutil.move(temp_file, conffile) + + finally: + try: + os.unlink(temp_file) + except OSError: + pass + + # 6. send sighup to dbuscron daemon + pid = get_dbuscron_pid() + os.kill(pid, signal.SIGHUP) + + print "Everything's OK, SIGHUP to dbuscron is sent." + + elif action == '-l': + f = open(conffile, 'r') + for l in f: + print l.strip() + f.close() + + elif action == '-k': check_syntax(conffile) - except CrontabParserError, e: - print e.message - print "File %s has syntax errors." % (conffile) - sys.exit(3) - print "File %s has no syntax errors." % (conffile) - - else: - print """ + print "File %s has no syntax errors." % (conffile) + + else: + print """ Usage: %(myname)s { -e | -l } @@ -95,3 +99,7 @@ Usage: """ % dict(myname=os.path.basename(sys.argv[0]), conffile=conffile) + except SystemError, e: + print e.message + sys.exit(1) + -- 1.7.9.5