Package net.sf.basedb.opengrid
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 Summary
Fields Modifier and Type Field Description private Set<String>
autoIndent
private Set<String>
autoUnindent
private Map<String,String>
bgProcesses
private int
indentLevel
private String
indentString
private String
logFolder
private int
nextBgPid
private StringBuilder
script
-
Constructor Summary
Constructors Constructor Description ScriptBuilder()
Create a new script builder using the working folder for log and progress information.ScriptBuilder(String logFolder)
Create a new script builder for a generic script. -
Method Summary
Modifier and Type Method Description String
bkgr(String cmd)
Adds a command that is executed in the background.String
bkgr(String... cmds)
Adds a batch of commands that are executed in the background.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.void
cmd(String cmd)
Run a command.void
comment(String comment)
Adds a comment to the script.void
echo(String text)
Echo the given text to stdoutvoid
export(String key, String value)
Add an exported variable declaration to the script: export key="value".void
join(ScriptBuilder other)
Join the other script with this script.int
length()
Get the current length of the script.void
newLine()
Adds an empty line.void
progress(int progress, String status)
Update the progress file.void
time(String comment)
Append the current time (hour:minute:second) and comment into the 'time.log' file in the log folder.String
toString()
Get the generated script.UploadSource
toUploadSource(String name)
Get an upload source representation for uploading the script to a remote server.void
var(String key, String value)
Add an variable declaration to the script: key="value".void
waitForProcess(String pid)
Wait for the given process to end.
-
Field Details
-
script
-
logFolder
-
nextBgPid
private int nextBgPid -
bgProcesses
-
indentLevel
private int indentLevel -
indentString
-
autoIndent
-
autoUnindent
-
-
Constructor Details
-
ScriptBuilder
public ScriptBuilder()Create a new script builder using the working folder for log and progress information. -
ScriptBuilder
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
Join the other script with this script.- Since:
- 1.5
-
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
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
Update the progress file. The existing file will be replaced.- Parameters:
progress
- The progress (in percent)status
- The status message
-
export
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
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
Run a command. -
checkIndent
If cmd starts with one of the given keys, apply delta to the indentation level and create a new indent-string. -
echo
Echo the given text to stdout -
bkgr
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 inwaitForProcess(String)
to add a wait statement that also catches the exit code of the background task. -
bkgr
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 inwaitForProcess(String)
to add a wait statement that also catches the exit code of the background task. -
waitForProcess
Wait for the given process to end.- Parameters:
pid
- The process id variable name returned bybkgr(String)
.- Throws:
IllegalArgumentException
- If the pid is unknown
-
length
public int length()Get the current length of the script. -
toString
Get the generated script.- Overrides:
toString
in classObject
- Throws:
IllegalStateException
- IfwaitForProcess(String)
hasn't been called for all commands started in the background (bkgr(String)
orbkgr(String...)
)
-
toUploadSource
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
-