Browse Source

Actually saving the modified crontab

Lukas Angerer 5 years ago
parent
commit
a87fac8c8b
3 changed files with 18 additions and 7 deletions
  1. 3 0
      data/settings.json
  2. 3 1
      scripts/cron-api.py
  3. 12 6
      scripts/cron/Parser.py

+ 3 - 0
data/settings.json

@@ -1,4 +1,7 @@
 {
+  "CronApi": {
+    "BaseUri": "http://172.17.0.1:5000"
+  },
   "Alerts": {
     "Command": "/home/pi/mpc-alarm-cron.sh",
     "Groups": [

+ 3 - 1
scripts/cron-api.py

@@ -20,7 +20,9 @@ def set_section(name):
     parser = Parser()
     cronFile = parser.parse()
     cronFile.set_section(name, request.data.decode())
-    return Response(parser.render(cronFile), mimetype="text/plain")
+    parser.update(cronFile)
+    cronFile = Parser().parse()
+    return Response(cronFile.get_section(name), mimetype="text/plain")
 
 if __name__ == "__main__":
     api.run(host = "0.0.0.0", port = 5000)

+ 12 - 6
scripts/cron/Parser.py

@@ -1,31 +1,37 @@
 import os
 import re
+import subprocess;
 from .CronFile import CronFile
 
 class Parser:
     def parse(self):
         cronFile = CronFile()
         crontab = ""
-        with os.popen("crontab -l") as stream:
-            crontab = stream.read()
-        result = crontab
+        
+        with subprocess.Popen(["crontab", "-l"], stdout=subprocess.PIPE, encoding="utf-8") as proc:
+            crontab = proc.communicate()[0]
+
         begin = re.search("^\s*#\s*BEGIN\((\w+)\).*$", crontab, flags=re.MULTILINE)
         while begin:
             section = begin.group(1)
             cronFile.add_fragment(None, crontab[:begin.span()[0]])
-        
+
             crontab = crontab[begin.span()[1]:]
             end = re.search("^\s*#\s*END\({0}\).*$".format(section), crontab, flags=re.MULTILINE)
             if end:
                 cronFile.add_fragment(section, crontab[:end.span()[0]])
                 crontab = crontab[end.span()[1]:]
-            
+
             begin = re.search("^\s*#\s*BEGIN\((\w+)\).*$", crontab, flags=re.MULTILINE)
 
         cronFile.add_fragment(None, crontab)
 
         return cronFile
-    
+
+    def update(self, cronFile):
+        with subprocess.Popen(["crontab", "-"], stdin=subprocess.PIPE, encoding="utf-8") as proc:
+            crontab = proc.communicate(self.render(cronFile))
+
     def render(self, cronFile):
         result = ""
         for fragment in cronFile.fragments: