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
25 class PythonExecutorUtils {
28 private val log = LoggerFactory.getLogger(PythonExecutorUtils::class.java)
30 fun getPythonComponent(executePath: String, pythonPath: MutableList<String>, content: String?, interfaceName: String,
31 properties: MutableMap<String, Any>): AbstractComponentFunction {
33 initPython(executePath, pythonPath, arrayListOf())
34 val pythonInterpreter = PythonInterpreter()
36 properties.forEach { (name, value) ->
37 pythonInterpreter.set(name, value)
40 pythonInterpreter.exec("import sys")
43 pythonInterpreter.exec(content)
46 val initCommand = interfaceName.plus(" = ").plus(interfaceName).plus("()")
47 pythonInterpreter.exec(initCommand)
48 val pyObject: PyObject = pythonInterpreter.get(interfaceName)
50 log.info("Component Object {}", pyObject)
52 return pyObject.__tojava__(AbstractComponentFunction::class.java) as AbstractComponentFunction
55 private fun initPython(executablePath: String,
56 pythonPath: MutableList<String>, argv: MutableList<String>) {
58 val props = Properties()
59 // Build up the python.path
60 val sb = StringBuilder()
61 sb.append(System.getProperty("java.class.path"))
63 for (p in pythonPath) {
64 sb.append(":").append(p)
66 log.debug("Paths : $sb")
68 props["python.import.site"] = "true"
69 props.setProperty("python.path", sb.toString())
70 props.setProperty("python.verbose", "error")
71 props.setProperty("python.executable", executablePath)
73 PythonInterpreter.initialize(System.getProperties(), props, argv.toTypedArray())