Python Skeleton Scripts

From PeformIQ Upgrade
Revision as of 09:27, 1 May 2008 by PeterHarding (talk | contribs) (New page: =Based on a server= <pre> #!/usr/bin/env python # # Purpose: # # $Id:$ # #------------------------------------------------------------------------------- """ Skeleton Python script...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Based on a server

#!/usr/bin/env python
#
#  Purpose: 
#
#  $Id:$
#
#-------------------------------------------------------------------------------

"""
  Skeleton Python script

  ...
"""
#-------------------------------------------------------------------------------

import os
import sys
import getopt
import logging

#-------------------------------------------------------------------------------

from datetime import datetime

#-------------------------------------------------------------------------------

__version__   = "1.0.0"
__id__        = "@(#)  skel.py  [%s]  05/03/2008"

verbose_flg   = False

debug_level   = 0

LOGFILE       = "xxxx.log"
PIDFILE       = "xxxx.pid"

tables        = []

log           = None
pid           = None

#===============================================================================

def INFO(msg):
   if log: log.info(' ' + msg)
   if verbose_flg: print "[dserver]  %s" % msg

#-------------------------------------------------------------------------------

def ERROR(msg):
   if log: log.error(msg)
   sys.stderr.write('[dserver]  %s\n' % msg)

#-------------------------------------------------------------------------------

def WARNING(msg):
   if log: log.warning('*****' + msg + '*****')
   if verbose_flg: print "[dserver]  %s" % msg

#===============================================================================

def init():
   global log
   global pid

   pid = os.getpid()

   log  = logging.getLogger('xxx')
   hdlr = logging.FileHandler(LOGFILE)
   fmtr = logging.Formatter('%(asctime)s %(levelname)s %(message)s')

   hdlr.setFormatter(fmtr)
   log.addHandler(hdlr)
   log.setLevel(logging.INFO)

   INFO("Started processing")

   if (not verbose_flg):
      INFO("PID is %d" % pid)

#===============================================================================

def main():
   global verbose_flg
   global debug_level

   try:
      opts, args = getopt.getopt(sys.argv[1:], "dD:vVw?")
   except getopt.error, msg:
      print __doc__
      return 1

   try:
      terminal_type = os.environ["TERM"]
   except KeyError, e:
      print "Set TERM environment variable and rerun!"
      return 1

   wrk_path  = os.getcwd()
   wrk_dir   = os.path.basename(wrk_path)

   data_dir = wrk_dir  + '/DATA/'
   pid_path = data_dir + PIDFILE

   # os.chdir(data_dir)

   for o, a in opts:
      if o == '-d':
         debug_level   += 1
      elif o == '-D':
         debug_level    = int(a)
      elif o == '-v':
         verbose_flg    = True
      elif o == '-V':
         print "[xxxx]  Version: %s" % __version__
         return 1
      elif o == '-?':
         print __doc__
         return 1

   print "[xxxx]  Working directory is %s" % os.getcwd()

   if (debug_level > 0): print "Debugging level set to %d" % debug_level

   if args:
      for arg in args:
         print arg

   init()

   return 0

#-------------------------------------------------------------------------------

if __name__ == '__main__' or __name__ == sys.argv[0]:
   try:
      sys.exit(main())
   except KeyboardInterrupt, e:
      print "[xxxx]  Interrupted!"

#-------------------------------------------------------------------------------

"""
Revision History:

     Date     Who   Description
   --------   ---   ------------------------------------------------------------
   20031014   plh   Initial implementation

Problems to fix:

To Do:

Issues:

"""