dbuscrontab utility: implemented -e/-l/-k actions, syntax checking
authorKonstantin Stepanov <kstep@p-nut.info>
Sun, 12 Dec 2010 01:05:36 +0000 (03:05 +0200)
committerKonstantin Stepanov <kstep@p-nut.info>
Sun, 12 Dec 2010 01:08:24 +0000 (03:08 +0200)
dbuscrontab.py

index b816cc8..2331220 100755 (executable)
@@ -1,9 +1,11 @@
 #!/usr/bin/python
 
 import os, sys, shutil, signal, tempfile, pipes
-crontab = '/etc/dbuscrontab'
+conffile = '/etc/dbuscrontab'
 pidfile = '/var/run/dbuscron.pid'
 
+from dbuscron.parser import CrontabParser
+
 def create_temp_file(orig_file):
     try:
         temp_file = tempfile.mktemp(prefix=os.path.basename(orig_file))
@@ -26,26 +28,59 @@ def get_dbuscron_pid():
     except:
         raise SystemError('Unable to get PID of dbuscron job.')
 
+def check_syntax(filename):
+    parser = CrontabParser(filename)
+    for rule, command in parser:
+        pass
+
 if __name__ == '__main__':
 
-# 1. create temporary config file copy
-    temp_file = create_temp_file(crontab)
-    mod_time = os.path.getmtime(temp_file)
+    try:
+        action = sys.argv[1]
+    except IndexError:
+        action = None
+
+    if action == '-e':
+        # 1. create temporary config file copy
+        temp_file = create_temp_file(conffile)
+        mod_time = os.path.getmtime(temp_file)
+
+        # 2. run system editor on this file
+        run_system_editor(temp_file)
 
-# 2. run system editor on this file
-    run_system_editor(temp_file)
+        # 3. check if this file is changed
+        if os.path.getmtime(temp_file) <= mod_time:
+            print 'File was not changed.'
+            sys.exit(2)
+
+        # TODO: 4. check this file's syntax
+        check_syntax(temp_file)
+
+        # 5. replace system wide config file with new one
+        shutil.move(temp_file, conffile)
+
+        # 6. send sighup to dbuscron daemon
+        pid = get_dbuscron_pid()
+        os.kill(pid, signal.SIGHUP)
+        print "Everything's OK, SIGHUP to dbuscron is sent."
+
+    elif action == '-l':
+        f = open(conffile, 'r')
+        for l in f:
+            print l.strip()
+        f.close()
 
-# 3. check if this file is changed
-    if os.path.getmtime(temp_file) <= mod_time:
-        print 'File was not changed.'
-        sys.exit(2)
+    elif action == '-k':
+        check_syntax(conffile)
 
-# TODO: 4. check this file's syntax
+    else:
+        print """
+Usage:
+    %(myname)s { -e | -l }
 
-# 5. replace system wide config file with new one
-    shutil.move(temp_file, crontab)
+    -e      edit %(conffile)s file
+    -l      list contents of %(conffile)s file
+    -k      check %(conffile)s's syntax
 
-# 6. send sighup to dbuscron daemon
-    pid = get_dbuscron_pid()
-    os.kill(pid, signal.SIGHUP)
+""" % dict(myname=os.path.basename(sys.argv[0]), conffile=conffile)