#!/usr/local/bin/tclsh # BioLog - Script to save data to BioCoRE from a Command Line # # http://www.ks.uiuc.edu/Research/biocore/dev/programs/biolog # # Use of this script indicates agreement with the BioCoRE # License ( http://www.ks.uiuc.edu/Research/biocore ) # # Author: Jim Phillips # set projectfile $env(HOME)/.biocoreProjects.tcl set sessionfile $env(HOME)/.biocore/.biocoreSession set backupbase $env(HOME)/biolog_save set apisource /usr/local/share/biocore/biocore.tcl #set apisource /home/kvandivo/biocore/externalAPI/tcl/biocore.tcl set tmpfile "/tmp/biolog_[pid].tmp" set biocoreHush 1 if [info exists env(EDITOR)] { set editor $env(EDITOR) } else { set editor vi } proc showHelp { } { global test_and_abort if $test_and_abort return global argv0 puts "usage: $argv0" puts { [-p ] [-s ] [-k ]... [-f] [-e|-i|]} puts { -f => do not ask before recording} puts { -e => input is an email message} puts { -i => log interactive shell} puts { -h => display usage information} puts -nonewline " $argv0" puts { -r => reload from saved backup file} } # parse command line arguments set parse_as_email 0 set confirm 1 set interactive 0 set test_and_abort 0 set keywords {} for { set i 0 } { $i < $argc } { incr i } { switch -- [lindex $argv $i] { -h { showHelp; set test_and_abort 1; } --help { showHelp; set test_and_abort 1; } -p { incr i; set project [lindex $argv $i]; } -s { incr i; set subject [lindex $argv $i]; } -r { incr i; set backupfile [lindex $argv $i]; } -k { incr i; lappend keywords [lindex $argv $i]; } -f { set confirm 0 } -e { set parse_as_email 1 } -i { set interactive 1 } default { set command_start $i; set i $argc; } } } if { [info exists command_start] } { for { set i $command_start } { $i < $argc } { incr i } { if { [info exists command] } { append command { }; }; append command [lindex $argv $i]; } } # source the API, optionally turning off information if { ! $confirm } { set biocoreHush 1 } source $apisource package require biocore 1.15 set headers {} append headers "DATE: [exec date]\n" append headers "HOST: [info hostname]\n" append headers "CWD: [pwd]\n" foreach k $keywords { append headers "KEYWORD: $k\n" } # source the projects list file if [catch "source $projectfile"] { showHelp puts stderr {} puts stderr "Error reading project data from $projectfile!" puts stderr {This file should contain at least "set Project(default)

"} puts stderr {where

is the id of the project you want entries logged to,} puts stderr {as given in "API Connection Information" linked from "Utilities".} puts stderr {You may define additional projects to use with the "-p" flag.} puts stderr {} puts stderr "The errors reading $projectfile were:" puts stderr $errorInfo set Project(ERROR_READING_PROJECT_FILE) -1 } if $test_and_abort { puts " defined projects: [lsort [array names Project]]" exit } proc saveBackup { } { global backupbase project subject headers data set datestr [clock format [clock seconds] -format %Y%m%d%H%M] set filename ${backupbase}.${datestr}.[pid] puts stderr "Saving backup of entry to \"$filename\"." lappend backup $project $subject $headers $data set file [open $filename w] puts $file $backup } proc loadBackup { filename } { global project subject headers data set backup [read [open $filename r]] set project [lindex $backup 0] set subject [lindex $backup 1] set headers [lindex $backup 2] set data [lindex $backup 3] } if { [info exists backupfile] } { if [catch {loadBackup $backupfile} cmderrs] { puts stderr "Error reading $backupfile. ($cmderrs)" exit } } if { ! [info exists ProjectDefault] } { set ProjectDefault default } if { ! [info exists project] } { set project $ProjectDefault } if [info exists Project($project)] { set ProjectID $Project($project) } else { puts stderr "unrecognized project key: $project" puts stderr "defined projects: [lsort [array names Project]]" set ProjectID -1; } if { ! [info exists backupfile] } { if { $interactive } { append headers "INTERACTIVE\n" if { $confirm } { puts "project $project, logging interactive shell, exit ends input..." } exec script $tmpfile <@ stdin >@ stdout 2>@ stderr set input [open $tmpfile r] append data [read $input] close $input file delete $tmpfile } elseif { [info exists command] } { append headers "COMMAND: $command\n" set input [open "|$command" r] append data [read $input] if [catch {close $input} cmderrs] { append data "*** ERRORS ***\n" append data $cmderrs } } else { if { $confirm } { puts "project $project, reading from stdin, control-D ends input..." } append data [read stdin] } if { $parse_as_email } { append headers "EMAIL\n" } # last chance to make sure subject is set if { ! [info exists subject] } { if { $interactive } { set subject {interactive} } elseif { $parse_as_email } { set lines [split $data "\n"] set email_from unknown foreach line $lines { if [regexp {^From: } $line] { set email_from [string range $line 6 end] break } } set email_subject unknown foreach line $lines { if [regexp {^Subject: } $line] { set email_subject [string range $line 9 end] break } } set subject "$email_subject ($email_from)" } elseif { [info exists command] } { set subject $command } else { set subject {comment} } } } proc editString { datavar multiline } { global tmpfile editor upvar $datavar data puts "using $editor $tmpfile" set tmp [open $tmpfile w] puts -nonewline $tmp $data close $tmp exec $editor $tmpfile <@ stdin >@ stdout 2>@ stderr set input [open $tmpfile r] set data {} if $multiline { append data [read $input] } else { append data [gets $input] } close $input file delete $tmpfile } if { $confirm } { set confirmed 0 while { ! $confirmed } { puts {========} puts -nonewline $data puts {========} puts "PROJECT: $project" puts "SUBJECT: $subject" puts -nonewline $headers puts {========} puts {Record this text? ([y]es/[e]dit/[s]ubject/[h]eaders/[a]bort)} set c [gets stdin] if [eof stdin] { puts {EOF detected on input, assuming yes.} set confirmed 1 } switch -- $c { y { set confirmed 1 } e { editString data 1 } s { editString subject 0 } h { editString headers 1 } a { puts {Aborting}; saveBackup; exit } } } } set entrytext $headers append entrytext "========\n" append entrytext $data ::biocore::NotebookEntry ne ::biocore::ne configure Title $subject ::biocore::ne configure Description $headers ::biocore::ne configure Type 4 ::biocore::ne configure Text $entrytext if { $ProjectID < 0 } { puts stderr "unrecognized project key: $project" puts stderr "defined projects: [lsort [array names Project]]" puts stderr "Aborting due to bad project key." saveBackup exit } set retval -1 while { $retval < 0 } { set anyErrors [::biocore::init $sessionfile] if { [string length $anyErrors] > 0} { puts stderr $anyErrors } set ::biocore::Project $ProjectID set cmderrs {} if { [catch {set retval [::biocore::saveNotebookEntry ::biocore::ne]} \ cmderrs] || $retval < 0 } { puts stderr "Error recording entry. ($cmderrs)" puts stderr "Biocore URL is $::biocore::URL" puts stderr "Biocore Session is $::biocore::Session" puts stderr "Biocore Project is $::biocore::Project" if { ! $confirm } { puts stderr {Failed to record entry. Aborting.} saveBackup exit } puts stderr "Restart control panel via Webstart and select \[r\] to continue." puts stderr {Try again? ([r]etry/[a]bort)} set c [gets stdin] if [eof stdin] { puts stderr {EOF detected on input, aborting.} saveBackup exit } switch -- $c { r { puts "Re-reading $sessionfile." } a { puts {Aborting.}; saveBackup; exit } } } else { if { $confirm } { puts {Recorded successfully.} } } }