diff --git a/common/source/java/ch/systemsx/cisd/common/jython/IJythonInterpreter.java b/common/source/java/ch/systemsx/cisd/common/jython/IJythonInterpreter.java index 68ec87109e08fbeb6914a8157d94eb0dcb36699b..4e70806cd4ce82f10d4376161b4e5e97ea75e886 100644 --- a/common/source/java/ch/systemsx/cisd/common/jython/IJythonInterpreter.java +++ b/common/source/java/ch/systemsx/cisd/common/jython/IJythonInterpreter.java @@ -29,4 +29,6 @@ public interface IJythonInterpreter void releaseResources(); IJythonFunction tryJythonFunction(String name); + + boolean isNextCommand(String lines); } \ No newline at end of file diff --git a/common/source/java/ch/systemsx/cisd/common/jython/JythonScriptCommand.java b/common/source/java/ch/systemsx/cisd/common/jython/JythonScriptCommand.java deleted file mode 100644 index dc618c10537c65bd33e1276980621645d3bed7ac..0000000000000000000000000000000000000000 --- a/common/source/java/ch/systemsx/cisd/common/jython/JythonScriptCommand.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2012 ETH Zuerich, CISD - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ch.systemsx.cisd.common.jython; - -import org.python.core.CompileMode; -import org.python.core.CompilerFlags; -import org.python.core.Py; -import org.python.core.PyException; -import org.python.core.PyObject; - -/** - * @author pkupczyk - */ -public class JythonScriptCommand extends JythonScriptLines -{ - - public boolean isNextCommand(String line) - { - try - { - PyObject object = - Py.compile_command_flags(getLines(), "<input>", CompileMode.single, - new CompilerFlags(), true); - return object != Py.None; - } catch (PyException e) - { - return false; - } - } - -} diff --git a/common/source/java/ch/systemsx/cisd/common/jython/JythonScriptSplitter.java b/common/source/java/ch/systemsx/cisd/common/jython/JythonScriptSplitter.java index 3d5b5e8348d633d54b37a83945b9e9620a364401..4254f92143f44bc8f0bc5bd0dcea795416ac90fa 100644 --- a/common/source/java/ch/systemsx/cisd/common/jython/JythonScriptSplitter.java +++ b/common/source/java/ch/systemsx/cisd/common/jython/JythonScriptSplitter.java @@ -18,8 +18,6 @@ package ch.systemsx.cisd.common.jython; import java.util.List; -import org.python.core.PyException; - /** * Splits jython code into smaller batches to overcome 64KB script size limitation. * @@ -32,16 +30,23 @@ public class JythonScriptSplitter private int batchSize = DEFAULT_BATCH_SIZE; - public List<String> split(String scriptToSplit) throws PyException + private IJythonInterpreter interpreter; + + public JythonScriptSplitter(IJythonInterpreter interpreter) + { + this.interpreter = interpreter; + } + + public List<String> split(String scriptToSplit) { JythonScript script = new JythonScript(scriptToSplit); JythonScriptBatches batches = new JythonScriptBatches(); JythonScriptBatch batch = new JythonScriptBatch(); - JythonScriptCommand command = new JythonScriptCommand(); + JythonScriptLines command = new JythonScriptLines(); for (String line : script.getLines()) { - if (command.getSize() > 0 && command.isNextCommand(line)) + if (command.getSize() > 0 && interpreter.isNextCommand(command.getLines())) { if (batch.getSize() > 0 && batch.getSize() + command.getSize() > getBatchSize()) { @@ -49,7 +54,7 @@ public class JythonScriptSplitter batch = new JythonScriptBatch(); } batch.addLines(command); - command = new JythonScriptCommand(); + command = new JythonScriptLines(); command.addLine(line); } else { diff --git a/common/source/java/ch/systemsx/cisd/common/jython/JythonUtils.java b/common/source/java/ch/systemsx/cisd/common/jython/JythonUtils.java index 10b1be5b5a35ae2da32af08deaf955bc5675d998..7eadffe6d0813008a17dc02c38e833fa56258951 100644 --- a/common/source/java/ch/systemsx/cisd/common/jython/JythonUtils.java +++ b/common/source/java/ch/systemsx/cisd/common/jython/JythonUtils.java @@ -18,15 +18,7 @@ package ch.systemsx.cisd.common.jython; import java.io.File; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; - -import org.python.core.Py; -import org.python.core.PyDictionary; -import org.python.core.PyFunction; -import org.python.core.PyObject; -import org.python.core.PySequenceList; import ch.systemsx.cisd.common.filesystem.FileUtilities; import ch.systemsx.cisd.common.jython.evaluator.EvaluatorException; @@ -39,53 +31,6 @@ import ch.systemsx.cisd.common.shared.basic.string.StringUtils; */ public class JythonUtils { - - /** - * Converts a {@link PyDictionary} to a Java map. - * - * @return a map equivalent to the given Jython dictionary. - */ - public static Map<String, String> convertPyDictToMap(PyDictionary result) - { - Map<String, String> javaMap = new HashMap<String, String>(); - for (Object item : result.items()) - { - PySequenceList tuple = (PySequenceList) item; - javaMap.put(tuple.get(0).toString(), tuple.get(1).toString()); - } - return javaMap; - } - - /** - * Tries to get a function defined in jython script - * - * @return a Jython function object, or <code>null</code> if function doesn't exist. - */ - public static PyFunction tryJythonFunction(PythonInterpreter interpreter, String functionName) - { - try - { - PyFunction function = interpreter.get(functionName, PyFunction.class); - return function; - } catch (Exception e) - { - return null; - } - } - - /** - * Turn all arguments into a python objects, and calls the specified function. - */ - public static PyObject invokeFunction(PyFunction function, Object... args) - { - PyObject[] pyArgs = new PyObject[args.length]; - for (int i = 0; i < args.length; i++) - { - pyArgs[i] = Py.java2py(args[i]); - } - return function.__call__(pyArgs); - } - /** * @return script string from file with given path * @throws EvaluatorException if the file doesn't exist or is empty diff --git a/common/source/java/ch/systemsx/cisd/common/jython/v25/Jython25InterpreterFactory.java b/common/source/java/ch/systemsx/cisd/common/jython/v25/Jython25InterpreterFactory.java index f5fa157e0b3bc9feb659a8c386aae84f973cc98d..a8750976a494707ff6b63d9eb0cd7adec062a61b 100644 --- a/common/source/java/ch/systemsx/cisd/common/jython/v25/Jython25InterpreterFactory.java +++ b/common/source/java/ch/systemsx/cisd/common/jython/v25/Jython25InterpreterFactory.java @@ -16,7 +16,11 @@ package ch.systemsx.cisd.common.jython.v25; +import org.python.core.CompileMode; +import org.python.core.CompilerFlags; +import org.python.core.Py; import org.python.core.PyBaseCode; +import org.python.core.PyException; import org.python.core.PyFunction; import org.python.core.PyInteger; import org.python.core.PyObject; @@ -25,7 +29,6 @@ import ch.systemsx.cisd.common.jython.IJythonFunction; import ch.systemsx.cisd.common.jython.IJythonInterpreter; import ch.systemsx.cisd.common.jython.IJythonInterpreterFactory; import ch.systemsx.cisd.common.jython.IJythonObject; -import ch.systemsx.cisd.common.jython.JythonUtils; import ch.systemsx.cisd.common.jython.PythonInterpreter; public class Jython25InterpreterFactory implements IJythonInterpreterFactory @@ -48,7 +51,12 @@ public class Jython25InterpreterFactory implements IJythonInterpreterFactory @Override public IJythonObject invoke(Object... arguments) { - PyObject result = JythonUtils.invokeFunction(function, arguments); + PyObject[] pyArgs = new PyObject[arguments.length]; + for (int i = 0; i < arguments.length; i++) + { + pyArgs[i] = Py.java2py(arguments[i]); + } + PyObject result = function.__call__(pyArgs); if (result == null) { return null; @@ -142,15 +150,30 @@ public class Jython25InterpreterFactory implements IJythonInterpreterFactory @Override public IJythonFunction tryJythonFunction(String name) { - PyFunction function = JythonUtils.tryJythonFunction(interpreter, name); - if (function == null) + try + { + PyFunction function = interpreter.get(name, PyFunction.class); + return new Jython25Function(function); + } catch (Exception e) { return null; } - else + } + + @Override + public boolean isNextCommand(String lines) + { + try { - return new Jython25Function(function); + PyObject object = + Py.compile_command_flags(lines, "<input>", CompileMode.single, + new CompilerFlags(), true); + return object != Py.None; + } catch (PyException e) + { + return false; } } + } } diff --git a/common/source/java/ch/systemsx/cisd/common/jython/v27/Jython27InterpreterFactory.java b/common/source/java/ch/systemsx/cisd/common/jython/v27/Jython27InterpreterFactory.java index b7abfc09c1382bbaf065b9531dd6d9f6e54623ac..a9f3baa6f15ab81614a27b30e5f98b2b065b43a6 100644 --- a/common/source/java/ch/systemsx/cisd/common/jython/v27/Jython27InterpreterFactory.java +++ b/common/source/java/ch/systemsx/cisd/common/jython/v27/Jython27InterpreterFactory.java @@ -16,7 +16,11 @@ package ch.systemsx.cisd.common.jython.v27; +import org.python27.core.CompileMode; +import org.python27.core.CompilerFlags; +import org.python27.core.Py; import org.python27.core.PyBaseCode; +import org.python27.core.PyException; import org.python27.core.PyFunction; import org.python27.core.PyInteger; import org.python27.core.PyObject; @@ -153,6 +157,21 @@ public class Jython27InterpreterFactory implements IJythonInterpreterFactory return new Jython27Function(function); } } + + @Override + public boolean isNextCommand(String lines) + { + try + { + PyObject object = + Py.compile_command_flags(lines, "<input>", CompileMode.single, + new CompilerFlags(), true); + return object != Py.None; + } catch (PyException e) + { + return false; + } + } } } \ No newline at end of file diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/jython/JythonScriptSplitterTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/jython/JythonScriptSplitterTest.java index bfde3af37c23c21cf8174daeb0e88b0cfec29aa8..3c277c8c737870e4936cd5d13cec5afc7cac88b9 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/jython/JythonScriptSplitterTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/jython/JythonScriptSplitterTest.java @@ -25,6 +25,7 @@ import org.testng.Assert; import org.testng.annotations.Test; import ch.systemsx.cisd.common.filesystem.FileUtilities; +import ch.systemsx.cisd.common.jython.v27.Jython27InterpreterFactory; /** * @author pkupczyk @@ -38,7 +39,7 @@ public class JythonScriptSplitterTest @Test public void testSplittingNullScriptShouldReturnNoBatches() { - JythonScriptSplitter splitter = new JythonScriptSplitter(); + JythonScriptSplitter splitter = new JythonScriptSplitter(new Jython27InterpreterFactory().createInterpreter()); List<String> batches = splitter.split(null); Assert.assertNotNull(batches); Assert.assertEquals(batches, Collections.emptyList()); @@ -47,7 +48,7 @@ public class JythonScriptSplitterTest @Test public void testSplittingEmptyScriptShouldReturnOneEmptyBatch() { - JythonScriptSplitter splitter = new JythonScriptSplitter(); + JythonScriptSplitter splitter = new JythonScriptSplitter(new Jython27InterpreterFactory().createInterpreter()); List<String> batches = splitter.split(""); Assert.assertNotNull(batches); Assert.assertEquals(batches.size(), 1); @@ -89,7 +90,7 @@ public class JythonScriptSplitterTest { String originalScript = getTestScriptCode(scriptSize); - JythonScriptSplitter splitter = new JythonScriptSplitter(); + JythonScriptSplitter splitter = new JythonScriptSplitter(new Jython27InterpreterFactory().createInterpreter()); splitter.setBatchSize(batchSize); List<String> batches = splitter.split(originalScript); diff --git a/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/DataSetRegistrationServiceV2.java b/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/DataSetRegistrationServiceV2.java index 6d815c8a3cefd4f0a286d966e126211d00f9e1d8..f122704e0549f468d751ced4c6c4054179bbdf8d 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/DataSetRegistrationServiceV2.java +++ b/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/DataSetRegistrationServiceV2.java @@ -73,6 +73,12 @@ public class DataSetRegistrationServiceV2<T extends DataSetInformation> extends public void addToPath(String... pythonPaths) { } + + @Override + public boolean isNextCommand(String line) + { + return false; + } }, null); } diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/api/v1/validation/ValidationScriptRunner.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/api/v1/validation/ValidationScriptRunner.java index c94df2b747c6e82b40e8b9fa8b8cea93eddd5e2e..d76e86bef6cb4211c7de4cf35c0538a400235a47 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/api/v1/validation/ValidationScriptRunner.java +++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/api/v1/validation/ValidationScriptRunner.java @@ -29,6 +29,7 @@ import org.python.core.Py; import org.python.core.PyDictionary; import org.python.core.PyFunction; import org.python.core.PyObject; +import org.python.core.PySequenceList; import ch.systemsx.cisd.common.jython.JythonUtils; import ch.systemsx.cisd.common.jython.PythonInterpreter; @@ -161,7 +162,7 @@ public class ValidationScriptRunner Map<String, String> javaResult = null; if (result instanceof PyDictionary) { - javaResult = JythonUtils.convertPyDictToMap((PyDictionary) result); + javaResult = convertPyDictToMap((PyDictionary) result); } else { javaResult = (Map<String, String>) result; @@ -171,6 +172,17 @@ public class ValidationScriptRunner return metadata; } + + private static Map<String, String> convertPyDictToMap(PyDictionary result) + { + Map<String, String> javaMap = new HashMap<String, String>(); + for (Object item : result.items()) + { + PySequenceList tuple = (PySequenceList) item; + javaMap.put(tuple.get(0).toString(), tuple.get(1).toString()); + } + return javaMap; + } public String getScriptString() {