|
@@ -1,31 +1,37 @@
|
|
|
import os
|
|
import os
|
|
|
import re
|
|
import re
|
|
|
|
|
+import subprocess;
|
|
|
from .CronFile import CronFile
|
|
from .CronFile import CronFile
|
|
|
|
|
|
|
|
class Parser:
|
|
class Parser:
|
|
|
def parse(self):
|
|
def parse(self):
|
|
|
cronFile = CronFile()
|
|
cronFile = CronFile()
|
|
|
crontab = ""
|
|
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)
|
|
begin = re.search("^\s*#\s*BEGIN\((\w+)\).*$", crontab, flags=re.MULTILINE)
|
|
|
while begin:
|
|
while begin:
|
|
|
section = begin.group(1)
|
|
section = begin.group(1)
|
|
|
cronFile.add_fragment(None, crontab[:begin.span()[0]])
|
|
cronFile.add_fragment(None, crontab[:begin.span()[0]])
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
crontab = crontab[begin.span()[1]:]
|
|
crontab = crontab[begin.span()[1]:]
|
|
|
end = re.search("^\s*#\s*END\({0}\).*$".format(section), crontab, flags=re.MULTILINE)
|
|
end = re.search("^\s*#\s*END\({0}\).*$".format(section), crontab, flags=re.MULTILINE)
|
|
|
if end:
|
|
if end:
|
|
|
cronFile.add_fragment(section, crontab[:end.span()[0]])
|
|
cronFile.add_fragment(section, crontab[:end.span()[0]])
|
|
|
crontab = crontab[end.span()[1]:]
|
|
crontab = crontab[end.span()[1]:]
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
begin = re.search("^\s*#\s*BEGIN\((\w+)\).*$", crontab, flags=re.MULTILINE)
|
|
begin = re.search("^\s*#\s*BEGIN\((\w+)\).*$", crontab, flags=re.MULTILINE)
|
|
|
|
|
|
|
|
cronFile.add_fragment(None, crontab)
|
|
cronFile.add_fragment(None, crontab)
|
|
|
|
|
|
|
|
return cronFile
|
|
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):
|
|
def render(self, cronFile):
|
|
|
result = ""
|
|
result = ""
|
|
|
for fragment in cronFile.fragments:
|
|
for fragment in cronFile.fragments:
|