Beispiel #1
0
    def __init__(self):
        self.monitor = UpdateMonitor(update_method=self.settingsChanged)
        self.enabled = utils.getSettingBool("enable_scheduler")
        self.next_run_path = xbmc.translatePath(
            utils.data_dir()) + 'next_run.txt'

        if (self.enabled):

            # sleep for 2 minutes so Kodi can start and time can update correctly
            xbmc.Monitor().waitForAbort(120)

            nr = 0
            if (xbmcvfs.exists(self.next_run_path)):

                fh = xbmcvfs.File(self.next_run_path)
                try:
                    # check if we saved a run time from the last run
                    nr = float(fh.read())
                except ValueError:
                    nr = 0

                fh.close()

            # if we missed and the user wants to play catch-up
            if (0 < nr <= time.time()
                    and utils.getSettingBool('schedule_miss')):
                utils.log("scheduled backup was missed, doing it now...")
                progress_mode = utils.getSettingInt('progress_mode')

                if (progress_mode == 0):
                    progress_mode = 1  # Kodi just started, don't block it with a foreground progress bar

                self.doScheduledBackup(progress_mode)

            self.setup()
Beispiel #2
0
    def checkTimer(self, settingName):
        result = ''

        # figure out if using standard or advanced timer
        if (utils.getSettingBool(settingName + '_advanced_timer')):
            # copy the expression
            result = utils.getSetting(settingName + "_cron_expression")
        else:
            result = '0 */' + str(
                self.timer_amounts[utils.getSetting(settingName +
                                                    "_timer")]) + ' * * *'

        return result
Beispiel #3
0
    def databaseUpdated(self, database):
        showDialogs = utils.getSettingBool(
            'notify_next_run'
        )  # if the user has selected to show dialogs for library operations
        # check if we should clean the library
        if (utils.getSettingBool('clean_libraries')):
            # check if should update while playing media
            if (not xbmc.Player().isPlaying()
                    or utils.getSettingBool("run_during_playback")):
                if (utils.getSettingInt("clean_timer") == 0):
                    # check if we should clean music, or video
                    aJob = CronSchedule()
                    aJob.name = utils.getString(30048)
                    aJob.timer_type = utils.__addon_id__
                    if ((utils.getSettingInt('library_to_clean') == 0
                         or utils.getSettingInt('library_to_clean') == 1)
                            and database == 'video'):
                        # create the clean job schedule
                        aJob.command = {
                            'method': 'VideoLibrary.Clean',
                            'params': {
                                'showdialogs': showDialogs
                            }
                        }
                    if ((utils.getSettingInt('library_to_clean') == 2
                         or utils.getSettingInt('library_to_clean') == 0)
                            and database == 'music'):
                        aJob.command = {
                            'method': 'AudioLibrary.Clean',
                            'params': {
                                'showdialogs': showDialogs
                            }
                        }

                    self.cleanLibrary(aJob)

        # writeLastRun will trigger notifications
        self.writeLastRun()
Beispiel #4
0
    def settingsChanged(self):
        current_enabled = utils.getSettingBool("enable_scheduler")

        if (current_enabled and not self.enabled):
            # scheduler was just turned on
            self.enabled = current_enabled
            self.setup()
        elif (not current_enabled and self.enabled):
            # schedule was turn off
            self.enabled = current_enabled

        if (self.enabled):
            # always recheck the next run time after an update
            self.findNextRun(time.time())
Beispiel #5
0
    def showNotify(self, displayToScreen=True):
        # go through and find the next schedule to run
        next_run_time = CronSchedule()
        for cronJob in self.schedules:
            if (cronJob.next_run < next_run_time.next_run
                    or next_run_time.next_run == 0):
                next_run_time = cronJob

        inWords = self.nextRunCountdown(next_run_time.next_run)
        # show the notification (if applicable)
        if (next_run_time.next_run > time.time()
                and utils.getSettingBool('notify_next_run')
                and displayToScreen):
            utils.showNotification(utils.getString(30000),
                                   inWords + " - " + next_run_time.name)

        return inWords
Beispiel #6
0
    def cleanLibrary(self, cronJob):
        # check if we should verify with user first unless we're on 'clean after update'
        if (utils.getSettingBool('user_confirm_clean')
                and utils.getSettingInt('clean_timer') != 0):
            # user can decide 'no' here and exit this
            runClean = xbmcgui.Dialog().yesno(utils.getString(30000),
                                              utils.getString(30052),
                                              line2=utils.getString(30053),
                                              autoclose=15000)
            if (not runClean):
                return

        # run the clean operation
        utils.log("Cleaning Database")
        cronJob.executeCommand()

        # write last run time, will trigger notifications
        self.writeLastRun()
Beispiel #7
0
    def start(self):

        # display upgrade messages if they exist
        if (utils.getSettingInt('upgrade_notes') < UPGRADE_INT):
            xbmcgui.Dialog().ok(utils.getString(30010), utils.getString(30132))
            utils.setSetting('upgrade_notes', str(UPGRADE_INT))

        # check if a backup should be resumed
        resumeRestore = self._resumeCheck()

        if (resumeRestore):
            restore = XbmcBackup()
            restore.selectRestore(self.restore_point)
            # skip the advanced settings check
            restore.skipAdvanced()
            restore.restore()

        while (not self.monitor.abortRequested()):

            if (self.enabled):
                # scheduler is still on
                now = time.time()

                if (self.next_run <= now):
                    progress_mode = utils.getSettingInt('progress_mode')
                    self.doScheduledBackup(progress_mode)

                    # check if we should shut the computer down
                    if (utils.getSettingBool("cron_shutdown")):
                        # wait 10 seconds to make sure all backup processes and files are completed
                        time.sleep(10)
                        xbmc.executebuiltin('ShutDown()')
                    else:
                        # find the next run time like normal
                        self.findNextRun(now)

            xbmc.sleep(500)

        # delete monitor to free up memory
        del self.monitor
Beispiel #8
0
    def evalSchedules(self, manual=False):
        if (not self.lock):
            now = time.time()

            count = 0
            player = xbmc.Player()
            while count < len(self.schedules):
                cronJob = self.schedules[count]

                if (cronJob.next_run <= now):
                    if (not player.isPlaying() or
                            utils.getSetting("run_during_playback") == "true"):

                        # check if run on idle is checked and screen is idle - disable this on manual run
                        if (not utils.getSettingBool('run_on_idle') or
                            (utils.getSettingBool('run_on_idle') and
                             (self.monitor.screensaver_running or manual))):

                            # check for valid network connection - check sources if setting enabled
                            if (self._networkUp() and
                                (not utils.getSettingBool('check_sources') or
                                 (utils.getSettingBool('check_sources')
                                  and self._checkSources(cronJob)))):

                                # check if this scan was delayed due to playback
                                if (cronJob.on_delay):
                                    # add another minute to the delay
                                    self.schedules[count].next_run = now + 60
                                    self.schedules[count].on_delay = False
                                    utils.log(cronJob.name +
                                              " paused due to playback")

                                elif (not self.scanRunning()):
                                    # run the command for this job
                                    utils.log(cronJob.name)

                                    if (cronJob.timer_type == 'xbmc'):
                                        cronJob.executeCommand()
                                    else:
                                        self.cleanLibrary(cronJob)

                                    # find the next run time
                                    cronJob.next_run = self.calcNextRun(
                                        cronJob.expression, now)
                                    self.schedules[count] = cronJob

                                elif (self.scanRunning()):
                                    self.schedules[count].next_run = now + 60
                                    utils.log(
                                        "Waiting for other scan to finish")
                            else:
                                utils.log("Network down, not running")
                        else:
                            utils.log("Skipping scan, only run when idle")
                    else:
                        self.schedules[count].on_delay = True
                        utils.log("Player is running, wait until finished")

                count = count + 1

            # write last run time
            now = time.time()
            self.last_run = now - (now % 60)
Beispiel #9
0
    def createSchedules(self, forceUpdate=False):
        utils.log("update timers")
        self.lock = True  # lock so the eval portion does not run
        self.schedules = []
        showDialogs = utils.getSettingBool(
            'notify_next_run'
        )  # if the user has selected to show dialogs for library operations

        if (utils.getSettingBool('clean_libraries')):
            # create clean schedule (if needed)
            if (utils.getSettingInt("clean_timer") != 0):

                if (utils.getSettingInt('library_to_clean') == 0
                        or utils.getSettingInt('library_to_clean') == 1):
                    # video clean schedule starts at 12am by default
                    aSchedule = CronSchedule()
                    aSchedule.name = utils.getString(30048)
                    aSchedule.timer_type = utils.__addon_id__
                    aSchedule.command = {
                        'method': 'VideoLibrary.Clean',
                        'params': {
                            'showdialogs': showDialogs
                        }
                    }
                    if (utils.getSettingInt("clean_timer") == 4):
                        aSchedule.expression = utils.getSetting(
                            "clean_video_cron_expression")
                    else:
                        aSchedule.expression = "0 0 " + aSchedule.cleanLibrarySchedule(
                            utils.getSettingInt("clean_timer"))
                    aSchedule.next_run = self.calcNextRun(
                        aSchedule.expression, time.time())

                    self.schedules.append(aSchedule)

                if (utils.getSettingInt('library_to_clean') == 2
                        or utils.getSettingInt('library_to_clean') == 0):
                    # music clean schedule starts at 2am by default
                    aSchedule = CronSchedule()
                    aSchedule.name = utils.getString(30049)
                    aSchedule.timer_type = utils.__addon_id__
                    aSchedule.command = {
                        'method': 'AudioLibrary.Clean',
                        'params': {
                            'showdialogs': showDialogs
                        }
                    }
                    if (utils.getSettingInt("clean_timer") == 4):
                        aSchedule.expression = utils.getSetting(
                            "clean_music_cron_expression")
                    else:
                        aSchedule.expression = "0 2 " + aSchedule.cleanLibrarySchedule(
                            utils.getSettingInt("clean_timer"))
                    aSchedule.next_run = self.calcNextRun(
                        aSchedule.expression, time.time())

                    self.schedules.append(aSchedule)

        if (utils.getSettingBool('update_video')):
            utils.log("Creating timer for Video Library")
            # create the video schedule
            aSchedule = CronSchedule()
            aSchedule.name = utils.getString(30012)
            aSchedule.command = {
                'method': 'VideoLibrary.Scan',
                'params': {
                    'showdialogs': showDialogs
                }
            }
            aSchedule.expression = self.checkTimer('video')
            aSchedule.next_run = self.calcNextRun(aSchedule.expression,
                                                  self.last_run)
            self.schedules.append(aSchedule)

        # add custom video paths (separate timers)
        customPaths = CustomPathFile('video')
        for aJob in customPaths.getSchedules(showDialogs):
            utils.log("Creating timer " + aJob.name)
            aJob.next_run = self.calcNextRun(aJob.expression, self.last_run)
            self.schedules.append(aJob)

        if (utils.getSettingBool('update_music')):
            utils.log("Creating timer for Music Library")
            # create the music schedule
            aSchedule = CronSchedule()
            aSchedule.name = utils.getString(30013)
            aSchedule.command = {
                'method': 'AudioLibrary.Scan',
                'params': {
                    'showdialogs': showDialogs
                }
            }
            aSchedule.expression = self.checkTimer('music')
            aSchedule.next_run = self.calcNextRun(aSchedule.expression,
                                                  self.last_run)

            self.schedules.append(aSchedule)

        # add custom music paths (separate timers)
        customPaths = CustomPathFile('music')
        for aJob in customPaths.getSchedules(showDialogs):
            utils.log("Creating timer " + aJob.name)
            aJob.next_run = self.calcNextRun(aJob.expression, self.last_run)
            self.schedules.append(aJob)

        # release the lock
        self.lock = False

        utils.log("Created " + str(len(self.schedules)) + " schedules",
                  xbmc.LOGDEBUG)

        # show any notifications
        self.showNotify(not forceUpdate)
Beispiel #10
0
from kodi_six import xbmcgui
import resources.lib.utils as utils
from resources.lib.service import AutoUpdater

autoUpdate = AutoUpdater()
runUpdate = False

if (not utils.getSettingBool('disable_manual_prompt')):
    nextRun = autoUpdate.showNotify(False)
    # check if we should run updates
    runUpdate = xbmcgui.Dialog().yesno(
        utils.getString(30000),
        "%s %s \n %s" %
        (utils.getString(30060), nextRun, utils.getString(30061)),
        autoclose=6000)
else:
    # the user has elected to skip the prompt
    runUpdate = True

if (runUpdate):
    # run the program
    utils.log("Update Library Manual Run...")

    # trick the auto updater into resetting the last_run time
    autoUpdate.last_run = 0
    autoUpdate.writeLastRun()

    # update the schedules and evaluate them in manual override mode
    autoUpdate.createSchedules(True)
    autoUpdate.evalSchedules(True)