Author: dmeyer
Date: Sun Jan 29 19:48:37 2006
New Revision: 7897

Added:
   trunk/apps/
   trunk/apps/bin/
   trunk/apps/bin/freevo-autoshutdown
      - copied, changed from r7895, /trunk/ui/src/helpers/shutdown.py
   trunk/apps/setup.py
Removed:
   trunk/ui/src/helpers/shutdown.py

Log:
move helpers/shutdown to new apps module

Copied: trunk/apps/bin/freevo-autoshutdown (from r7895, 
/trunk/ui/src/helpers/shutdown.py)
==============================================================================
--- /trunk/ui/src/helpers/shutdown.py   (original)
+++ trunk/apps/bin/freevo-autoshutdown  Sun Jan 29 19:48:37 2006
@@ -1,3 +1,4 @@
+#! /usr/bin/python
 # -*- coding: iso-8859-1 -*-
 # -----------------------------------------------------------------------------
 # shutdown.py - auto shutdown helper
@@ -9,27 +10,15 @@
 # the recordserver to power on and off the pc for recordings.
 #
 # On startup the helper will wait at least 30 minutes before doing anything.
-# After that time, it will send a status rpc to all entities on the mbus.
-# it will check 'idle', 'busy' and 'wakeup' from the return. Entities not
-# answering will be ignored. 'idle' is the idle time of an entity (e.g. the
-# Freevo GUI). When the idletime is less than 30 minutes, the system won't shut
-# down. The Freevo GUI will only report an idle time greater zero if it is
-# showing the menu and the idle time will be the time between the last event
-# and the current time in minutes. The 'busy' attribute is a hint how long
-# the entity will be busy (minimum) to give the helper an idea how long to
-# wait. The 'wakeup' attribute defines a wakeup time that will be set using an
+# It will check 'idle', 'busy' and 'wakeup' from mbus status messages.
+# The 'wakeup' attribute defines a wakeup time that will be set using an
 # external program defined in config.SHUTDOWN_WAKEUP_CMD to schedule the
 # wakeup.
 #
-# If the idle time is lower 30 minutes or an entity is busy, the helper will
-# wait min(max(30 - idletime, busy), 30) minutes before the next check. When
-# an entity joins or leaves the mbus, the timer for the next check will be set
-# to one minute again.
-#
 # If no entity is against system shutdown the helper will check if important
 # programs are running. Some of this programs are defined in the helper, like
 # wget, encoder and burning applications. The user can add more programs using
-# the config variable SHUTDOWN_IMPORTANT_PROGRAMS. If you use the pc also for
+# the config variable important_programs. If you use the pc also for
 # daily work and you shut down X at the end, the windowmanager is a good
 # program to add here.
 #
@@ -43,7 +32,7 @@
 # the status, the system will shut down.
 #
 #
-# Note: There is a X idletime checker missing. If the user diesn't shut down
+# Note: There is a X idletime checker missing. If the user doesn't shut down
 # X and uses software suspend, there is no way to detect user activity on X.
 #
 # -----------------------------------------------------------------------------
@@ -78,32 +67,68 @@
 import popen2
 import time
 
-# mbus
-import mbus
+# insert freevo path information
+__site__ = '../lib/python%s.%s/site-packages' % sys.version_info[:2]
+__site__ = os.path.normpath(os.path.join(os.path.dirname(__file__), __site__))
+if not __site__ in sys.path:
+    sys.path.insert(0, __site__)
 
 # kaa imports
 import kaa.notifier
 
 # freevo core imports
 import freevo.ipc
-
-# freevo ui imports
-import config
+from freevo.conf import import Config, Var
 
 # get logging object
 log = logging.getLogger()
 
 # important programs that would prevent a shutdown when running. To add
