3 import os, sys, shutil, signal, tempfile, pipes
4 conffile = '/etc/dbuscrontab'
5 pidfile = '/var/run/dbuscron.pid'
7 from dbuscron.parser import CrontabParser, CrontabParserError
9 def create_temp_file(orig_file):
11 temp_file = tempfile.mktemp(prefix=os.path.basename(orig_file))
12 shutil.copy(orig_file, temp_file)
15 raise SystemError('Unable to make copy of dbuscrontab file.')
17 def run_system_editor(filename):
18 editor = pipes.quote(os.environ.get('EDITOR', '/usr/bin/vim'))
19 if os.system(editor + ' ' + pipes.quote(filename)) != 0:
20 raise SystemError('Editor returned non-zero status value.')
22 def get_dbuscron_pid():
24 f = os.popen('initctl status dbuscron', 'r')
27 return int(status.strip().split(' ').pop())
29 raise SystemError('Unable to get PID of dbuscron job.')
31 def check_syntax(filename):
32 parser = CrontabParser(filename)
34 for rule, command in parser:
36 except CrontabParserError, e:
38 raise SystemError("File %s has syntax errors." % (filename))
40 if __name__ == '__main__':
50 # 1. create temporary config file copy
51 temp_file = create_temp_file(conffile)
52 mod_time = os.path.getmtime(temp_file)
55 # 2. run system editor on this file
56 run_system_editor(temp_file)
58 # 3. check if this file is changed
59 if os.path.getmtime(temp_file) <= mod_time:
60 print 'File was not changed.'
63 # 4. check this file's syntax
64 check_syntax(temp_file)
66 # 5. replace system wide config file with new one
67 shutil.move(temp_file, conffile)
75 # 6. send sighup to dbuscron daemon
76 pid = get_dbuscron_pid()
77 os.kill(pid, signal.SIGHUP)
79 print "Everything's OK, SIGHUP to dbuscron is sent."
82 f = open(conffile, 'r')
88 check_syntax(conffile)
89 print "File %s has no syntax errors." % (conffile)
94 %(myname)s { -e | -l }
96 -e edit %(conffile)s file
97 -l list contents of %(conffile)s file
98 -k check %(conffile)s's syntax
100 """ % dict(myname=os.path.basename(sys.argv[0]), conffile=conffile)
102 except SystemError, e: