2012-09-05 22:50:25 +02:00
|
|
|
import xbmc
|
2012-09-06 22:28:41 +02:00
|
|
|
import datetime
|
2012-09-05 22:50:25 +02:00
|
|
|
import time
|
2012-09-13 16:52:17 +02:00
|
|
|
import os
|
2012-09-05 22:50:25 +02:00
|
|
|
import resources.lib.utils as utils
|
2012-09-06 22:28:41 +02:00
|
|
|
from resources.lib.croniter import croniter
|
2012-09-05 22:50:25 +02:00
|
|
|
from resources.lib.backup import XbmcBackup
|
|
|
|
|
|
|
|
class BackupScheduler:
|
|
|
|
enabled = "false"
|
2012-09-06 22:28:41 +02:00
|
|
|
next_run = 0
|
2012-09-13 16:52:17 +02:00
|
|
|
settings_update_time = 0
|
2012-09-06 22:28:41 +02:00
|
|
|
|
2012-09-05 22:50:25 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.enabled = utils.getSetting("enable_scheduler")
|
2012-09-06 22:28:41 +02:00
|
|
|
|
|
|
|
if(self.enabled == "true"):
|
|
|
|
self.setup()
|
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
#scheduler was turned on, find next run time
|
|
|
|
utils.log("scheduler enabled, finding next run time")
|
2012-09-13 16:52:17 +02:00
|
|
|
self.findNextRun(time.time(),True)
|
2012-09-06 22:28:41 +02:00
|
|
|
utils.log("scheduler will run again on " + datetime.datetime.fromtimestamp(self.next_run).strftime('%m-%d-%Y %H:%M'))
|
2012-09-05 22:50:25 +02:00
|
|
|
|
|
|
|
def start(self):
|
|
|
|
while(not xbmc.abortRequested):
|
2012-09-13 16:52:17 +02:00
|
|
|
current_enabled = utils.getSetting("enable_scheduler")
|
|
|
|
|
|
|
|
if(current_enabled == "true" and self.enabled == "false"):
|
|
|
|
#scheduler was just turned on
|
|
|
|
self.enabled = current_enabled
|
|
|
|
self.setup()
|
|
|
|
elif (current_enabled == "false" and self.enabled == "true"):
|
|
|
|
#schedule was turn off
|
|
|
|
self.enabled = current_enabled
|
|
|
|
elif(self.enabled == "true"):
|
|
|
|
#scheduler is still on
|
2012-09-06 22:28:41 +02:00
|
|
|
now = time.time()
|
|
|
|
|
|
|
|
if(self.next_run <= now):
|
|
|
|
if(utils.getSetting('run_silent') == 'false'):
|
2012-09-12 17:02:33 +02:00
|
|
|
utils.showNotification(utils.getString(30053))
|
2012-09-06 22:28:41 +02:00
|
|
|
#run the job in backup mode, hiding the dialog box
|
|
|
|
backup = XbmcBackup()
|
|
|
|
backup.run(XbmcBackup.Backup,True)
|
2012-12-13 22:11:25 +01:00
|
|
|
|
|
|
|
#check if we should shut the computer down
|
|
|
|
if(utils.getSetting("cron_shutdown") == 'true'):
|
|
|
|
#wait 10 seconds to make sure all backup processes and files are completed
|
|
|
|
time.sleep(10)
|
|
|
|
xbmc.executebuiltin('Quit()')
|
|
|
|
else:
|
|
|
|
#find the next run time like normal
|
|
|
|
self.findNextRun(now,True)
|
2012-10-22 21:19:03 +02:00
|
|
|
else:
|
|
|
|
self.findNextRun(now)
|
2012-09-06 22:28:41 +02:00
|
|
|
|
2012-09-05 22:50:25 +02:00
|
|
|
time.sleep(10)
|
|
|
|
|
2012-09-13 16:52:17 +02:00
|
|
|
def findNextRun(self,now,forceUpdate = False):
|
|
|
|
mod_time = self.settings_update_time
|
|
|
|
|
|
|
|
#check if the schedule has been modified
|
|
|
|
try:
|
|
|
|
#get the last modified time of the file
|
|
|
|
mod_time = os.path.getmtime(xbmc.translatePath(utils.data_dir()) + "settings.xml")
|
|
|
|
except:
|
|
|
|
#don't do anything here
|
|
|
|
mod_time = self.settings_update_time
|
|
|
|
|
|
|
|
if(mod_time > self.settings_update_time or forceUpdate):
|
|
|
|
self.settings_update_time = mod_time
|
|
|
|
|
|
|
|
#find the cron expression and get the next run time
|
|
|
|
cron_exp = self.parseSchedule()
|
2012-09-06 22:28:41 +02:00
|
|
|
|
2012-09-13 16:52:17 +02:00
|
|
|
cron_ob = croniter(cron_exp,datetime.datetime.fromtimestamp(now))
|
|
|
|
new_run_time = cron_ob.get_next(float)
|
2012-09-06 22:28:41 +02:00
|
|
|
|
2012-09-13 16:52:17 +02:00
|
|
|
if(new_run_time != self.next_run):
|
|
|
|
self.next_run = new_run_time
|
|
|
|
utils.log("scheduler will run again on " + datetime.datetime.fromtimestamp(self.next_run).strftime('%m-%d-%Y %H:%M'))
|
2012-09-06 22:28:41 +02:00
|
|
|
|
2012-09-05 22:50:25 +02:00
|
|
|
def parseSchedule(self):
|
|
|
|
schedule_type = int(utils.getSetting("schedule_interval"))
|
|
|
|
cron_exp = utils.getSetting("cron_schedule")
|
|
|
|
|
|
|
|
hour_of_day = utils.getSetting("schedule_time")
|
|
|
|
hour_of_day = int(hour_of_day[0:2])
|
|
|
|
if(schedule_type == 0):
|
|
|
|
#every day
|
|
|
|
|
|
|
|
cron_exp = "0 " + str(hour_of_day) + " * * *"
|
|
|
|
elif(schedule_type == 1):
|
|
|
|
#once a week
|
|
|
|
day_of_week = utils.getSetting("day_of_week")
|
|
|
|
cron_exp = "0 " + str(hour_of_day) + " * * " + day_of_week
|
|
|
|
elif(schedule_type == 2):
|
|
|
|
#first day of month
|
|
|
|
cron_exp = "0 " + str(hour_of_day) + " 1 * *"
|
|
|
|
|
|
|
|
return cron_exp
|
|
|
|
|
|
|
|
BackupScheduler().start()
|