source: branches/multichoice/gui/apps/launcher.tcl @ 6371

Last change on this file since 6371 was 6371, checked in by dkearney, 8 years ago

merging changes from trunk into multichoice branch

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