3 import os, sys, shutil, signal, tempfile, pipes
4 conffile = '/etc/dbuscrontab'
5 pidfile = '/var/run/dbuscron.pid'
7 from dbuscron.parser import CrontabParser
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)
33 for rule, command in parser:
36 if __name__ == '__main__':
44 # 1. create temporary config file copy
45 temp_file = create_temp_file(conffile)
46 mod_time = os.path.getmtime(temp_file)
48 # 2. run system editor on this file
49 run_system_editor(temp_file)
51 # 3. check if this file is changed
52 if os.path.getmtime(temp_file) <= mod_time:
53 print 'File was not changed.'
56 # TODO: 4. check this file's syntax
57 check_syntax(temp_file)
59 # 5. replace system wide config file with new one
60 shutil.move(temp_file, conffile)
62 # 6. send sighup to dbuscron daemon
63 pid = get_dbuscron_pid()
64 os.kill(pid, signal.SIGHUP)
65 print "Everything's OK, SIGHUP to dbuscron is sent."
68 f = open(conffile, 'r')
74 check_syntax(conffile)
79 %(myname)s { -e | -l }
81 -e edit %(conffile)s file
82 -l list contents of %(conffile)s file
83 -k check %(conffile)s's syntax
85 """ % dict(myname=os.path.basename(sys.argv[0]), conffile=conffile)