* updated to Groovy 2.0

* bundled @Grape support into the fatjar
* automatically trust all local scripts and endorsed online scripts, no need for -trust-script anymore in these cases
This commit is contained in:
Reinhard Pointner 2012-07-09 19:46:18 +00:00
parent b771eb7286
commit c833f0c521
5 changed files with 54 additions and 16 deletions

View File

@ -124,6 +124,11 @@
<include name="groovy*/**" /> <include name="groovy*/**" />
<include name="org/codehaus/groovy/**" /> <include name="org/codehaus/groovy/**" />
<include name="META-INF/dgminfo" /> <include name="META-INF/dgminfo" />
<include name="META-INF/services/**" />
</zipfileset>
<zipfileset src="${dir.lib}/ivy.jar">
<include name="org/apache/ivy/**" />
</zipfileset> </zipfileset>
<zipfileset src="${dir.lib}/icu4j.jar"> <zipfileset src="${dir.lib}/icu4j.jar">

Binary file not shown.

BIN
lib/ivy.jar Normal file

Binary file not shown.

View File

@ -2,6 +2,7 @@
package net.sourceforge.filebot.cli; package net.sourceforge.filebot.cli;
import static net.sourceforge.filebot.Settings.*;
import static net.sourceforge.filebot.cli.CLILogging.*; import static net.sourceforge.filebot.cli.CLILogging.*;
import static net.sourceforge.tuned.ExceptionUtilities.*; import static net.sourceforge.tuned.ExceptionUtilities.*;
import static net.sourceforge.tuned.FileUtilities.*; import static net.sourceforge.tuned.FileUtilities.*;
@ -32,6 +33,7 @@ import org.kohsuke.args4j.CmdLineParser;
import net.sourceforge.filebot.Analytics; import net.sourceforge.filebot.Analytics;
import net.sourceforge.filebot.MediaTypes; import net.sourceforge.filebot.MediaTypes;
import net.sourceforge.filebot.cli.ScriptShell.Script;
import net.sourceforge.filebot.cli.ScriptShell.ScriptProvider; import net.sourceforge.filebot.cli.ScriptShell.ScriptProvider;
import net.sourceforge.filebot.web.CachedResource; import net.sourceforge.filebot.web.CachedResource;
@ -119,13 +121,15 @@ public class ArgumentProcessor {
} }
} else { } else {
// execute user script // execute user script
System.setProperty("grape.root", new File(getApplicationFolder(), "grape").getAbsolutePath());
Bindings bindings = new SimpleBindings(); Bindings bindings = new SimpleBindings();
bindings.put("args", args.getFiles(false)); bindings.put("args", args.getFiles(false));
ScriptProvider scriptProvider = new DefaultScriptProvider(); ScriptProvider scriptProvider = new DefaultScriptProvider(args.trustScript);
Analytics.trackEvent("CLI", "ExecuteScript", scriptProvider.getScriptLocation(args.script).getScheme()); Analytics.trackEvent("CLI", "ExecuteScript", scriptProvider.getScriptLocation(args.script).getScheme());
ScriptShell shell = new ScriptShell(cli, args, args.parameters, args.trustScript, AccessController.getContext(), scriptProvider); ScriptShell shell = new ScriptShell(cli, args, args.parameters, AccessController.getContext(), scriptProvider);
shell.runScript(args.script, bindings); shell.runScript(args.script, bindings);
} }
@ -147,6 +151,14 @@ public class ArgumentProcessor {
public static class DefaultScriptProvider implements ScriptProvider { public static class DefaultScriptProvider implements ScriptProvider {
private final boolean trustRemoteScript;
public DefaultScriptProvider(boolean trustRemoteScript) {
this.trustRemoteScript = trustRemoteScript;
}
@Override @Override
public URI getScriptLocation(String input) { public URI getScriptLocation(String input) {
try { try {
@ -155,7 +167,7 @@ public class ArgumentProcessor {
try { try {
// fn:sortivo // fn:sortivo
if (input.startsWith("fn:")) { if (input.startsWith("fn:")) {
return new URI("http", "filebot.sourceforge.net", "/scripts/" + input.substring(3) + ".groovy", null); return new URI("fn", input.substring(3), null, null, null);
} }
// script:println 'hello world' // script:println 'hello world'
@ -182,28 +194,37 @@ public class ArgumentProcessor {
@Override @Override
public String fetchScript(URI uri) throws IOException { public Script fetchScript(URI uri) throws IOException {
if (uri.getScheme().equals("file")) { if (uri.getScheme().equals("file")) {
return readAll(new InputStreamReader(new FileInputStream(new File(uri)), "UTF-8")); return new Script(readAll(new InputStreamReader(new FileInputStream(new File(uri)), "UTF-8")), true);
} }
if (uri.getScheme().equals("system")) { if (uri.getScheme().equals("system")) {
return readAll(new InputStreamReader(System.in)); return new Script(readAll(new InputStreamReader(System.in)), true);
} }
if (uri.getScheme().equals("script")) { if (uri.getScheme().equals("script")) {
return uri.getAuthority(); return new Script(uri.getAuthority(), true);
}
String url = uri.toString();
boolean trusted = trustRemoteScript;
// special handling for endorsed online scripts
if (uri.getScheme().endsWith("fn")) {
url = "http://filebot.sourceforge.net/scripts/" + uri.getAuthority() + ".groovy";
trusted = true;
} }
// fetch remote script only if modified // fetch remote script only if modified
CachedResource<String> script = new CachedResource<String>(uri.toString(), String.class, 24 * 60 * 60 * 1000) { CachedResource<String> script = new CachedResource<String>(url, String.class, 24 * 60 * 60 * 1000) {
@Override @Override
public String process(ByteBuffer data) { public String process(ByteBuffer data) {
return Charset.forName("UTF-8").decode(data).toString(); return Charset.forName("UTF-8").decode(data).toString();
} }
}; };
return script.get(); return new Script(script.get(), trusted);
} }
} }

View File

@ -43,14 +43,12 @@ import net.sourceforge.filebot.web.MovieIdentificationService;
class ScriptShell { class ScriptShell {
private final ScriptEngine engine = new GroovyScriptEngineFactory().getScriptEngine(); private final ScriptEngine engine = new GroovyScriptEngineFactory().getScriptEngine();
private final boolean trustScript;
private final ScriptProvider scriptProvider; private final ScriptProvider scriptProvider;
public ScriptShell(CmdlineInterface cli, ArgumentBean args, Map<String, ?> parameters, boolean trustScript, AccessControlContext acc, ScriptProvider scriptProvider) throws ScriptException { public ScriptShell(CmdlineInterface cli, ArgumentBean args, Map<String, ?> parameters, AccessControlContext acc, ScriptProvider scriptProvider) throws ScriptException {
this.scriptProvider = scriptProvider; this.scriptProvider = scriptProvider;
this.trustScript = trustScript;
// setup script context // setup script context
ScriptContext context = new SimpleScriptContext(); ScriptContext context = new SimpleScriptContext();
@ -68,18 +66,32 @@ class ScriptShell {
public URI getScriptLocation(String input); public URI getScriptLocation(String input);
public String fetchScript(URI uri) throws Exception; public Script fetchScript(URI uri) throws Exception;
}
public static class Script {
public final String code;
public final boolean trusted;
public Script(String code, boolean trusted) {
this.code = code;
this.trusted = trusted;
}
} }
public Object runScript(String input, Bindings bindings) throws Throwable { public Object runScript(String input, Bindings bindings) throws Throwable {
URI resource = scriptProvider.getScriptLocation(input); URI resource = scriptProvider.getScriptLocation(input);
String script = scriptProvider.fetchScript(resource); Script script = scriptProvider.fetchScript(resource);
return evaluate(script, bindings);
return evaluate(script.code, bindings, script.trusted);
} }
public Object evaluate(final String script, final Bindings bindings) throws Throwable { public Object evaluate(final String script, final Bindings bindings, boolean trustScript) throws Throwable {
try { try {
if (trustScript) { if (trustScript) {
return engine.eval(script, bindings); return engine.eval(script, bindings);