source: trunk/gui/apps/launcher.tcl @ 4970

Last change on this file since 4970 was 4970, checked in by mmc, 6 years ago

Fixed auto-execution via TOOL_PARAMETERS to produce status output in a
file called rappture.status. This makes it easier for the web service
to monitor progress and know when everything is finished. Also, fixed
auto-execution to move the run file to the data/results directory and
clean up the original driver file.

  • Property svn:executable set to *
File size: 9.9 KB
Line 
1#!/bin/sh
2# -*- mode: Tcl -*-
3# ----------------------------------------------------------------------
4#  RAPPTURE LAUNCHER
5#
6#  This script is invoked by the "rappture" command.  It parses
7#  various input options and selects the proper main program to
8#  run depending on the function (build tool, run tool, tester, etc.).
9#
10#  RUN AS FOLLOWS:
11#    rappture ?-run? ?-tool <toolfile>?
12#    rappture -builder ?-tool <toolfile>?
13#    rappture -tester ?-tool <toolfile>? ?-testdir <directory>?
14#    rappture -execute driver.xml ?-tool <toolfile>?
15#
16#  The default option is "-run", which brings up the GUI used to
17#  run the tool.  If the <toolfile> is not specified, it defaults
18#  to "tool.xml" in the current working directory.
19#
20# ======================================================================
21#  AUTHOR:  Michael McLennan, Purdue University
22#  Copyright (c) 2004-2012  HUBzero Foundation, LLC
23#
24#  See the file "license.terms" for information on usage and
25#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
26# ======================================================================
27set mainscript ""
28set alist ""
29set loadlist ""
30set toolxml ""
31
32# ----------------------------------------------------------------------
33#  Look for parameters passed into the tool session.  If there are
34#  any "file" parameters, they indicate files that should be loaded
35#  for browsing or executed to get results:
36#
37#    file(load):/path/to/run.xml
38#    file(execute):/path/to/driver.xml
39# ----------------------------------------------------------------------
40set params(opt) ""
41set params(load) ""
42set params(execute) ""
43set params(input) ""
44
45if {[info exists env(TOOL_PARAMETERS)]} {
46    # if we can't find the file, wait a little
47    set ntries 25
48    while {$ntries > 0 && ![file exists $env(TOOL_PARAMETERS)]} {
49        after 200
50        incr ntries -1
51    }
52
53    if {![file exists $env(TOOL_PARAMETERS)]} {
54        # still no file after all that? then skip parameters
55        puts stderr "WARNING: can't read tool parameters in file \"$env(TOOL_PARAMETERS)\"\nFile not found."
56
57    } elseif {[catch {
58        # read the file and parse the contents
59        set fid [open $env(TOOL_PARAMETERS) r]
60        set info [read $fid]
61        close $fid
62    } result] != 0} {
63        puts stderr "WARNING: can't read tool parameters in file \"$env(TOOL_PARAMETERS)\"\n$result"
64
65    } else {
66        # parse the contents of the tool parameter file
67        foreach line [split $info \n] {
68            set line [string trim $line]
69            if {$line eq "" || [regexp {^#} $line]} {
70                continue
71            }
72
73            if {[regexp {^([a-zA-Z]+)(\(into\:)(.+)\)\:(.+)$} $line match type name path value]
74                || [regexp {^([a-zA-Z]+)(\([^)]+\))?\:(.+)} $line match type name value]} {
75                if {$type eq "file"} {
76                    switch -exact -- $name {
77                        "(load)" - "" {
78                            lappend params(load) $value
79                            set params(opt) "-load"
80                        }
81                        "(execute)" {
82                            set params(execute) $value
83                            set params(opt) "-execute"
84                        }
85                        "(input)" {
86                            set params(input) $value
87                            set params(opt) "-input"
88                        }
89                        "(into:" {
90                            namespace eval ::Rappture { # forward decl }
91                            set ::Rappture::parameters($path) $value
92                        }
93                        default {
94                            puts stderr "WARNING: directive $name not recognized for file parameter \"$value\""
95                        }
96                    }
97                }
98            }
99        }
100    }
101}
102
103# scan through the arguments and look for the function
104while {[llength $argv] > 0} {
105    set opt [lindex $argv 0]
106    set argv [lrange $argv 1 end]
107
108    if {[string index $opt 0] == "-"} {
109        switch -- $opt {
110            -run {
111                package require RapptureGUI
112                set guidir $RapptureGUI::library
113                set mainscript [file join $guidir scripts main.tcl]
114                set reqpkgs Tk
115            }
116            -builder {
117                package require RapptureBuilder
118                set blddir $RapptureBuilder::library
119                set mainscript [file join $blddir scripts main.tcl]
120                set reqpkgs Tk
121            }
122            -tester {
123                package require RapptureTester
124                set testdir $RapptureTester::library
125                set mainscript [file join $testdir scripts main.tcl]
126                set reqpkgs Tk
127            }
128            -execute {
129                # for web services and simulation cache -- don't load Tk
130                set reqpkgs ""
131                if {[llength $argv] < 1} {
132                    puts stderr "error: missing driver.xml file for -execute option"
133                    exit 1
134                }
135                set driverxml [lindex $argv 0]
136                set argv [lrange $argv 1 end]
137
138                if {![file readable $driverxml]} {
139                    puts stderr "error: driver file \"$driverxml\" not found"
140                    exit 1
141                }
142
143                set dir [file dirname [info script]]
144                set mainscript [file join $dir execute.tcl]
145            }
146            -tool {
147                set toolxml [lindex $argv 0]
148                set argv [lrange $argv 1 end]
149                if {![file exists $toolxml]} {
150                    puts stderr "file not found: $toolxml"
151                    exit 1
152                }
153                lappend alist -tool $toolxml
154            }
155            -testdir - -nosim {
156                lappend alist $opt [lindex $argv 0]
157                set argv [lrange $argv 1 end]
158            }
159            -auto {
160                # for the tester in automatic mode -- don't load Tk
161                package require RapptureTester
162                set testdir $RapptureTester::library
163                set mainscript [file join $testdir scripts auto.tcl]
164                set reqpkgs ""
165            }
166            -load {
167                while { [llength $argv] > 0 } {
168                    set val [lindex $argv 0]
169                    if { [string index $val 0] == "-" } {
170                        break
171                    }
172                    lappend loadlist $val
173                    set argv [lrange $argv 1 end]
174                }
175            }
176            default {
177                puts stderr "usage:"
178                puts stderr "  rappture ?-run? ?-tool toolFile? ?-nosim 0/1? ?-load file file ...?"
179                puts stderr "  rappture -builder ?-tool toolFile?"
180                puts stderr "  rappture -tester ?-auto? ?-tool toolFile? ?-testdir directory?"
181                puts stderr "  rappture -execute driver.xml ?-tool toolFile?"
182                exit 1
183            }
184        }
185    }
186}
187
188# If no arguments, check to see if there are any tool parameters.
189# If not, then assume that it's the -run option.
190if {$mainscript eq ""} {
191    switch -- $params(opt) {
192        -load {
193            # add tool parameters to the end of any files given on cmd line
194            set loadlist [concat $loadlist $params(load)]
195            set alist [concat $alist -load $loadlist]
196
197            package require RapptureGUI
198            set guidir $RapptureGUI::library
199            set mainscript [file join $guidir scripts main.tcl]
200            set reqpkgs Tk
201        }
202        -execute {
203            if {[llength $params(execute)] != 1} {
204                puts stderr "ERROR: wrong number of (execute) files in TOOL_PARAMETERS (should be only 1)"
205                exit 1
206            }
207            set driverxml [lindex $params(execute) 0]
208            if {![file readable $driverxml]} {
209                puts stderr "error: driver file \"$driverxml\" not found"
210                exit 1
211            }
212            set dir [file dirname [info script]]
213            set mainscript [file join $dir execute.tcl]
214            set reqpkgs ""
215
216            # When executing from TOOL_PARAMETERS file directives,
217            # report status, clean up, and save output to data/results.
218            # This helps the web services interface do its thing.
219            set alist [list \
220                -output @default \
221                -status rappture.status \
222                -cleanup yes]
223        }
224        "" - "-input" {
225            package require RapptureGUI
226            set guidir $RapptureGUI::library
227            set mainscript [file join $guidir scripts main.tcl]
228            set reqpkgs Tk
229
230            # finalize the -input argument for "rappture -run"
231            if {$params(input) ne ""} {
232                if {![file readable $params(input)]} {
233                    puts stderr "error: driver file \"$params(input)\" not found"
234                    exit 1
235                }
236                set alist [concat $alist -input $params(input)]
237            }
238
239            # finalize any pending -load arguments for "rappture -run"
240            if {[llength $loadlist] > 0} {
241                set alist [concat $alist -load $loadlist]
242            }
243        }
244        default {
245            puts stderr "internal error: funny action \"$params(opt)\" inferred from TOOL_PARAMETERS"
246            exit 1
247        }
248    }
249} else {
250    # finalize any pending -load arguments for "rappture -run"
251    if {[llength $loadlist] > 0} {
252        set alist [concat $alist -load $loadlist]
253    }
254}
255
256# Invoke the main program with the args
257
258# Note: We're sourcing the driver file "main.tcl" rather than exec-ing
259#       wish because we want to see stderr and stdout messages when they
260#       are written, rather than when the program completes.  It also
261#       eliminates one process waiting for the other to complete. If
262#       "exec" is needed, then the following could be replaced with
263#       blt::bgexec.  It doesn't try to redirect stderr into a file.
264set argv $alist
265foreach name $reqpkgs {
266    package require $name
267}
268source  $mainscript
Note: See TracBrowser for help on using the repository browser.