Files
dev/script/pyt.py
2018-01-19 10:56:53 +01:00

68 lines
2.0 KiB
Python
Executable File

def cmd(c):
import subprocess
proc = subprocess.Popen(c, stdout=subprocess.PIPE, shell=True)
(ret, err) = proc.communicate()
if (err is not None) and err!="":
raise Exception(err)
return ret
def run_cpython(script_name, args = []):
script = get_context().scriptManager.library.resolveFile(script_name)
c = "python " + script + " "
for arg in args:
c = c + str(arg) + " "
return cmd(c)
def call_cpython(script_name, method_name, args = []):
launcher = get_context().scriptManager.library.resolveFile("launcher")
script = os.path.abspath(get_context().scriptManager.library.resolveFile(script_name))
jsonargs = json.dumps(args)
a = []
a.append(script)
a.append(method_name)
a.append('"' + jsonargs + '"')
ret = run_cpython(launcher, a)
ret = ret[0:-2]
jsonret = ret[ret.rfind('\n')+1:]
return json.loads(jsonret)
def call_cpython(script_name, method_name, args = []):
script = os.path.abspath(get_context().scriptManager.library.resolveFile(script_name))
jsonargs = json.dumps(args)
launcher = ""
launcher = launcher + "script = '" +script + "'\n"
launcher = launcher + "function = '" +method_name + "'\n"
launcher = launcher + "jsonargs = '" +jsonargs + "'\n"
launcher = launcher + """import sys
import json
import os
args =json.loads(jsonargs)
i = script.rfind(os.sep)
module = script[i+1:-3]
sys.path.insert(1,script[:i+1])
exec 'from ' + module + ' import ' + function + ' as function'
print json.dumps(function(*args))
"""
launcher = launcher.replace('\n', ';')
launcher = launcher.replace('\\', '\\\\')
c = 'python -c "' + launcher + '"'
ret = cmd(c)
ret = ret[0:-2]
jsonret = ret[ret.rfind('\n')+1:]
return json.loads(jsonret)
print cmd("dir")
print "--------"
print run_cpython("np", ["asd", 2])
print "--------"
ret = call_cpython("np","add", [[1,2,3,4,5], [3,3,3,3,3]])
print ret