Changeset 892


Ignore:
Timestamp:
Feb 16, 2008, 1:27:34 PM (17 years ago)
Author:
dkearney
Message:

updated code to make it a little more user friendly by catching more exceptions, avoiding the pesky self terminating current tags (<current/>), and allowing the code to more easily be run from within a python interactive shell. not so randomly moved code around and updated domumentation.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gui/apps/rerun

    r626 r892  
    1010#
    1111#  RUN AS FOLLOWS:
    12 #    rerun [-h | -d driver_path] <run.xml-file>
     12#    rerun [-h | -d rappture_path] <runFile>
    1313#
    1414#      -h | --help                  - print the help menu
    15 #      -d | --driver driver_path    - specify the path to the driver
    16 #                                     program. You will need to specify
    17 #                                     the path if the driver program is
     15#      -d | --driver rappture_path  - specify the path to the rappture
     16#                                     command. You will need to specify
     17#                                     the path if the rappture command is
    1818#                                     not in your PATH environment
    1919#                                     variable.
    2020#
    21 #      <run.xml-file> - the run.xml file you containing an output
     21#      <runFile>      - the run.xml file containing an output
    2222#                       section you would like to re-generate.
    2323#
    2424# ======================================================================
    2525#  AUTHOR:  Derrick Kearney, Purdue University
    26 #  Copyright (c) 2004-2005  Purdue Research Foundation
     26#  Copyright (c) 2004-2008  Purdue Research Foundation
    2727#
    2828#  See the file "license.terms" for information on usage and
     
    3434import getopt, shutil, re, os, sys, tempfile
    3535
    36 def printHelp():
    37     print """rerun [-h | -d driver_path] runFile
     36def help():
     37    return """rerun [-h | -d rappture_path] runFile
     38      Visualize the output from a run.xml. This tool does not resimulate.
    3839
    3940      -h | --help                  - print the help menu
    40       -d | --driver driver_path    - specify the path to the driver
    41                                      program. You will need to specify
    42                                      the path if the driver program is
     41      -d | --driver rappture_path  - specify the path to the rappture
     42                                     command. You will need to specify
     43                                     the path if the rappture command is
    4344                                     not in your PATH environment
    4445                                     variable.
    4546
    46       <run.xml-file> - the run.xml file you containing an output
     47      <runFile>      - the run.xml file containing an output
    4748                       section you would like to re-generate.
    4849"""
    49     sys.exit()
    5050
     51def main(argv=None):
    5152
    52 if __name__ == '__main__' :
     53    if argv is None:
     54        argv = sys.argv
    5355
    54     if len(sys.argv) < 2:
    55         printHelp()
    56 
    57     if sys.argv[1] in ("-h", "--help"):
    58         printHelp()
     56    if len(argv) < 2:
     57        print >>sys.stderr, "%s requires at least 2 arguments" % (argv[0])
     58        print >>sys.stderr, help()
     59        return 2
    5960
    6061    longOpts = ["driver=","help"]
    6162    shortOpts = "d:h"
    62     opts, args = getopt.getopt(sys.argv[1:], shortOpts, longOpts)
     63    try:
     64        opts, args = getopt.getopt(argv[1:], shortOpts, longOpts)
     65    except getopt.GetoptError, msg:
     66        print >>sys.stderr, msg
     67        print >>sys.stderr, help()
     68        return 2
    6369
    6470    driverPath = 'rappture'
    65     runFile = args[0]
    6671
    6772    # match options
     
    7075            driverPath = v
    7176        elif o in ("-h", "--help"):
    72             printHelp()
     77            print >>sys.stderr, help()
     78            return 2
     79
     80    runFile = args[0]
    7381
    7482    # look for the text <command> (any characters) </command>
    7583    re_command = re.compile(r'<command>.*</command>', re.I|re.DOTALL)
    7684
    77     # prepare text variables
     85    try:
     86        xmlfile = open(runFile, "rb").read()
     87    except IOError, msg:
     88        print >>sys.stderr, msg
     89        return 2
     90
    7891    myTmpFilefd, myTmpFileName = tempfile.mkstemp()
    7992    myTmpFile = os.fdopen(myTmpFilefd,'w+b')
    80     outCmd = '<command>echo =RAPPTURE-RUN=>' + myTmpFileName.encode('string_escape') + '</command>'
     93
     94    outCmd =  '<command>echo =RAPPTURE-RUN=>' \
     95            + myTmpFileName.encode('string_escape') \
     96            + '</command>'
     97
     98
    8199    # do the file <command> switheroo!
    82     xmlfile = open(runFile, "rb").read()
    83100    outText = re_command.sub(outCmd,xmlfile)
     101
     102    # This should be fixed in rappture,
     103    # until it is fixed there, we do it here
     104    # look for the pesty self terminating current tags <current/>
     105    # do the file <current/> switheroo!
     106    re_current = re.compile(r'<current/>',re.I|re.DOTALL)
     107    outText = re_current.sub('',outText)
     108
    84109    myTmpFile.write(outText)
    85110    myTmpFile.flush()
    86111
    87112    # run driver on the temporary run file
    88     print "myTmpFileName = %s" % (myTmpFileName)
    89113    systemCmd = '%s -tool %s' % (driverPath, myTmpFileName)
     114    #FIXME: do we really need to shell out? Rappture.tools.getCommandOutput()?
    90115    os.system(systemCmd)
    91116
    92117    # presto-change-o
    93 
     118   
    94119    # clean up our tmp file
    95120    # tempfile module does not auto delete files created by mkstemp on close
     
    100125        os.remove(myTmpFileName)
    101126
    102     # exit gracefully
    103     sys.exit()
     127    return 0
     128
     129if __name__ == '__main__' :
     130    # call main and exit gracefully
     131    sys.exit(main())
Note: See TracChangeset for help on using the changeset viewer.