Class ScriptBuilder

java.lang.Object
net.sf.basedb.opengrid.ScriptBuilder

public class ScriptBuilder extends Object
Utility class for generating shell scripts.
Since:
1.0
Author:
nicklas
  • Field Details

    • script

      private final StringBuilder script
    • logFolder

      private final String logFolder
    • nextBgPid

      private int nextBgPid
    • bgProcesses

      private Map<String,String> bgProcesses
    • indentLevel

      private int indentLevel
    • indentString

      private String indentString
    • autoIndent

      private Set<String> autoIndent
    • autoUnindent

      private Set<String> autoUnindent
  • Constructor Details

    • ScriptBuilder

      public ScriptBuilder()
      Create a new script builder using the working folder for log and progress information.
    • ScriptBuilder

      public ScriptBuilder(String logFolder)
      Create a new script builder for a generic script.
      Parameters:
      logFolder - Path to a folder to use for outputting log and progress information. If null, the folder set by ${WD} is used (=working directory for jobs executed by the Open Grid cluster).
  • Method Details

    • join

      public void join(ScriptBuilder other)
      Join the other script with this script.
      Since:
      1.5
    • comment

      public void comment(String comment)
      Adds a comment to the script.
      Parameters:
      comment - The comment (must not contain any new lines)
    • newLine

      public void newLine()
      Adds an empty line.
    • time

      public void time(String comment)
      Append the current time (hour:minute:second) and comment into the 'time.log' file in the log folder. Eg: '12:23:45 Demux complete'.
    • progress

      public void progress(int progress, String status)
      Update the progress file. The existing file will be replaced.
      Parameters:
      progress - The progress (in percent)
      status - The status message
    • export

      public void export(String key, String value)
      Add an exported variable declaration to the script: export key="value". The value is surrounded with ". Nothing is added if the value is null.
      Since:
      1.5
    • var

      public void var(String key, String value)
      Add an variable declaration to the script: key="value". The value is surrounded with ". Nothing is added if the value is null.
      Since:
      1.5
    • cmd

      public void cmd(String cmd)
      Run a command.
    • checkIndent

      private void checkIndent(String cmd, Collection<String> keys, int delta)
      If cmd starts with one of the given keys, apply delta to the indentation level and create a new indent-string.
    • echo

      public void echo(String text)
      Echo the given text to stdout
    • bkgr

      public String bkgr(String cmd)
      Adds a command that is executed in the background. The return value is the name of a variable containing the process id of the background task. Use this in waitForProcess(String) to add a wait statement that also catches the exit code of the background task.
    • bkgr

      public String bkgr(String... cmds)
      Adds a batch of commands that are executed in the background. Note that it is only the batch as a whole that is executed in the background, the individual commands are executed in serial. The return value is the name of a variable containing the process id of the background task. Use this in waitForProcess(String) to add a wait statement that also catches the exit code of the background task.
    • waitForProcess

      public void waitForProcess(String pid)
      Wait for the given process to end.
      Parameters:
      pid - The process id variable name returned by bkgr(String).
      Throws:
      IllegalArgumentException - If the pid is unknown
    • length

      public int length()
      Get the current length of the script.
    • toString

      public String toString()
      Get the generated script.
      Overrides:
      toString in class Object
      Throws:
      IllegalStateException - If waitForProcess(String) hasn't been called for all commands started in the background (bkgr(String) or bkgr(String...))
    • toUploadSource

      public UploadSource toUploadSource(String name)
      Get an upload source representation for uploading the script to a remote server.
      Parameters:
      name - The name to give to the file on the remote system
      Since:
      1.5