Skip to content
Snippets Groups Projects
call_ioc_ins 5.29 KiB
Newer Older
maden's avatar
maden committed
#! /usr/bin/env python
#
#  $Source: /cvs/G/DRV/misc/App/scripts/call_ioc_ins,v $
maden's avatar
maden committed
#  $Revision: 1.7 $   $Date: 2004/10/07 12:08:26 $
#
#   Obsolete Obsolete Obsolete Obsolete Obsolete Obsolete Obsolete Obsolete
#
#     This script is obsolete. It may be of some interest as an example 
#     of URL access from python.
# 
#   Obsolete Obsolete Obsolete Obsolete Obsolete Obsolete Obsolete Obsolete

maden's avatar
maden committed
'''
  Insert boot information about IOCs into the ssrm_public Oracle database.

  Usage:
  -----
maden's avatar
maden committed
    call_ioc_ins [--help] [-v] [--debug] [-t=<secs>] \\
maden's avatar
maden committed
          <system>     <ipadd>      <procnum>    \\
          <device>     <bootpc>     <slsbase>    \\
          <bootfile>   <script>     <vxworks>    \\
          <epicsver>   <vxworksver> <ethaddr>
maden's avatar
maden committed

               Default time-out (-t) = 10 secs.
maden's avatar
maden committed
  Example:
  -------
     call_ioc_ins X04SA-VME-PLD 172.19.151.29 0       \\
                  dc            pc3018        /work   \\
                  /ioc/X04SA-VME-PLD/vxWorks          \\
                  /ioc/X04SA-VME-PLD/startup.script   \\
                  /work/epics/base/bin/mv2306/vxWorks \\
                  3.13.2        5.3.1         08:00:3e:2e:78:1d
'''
#--------------------------------------------------------------------

maden's avatar
maden committed
import    os, sys
import    commands
maden's avatar
maden committed
import    urllib
import    string
import    getopt
import    time
maden's avatar
maden committed
import    signal
maden's avatar
maden committed
#--------------------------------------------------------------------

def showVersion ():
#   ===========
  '''
  $Source: /cvs/G/DRV/misc/App/scripts/call_ioc_ins,v $
maden's avatar
maden committed
  $Revision: 1.7 $   $Date: 2004/10/07 12:08:26 $
maden's avatar
maden committed
  Installed Location: $SLSBASE/sls/bin
  '''
  print showVersion.__doc__
  return
#---------------------------------------------------------------------------

def showUsage ():
#   =========

  print __doc__
  return

#---------------------------------------------------------------------------
#                           The program starts here!
if __name__ == "__main__":

maden's avatar
maden committed
  sys.stderr = sys.stdout
maden's avatar
maden committed
                      #
                      # Analyse the options
                      #
maden's avatar
maden committed
  debug   = 0
  timeout = 10
  try:
    (opts, items) = getopt.getopt (sys.argv[1:], "h?vt:", \
                              ("help", "debug"))
maden's avatar
maden committed
    for opt in opts:
      if opt[0] == "-h":       raise "Help"
      if opt[0] == "-?":       raise "Help"
      if opt[0] == "--help":   raise "Help"
      if opt[0] == "-v":       raise "Version"
maden's avatar
maden committed
      if opt[0] == "-t":
        timeout = int (opt[1])
        if timeout <= 0:
          print "\aTime-out must be a positive integer."
          raise
        #endif
      #endif
      if opt[0] == "--debug": debug = 1
maden's avatar
maden committed
    #endfor
maden's avatar
maden committed
  except "Help":
    showUsage ()
    sys.exit (0)
  except "Version":
    showVersion ()
    sys.exit (0)
  except:
    print "\aBad option. Specify \"-h\" for help."
    sys.exit (1)
  #endtry

  if debug:
    print "Time-out = %d secs" % timeout
    print "Arguments:"
    for i in range (len (items)):
      print "     ", items[i]
    #endfor
  #endif
maden's avatar
maden committed

maden's avatar
maden committed
  if len (items) != 12:
    print "\aTwelve arguments are needed, not %d" % len (items)
maden's avatar
maden committed
    sys.exit (1)
  #endif

  args = {}
  args["SYSTEM"]     = items[ 0]
  args["IPADDR"]     = items[ 1]
  args["PROCNUM"]    = items[ 2]
  args["DEVICE"]     = items[ 3]
  args["BOOTPC"]     = items[ 4]
  args["SLSBASE"]    = items[ 5]
  args["BOOTFILE"]   = items[ 6]
  args["SCRIPT"]     = items[ 7]
  args["VXWORKS"]    = items[ 8]
  args["EPICSVER"]   = items[ 9]
  args["VXWORKSVER"] = items[10]
  args["ETHADDR"]    = items[11]

  encArgs = urllib.urlencode (args)

  url = "http://pc4860.psi.ch/testplan/IOC_INFOS/ioc_boot_ins.php?" + encArgs
  if debug:
    print "The URL is \"%s\"" % url
    print "Forking child process to do the work ..."
  #endif
  childPID = os.fork ()
  if childPID == 0:
maden's avatar
maden committed
    try:
      ufo = urllib.urlopen (url)     # Query the database
      lines = ufo.readlines ()       # Get the result
      ufo.close ()
    except:
      print "Error inserting data into database!"
maden's avatar
maden committed
      raise
    #endtry
    sys.exit (0)
maden's avatar
maden committed
  else:
    if debug: print "Child's pid = %d" % childPID
    for i in range (int (timeout)):
      time.sleep (1)
      (pid, status) = os.waitpid (childPID, os.WNOHANG)
      if debug: print "waitpid return status = (%d, %d)" % (pid, status)
      if pid == childPID:
        if debug: print "Child has exited."
        sys.exit (0)
      #endif
    #endfor
    os.kill (childPID, signal.SIGKILL)
    print "Time-out inserting data into database!"
maden's avatar
maden committed
  #endif
maden's avatar
maden committed
#endif

#--------------------------------------------------#
# emacs setup - force text mode to prevent emacs   #
#               from helping with the indentation! #
# Local Variables:                                 #
# mode:text                                        #
# indent-tabs-mode:nil                             #
# End:                                             #
#--------------------------------------------------#
#
#  $Log: call_ioc_ins,v $
maden's avatar
maden committed
#  Revision 1.7  2004/10/07 12:08:26  maden
#  Mark this file as obsolete
#
#  Revision 1.6  2004/07/26 14:27:13  maden
#  No longer python2 dependent (there is no python2 on boot nodes yet).
#
maden's avatar
maden committed
#  Revision 1.5  2004/07/26 11:39:38  maden
#  Add time-out
#
#  Revision 1.4  2004/07/26 09:33:25  maden
#  Disable database update ... server is in trouble.
#
#
maden's avatar
maden committed
#---------------------------------------------- End of $RCSfile: call_ioc_ins,v $