Commit 287bb50b authored by Julien Muchembled's avatar Julien Muchembled

dcron: fix conversion from systemd timespecs with steps, and add minutely/yearly aliases

parent 2e9f4c82
...@@ -76,10 +76,12 @@ class Part(GenericBaseRecipe): ...@@ -76,10 +76,12 @@ class Part(GenericBaseRecipe):
day_of_week_dict = dict((name, dow) for dow, name in enumerate( day_of_week_dict = dict((name, dow) for dow, name in enumerate(
"sunday monday tuesday wednesday thursday friday saturday".split()) "sunday monday tuesday wednesday thursday friday saturday".split())
for name in (name, name[:3])) for name in (name, name[:3]))
symbolic_dict = dict(hourly = '0 * * * *', symbolic_dict = dict(minutely = '* * * * *',
hourly = '0 * * * *',
daily = '0 0 * * *', daily = '0 0 * * *',
weekly = '0 0 * * 0',
monthly = '0 0 1 * *', monthly = '0 0 1 * *',
weekly = '0 0 * * 0') yearly = '0 0 1 1 *')
def systemd_to_cron(spec): def systemd_to_cron(spec):
"""Convert from systemd.time(7) calendar spec to crontab spec""" """Convert from systemd.time(7) calendar spec to crontab spec"""
...@@ -118,13 +120,22 @@ def systemd_to_cron(spec): ...@@ -118,13 +120,22 @@ def systemd_to_cron(spec):
raise ValueError raise ValueError
month, day = day month, day = day
hour, minute = time hour, minute = time
spec = minute, hour, day, month, dow spec = [minute, hour, day, month, dow]
for x, (y, z) in zip(spec, ((0, 60), (0, 24), (1, 31), (1, 12))): for i, (y, z) in enumerate(((0, 60), (0, 24), (1, 31), (1, 12))):
x = spec[i]
if x != '*': if x != '*':
for x in x.split(','): for x in x.split(','):
x = map(int, x.split('/', 1)) x = map(int, x.split('/', 1))
x[0] -= y a = x[0] - y
if x[0] < 0 or len(x) > 1 and x[0] >= x[1] or z <= sum(x): if 0 <= a < z:
if len(x) == 1:
continue
b = x[1]
if b > 0:
a = (z - a - 1) // b * b
if a:
spec[i] = '%s-%s/%s' % (x[0], x[0] + a, b)
continue
raise ValueError raise ValueError
return ' '.join(spec) return ' '.join(spec)
...@@ -13,12 +13,13 @@ class TestDcron(unittest.TestCase): ...@@ -13,12 +13,13 @@ class TestDcron(unittest.TestCase):
_("10-15", "0 0 15 10 *") _("10-15", "0 0 15 10 *")
_("monday *-12-* 17:00", "00 17 * 12 1") _("monday *-12-* 17:00", "00 17 * 12 1")
_("12,14,13,12:20,10,30", "20,10,30 12,14,13,12 * * *") # TODO: sort _("12,14,13,12:20,10,30", "20,10,30 12,14,13,12 * * *") # TODO: sort
_("*-1/2-1,3 *:30", "30 * 1,3 1/2 *") _("*-1/2-1,3 *:30", "30 * 1,3 1-11/2 *")
_("03-05 08:05", "05 08 05 03 *") _("03-05 08:05", "05 08 05 03 *")
_("08:05:00", "05 08 * * *") _("08:05:00", "05 08 * * *")
_("05:40", "40 05 * * *") _("05:40", "40 05 * * *")
_("Sat,Sun 12-* 08:05", "05 08 * 12 0,6") _("Sat,Sun 12-* 08:05", "05 08 * 12 0,6")
_("Sat,Sun 08:05", "05 08 * * 0,6") _("Sat,Sun 08:05", "05 08 * * 0,6")
_("*:25/20", "25-45/20 * * * *")
def _(systemd): def _(systemd):
self.assertRaises(Exception, systemd_to_cron, systemd) self.assertRaises(Exception, systemd_to_cron, systemd)
...@@ -31,7 +32,7 @@ class TestDcron(unittest.TestCase): ...@@ -31,7 +32,7 @@ class TestDcron(unittest.TestCase):
_("08:05:40") _("08:05:40")
_("2003-03-05") _("2003-03-05")
_("0-1"); _("13-1"); _("6/4-1"); _("5/8-1") _("0-1"); _("13-1"); _("8/5-1")
_("1-0"); _("1-32"); _("1-4/3"); _("1-14/18") _("1-0"); _("1-32"); _("1-14/18")
_("24:0");_("9/9:0"); _("8/16:0") _("24:0"); _("8/16:0")
_("0:60"); _("0:22/22"); _("0:15/45") _("0:60"); _("0:15/45")
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment