Author: duncan
Date: Fri Sep 29 17:26:00 2006
New Revision: 8273

Added:
   branches/rel-1-5/freevo/src/plugins/autoshutdown.py
Modified:
   branches/rel-1-5/freevo/freevo_config.py

Log:
[ 1567885 ] Extensive wakup/shutdown (nvram) plugin
Patch applied and config updated.


Modified: branches/rel-1-5/freevo/freevo_config.py
==============================================================================
--- branches/rel-1-5/freevo/freevo_config.py    (original)
+++ branches/rel-1-5/freevo/freevo_config.py    Fri Sep 29 17:26:00 2006
@@ -230,7 +230,8 @@
         new tv.ivtv_xine_tv plugin (the latter should be e.g. "/tmp/xine-buf-" 
and point
         to a place with enough free diskspace (several gigabytes).
         Added TV_RECORD_PADDING_PRE/POST for separately setting 
TV_RECORD_PADDING
-        Added TV_RECORDFILE_OKLETTERS for characters allowed in recording 
filenames.''' ),
+        Added TV_RECORDFILE_OKLETTERS for characters allowed in recording 
filenames.
+     Added AUTOSHUTDOWN_ settings to turn off and on the machine 
automatically''' ),
 ]
 
 
@@ -322,6 +323,136 @@
 # Location of the reboot-flag used if nvram-wakeup returns 1:
 NVRAM_REBOOT_FLAG = '%s/reboot_flag' % FREEVO_CACHEDIR
 
+# ======================================================================
+# AUTOSHUTDOWN CONFIGURATION
+# ======================================================================
+
+# replace the default shutdown plugin
+#plugin.remove('shutdown')
+#plugin.activate('autoshutdown', level=90)
+
+# activate the timer
+#plugin.activate('autoshutdown.autoshutdowntimer')
+
+
+# -- autoshutdown menu item configuration --
+
+# CONFIRM_SHUTDOWN
+# Set to True to popup dialog boxes for confirmation.
+# this applies to menu items only.
+AUTOSHUTDOWN_CONFIRM = True
+
+
+# -- autoshutdown timer configuration --
+
+# TIMER_TIMEOUT
+# Set the timeout in minutes after which the system
+# is shutdown. The allowed idle time and the running
+# processes (see below) are evaluated to determine if
+# a shutdown is allowed. Menu navigation in freevo will
+# reset the timer.
+AUTOSHUTDOWN_TIMER_TIMEOUT=30
+
+
+# -- autoshutdown behaviour configuration --
+
+# PRETEND
+# Set to True to disable the actual shutdown command.
+AUTOSHUTDOWN_PRETEND = False
+
+# PROCESS_LIST
+# List the processes that will prevent an automatic
+# shutdown. If there are important programs that
+# should not be interrupted, then add them to this
+# list. Set to None if a shutdown is always allowed.
+AUTOSHUTDOWN_PROCESS_LIST = [
+       'emerge',
+       'tvgids',
+       'transcode',
+       'cdrecord',
+       'mplayer',
+       'top'
+]
+
+# DEFAULT_WAKEUP_TIME
+# Set the default time at which to wakeup if there
+# are no recordings scheduled. The time is specified
+# in localtime 24 hour format. Set to None to disable
+# default wakeup time.
+AUTOSHUTDOWN_DEFAULT_WAKEUP_TIME = "13:00"
+
+# FORCE_DEFAULT_WAKEUP
+# Set to True to always wakeup at the default wakeup
+# time. Set to False to only wakeup at the default
+# wakeup time when no recordings are scheduled.
+AUTOSHUTDOWN_FORCE_DEFAULT_WAKEUP = True
+
+# ALLOWED_IDLE_TIME
+# The number of minutes that may be spent idle until
+# the next scheduled recording or default wakeup. That
+# is, if the gap between "now" and the next recording
+# or default wakeup is less than the allowed idle time
+# then a shutdown is not performed but the system is
+# left running. If the period from now to the next
+# recording or default wakeup is more than the allowed
+# idle time, then the system is shut down and a wakeup
+# is scheduled. Use this to minimize the number of
+# shutdown/boot sequences when many short programs are
+# recorded in a short period of time. Note that this
+# variable is used by both the timer and the menu.
+AUTOSHUTDOWN_ALLOWED_IDLE_TIME = 45
+
+
+# -- autoshutdown nvram-wakeup configuration --
+
+# The nvram-wakeup utility is used to write the
+# wakeup alarm to the RTC in bios. Read the
+# nvram-wakeup documentation about this topic,
+# a working nvram-wakeup configuration is needed.
+
+# NVRAM_CMD / NVRAM_OPT
+# Path to nvram-wakeup and options. Options can
+# be used to specify a config file.
+AUTOSHUTDOWN_NVRAM_CMD = "/usr/bin/nvram-wakeup"
+AUTOSHUTDOWN_NVRAM_OPT = "--syslog"
+
+# WAKEUP_NEEDS_REBOOT
+# Set to True if the bios needs a reboot to catch
+# up with the rtc alarm that nvram-wakeup sets. The
+# boot loader options should be set too. Read the
+# nvram-wakeup documentation about this topic.
+AUTOSHUTDOWN_BIOS_NEEDS_REBOOT = True
+
+# -- if the bios needs a reboot --
+
+# BOOT_LOADER
+# Set to "GRUB" or "LILO" Only needed if bios needs
+# a reboot to initialize the RTC wakeup call.
+AUTOSHUTDOWN_BOOT_LOADER = "GRUB"
+
+# REMOUNT_BOOT_CMD / REMOUNT_BOOT_OPT
+# Grub needs to write to /boot/grub/grub.conf. Set
+# the command and options to remount the /boot
+# partition writeable. Set to None if this is not
+# needed.
+AUTOSHUTDOWN_REMOUNT_BOOT_CMD = "/bin/mount"
+AUTOSHUTDOWN_REMOUNT_BOOT_OPT = "/boot -o remount,rw"
+
+# GRUB_CMD / GRUB_OPT
+# Grub-set-default command and options that will
+# reboot and poweroff the system.
+AUTOSHUTDOWN_GRUB_CMD = "/sbin/grub-set-default 0"
+AUTOSHUTDOWN_GRUB_OPT = "0"
+
+# LILO_CMD / LILO_OPT
+# Lilo command with options that will reboot and
+# poweroff the system.
+AUTOSHUTDOWN_LILO_CMD = "/sbin/lilo"
+AUTOSHUTDOWN_LILO_OPT = "-R PowerOff"
+
+# ======================================================================
+# Events
+# ======================================================================
 #
 # You can add more keybindings by adding them to the correct hash. 
 # e.g. If you want to send 'contrast -100' to mplayer by pressing the '1' key, 
@@ -1152,6 +1283,10 @@
 MPLAYER_VF_INTERLACED  = 'pp=de/fd'
 MPLAYER_VF_PROGRESSIVE = 'pp=de'
 
+# default to XINE_VO/AO_DEV:
+XINE_TV_VO_DEV = None
+XINE_TV_AO_DEV = None
+XINE_TV_TIMESHIFT_FILEMASK = "you must set XINE_TV_TIMESHIFT_FILEMASK in your 
local_conf.py (e.g.=/tmp/xine-buf-)"
 
 # ======================================================================
 # Xine settings:

