source: branches/1.5/apps/execute.tcl @ 5901

Last change on this file since 5901 was 5901, checked in by gah, 9 years ago

first pass on new build

  • Property svn:executable set to *
File size: 6.1 KB
Line 
1#! /bin/sh
2# ----------------------------------------------------------------------
3#  RAPPTURE PROGRAM EXECUTION
4#
5#  This script implements the -execute option for Rappture.  It
6#  provides a way to run a specific Rappture simulation without
7#  invoking the Rappture GUI.  Instead, it takes a driver file with
8#  the required parameters, submits that for execution, and then
9#  returns the run.xml file.  If the -tool file is specified, then
10#  it double-checks the driver against the tool to make sure that
11#  the requested tool and version are compatible.
12#
13#  This is normally invoked by launcher.tcl, so it expects $driverxml
14#  and $toolxml variable to be already set.
15#
16#  USAGE: execute.tcl
17# ======================================================================
18#  AUTHORS:  Michael McLennan, Purdue University
19#  Copyright (c) 2004-2012  HUBzero Foundation, LLC
20#
21#  See the file "license.terms" for information on usage and
22#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
23# ======================================================================
24#\
25exec tclsh "$0" $*
26# ----------------------------------------------------------------------
27# tclsh executes everything from here on...
28
29# bring in the Rappture object system
30package require Rappture
31Rappture::objects::init
32Rappture::resources::load
33
34# Parse command line options
35# ----------------------------------------------------------------------
36Rappture::getopts argv params {
37    value -tool ""
38    value -status ""
39    value -output ""
40    value -cleanup no
41}
42
43# load the XML info in the driver file
44if {[catch {Rappture::library $driverxml} result]} {
45    puts stderr "ERROR while loading driver file \"$driverxml\""
46    puts stderr $result
47    exit 1
48}
49set driverobj $result
50
51# If tool.xml is not specified, try to find it the way Rappture would.
52if {$params(-tool) ne ""} {
53    set toolxml $params(-tool)
54}
55if {$toolxml eq ""} {
56    if {[file isfile tool.xml]} {
57        set toolxml [file normalize tool.xml]
58    } elseif {[file isfile [file join rappture tool.xml]]} {
59        set toolxml [file normalize [file join rappture tool.xml]]
60    }
61}
62
63# If there's still no tool.xml, then see if we can find tooldir in driver
64if {$toolxml eq ""} {
65    set tooldir [$driverobj get tool.version.application.directory(tool)]
66    if {$tooldir eq ""} {
67        puts stderr "ERROR: missing -tool option, and driver file doesn't contain sufficient detail to locate the desired tool."
68        exit 1
69    }
70
71    set toolxml [file join $tooldir tool.xml]
72    if {![file exists $toolxml]} {
73        puts stderr "ERROR: missing tool.xml file \"$toolxml\""
74        exit 1
75    }
76}
77
78set installdir [file dirname [file normalize $toolxml]]
79set toolobj [Rappture::library $toolxml]
80set TaskObj [Rappture::Task ::#auto $toolobj $installdir]
81set LogFid ""
82
83# Define some things that we need for logging status...
84# ----------------------------------------------------------------------
85proc log_output {message} {
86    global LogFid
87
88    if {$LogFid ne ""} {
89        #
90        # Scan through and pick out any =RAPPTURE-PROGRESS=> messages.
91        #
92        set percent ""
93        while {[regexp -indices \
94                {=RAPPTURE-PROGRESS=> *([-+]?[0-9]+) +([^\n]*)(\n|$)} $message \
95                 match percent mesg]} {
96
97            foreach {i0 i1} $percent break
98            set percent [string range $message $i0 $i1]
99
100            foreach {i0 i1} $mesg break
101            set mesg [string range $message $i0 $i1]
102
103            foreach {i0 i1} $match break
104            set message [string replace $message $i0 $i1]
105        }
106        if {$percent ne ""} {
107            # report the last percent progress found
108            log_append progress "$percent% - $mesg"
109        }
110    }
111}
112
113# Actually write to the log file
114proc log_append {level message} {
115    global LogFid
116
117    if {$LogFid ne ""} {
118        set date [clock format [clock seconds] -format {%Y-%m-%dT%H:%M:%S%z}]
119        set host [info hostname]
120        puts $LogFid "$date $host rappture [pid] \[$level\] $message"
121        flush $LogFid
122    }
123}
124
125# Actually write to the log file
126proc log_stats {args} {
127    set line ""
128    foreach {key val} $args {
129        append line "$key=$val "
130    }
131    log_append usage $line
132}
133
134# Apply effects of all other command line options
135# ----------------------------------------------------------------------
136if {$params(-status) ne ""} {
137    set LogFid [open $params(-status) w]
138    $TaskObj configure -logger {log_append status} -jobstats log_stats
139}
140
141if {$params(-output) eq ""} {
142    # no output? then run quietly and don't try to save results
143    $TaskObj configure -jobstats "" -resultdir ""
144}
145
146# Transfer input values from driver to TaskObj, and then run.
147# ----------------------------------------------------------------------
148
149# copy inputs from the test into the run file
150$TaskObj reset
151foreach path [Rappture::entities -as path $driverobj input] {
152    if {[$driverobj element -as type $path.current] ne ""} {
153        lappend args $path [$driverobj get $path.current]
154    }
155}
156
157if {$params(-status) ne ""} {
158    # recording status? then look through output for progress messages
159    lappend args -output log_output
160}
161
162# run the desired case...
163foreach {status result} [eval $TaskObj run $args] break
164
165if {$status == 0 && [Rappture::library isvalid $result]} {
166    set runxml $result
167    $runxml put output.status ok
168} else {
169    # build a run file for the result output
170    set info "<?xml version=\"1.0\"?>\n[$driverobj xml]"
171    set runxml [Rappture::LibraryObj ::#auto $info]
172    $runxml put output.log $result
173    $runxml put output.status failed
174}
175
176# Handle output
177# ----------------------------------------------------------------------
178switch -- $params(-output) {
179    "" {
180        # no output file -- write to stdout
181        puts "<?xml version=\"1.0\"?>\n[$runxml xml]"
182    }
183    "@default" {
184        # do the usual Rappture thing -- move to results dir
185        # but ignore any errors if it fails
186        catch {$TaskObj save $runxml}
187    }
188    default {
189        # save to the specified file
190        $TaskObj save $runxml $params(-output)
191    }
192}
193
194if {$params(-cleanup)} {
195    file delete -force -- $driverxml
196}
197
198log_append status "exit $status"
199exit $status
Note: See TracBrowser for help on using the repository browser.