-# programs to this list, use the config variable SHUTDOWN_IMPORTANT_PROGRAMS
-_important_programs = [ 'wget', 'cdrecord', 'growisofs', 'cdparanoia', 'lame',
-                        'mencoder', 'transcode', 'dvdauthor', 'mpeg2enc',
-                        'rsync', 'gcc', 'g++', 'make', 'scp', 'scp2', 'sftp',
-                        'ncftp', 'ftp', 'mkisofs' ]
-
-# internal variables
-POLL_INTERVALL = 60
-USER_IDLETIME  = 30
-APP_IDLETIME   = 30
+# programs to this list, use the config variable important_apps
+important_apps = [ 'wget', 'cdrecord', 'growisofs', 'cdparanoia', 'lame',
+                   'mencoder', 'transcode', 'dvdauthor', 'mpeg2enc',
+                   'rsync', 'gcc', 'g++', 'make', 'scp', 'scp2', 'sftp',
+                   'ncftp', 'ftp', 'mkisofs' ]
+
+
+# important_apps as text for config description. Format the list comma
+# seperated with 75 chars max.
+important_apps_t = []
+for app in important_apps:
+    if not important_apps_t or len(important_apps_t[-1]) + len(app) > 73:
+        important_apps_t.append('    %s' % app)
+    else:
+        important_apps_t[-1] += ', %s' % app
+
+
+# config object for this application
+config = Config(app='autoshutdown', desc=_('autoshutdown configuration'), 
schema=[
+
+    Var(name='wakeup_command', default='', desc=_('''
+    Command to call to schedule system wakeup. The program is a
+    string with %s as variable for the real wakeup time.
+    Example.: nvram-wakeup -C /etc/nvram-wakeup.conf --directisa -s %s''')),
+
+    Var(name='shutdown_command', default='shutdown -h now',
+        desc=_('Command to shutdown the system')),
+
+    Var(name='user_idletime', default=30, desc=_('''
+    Time in seconds for shells (local and remote) to be idle before shuting
+    down the system.''')),
+
+    Var(name='freevo_idletime', default=30,
+        desc=_('Minimum idletime other Freevo applications before shutdown.')),
+
+    Var(name='important_apps', default='', desc=_('''
+    Comma separated list of applications preventing shutdown. If one of the
+    applications are running, the system won\'t shut down. E.g. to prevent
+    shutdown when using X, set this variable to your windowmanager. The
+    following applications are already defined as important apps: \n%s
+    ''' % '\n'.join(important_apps_t) )),
+
+    ])
+
+
 
 class Shutdown(object):
     """
@@ -111,8 +136,7 @@
     """
     def __init__(self):
         # notifier check timer
-        self.timer = kaa.notifier.Timer(self.check_shutdown)
-        self.timer.start(POLL_INTERVALL)
+        kaa.notifier.Timer(self.check_shutdown).start(60)
         # counter how often the shutdown function warns before real
         # shutdown (31 == 30 minutes)
         self.shutdown_counter = 31
@@ -129,6 +153,11 @@
         # helper function to get all entities
         self.get_entities = mbus.get_entities
 
+        self.important_apps = important_apps[:]
+        for app in config.important_apps.split(','):
+            self.important_apps.append(app.strip())
+
+
 
     def status_change(self, entity):
         """
@@ -136,7 +165,7 @@
         """
         log.info('Entity %s: %s', entity, entity.status)
 
-        
+
     def check_programs(self):
         """
         Check for running programs who could prevent a shutdown.
@@ -146,8 +175,7 @@
                 cmdline = os.path.join('/proc/', id, 'cmdline')
                 f = open(cmdline)
                 cmd = os.path.basename(f.readline().split('\0')[0])
-                if cmd in _important_programs + \
-                       config.SHUTDOWN_IMPORTANT_PROGRAMS:
+                if cmd in self.important_apps:
                     log.info('%s is running, not ready for shutdown' % cmd)
                     return False
                 f.close()
@@ -184,7 +212,7 @@
         wakeup = 0
         idle = 1000
         busy = 0
-        
+
         for entity in self.get_entities():
             if entity.status.get('idle') != None:
                 idle = min(idle, entity.status.get('idle'))
@@ -194,8 +222,8 @@
                 wakeup = min(wakeup, entity.status.get('wakeup')) or \
                          entity.status.get('wakeup')
         return idle, busy, wakeup
-    
-    
+
+
     def check_shutdown(self):
         """
         Check if shutdown is possible and shutdown the system.
@@ -207,14 +235,14 @@
             # there is a new wakeup time
             log.info('Set wakeup time: %s' % wakeuptime)
             self.wakeuptime = wakeuptime
-            kaa.notifier.Process(config.SHUTDOWN_WAKEUP_CMD % 
wakeuptime).start()
+            kaa.notifier.Process(config.wakeup_command % wakeuptime).start()
 
         # check idletime
-        if idletime < APP_IDLETIME:
+        if idletime < config.freevo_idletime:
             log.info('Entity idletime is %s, continue' % idletime)
             self.shutdown_counter = max(self.shutdown_counter, 6)
             return True
-        
+
         # check busy time
         if busytime:
             log.info('Entity is busy at least %s minutes' % busytime)
@@ -227,7 +255,7 @@
             log.info('Wakeup time is in %s minutes, do not shutdown' % wakeup)
             self.shutdown_counter = max(self.shutdown_counter, 6)
             return True
-        
+
         # Wakeup seems possible based on the mbus entities, check important
         # programs next and wait 5 minutes if one is running
         if not self.check_programs():
@@ -241,7 +269,7 @@
             if idle < 1000:
                 # there is a login, print idle time for debug
                 log.info('user idle time: %s minutes' % idle)
-            if idle <= USER_IDLETIME:
+            if idle <= config.user_idletime:
                 self.shutdown_counter = max(self.shutdown_counter, 6)
                 return True
         except:
@@ -251,7 +279,7 @@
 
         # count down shutdown_counter
         self.shutdown_counter -= 1
-        
+
         if self.shutdown_counter:
             # let's warn this time
             log.warning('system shutdown system in %s minutes' % \
@@ -262,19 +290,18 @@
         log.warning('shutdown system')
 
         # shutdown the system
-        kaa.notifier.Process(config.SHUTDOWN_SYS_CMD).start()
+        kaa.notifier.Process(config.shutdown_command).start()
 
-        # set new timer in case we hibernate, set self.timer so it looks like
-        # the normal startup timer
+        # reset counter in case we hibernate
         self.shutdown_counter = 31
         return True
 
 
 
 def help():
-    print 'Freevo shutdown helper'
+    print 'Freevo Autoshutdown Application'
     print
-    print 'usage: freevo shutdown'
+    print 'usage: freevo-autoshutdown [ --help ]'
     print
     print 'This helper will auto shutdown the pc and schedule a wakeup timer.'
     print 'It is written for the use with the recordserver to power up the pc'
@@ -284,63 +311,19 @@
     print 'logins on the machine (idle time < 30 minutes will prevent system'
     print 'shutdown) and if important prograns are running.'
     print
-    if not hasattr(config, 'SHUTDOWN_IMPORTANT_PROGRAMS'):
-        print 'You need to set SHUTDOWN_IMPORTANT_PROGRAMS in local_conf.py to'
-        print 'define a list of programs which should prevent a shutdown. When'
-        print 'the machine is also used a workstation, the windowmanager is a'
-        print 'good program to add here.'
-        print
-        print 'Examples:'
-        print '# no special programs'
-        print 'SHUTDOWN_IMPORTANT_PROGRAMS = [ ]'
-        print '# sawfish window manager'
-        print 'SHUTDOWN_IMPORTANT_PROGRAMS = [ \'sawfish\' ]'
-        print
-        print 'Besides SHUTDOWN_IMPORTANT_PROGRAMS the following programs will'
-        print 'prevent a system shutdown:'
-        prg = _important_programs
-    else:
-        print 'The following programs will prevent system shutdown:'
-        prg = _important_programs + config.SHUTDOWN_IMPORTANT_PROGRAMS
-    prg = ' '.join(prg)
-    while len(prg) > 75:
-        line = prg[:prg[:70].rfind(' ')]
-        print line
-        prg = prg[len(line)+1:]
-    print prg
-    print
-    if not hasattr(config, 'SHUTDOWN_WAKEUP_CMD'):
-        print
-        print 'Set SHUTDOWN_WAKEUP_CMD in the local_conf.py to a program that'
-        print 'can schedule a wakeup. The program is a string with %s as'
-        print 'variable for the real wakeup time.'
-        print
-        print 'Example:'
-        print '# use nvram_wakeup:'
-        print 'SHUTDOWN_WAKEUP_CMD = \'/usr/local/bin/nvram-wakeup -C \' +'
-        print '    \'/etc/nvram-wakeup.conf --directisa -s %s'
-        print
+    print 'Please read the comments in the config file for more details.'
+    print 'Using config file %s' % config.get_filename()
     sys.exit(0)
 
 
 
+# load config and save again to make sure the file is created
+config.load()
+config.save()
 
 if len(sys.argv) > 1 and sys.argv[1] in ('-h', '--help'):
     help()
 
-if not hasattr(config, 'SHUTDOWN_WAKEUP_CMD'):
-    print 'Error: SHUTDOWN_WAKEUP_CMD not found.',
-    print 'Please read the configuration help.'
-    print
-    help()
-
-if not hasattr(config, 'SHUTDOWN_IMPORTANT_PROGRAMS'):
-    print 'Error: SHUTDOWN_IMPORTANT_PROGRAMS not found',
-    print 'Please read the configuration help.'
-    print
-    help()
-
-
 # the shutdown object
 s = Shutdown()
 

Added: trunk/apps/setup.py
==============================================================================
--- (empty file)
+++ trunk/apps/setup.py Sun Jan 29 19:48:37 2006
@@ -0,0 +1,43 @@
+# -*- coding: iso-8859-1 -*-
+# -----------------------------------------------------------------------------
+# setup.py - setup script for installing the freevo module
+# -----------------------------------------------------------------------------
+# $Id: setup.py 7806 2005-11-23 19:27:23Z dmeyer $
+#
+# -----------------------------------------------------------------------------
+# Freevo - A Home Theater PC framework
+# Copyright (C) 2002-2005 Krister Lagerstrom, Dirk Meyer, et al.
+#
+# First Edition: Dirk Meyer <[EMAIL PROTECTED]>
+# Maintainer:    Dirk Meyer <[EMAIL PROTECTED]>
+#
+# Please see the file doc/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
+#
+# -----------------------------------------------------------------------------
+
+from freevo.distribution import setup
+
+# now start the python magic
+setup (name         = 'freevo-apps',
+       module       = 'apps',
+       version      = '2.0',
+       description  = 'Freevo Helper Applications',
+       author       = 'Dirk Meyer, et al.',
+       author_email = '[email protected]',
+       url          = 'http://www.freevo.org',
+       license      = 'GPL',
+       )


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to