Added: branches/rel-1-5/freevo/src/plugins/autoshutdown.py
==============================================================================
--- (empty file)
+++ branches/rel-1-5/freevo/src/plugins/autoshutdown.py Fri Sep 29 17:26:00 2006
@@ -0,0 +1,618 @@
+# -*- coding: iso-8859-1 -*-
+# -----------------------------------------------------------------------
+# autoshutdown.py - Automated Shutdown
+# -----------------------------------------------------------------------
+# $Id: autoshutdown.py 8265 2006-09-29 12:50:51Z duncan $
+#
+# Author: [EMAIL PROTECTED]
+# Notes:
+# Todo:        
+#
+# -----------------------------------------------------------------------
+# Freevo - A Home Theater PC framework
+# Copyright (C) 2002 Krister Lagerstrom, et al. 
+# Please see the file freevo/Docs/CREDITS for a complete list of authors.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
+# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+# Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+# ----------------------------------------------------------------------- */
+
+
+import os
+import time
+import sys
+import commands
+import config
+import plugin
+import menu
+import event as em
+from gui import ConfirmBox
+from item import Item
+from gui.AlertBox import AlertBox
+import tv.record_client as record_client
+
+DEBUG = config.DEBUG
+
+# Exception handling classes
+class ExInternalError : pass
+
+class ExNoRecordServer(Exception) : pass
+
+class ExNoDefaultWakeup(Exception) : pass
+
+class ExIndexNotAvailable(Exception) : pass
+
+class ExNoWakeupNeeded(Exception) : pass
+
+class ExNextWakeupSoon(Exception) : pass
+
+class ExProcessRunning(Exception) : pass
+
+class ExRecordingInProgress(Exception) : pass
+
+# ***************************************************************
+# CLASS PluginInterface
+# ***************************************************************
+class PluginInterface(plugin.MainMenuPlugin):
+       def items(self, parent):
+               return [ ShutdownMenuItem(parent) ]
+
+
+# ***************************************************************
+# CLASS ShutdownMenuItem
+# ***************************************************************
+class ShutdownMenuItem(Item):
+       def __init__(self, parent=None):
+               Item.__init__(self, parent, skin_type='shutdown')
+               self.idletimer = plugin.getbyname('autoshutdowntimer')
+
+       # -----------------------------------------------------------
+       # TEXT FORMATTING
+       # -----------------------------------------------------------
+       def message_check(self, wakeup=False):
+               try:
+                       is_shutdown_allowed()
+               except ExRecordingInProgress:
+                       msg = _("A recording is in progress.")
+               except ExNextWakeupSoon:
+                       if (wakeup):
+                               msg = _("Would wakeup again within %d minutes." 
% int(config.AUTOSHUTDOWN_ALLOWED_IDLE_TIME))
+                       else:
+                               msg = ""
+               except ExProcessRunning:
+                       msg = _("There are important processes running.")
+               else:
+                       if (wakeup == True):
+                               try:
+                                       next = get_next_wakeup()
+                               except ExNoWakeupNeeded:
+                                       msg = _("No wakeup scheduled.")
+                               else:
+                                       next_msg = time.strftime(
+                                               config.TV_DATETIMEFORMAT,
+                                               time.localtime(next)
+                                       )
+                                       next_min = int((next - time.time()) / 
60)
+                                       msg = _("The next wakeup is scheduled 
at") + "\n" + next_msg
+                       else:
+                               msg = ""
+               return msg
+
+
+       # -----------------------------------------------------------
+       # ACTIONS
+       # -----------------------------------------------------------
+       def actions(self):
+               if (self.idletimer.ispaused()):
+                       itemname = _('Resume automatic shutdown')
+               else:
+                       itemname = _('Pause automatic shutdown')
+               items = [
+                       (self.confirm_shutdown_wakeup,  _('Shutdown and 
wakeup') ),
+                       (self.confirm_toggle_timer,                     
itemname ),
+                       (self.confirm_restart_system,   _('Restart system') ),
+                       (self.confirm_shutdown_system,  _('Shutdown system') ),
+                       (self.confirm_shutdown_freevo,  _('Shutdown freevo') ),
+               ]
+               return items
+
+
+       # -----------------------------------------------------------
+       # CONFIRMATION
+       # -----------------------------------------------------------
+       def confirm_shutdown_wakeup(self, arg=None, menuw=None):
+               if config.AUTOSHUTDOWN_CONFIRM:
+                       title = _('SHUTDOWN SYSTEM?')
+                       info = self.message_check(wakeup=True)
+                       msg = title + "\n\n" + info
+                       ConfirmBox(text=msg, handler=self.shutdown_wakeup, 
default_choice=1).show()
+               else:
+                       self.shutdown_wakeup(arg, menuw)
+
+
+       def confirm_toggle_timer(self, arg=None, menuw=None):
+               self.toggle_timer(arg, menuw)
+
+
+       def confirm_restart_system(self, arg=None, menuw=None):
+               if config.AUTOSHUTDOWN_CONFIRM:
+                       title = _('RESTART SYSTEM?')
+                       info = self.message_check(wakeup=False)
+                       if (info == None):
+                               info = ""
+                       else:
+                               info = "\n\n" + info
+                       msg =  title + "\n" + _("(wakeup disabled)") + info
+                       ConfirmBox(text=msg, handler=self.restart_system, 
default_choice=1).show()
+               else:
+                       self.restart_system(arg, menuw)
+
+
+       def confirm_shutdown_system(self, arg=None, menuw=None):
+               if config.AUTOSHUTDOWN_CONFIRM:
+                       title = _('SHUTDOWN SYSTEM?')
+                       info = self.message_check(wakeup=False)
+                       if (info == None):
+                               info = ""
+                       else:
+                               info = "\n\n" + info
+                       msg =  title + "\n" + _("(wakeup disabled)") + info
+                       ConfirmBox(text=msg, handler=self.shutdown_system, 
default_choice=1).show()
+               else:
+                       self.shutdown_system(arg, menuw)
+
+
+       def confirm_shutdown_freevo(self, arg=None, menuw=None):
+               if config.AUTOSHUTDOWN_CONFIRM:
+                       title = _('SHUTDOWN FREEVO?')
+                       info = self.message_check(wakeup=False)
+                       if (info == None):
+                               info = ""
+                       else:
+                               info = "\n\n" + info
+                       msg =  title + "\n" + _("(wakeup disabled)") + info
+                       ConfirmBox(text=msg, handler=self.shutdown_freevo, 
default_choice=1).show()
+               else:
+                       self.shutdown_freevo(arg, menuw)
+
+
+       # -----------------------------------------------------------
+       # ACTIONS
+       # -----------------------------------------------------------
+       def shutdown_wakeup(self, arg=None, menuw=None):
+               shutdown_action(action=Shutdown.SHUTDOWN_WAKEUP)
+
+
+       def toggle_timer(self, arg=None, menuw=None):
+               if (self.idletimer.ispaused()):
+                       newname = _('Pause automatic shutdown')
+                       self.idletimer.resume()
+               else:
+                       newname = _('Resume automatic shutdown')
+                       self.idletimer.pause()
+               old = menuw.menustack[-1].selected
+               pos = 
menuw.menustack[-1].choices.index(menuw.menustack[-1].selected)
+               new = menu.MenuItem(newname, old.function, old.arg, old.type)
+#              new.image = old.image
+#              if hasattr(old, 'display_type'):
+#                      new.display_type = old.display_type
+               menuw.menustack[-1].choices[pos] = new
+               menuw.menustack[-1].selected = menuw.menustack[-1].choices[pos]
+               menuw.init_page()
+               menuw.refresh()
+       def restart_system(self, arg=None, menuw=None):
+               shutdown_action(action=Shutdown.RESTART_SYSTEM)
+
+
+       def shutdown_system(self, arg=None, menuw=None):
+               shutdown_action(action=Shutdown.SHUTDOWN_SYSTEM)
+
+
+       def shutdown_freevo(self, arg=None, menuw=None):
+               shutdown_action(action=Shutdown.SHUTDOWN_FREEVO)
+
+
+# ***************************************************************
+# CLASS autoshutdowntimer
+# ***************************************************************
+class autoshutdowntimer(plugin.DaemonPlugin):
+       def __init__(self):
+               plugin.DaemonPlugin.__init__(self)
+               self.plugin_name = 'autoshutdowntimer'
+               self.event_listener = True
+               # poll interval in 1/100 second
+               self.poll_interval = 2000
+               self.reset()
+               self.resume()
+               _debug_("autoshutdown timer initialized")
+
+
+       def ispaused(self):
+               return self.lock
+
+
+       def pause(self):
+               self.lock = True
+               _debug_("autoshutdown timer paused")
+
+
+       def resume(self):
+               self.lock = False
+               self.reset()
+               _debug_("autoshutdown timer resumed")
+
+
+       def reset(self):
+               self.idle_base = time.time()
+               self.delay = 0
+               _debug_("autoshutdown timer reset")
+
+
+       def eventhandler(self, event = None, menuw=None, arg=None):
+               if not self.lock:
+                       if not event.name == 'IDENTIFY_MEDIA' and not 
event.name == 'SCREENSAVER_START':
+                               self.reset()
+                               _debug_("timer reset, received event %s" % 
event.name)
+               return FALSE
+
+
+       def poll(self):
+               if not self.lock:
+                       # calculate passed and remaining time
+                       tdif = (time.time() - self.idle_base)
+                       trem = (config.AUTOSHUTDOWN_TIMER_TIMEOUT + self.delay 
- (tdif/60))
+                       if (tdif > ((config.AUTOSHUTDOWN_TIMER_TIMEOUT + 
self.delay) * 60) ):
+                               try:
+                                       is_shutdown_allowed()
+                               except ExRecordingInProgress:
+                                       _debug_("not shuttng down, a recording 
is in progress")
+                                       _debug_("retry in 5 minutes")
+                                       self.delay += 5;
+                               except ExNextWakeupSoon:
+                                       _debug_("not shuttng down, next wakeup 
is nearby")
+                                       self.reset();
+                               except ExProcessRunning:
+                                       _debug_("not shuttng down, an external 
process is running")
+                                       _debug_("retry in 5 minutes")
+                                       self.delay += 5;
+                               else:
+                                       _debug_("Shutdown issued by 
autoshutdown timer!")
+                                       
shutdown_action(action=Shutdown.SHUTDOWN_WAKEUP)
+                       else:
+                               _debug_("idle for %d seconds, %d minutes 
remaining" % (tdif, trem))
+
+
+# ***************************************************************
+# CLASS SHUTDOWN
+# ***************************************************************
+class Shutdown:
+       SHUTDOWN_WAKEUP, RESTART_SYSTEM, SHUTDOWN_SYSTEM, SHUTDOWN_FREEVO, 
IGNORE  = range(5)
+
+
+# ***************************************************************
+# PUBLIC HELPER FUNTIONS
+# ***************************************************************
+# -----------------------------------------------------------
+# is_shutdown_allowed
+# checks if a shutdown is allowed
+# -----------------------------------------------------------
+# Input:       None
+# Result:      True or excetion
+# Raises:      ExRecordingInProgress if there is a recording
+#                      ExNextWakeupSoon if the next wakeup is nearby
+#                      ExProcessRunning if there is an important process
+# -----------------------------------------------------------
+def is_shutdown_allowed():
+       now = time.time()
+       try:
+               t = __get_scheduled_recording(0)
+       except ExIndexNotAvailable:
+               t = now + (config.AUTOSHUTDOWN_ALLOWED_IDLE_TIME*60) + 1
+       except ExNoRecordServer:
+               t = now + (config.AUTOSHUTDOWN_ALLOWED_IDLE_TIME*60) + 1
+       if (t - now < 0):
+               raise ExRecordingInProgress
+       if ((t - now) <= (config.AUTOSHUTDOWN_ALLOWED_IDLE_TIME*60)):
+               raise ExNextWakeupSoon
+       if (__check_processes()):
+               raise ExProcessRunning
+       return True
+
+
+# -----------------------------------------------------------
+# get_next_wakeup
+# Calculate the next wakeup time in seconds UTC
+# -----------------------------------------------------------
+# Input:       None
+# Result:      UTC time of next wakeup
+# Raises:      ExNoWakeupNeeded if no wakeup needed
+# -----------------------------------------------------------
+def get_next_wakeup():
+       scheduled_utc_s = 0
+       default_utc_s = 0
+       now = time.time()
+       i = 0
+       # find first scheduled recording in the future
+       while scheduled_utc_s < now:
+               try:
+                       scheduled_utc_s = __get_scheduled_recording(i)
+               except ExNoRecordServer:
+                       _debug_("Record serer is down")
+                       break
+               except ExIndexNotAvailable:
+                       _debug_("No more recordings available")
+                       break
+               i = i + 1
+       # find next default wakeup
+       try:
+               default_utc_s = __get_next_default_wakeup()
+       except ExNoDefaultWakeup:
+                       _debug_("Default wakeup is disabled")
+       # test which wakeup time applies
+       if (default_utc_s == 0) and (scheduled_utc_s == 0):
+                       # no default and scheduled wakeups available
+                       _debug_("No wakeup time available")
+                       raise ExNoWakeupNeeded
+       elif (default_utc_s > 0) and (scheduled_utc_s > 0):
+               # default wakeup and scheduled wakeups available
+               if (
+                       (config.AUTOSHUTDOWN_FORCE_DEFAULT_WAKEUP == True) and
+                       (default_utc_s < scheduled_utc_s)
+               ):
+                       # forced default wakeup is sooner than scheduled wakeup
+                       wakeup = default_utc_s
+               else:
+                       # no forced default wakeup or scheduled wakeup is sooner
+                       wakeup = scheduled_utc_s
+       else: # (default_utc_s > 0) xor (scheduled_utc_s > 0):
+               # pick the largest one
+               wakeup = max(default_utc_s, scheduled_utc_s)
+               _debug_("Picked wakeup at %s" % time.ctime(wakeup))
+       return wakeup
+
+
+# -----------------------------------------------------------
+# shutdown_action
+# schedules a wakeup and shuts down
+# -----------------------------------------------------------
+# Input:       action (type Shutdown)
+# Result:      -
+# Raises:      -
+# -----------------------------------------------------------
+def shutdown_action(action=None):
+       if (action == Shutdown.SHUTDOWN_WAKEUP):
+               _debug_("shutdown wakeup")
+               action = __schedule_wakeup_and_shutdown()
+       if (action == Shutdown.RESTART_SYSTEM):
+               _debug_("restart system")
+               __cleanup_freevo()
+               __syscall(config.RESTART_SYS_CMD, (config.AUTOSHUTDOWN_PRETEND 
== True))
+               # wait until the system halts/reboots
+               while 1:
+                       time.sleep(1)
+       elif (action == Shutdown.SHUTDOWN_SYSTEM):
+               _debug_("shutdown system")
+               __cleanup_freevo()
+               __syscall(config.SHUTDOWN_SYS_CMD, (config.AUTOSHUTDOWN_PRETEND 
== True))
+               # wait until the system halts/reboots
+               while 1:
+                       time.sleep(1)
+       elif (action == Shutdown.SHUTDOWN_FREEVO):
+               _debug_("shutdown freevo")
+               __cleanup_freevo()
+               sys.exit(0)
+       elif (action == Shutdown.IGNORE):
+               pass
+       else:
+               raise ExInternalError
+       return
+
+
+# ***************************************************************
+# PRIVATE HELPER FUNTIONS
+# ***************************************************************
+# -----------------------------------------------------------
+# __schedule_wakeup
+# Schedules a wakeup in the bios
+# -----------------------------------------------------------
+# Input:       -
+# Result:      next action (shutdown or reboot)
+# Raises:      -
+# -----------------------------------------------------------
+def __schedule_wakeup_and_shutdown():
+       try:
+               wakeup_utc_s = get_next_wakeup()
+       except ExNoWakeupNeeded:
+               _debug_("No wakeup needed, shutting down")
+               if config.AUTOSHUTDOWN_PRETEND == False:
+                       next_action = Shutdown.SHUTDOWN_SYSTEM
+               else:
+                       next_action = Shutdown.IGNORE
+       else:
+               cmd = "%s %s --settime %d" % (config.AUTOSHUTDOWN_NVRAM_CMD, 
config.AUTOSHUTDOWN_NVRAM_OPT, int(wakeup_utc_s))
+               __syscall(cmd)
+               if (config.AUTOSHUTDOWN_BIOS_NEEDS_REBOOT == True):
+                       # needs a reboot
+                       if config.AUTOSHUTDOWN_BOOT_LOADER == "GRUB":
+                               if config.AUTOSHUTDOWN_REMOUNT_BOOT_CMD != None:
+                                       cmd = "%s %s" % 
(config.AUTOSHUTDOWN_REMOUNT_BOOT_CMD, config.AUTOSHUTDOWN_REMOUNT_BOOT_OPT)
+                               cmd = config.AUTOSHUTDOWN_GRUB_CMD
+                               __syscall(cmd)
+                               _debug_("Wakeup set, reboot needed")
+                               next_action = Shutdown.RESTART_SYSTEM
+                       elif config.AUTOSHUTDOWN_BOOT_LOADER== "LILO":
+                               cmd = "%s %s" % (config.AUTOSHUTDOWN_LILO_CMD, 
config.AUTOSHUTDOWN_LILO_OPT)
+                               __syscall(cmd)
+                               _debug_("Wakeup set, reboot needed")
+                               next_action = Shutdown.RESTART_SYSTEM
+                       else:
+                               raise ExInternalError
+               else:
+                       _debug_("Wakeup set, shutdown needed")
+                       next_action =  Shutdown.SHUTDOWN_SYSTEM
+       return next_action
+
+
+# -----------------------------------------------------------
+# __cleanup_freevo
+# Performs necessary actions for freevo shutdown
+# -----------------------------------------------------------
+# Input:       -
+# Result:      -
+# Raises:      -
+# -----------------------------------------------------------
+def __cleanup_freevo():
+       import osd
+       import plugin
+       import rc
+       import util.mediainfo
+       osd = osd.get_singleton()
+       util.mediainfo.sync()
+       if not config.HELPER:
+               if not osd.active:
+                       # this function is called from the signal
+                       # handler, but we are dead already.
+                       sys.exit(0)
+               osd.clearscreen(color=osd.COL_BLACK)
+               osd.drawstringframed(
+                       _('shutting down...'),
+                       0, 0, osd.width, osd.height,
+                       osd.getfont(config.OSD_DEFAULT_FONTNAME,
+                       config.OSD_DEFAULT_FONTSIZE),
+                       fgcolor=osd.COL_ORANGE,
+                       align_h='center', align_v='center'
+               )
+               osd.update()
+               time.sleep(0.5)
+       # shutdown all daemon plugins
+       plugin.shutdown()
+       # shutdown registered callbacks
+       rc.shutdown()
+       if not config.HELPER:
+               # shutdown the screen
+               osd.clearscreen(color=osd.COL_BLACK)
+               osd.shutdown()
+
+
+# -----------------------------------------------------------
+# __get_scheduled_recording
+# Get the start time of a recording from the reordserver
+# -----------------------------------------------------------
+# Input:       index 0..n
+# Result:      UTC time of next recording
+# Raises:      ExNoRecordServer if the recordserver is down
+#                      ExIndexNotAvailable
+# -----------------------------------------------------------
+def __get_scheduled_recording(index):
+       try:
+               (result, response) = record_client.updateFavoritesSchedule()
+               (result, schedule) = record_client.getScheduledRecordings()
+       except:
+               raise ExNoRecordServer
+       else:
+               scheduled_programs = []
+               if result > 0:
+                       proglist = schedule.getProgramList().values()
+                       if ((index + 1) > len(proglist) ):
+                               raise ExIndexNotAvailable
+                       else:
+                               f = lambda a, b: cmp(a.start, b.start)
+                               proglist.sort(f)
+                               wakeup = proglist[index].start
+                               _debug_("Scheduled recording %d at %s is %s" % 
(index, time.ctime(wakeup), proglist[index]))
+               else:
+                       raise ExIndexNotAvailable
+       return wakeup
+
+
+# -----------------------------------------------------------
+# __get_next_default_wakeup
+# Calculate the next default wakeup time in seconds UTC
+# -----------------------------------------------------------
+# Input:       None
+# Result:      UTC time of next default wakeup
+# Raises:      ExNoDefaultWakeup if default wakeup not available
+# -----------------------------------------------------------
+def __get_next_default_wakeup():
+       if (config.AUTOSHUTDOWN_DEFAULT_WAKEUP_TIME == None):
+               raise ExNoDefaultWakeup
+       else:
+               # get local time in seconds
+               today_loc_t = time.localtime()
+               today_loc_s = time.mktime(today_loc_t)
+               # split default time in hour and minute parts
+               def_wakeup_loc_t = 
config.AUTOSHUTDOWN_DEFAULT_WAKEUP_TIME.split(':')
+               def_wakeup_loc_t[0] = int(def_wakeup_loc_t[0])
+               def_wakeup_loc_t[1] = int(def_wakeup_loc_t[1])
+               # calculate the default wakeup time in utc seconds
+               # test def_wakeup against current local time
+               if ((def_wakeup_loc_t[0]*60 + def_wakeup_loc_t[1]) < 
(today_loc_t[3]*60 + today_loc_t[4])):
+                       # def_wakeup is in the past; add a day
+                       next_def_loc_t = (
+                               today_loc_t[0], today_loc_t[1], today_loc_t[2], 
def_wakeup_loc_t[0] + 24,
+                               def_wakeup_loc_t[1], 0, today_loc_t[6], 
today_loc_t[7], today_loc_t[8]
+                       )
+               else:
+                       # def_wakeup is in the future
+                       next_def_loc_t = (
+                               today_loc_t[0], today_loc_t[1], today_loc_t[2], 
def_wakeup_loc_t[0],
+                               def_wakeup_loc_t[1], 0, today_loc_t[6], 
today_loc_t[7], today_loc_t[8]
+                       )
+               # convert next_def to utc seconds
+               wakeup = time.mktime(next_def_loc_t)
+               _debug_("Default wakeup at %s" % time.ctime(wakeup))
+       return wakeup
+
+
+# -----------------------------------------------------------
+# __check_processes
+# checks if important processes are running
+# -----------------------------------------------------------
+# Input:       None
+# Result:      True/False
+# Raises:      -
+# -----------------------------------------------------------
+def __check_processes():
+       if config.AUTOSHUTDOWN_PROCESS_LIST == None:
+               return False
+       else:
+               delimiter='|'
+               searchstring = delimiter.join(config.AUTOSHUTDOWN_PROCESS_LIST)
+               cmd = 'ps -ef | egrep -v "grep" | egrep "(%s)"' % searchstring
+               result = __syscall(cmd)
+               if (result == 0):
+                       _debug_('external process(es) running')
+                       return True
+               else:
+                       _debug_('no external process(es) running')
+                       return False
+
+
+# -----------------------------------------------------------
+# __syscall
+# Calls system command and logs it
+# -----------------------------------------------------------
+# Input:       cmd, pretend
+# Result:      -
+# Raises:      -
+# -----------------------------------------------------------
+def __syscall(cmd, pretend=False):
+       result = 0
+       if (pretend == True):
+               _debug_("Pretending syscall: %s" % cmd)
+       else:
+               _debug_("Executing syscall: %s" % cmd)
+               result = os.system(cmd)
+       return result

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to