#! /usr/bin/env python
# -*- coding: ISO-8859-1 -*-
#
# UnderCoverCron.py
# (C)2003 Romain GUY <romain.guy@jext.org>
# Licensed under GPL
# Based on Emilio Schapira work (cron.py)
#
# 08/27/2003 - 04:39:49

"""
UnderCoverCron runs a backup at specified times.
"""

##########################################################################################
# MODULE INFORMATIONS
##########################################################################################

__author__    = "Romain GUY (romain.guy@jext.org)"
__version__   = "1.0"
__copyright__ = "Copyright (C)2003 Romain GUY"
__license__   = "LGPL"

##########################################################################################
# IMPORTS
##########################################################################################

import time
import os
import string
import threading
import UnderCover
from xml.dom.minidom import parse

##########################################################################################
# GLOBALS
##########################################################################################

try:
  True, False
except:
  True, False = 1, 0

##########################################################################################
# CLASSES
##########################################################################################

class UnderCoverCron(object):
  def __init__(self, wrapper):
    self._isAlive           = True
    self._stopEvent         = threading.Event()
    self._isServerAlive     = True
    self._underCoverWrapper = wrapper

  def start(self):
    self._serverThread  = threading.Thread(target = self.doCron)
    self._serverThread.start()

  def stop(self):
    self._isServerAlive = False
    self._underCoverWrapper.stop()

  def _match(self, value, expr):
    if expr == '*':
      return True
    for v in string.split(expr, ','):
      if int(v) == value:
        return True
    return False

  def doCron(self):
    tt = time.localtime(time.time() + 60)
    tt = tt[:5] + (0,) + tt[6:]
    nextTime = time.mktime(tt)
    
    while self._isServerAlive:
      currentTime = time.time()
      if currentTime < nextTime:
        self._stopEvent.wait(nextTime - currentTime + .1)
  
      curTuple = time.localtime(nextTime)
  
      try:
        dom   = parse("undercovercron.conf")
        root  = dom.documentElement
        times = root.getElementsByTagName("action")[0].getElementsByTagName("at")

        remotePath = root.getElementsByTagName("remotePath")[0].firstChild.nodeValue
        localPaths = [node.firstChild.nodeValue for node in root.getElementsByTagName("localPaths")[0].getElementsByTagName("path")]

        for node in times:
          line      = node.firstChild.nodeValue
          tokens    = string.split(line)
          timeMatch = self._match(curTuple[4], tokens[0])
          timeMatch = timeMatch and self._match(curTuple[3], tokens[1]) 
          timeMatch = timeMatch and self._match(curTuple[2], tokens[2])
          timeMatch = timeMatch and self._match(curTuple[1], tokens[3])
          timeMatch = timeMatch and self._match(curTuple[6], tokens[4])

          if timeMatch:
            self._underCoverWrapper.backup(remotePath, localPaths)

        dom.unlink()
      except Exception, e:
        pass
      nextTime = nextTime + 60

if __name__ == '__main__':
  wrapper = UnderCover.UnderCover()
  if wrapper.start():
    cron = UnderCoverCron(wrapper)
    cron.start()

# end of file