* allow passing of variables directly into the groovy shell

This commit is contained in:
Reinhard Pointner 2012-04-15 15:14:53 +00:00
parent 4b50c9b042
commit 7b82fce064
3 changed files with 39 additions and 7 deletions

View File

@ -13,6 +13,7 @@ import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import org.kohsuke.args4j.Argument; import org.kohsuke.args4j.Argument;
@ -104,6 +105,9 @@ public class ArgumentBean {
@Argument @Argument
public List<String> arguments = new ArrayList<String>(); public List<String> arguments = new ArrayList<String>();
// optional parameters
public Map<String, String> parameters;
public boolean runCLI() { public boolean runCLI() {
return rename || getSubtitles || getMissingSubtitles || check || list || mediaInfo || extract || script != null; return rename || getSubtitles || getMissingSubtitles || check || list || mediaInfo || extract || script != null;

View File

@ -8,8 +8,12 @@ import static net.sourceforge.tuned.FileUtilities.*;
import java.io.File; import java.io.File;
import java.security.AccessController; import java.security.AccessController;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import javax.script.Bindings; import javax.script.Bindings;
@ -28,8 +32,23 @@ public class ArgumentProcessor {
final ArgumentBean bean = new ArgumentBean(); final ArgumentBean bean = new ArgumentBean();
if (args != null && args.length > 0) { if (args != null && args.length > 0) {
List<String> arguments = new ArrayList<String>();
Map<String, String> parameters = new HashMap<String, String>();
for (String it : args) {
if (it.startsWith("-X")) {
String[] pair = it.substring(2).split("=", 2);
if (pair.length == 2) {
parameters.put(pair[0], pair[1]);
}
} else {
arguments.add(it);
}
}
CmdLineParser parser = new CmdLineParser(bean); CmdLineParser parser = new CmdLineParser(bean);
parser.parseArgument(args); parser.parseArgument(arguments);
bean.parameters = parameters;
} }
return bean; return bean;
@ -92,7 +111,7 @@ public class ArgumentProcessor {
bindings.put("args", args.getFiles(false)); bindings.put("args", args.getFiles(false));
Analytics.trackEvent("CLI", "ExecuteScript", args.getScriptLocation().getScheme()); Analytics.trackEvent("CLI", "ExecuteScript", args.getScriptLocation().getScheme());
ScriptShell shell = new ScriptShell(cli, args, args.trustScript, AccessController.getContext()); ScriptShell shell = new ScriptShell(cli, args, args.parameters, args.trustScript, AccessController.getContext());
shell.run(args.getScriptLocation(), bindings); shell.run(args.getScriptLocation(), bindings);
} }
@ -108,6 +127,7 @@ public class ArgumentProcessor {
public void printHelp(ArgumentBean argumentBean) { public void printHelp(ArgumentBean argumentBean) {
new CmdLineParser(argumentBean).printUsage(System.out); new CmdLineParser(argumentBean).printUsage(System.out);
System.out.println(" -X<name>=<value> : Define script variable");
} }
} }

View File

@ -23,6 +23,8 @@ import java.security.Permissions;
import java.security.PrivilegedActionException; import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction; import java.security.PrivilegedExceptionAction;
import java.security.ProtectionDomain; import java.security.ProtectionDomain;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PropertyPermission; import java.util.PropertyPermission;
import javax.script.Bindings; import javax.script.Bindings;
@ -51,12 +53,12 @@ class ScriptShell {
private final boolean trustScript; private final boolean trustScript;
public ScriptShell(CmdlineInterface cli, ArgumentBean args, boolean trustScript, AccessControlContext acc) throws ScriptException { public ScriptShell(CmdlineInterface cli, ArgumentBean args, Map<String, ?> parameters, boolean trustScript, AccessControlContext acc) throws ScriptException {
this.trustScript = trustScript; this.trustScript = trustScript;
// setup script context // setup script context
ScriptContext context = new SimpleScriptContext(); ScriptContext context = new SimpleScriptContext();
context.setBindings(initializeBindings(cli, args, acc), ScriptContext.GLOBAL_SCOPE); context.setBindings(initializeBindings(cli, args, parameters, acc), ScriptContext.GLOBAL_SCOPE);
engine.setContext(context); engine.setContext(context);
// import additional functions into the shell environment // import additional functions into the shell environment
@ -65,9 +67,14 @@ class ScriptShell {
} }
protected Bindings initializeBindings(CmdlineInterface cli, ArgumentBean args, AccessControlContext acc) { protected Bindings initializeBindings(CmdlineInterface cli, ArgumentBean args, Map<String, ?> parameters, AccessControlContext acc) {
Bindings bindings = new SimpleBindings(); Bindings bindings = new SimpleBindings();
// bind external parameters
for (Entry<String, ?> it : parameters.entrySet()) {
bindings.put(it.getKey(), it.getValue());
}
// bind API objects // bind API objects
bindings.put("_cli", PrivilegedInvocation.newProxy(CmdlineInterface.class, cli, acc)); bindings.put("_cli", PrivilegedInvocation.newProxy(CmdlineInterface.class, cli, acc));
bindings.put("_script", new File(args.script)); bindings.put("_script", new File(args.script));
@ -77,8 +84,9 @@ class ScriptShell {
bindings.put("_log", CLILogger); bindings.put("_log", CLILogger);
// bind Java properties and environment variables // bind Java properties and environment variables
bindings.put("_prop", new AssociativeScriptObject(System.getProperties())); bindings.put("_parameter", new AssociativeScriptObject(parameters));
bindings.put("_env", new AssociativeScriptObject(System.getenv())); bindings.put("_system", new AssociativeScriptObject(System.getProperties()));
bindings.put("_environment", new AssociativeScriptObject(System.getenv()));
// bind console object // bind console object
bindings.put("console", System.console()); bindings.put("console", System.console());