#!/usr/pkg/bin/python """determine if this computer is not being actively used just checks the first 12 ttys and mouse atimes and returns the amount of time since last accessed""" Copyright = """ quiescent -- how long the computer has been idle Copyright (C) 2004 John Comeau 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 MERCHANTABILITY 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., 675 Mass Ave, Cambridge, MA 02139, USA. """ errormessage = "Not all needed libraries found, upgrade or check path" try: True # not defined in older Python releases except: True, False = 1, 0 try: import sys, os, types, re, pwd sys.path.append(os.sep.join([pwd.getpwuid(os.geteuid())[5], 'lib', 'python'])) from com.jcomeau import gpl, jclicense except: try: sys.stderr.write("%s\n" % errormessage) except: print errormessage raise # get name this program was called as self = sys.argv[0].split(os.sep)[-1] command = self.split('.')[0] # chop any suffix (extension) # now get name we gave it when we wrote it originalself = Copyright.split()[0] # globals and routines that should be in every program # (yes, you could import them, but there are problems in that approach too) def DebugPrint(*whatever): return False # defined instead by pytest module, use that for debugging # other globals, specific to this program import time def minutes(): """return how many full minutes since last accessed via tty or mouse if 63 or over, return 63, as this allows other errorlevel ($?) bits to be used""" atimes = [0] # have something in the array in case all stats fail for tty in range(1, 13): try: atimes.append(os.stat("/dev/tty%d" % tty)[7]) except: DebugPrint("failed stat: /dev/tty%d" % tty) pass try: atimes.append(os.stat("/dev/mouse")[7]) except: DebugPrint("failed stat: /dev/mouse") pass DebugPrint('atimes: ', atimes, 'minimum: ', min(atimes)) minutes = int((time.time() - max(atimes)) / 60) DebugPrint('minutes quiescent: ', minutes) return min(63, minutes) if __name__ == '__main__': # if this program was imported by another, the above test will fail, # and this following code won't be used... sys.exit(minutes()) else: # if you want something to be done on import, do it here; otherwise pass pass