1 | #!/usr/bin/env python |
---|
2 | # ---------------------------------------------------------------------- |
---|
3 | # rerun |
---|
4 | # |
---|
5 | # script to view the output of a previously created run.xml file |
---|
6 | # rerunning the simulation with new data is outside of the scope |
---|
7 | # of this script. it is currently only for viewing the output |
---|
8 | # of a previously created run.xml file. |
---|
9 | # |
---|
10 | # |
---|
11 | # RUN AS FOLLOWS: |
---|
12 | # rerun [-h | -d rappture_path] <runFile> |
---|
13 | # |
---|
14 | # -h | --help - print the help menu |
---|
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 |
---|
18 | # not in your PATH environment |
---|
19 | # variable. |
---|
20 | # |
---|
21 | # <runFile> - the run.xml file containing an output |
---|
22 | # section you would like to re-generate. |
---|
23 | # |
---|
24 | # ====================================================================== |
---|
25 | # AUTHOR: Derrick Kearney, Purdue University |
---|
26 | # Copyright (c) 2004-2008 Purdue Research Foundation |
---|
27 | # |
---|
28 | # See the file "license.terms" for information on usage and |
---|
29 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
30 | # ====================================================================== |
---|
31 | # |
---|
32 | # |
---|
33 | |
---|
34 | import getopt, shutil, re, os, sys, tempfile |
---|
35 | |
---|
36 | def help(): |
---|
37 | return """rerun [-h | -d rappture_path] runFile |
---|
38 | Visualize the output from a run.xml. This tool does not resimulate. |
---|
39 | |
---|
40 | -h | --help - print the help menu |
---|
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 |
---|
44 | not in your PATH environment |
---|
45 | variable. |
---|
46 | |
---|
47 | <runFile> - the run.xml file containing an output |
---|
48 | section you would like to re-generate. |
---|
49 | """ |
---|
50 | |
---|
51 | def main(argv=None): |
---|
52 | |
---|
53 | if argv is None: |
---|
54 | argv = sys.argv |
---|
55 | |
---|
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 |
---|
60 | |
---|
61 | longOpts = ["driver=","help"] |
---|
62 | shortOpts = "d:h" |
---|
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 |
---|
69 | |
---|
70 | driverPath = 'rappture' |
---|
71 | |
---|
72 | # match options |
---|
73 | for o, v in opts: |
---|
74 | if o in ("-d", "--driver"): |
---|
75 | driverPath = v |
---|
76 | elif o in ("-h", "--help"): |
---|
77 | print >>sys.stderr, help() |
---|
78 | return 2 |
---|
79 | |
---|
80 | runFile = args[0] |
---|
81 | |
---|
82 | # look for the text <command> (any characters) </command> |
---|
83 | re_command = re.compile(r'<command>.*</command>', re.I|re.DOTALL) |
---|
84 | |
---|
85 | try: |
---|
86 | xmlfile = open(runFile, "rb").read() |
---|
87 | except IOError, msg: |
---|
88 | print >>sys.stderr, msg |
---|
89 | return 2 |
---|
90 | |
---|
91 | myTmpFilefd, myTmpFileName = tempfile.mkstemp() |
---|
92 | myTmpFile = os.fdopen(myTmpFilefd,'w+b') |
---|
93 | |
---|
94 | outCmd = '<command>echo =RAPPTURE-RUN=>' \ |
---|
95 | + myTmpFileName.encode('string_escape') \ |
---|
96 | + '</command>' |
---|
97 | |
---|
98 | |
---|
99 | # do the file <command> switheroo! |
---|
100 | 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/>)|(<current>\s*</current>)',re.I|re.DOTALL) |
---|
107 | outText = re_current.sub('',outText) |
---|
108 | |
---|
109 | myTmpFile.write(outText) |
---|
110 | myTmpFile.flush() |
---|
111 | |
---|
112 | # run driver on the temporary run file |
---|
113 | systemCmd = '%s -tool %s' % (driverPath, myTmpFileName) |
---|
114 | #FIXME: do we really need to shell out? Rappture.tools.getCommandOutput()? |
---|
115 | os.system(systemCmd) |
---|
116 | |
---|
117 | # presto-change-o |
---|
118 | |
---|
119 | # clean up our tmp file |
---|
120 | # tempfile module does not auto delete files created by mkstemp on close |
---|
121 | myTmpFile.close() |
---|
122 | if os.path.exists(myTmpFileName): |
---|
123 | # python2.3 seems to have a bug where the tempfile |
---|
124 | # is automatically removed. this is fixed in python2.5 |
---|
125 | os.remove(myTmpFileName) |
---|
126 | |
---|
127 | return 0 |
---|
128 | |
---|
129 | if __name__ == '__main__' : |
---|
130 | # call main and exit gracefully |
---|
131 | sys.exit(main()) |
---|