Skip to content

Instantly share code, notes, and snippets.

@memory
Forked from ajdiaz/python-uschedule.py
Created January 29, 2012 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save memory/1700987 to your computer and use it in GitHub Desktop.
Save memory/1700987 to your computer and use it in GitHub Desktop.
python snippet to handle uschedule jobs
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
"""
Python uschedule handler
------------------------
This module provide a methods to handle uschedule_ scheduler by Uwe Ohse,
which is similar to cron, but safer. Here is an usage example::
>>> from uschedule import Scheduler
>>> s = Scheduler("mydir")
>>> s.schedule("ls","*-*-* 08:05:40")
.. _uschedule: http://www.ohse.de/uwe/uschedule.html
:author: Andres J. Diaz <ajdiaz@connectical.com>
:date: 2010-08-19
"""
import pytai, shutil, os
DEFAULT_EPOCH = 4611686018427387914L # 1972-01-01 for TAI dates.
FLAG_NULL1 = 0x01
FLAG_NULL2 = 0x02
def parse_spec(date):
date = date.replace("\t"," ").strip()
wd = 0
ret = ""
try:
weekday, date, time = date.split(" ")
wd = parse_weekday(weekday)
if wd != 0 and wd != 127:
ret += "W%d" % wd
except ValueError:
date, time = date.split(" ")
ret += parse_stamp(date, "-", [ "Y", "M", "D" ],
lambda (x,y):list.insert(x,0,y), [ 2100, 12, 31 ])
ret += parse_stamp(time, ":", [ "h", "m", "s" ],
lambda (x,y):list.insert(x,0,y), [ 23, 59, 59 ])
return ret
def parse_weekday(weekday):
wd = 0
for opt in weekday.split(","):
if opt[0:3] == "Sun":
wd|=1
elif opt[0:3] == "Mon":
wd|=2
elif opt[0:3] == "Tue":
wd|=4
elif opt[0:3] == "Wed":
wd|=8
elif opt[0:3] == "Thu":
wd|=16
elif opt[0:3] == "Fri":
wd|=32
elif opt[0:3] == "Sat":
wd|=64
else:
raise ValueError("Bad weekday")
return wd
def is_complete_spec(spec):
try:
s = parse_spec(spec)
if "W" in s: return False
if "Y" in s and "M" in s and "D" in s and \
"h" in s and "m" in s and "s" in s:
return True
else:
return False
except:
return False
def parse_stamp(date, sep, stamp, insertfn, max_val):
split = date.replace("+","/").split(sep)
field = len(split)
ret = ""
if field != 3:
insertfn((split,"-"))
for i in range(len(split)):
try:
if split[i] == "-" or split[i] == "*":
continue
if "/" in split[i]:
init, step = map(int, split[i].split("/"))
init -= 1
ret += "%c%d" % (stamp[i], init)
for j in range((init+step), max_val[i], step):
ret += ",%d" % j
else:
ret += "%c%s" % (stamp[i], split[i])
except ValueError:
raise ValueError("Syntax error in time spec")
return ret
class Scheduler(object):
""" """
def __init__(self, directory):
self.directory = directory
def add_command(self, command):
shutil.copy(command, os.path.join(self.directory, "commands"))
def schedule(self, command, timespec, count=0, every=0, comment="",
from_date="", to_date="", late=3600, flags=0):
timespec = parse_spec(timespec)
tailast = pytai.tai()
tailast.from_tai64n_ext("0")
sflags = ""
if flags & FLAG_NULL1:
sflags += "null1"
if flags & FLAG_NULL2:
if len(sflags):
sflags += ",null2"
else:
sflags += "null2"
shutil.copy(
os.path.join(self.directory, "commands/" + command),
os.path.join(self.directory,
"@%s:%s:%d:%d:%s:%s:%s:%s:%s:%d" % (
tailast.to_tai64n_ext()[0:16], command, late, count,
timespec, comment, sflags, from_date, to_date, every)) )
if __name__ == "__main__":
import sys
s = Scheduler(sys.argv[1])
s.schedule("1", sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment