source: trunk/gui/scripts/tool.tcl @ 3186

Last change on this file since 3186 was 3186, checked in by mmc, 8 years ago

Added the Rappture::Logger facility and updated many (not all)
of the input/output controls to use it. This logs user activity
so you can see how they are interacting with the widgets during
the course of a session. If the RAPPTURE_LOG variable is set to
a directory (like /tmp), then Rappture creates a unique log file
in this directory for each tool session and writes logging info
into that file. The middleware ensures that the file is write-only
so that private activity information doesn't get out to people who
try to snoop around.

File size: 14.9 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: tool - represents an entire tool
3#
4#  This object represents an entire tool defined by Rappture.
5#  Each tool resides in an installation directory with other tool
6#  resources (libraries, examples, etc.).  Each tool is defined by
7#  its inputs and outputs, which are tied to various widgets in the
8#  GUI.  Each tool tracks the inputs, knows when they're changed,
9#  and knows how to run itself to produce new results.
10# ======================================================================
11#  AUTHOR:  Michael McLennan, Purdue University
12#  Copyright (c) 2004-2012  HUBzero Foundation, LLC
13#
14#  See the file "license.terms" for information on usage and
15#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
16# ======================================================================
17package require BLT
18
19itcl::class Rappture::Tool {
20    inherit Rappture::ControlOwner
21
22    constructor {xmlobj installdir args} {
23        Rappture::ControlOwner::constructor ""
24    } { # defined below }
25
26    destructor { # defined below }
27
28    public method installdir {} { return $_installdir }
29
30    public method run {args}
31    public method abort {}
32    public method reset {}
33
34    protected method _mkdir {dir}
35    protected method _output {data}
36
37    private variable _origxml ""     ;# copy of original XML (for reset)
38    private variable _installdir ""  ;# installation directory for this tool
39    private variable _outputcb ""    ;# callback for tool output
40    private common job               ;# array var used for blt::bgexec jobs
41    private common jobnum 0          ;# counter for unique job number
42
43    # get global resources for this tool session
44    public proc resources {{option ""}}
45
46    public common _resources
47    public proc setAppName {name}   { set _resources(-appname) $name }
48    public proc setHubName {name}   { set _resources(-hubname) $name }
49    public proc setHubURL {name}    { set _resources(-huburl) $name }
50    public proc setSession {name}   { set _resources(-session) $name }
51    public proc setJobPrt {name}    { set _resources(-jobprotocol) $name }
52    public proc setResultDir {name} { set _resources(-resultdir) $name }
53}
54
55# must use this name -- plugs into Rappture::resources::load
56proc tool_init_resources {} {
57    Rappture::resources::register \
58        application_name  Rappture::Tool::setAppName \
59        application_id    Rappture::Tool::setAppId \
60        hub_name          Rappture::Tool::setHubName \
61        hub_url           Rappture::Tool::setHubURL \
62        session_token     Rappture::Tool::setSession \
63        job_protocol      Rappture::Tool::setJobPrt \
64        results_directory Rappture::Tool::setResultDir
65}
66
67# ----------------------------------------------------------------------
68# CONSTRUCTOR
69# ----------------------------------------------------------------------
70itcl::body Rappture::Tool::constructor {xmlobj installdir args} {
71    if {![Rappture::library isvalid $xmlobj]} {
72        error "bad value \"$xmlobj\": should be Rappture::Library"
73    }
74    set _xmlobj $xmlobj
75
76    # stash a copy of the original XML for later "reset" operations
77    set _origxml [Rappture::LibraryObj ::#auto "<?xml version=\"1.0\"?><run/>"]
78    $_origxml copy "" from $_xmlobj ""
79
80    if {![file exists $installdir]} {
81        error "directory \"$installdir\" doesn't exist"
82    }
83    set _installdir $installdir
84
85    eval configure $args
86}
87
88# ----------------------------------------------------------------------
89# DESTRUCTOR
90# ----------------------------------------------------------------------
91itcl::body Rappture::Tool::destructor {} {
92    itcl::delete object $_origxml
93}
94
95# ----------------------------------------------------------------------
96# USAGE: resources ?-option?
97#
98# Clients use this to query information about the tool.
99# ----------------------------------------------------------------------
100itcl::body Rappture::Tool::resources {{option ""}} {
101    if {$option == ""} {
102        return [array get _resources]
103    }
104    if {[info exists _resources($option)]} {
105        return $_resources($option)
106    }
107    return ""
108}
109
110# ----------------------------------------------------------------------
111# USAGE: run ?<path1> <value1> <path2> <value2> ...? ?-output <callbk>?
112#
113# This method causes the tool to run.  All widgets are synchronized
114# to the current XML representation, and a "driver.xml" file is
115# created as the input for the run.  That file is fed to the tool
116# according to the <tool><command> string, and the job is executed.
117#
118# Any "<path> <value>" arguments are used to override the current
119# settings from the GUI.  This is useful, for example, when filling
120# in missing simulation results from the analyzer.
121#
122# If the -output argument is included, then the next arg is a
123# callback command for output messages.  Any output that comes in
124# while the tool is running is sent back to the caller, so the user
125# can see progress running the tool.
126#
127# Returns a list of the form {status result}, where status is an
128# integer status code (0=success) and result is the output from the
129# simulator.  Successful output is something like {0 run1293921.xml},
130# where 0=success and run1293921.xml is the name of the file containing
131# results.
132# ----------------------------------------------------------------------
133itcl::body Rappture::Tool::run {args} {
134    global errorInfo
135
136    #
137    # Make sure that we save the proper application name.
138    # Actually, the best place to get this information is
139    # straight from the "installtool" script, but just in
140    # case we have an older tool, we should insert the
141    # tool name from the resources config file.
142    #
143    if {[info exists _resources(-appname)]
144          && "" != $_resources(-appname)
145          && "" == [$_xmlobj get tool.name]} {
146        $_xmlobj put tool.name $_resources(-appname)
147    }
148
149    # sync all widgets to the XML tree
150    sync
151
152    # if there are any args, use them to override parameters
153    set _outputcb ""
154    foreach {path val} $args {
155        if {$path == "-output"} {
156            set _outputcb $val
157        } else {
158            $_xmlobj put $path.current $val
159        }
160    }
161
162    foreach item {control output error} { set job($item) "" }
163
164    # write out the driver.xml file for the tool
165    set file "driver[pid].xml"
166    set status [catch {
167        set fid [open $file w]
168        puts $fid "<?xml version=\"1.0\"?>"
169        puts $fid [$_xmlobj xml]
170        close $fid
171    } result]
172
173    # set limits for cpu time
174    set limit [$_xmlobj get tool.limits.cputime]
175    if {"" == $limit || [catch {Rappture::rlimit set cputime $limit}]} {
176        Rappture::rlimit set cputime 900  ;# 15 mins by default
177    }
178
179    # execute the tool using the path from the tool description
180    if {$status == 0} {
181        set cmd [$_xmlobj get tool.command]
182        regsub -all @tool $cmd $_installdir cmd
183        regsub -all @driver $cmd $file cmd
184        regsub -all {\\} $cmd {\\\\} cmd
185        set cmd [string trimleft $cmd " "]
186        if { $cmd == "" } {
187            puts stderr "cmd is empty"
188            return [list 1 "Command is empty.\n\nThere is no command specified by\n\n <command>\n </command>\n\nin the tool.xml file."]
189        }
190        # if job_protocol is "submit", then use use submit command
191        if {[resources -jobprotocol] == "submit"} {
192            set cmd [linsert $cmd 0 submit --local]
193        }
194
195        # starting job...
196        Rappture::Logger::log run started
197        Rappture::rusage mark
198
199        if {0 == [string compare -nocase -length 5 $cmd "ECHO "] } {
200            set status 0;
201            set job(output) [string range $cmd 5 end]
202        } else {
203            set status [catch {
204                eval blt::bgexec \
205                    ::Rappture::Tool::job(control) \
206                    -keepnewline yes \
207                    -killsignal SIGTERM \
208                    -onoutput [list [itcl::code $this _output]] \
209                    -output ::Rappture::Tool::job(output) \
210                    -error ::Rappture::Tool::job(error) \
211                    $cmd
212            } result]
213
214            if { $status != 0 } {
215                # We're here because the exec-ed program failed
216                if { $::Rappture::Tool::job(control) != "" } {
217                    foreach { token pid code mesg } \
218                        $::Rappture::Tool::job(control) break
219                    if { $token == "EXITED" } {
220                        # This means that the program exited normally but
221                        # returned a non-zero exitcode.  Consider this an
222                        # invalid result from the program.  Append the stderr
223                        # from the program to the message.
224                        set logmesg "Program finished: exit code is $code"
225                        set result "$logmesg\n\n$::Rappture::Tool::job(error)"
226                    } elseif { $token == "abort" }  {
227                        # The user pressed the abort button.
228                        set logmesg "Program terminated by user."
229                        set result "$logmesg\n\n$::Rappture::Tool::job(output)"
230                    } else {
231                        # Abnormal termination
232                        set logmesg "Abnormal program termination: $mesg"
233                        set result "$logmesg\n\n$::Rappture::Tool::job(output)"
234                    }
235                }
236                Rappture::Logger::log run failed [list $logmesg]
237                return [list $status $result]
238            }
239        }
240        # ...job is finished
241        array set times [Rappture::rusage measure]
242
243        if {[resources -jobprotocol] != "submit"} {
244            set id [$_xmlobj get tool.id]
245            set vers [$_xmlobj get tool.version.application.revision]
246            set simulation simulation
247            if { $id != "" && $vers != "" } {
248                set pid [pid]
249                set simulation ${pid}_${id}_r${vers}
250            }
251            puts stderr "MiddlewareTime: job=[incr jobnum] event=$simulation start=$times(start) walltime=$times(walltime) cputime=$times(cputime) status=$status"
252
253            #
254            # Scan through stderr channel and look for statements that
255            # represent grid jobs that were executed.  The statements
256            # look like this:
257            #
258            # MiddlewareTime: job=1 event=simulation start=3.001094 ...
259            #
260            set subjobs 0
261            while {[regexp -indices {(^|\n)MiddlewareTime:( +[a-z]+=[^ \n]+)+(\n|$)} $job(error) match]} {
262                foreach {p0 p1} $match break
263                if {[string index $job(error) $p0] == "\n"} { incr p0 }
264
265                catch {unset data}
266                array set data {
267                    job 1
268                    event simulation
269                    start 0
270                    walltime 0
271                    cputime 0
272                    status 0
273                }
274                foreach arg [lrange [string range $job(error) $p0 $p1] 1 end] {
275                    foreach {key val} [split $arg =] break
276                    set data($key) $val
277                }
278                set data(job) [expr {$jobnum+$data(job)}]
279                set data(event) "subsimulation"
280                set data(start) [expr {$times(start)+$data(start)}]
281
282                set stmt "MiddlewareTime:"
283                foreach key {job event start walltime cputime status} {
284                    # add required keys in a particular order
285                    append stmt " $key=$data($key)"
286                    unset data($key)
287                }
288                foreach key [array names data] {
289                    # add anything else that the client gave -- venue, etc.
290                    append stmt " $key=$data($key)"
291                }
292                puts stderr $stmt
293                incr subjobs
294
295                # done -- remove this statement
296                set job(error) [string replace $job(error) $p0 $p1]
297            }
298            incr jobnum $subjobs
299        }
300
301    } else {
302        set job(error) "$result\n$errorInfo"
303    }
304    if {$status == 0} {
305        file delete -force -- $file
306    }
307
308    # see if the job was aborted
309    if {[regexp {^KILLED} $job(control)]} {
310        Rappture::Logger::log run aborted
311        return [list 0 "ABORT"]
312    }
313
314    #
315    # If successful, return the output, which should include
316    # a reference to the run.xml file containing results.
317    #
318    if {$status == 0} {
319        set result [string trim $job(output)]
320        if {[regexp {=RAPPTURE-RUN=>([^\n]+)} $result match file]} {
321            set status [catch {Rappture::library $file} result]
322            if {$status != 0} {
323                global errorInfo
324                set result "$result\n$errorInfo"
325            }
326
327            # if there's a results_directory defined in the resources
328            # file, then move the run.xml file there for storage
329            if {[info exists _resources(-resultdir)]
330                  && "" != $_resources(-resultdir)} {
331                catch {
332                    if {![file exists $_resources(-resultdir)]} {
333                        _mkdir $_resources(-resultdir)
334                    }
335                    file rename -force -- $file $_resources(-resultdir)
336                }
337            }
338        } else {
339            set status 1
340            set result "Can't find result file in output.\nDid you call Rappture
341::result in your simulator?"
342        }
343    } elseif {$job(output) ne "" || $job(error) ne ""} {
344        set result [string trim "$job(output)\n$job(error)"]
345    }
346
347    # log final status for the run
348    if {$status == 0} {
349        Rappture::Logger::log run finished
350    } else {
351        Rappture::Logger::log run failed [list $result]
352    }
353
354    return [list $status $result]
355}
356
357# ----------------------------------------------------------------------
358# USAGE: _mkdir <directory>
359#
360# Used internally to create the <directory> in the file system.
361# The parent directory is also created, as needed.
362# ----------------------------------------------------------------------
363itcl::body Rappture::Tool::_mkdir {dir} {
364    set parent [file dirname $dir]
365    if {"." != $parent && "/" != $parent} {
366        if {![file exists $parent]} {
367            _mkdir $parent
368        }
369    }
370    file mkdir $dir
371}
372
373
374# ----------------------------------------------------------------------
375# USAGE: abort
376#
377# Clients use this during a "run" to abort the current job.
378# Kills the job and forces the "run" method to return.
379# ----------------------------------------------------------------------
380itcl::body Rappture::Tool::abort {} {
381    Rappture::Logger::log run abort
382    set job(control) "abort"
383}
384
385# ----------------------------------------------------------------------
386# USAGE: reset
387#
388# Resets all input values to their defaults.  Sometimes used just
389# before a run to reset to a clean state.
390# ----------------------------------------------------------------------
391itcl::body Rappture::Tool::reset {} {
392    $_xmlobj copy "" from $_origxml ""
393    foreach path [Rappture::entities -as path $_xmlobj input] {
394        if {[$_xmlobj element -as type $path.default] ne ""} {
395            set defval [$_xmlobj get $path.default]
396            $_xmlobj put $path.current $defval
397        }
398    }
399}
400
401# ----------------------------------------------------------------------
402# USAGE: _output <data>
403#
404# Used internally to send each bit of output <data> coming from the
405# tool onto the caller, so the user can see progress.
406# ----------------------------------------------------------------------
407itcl::body Rappture::Tool::_output {data} {
408    if {[string length $_outputcb] > 0} {
409        uplevel #0 $_outputcb [list $data]
410    }
411}
Note: See TracBrowser for help on using the repository browser.