moved shell part into modules
[dbuscron] / dbuscron / shell / edit.py
1
2 import os, sys, shutil, signal, tempfile, pipes
3 conffile = '/etc/dbuscrontab'
4 pidfile = '/var/run/dbuscron.pid'
5
6 from dbuscron.parser import CrontabParser, CrontabParserError
7
8 def create_temp_file(orig_file):
9     try:
10         temp_file = tempfile.mktemp(prefix=os.path.basename(orig_file))
11         shutil.copy(orig_file, temp_file)
12         return temp_file
13     except:
14         raise SystemError('Unable to make copy of dbuscrontab file.')
15
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.')
20
21 def get_dbuscron_pid():
22     try:
23         f = os.popen('initctl status dbuscron', 'r')
24         status = f.readline()
25         f.close()
26         return int(status.strip().split(' ').pop())
27     except:
28         raise SystemError('Unable to get PID of dbuscron job.')
29
30 def check_syntax(filename):
31     parser = CrontabParser(filename)
32     try:
33         for rule, command in parser:
34             pass
35     except CrontabParserError, e:
36         print e.message
37         raise SystemError("File %s has syntax errors." % (filename))
38
39 def run():
40
41     try:
42         action = sys.argv[1]
43     except IndexError:
44         action = None
45
46     try:
47         if action == '-e':
48
49             # 1. create temporary config file copy
50             temp_file = create_temp_file(conffile)
51             mod_time = os.path.getmtime(temp_file)
52
53             try:
54                 # 2. run system editor on this file
55                 run_system_editor(temp_file)
56
57                 # 3. check if this file is changed
58                 if os.path.getmtime(temp_file) <= mod_time:
59                     print 'File was not changed.'
60                     sys.exit(2)
61
62                 # 4. check this file's syntax
63                 check_syntax(temp_file)
64
65                 # 5. replace system wide config file with new one
66                 shutil.move(temp_file, conffile)
67
68             finally:
69                 try:
70                     os.unlink(temp_file)
71                 except OSError:
72                     pass
73
74             # 6. send sighup to dbuscron daemon
75             pid = get_dbuscron_pid()
76             os.kill(pid, signal.SIGHUP)
77
78             print "Everything's OK, SIGHUP to dbuscron is sent."
79
80         elif action == '-l':
81             f = open(conffile, 'r')
82             for l in f:
83                 print l.strip()
84             f.close()
85
86         elif action == '-k':
87             check_syntax(conffile)
88             print "File %s has no syntax errors." % (conffile)
89
90         else:
91             print """
92 Usage:
93     %(myname)s { -e | -l }
94
95     -e      edit %(conffile)s file
96     -l      list contents of %(conffile)s file
97     -k      check %(conffile)s's syntax
98
99 """ % dict(myname=os.path.basename(sys.argv[0]), conffile=conffile)
100
101     except SystemError, e:
102         print e.message
103         sys.exit(1)
104