2 * Copyright © 2017-2018 AT&T Intellectual Property.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor.utils
19 import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.AbstractComponentFunction
20 import org.python.core.PyObject
21 import org.python.util.PythonInterpreter
22 import org.slf4j.LoggerFactory
26 class PythonExecutorUtils {
29 private val log = LoggerFactory.getLogger(PythonExecutorUtils::class.java)
31 fun getPythonComponent(executePath: String, pythonPath: MutableList<String>, content: String?, interfaceName: String,
32 properties: MutableMap<String, Any>): AbstractComponentFunction {
34 initPython(executePath, pythonPath, arrayListOf())
35 val pythonInterpreter = PythonInterpreter()
37 properties.forEach { (name, value) ->
38 pythonInterpreter.set(name, value)
41 pythonInterpreter.exec("import sys")
44 pythonInterpreter.exec(content)
47 val initCommand = interfaceName.plus(" = ").plus(interfaceName).plus("()")
48 pythonInterpreter.exec(initCommand)
49 val pyObject: PyObject = pythonInterpreter.get(interfaceName)
51 log.info("Component Object {}", pyObject)
53 return pyObject.__tojava__(AbstractComponentFunction::class.java) as AbstractComponentFunction
56 private fun initPython(executablePath: String,
57 pythonPath: MutableList<String>, argv: MutableList<String>) {
59 val props = Properties()
60 // Build up the python.path
61 val sb = StringBuilder()
62 sb.append(System.getProperty("java.class.path"))
64 for (p in pythonPath) {
65 sb.append(File.pathSeparator).append(p)
67 log.debug("Python Paths : $sb")
69 props["python.import.site"] = "true"
70 props.setProperty("python.path", sb.toString())
71 props.setProperty("python.verbose", "error")
72 props.setProperty("python.executable", executablePath)
74 PythonInterpreter.initialize(System.getProperties(), props, argv.toTypedArray())