2 import os, sys, shutil, signal, tempfile, pipes
3 conffile = '/etc/dbuscrontab'
4 pidfile = '/var/run/dbuscron.pid'
6 from dbuscron.parser import CrontabParser, CrontabParserError
8 def create_temp_file(orig_file):
10 temp_file = tempfile.mktemp(prefix=os.path.basename(orig_file))
11 shutil.copy(orig_file, temp_file)
14 raise SystemError('Unable to make copy of dbuscrontab file.')
16 def run_system_editor(filename):
17 editor = pipes.quote(os.environ.get('EDITOR', '/usr/bin/vim'))
18 if os.system(editor + ' ' + pipes.quote(filename)) != 0:
19 raise SystemError('Editor returned non-zero status value.')
21 def get_dbuscron_pid():
23 f = os.popen('initctl status dbuscron', 'r')
26 return int(status.strip().split(' ').pop())
28 raise SystemError('Unable to get PID of dbuscron job.')
30 def check_syntax(filename):
31 parser = CrontabParser(filename)
33 for rule, command in parser:
35 except CrontabParserError, e:
37 raise SystemError("File %s has syntax errors." % (filename))
49 # 1. create temporary config file copy
50 temp_file = create_temp_file(conffile)
51 mod_time = os.path.getmtime(temp_file)
54 # 2. run system editor on this file
55 run_system_editor(temp_file)
57 # 3. check if this file is changed
58 if os.path.getmtime(temp_file) <= mod_time:
59 print 'File was not changed.'
62 # 4. check this file's syntax
63 check_syntax(temp_file)
65 # 5. replace system wide config file with new one
66 shutil.move(temp_file, conffile)
74 # 6. send sighup to dbuscron daemon
75 pid = get_dbuscron_pid()
76 os.kill(pid, signal.SIGHUP)
78 print "Everything's OK, SIGHUP to dbuscron is sent."
81 f = open(conffile, 'r')
87 check_syntax(conffile)
88 print "File %s has no syntax errors." % (conffile)
93 %(myname)s { -e | -l }
95 -e edit %(conffile)s file
96 -l list contents of %(conffile)s file
97 -k check %(conffile)s's syntax
99 """ % dict(myname=os.path.basename(sys.argv[0]), conffile=conffile)
101 except SystemError, e: