2 import os, sys, shutil, signal, tempfile, pipes
3 pidfile = '/var/run/dbuscron.pid'
5 from dbuscron.parser import CrontabParser, CrontabParserError
7 def create_temp_file(orig_file):
9 temp_file = tempfile.mktemp(prefix=os.path.basename(orig_file))
10 shutil.copy(orig_file, temp_file)
13 raise SystemError('Unable to make copy of dbuscrontab file.')
15 def run_system_editor(filename):
16 editor = pipes.quote(os.environ.get('EDITOR', '/usr/bin/vim'))
17 if os.system(editor + ' ' + pipes.quote(filename)) != 0:
18 raise SystemError('Editor returned non-zero status value.')
20 def get_dbuscron_pid_from_upstart():
21 f = os.popen('initctl status dbuscron', 'r')
24 return int(status.strip().split(' ').pop())
26 def get_dbuscron_pid_from_pidfile():
27 f = open(pidfile, 'r')
32 def get_dbuscron_pid():
34 return get_dbuscron_pid_from_upstart()
37 return get_dbuscron_pid_from_pidfile()
39 raise SystemError('Unable to get PID of dbuscron job.')
41 def check_syntax(filename):
42 parser = CrontabParser(filename)
44 for rule, command in parser:
46 except CrontabParserError, e:
48 raise SystemError("File %s has syntax errors." % (filename))
58 conffile = sys.argv[2]
60 conffile = '/etc/dbuscrontab'
65 # 1. create temporary config file copy
66 temp_file = create_temp_file(conffile)
67 mod_time = os.path.getmtime(temp_file)
70 # 2. run system editor on this file
71 run_system_editor(temp_file)
73 # 3. check if this file is changed
74 if os.path.getmtime(temp_file) <= mod_time:
75 print 'File was not changed.'
78 # 4. check this file's syntax
79 check_syntax(temp_file)
81 # 5. replace system wide config file with new one
82 shutil.move(temp_file, conffile)
90 # 6. send sighup to dbuscron daemon
91 pid = get_dbuscron_pid()
92 os.kill(pid, signal.SIGHUP)
94 print "Everything's OK, SIGHUP to dbuscron is sent."
97 f = open(conffile, 'r')
103 check_syntax(conffile)
104 print "File %s has no syntax errors." % (conffile)
109 %(myname)s { -e | -l | -k } [config-file]
112 -l list contents of config file
113 -k check config file's syntax
115 Default for config-file is %(conffile)s
116 """ % dict(myname=os.path.basename(sys.argv[0]), conffile=conffile)
118 except SystemError, e: