|
|
@ -1,3 +1,4 @@
|
|
|
|
|
|
|
|
import enum
|
|
|
|
import re
|
|
|
|
import re
|
|
|
|
import time
|
|
|
|
import time
|
|
|
|
import dbconn
|
|
|
|
import dbconn
|
|
|
@ -12,14 +13,26 @@ import sys
|
|
|
|
import os
|
|
|
|
import os
|
|
|
|
from engine.utils import base62uuid
|
|
|
|
from engine.utils import base62uuid
|
|
|
|
import atexit
|
|
|
|
import atexit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import threading
|
|
|
|
|
|
|
|
import ctypes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RunType(enum.Enum):
|
|
|
|
|
|
|
|
Threaded = 0
|
|
|
|
|
|
|
|
IPC = 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
server_mode = RunType.Threaded
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
server_bin = 'server.bin' if server_mode == RunType.IPC else 'server.so'
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
os.remove('server.bin')
|
|
|
|
os.remove(server_bin)
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
print(type(e), e)
|
|
|
|
print(type(e), e)
|
|
|
|
|
|
|
|
|
|
|
|
nullstream = open(os.devnull, 'w')
|
|
|
|
nullstream = open(os.devnull, 'w')
|
|
|
|
|
|
|
|
|
|
|
|
subprocess.call(['make', 'server.bin'], stdout=nullstream)
|
|
|
|
subprocess.call(['make', server_bin], stdout=nullstream)
|
|
|
|
cleanup = True
|
|
|
|
cleanup = True
|
|
|
|
|
|
|
|
|
|
|
|
def rm():
|
|
|
|
def rm():
|
|
|
@ -45,9 +58,7 @@ def rm():
|
|
|
|
cleanup = False
|
|
|
|
cleanup = False
|
|
|
|
nullstream.close()
|
|
|
|
nullstream.close()
|
|
|
|
|
|
|
|
|
|
|
|
atexit.register(rm)
|
|
|
|
def init_ipc():
|
|
|
|
|
|
|
|
|
|
|
|
def init():
|
|
|
|
|
|
|
|
global shm, server, basecmd, mm
|
|
|
|
global shm, server, basecmd, mm
|
|
|
|
shm = base62uuid()
|
|
|
|
shm = base62uuid()
|
|
|
|
if sys.platform != 'win32':
|
|
|
|
if sys.platform != 'win32':
|
|
|
@ -70,6 +81,100 @@ def init():
|
|
|
|
mm.flush()
|
|
|
|
mm.flush()
|
|
|
|
server = subprocess.Popen(["./server.bin", shm])
|
|
|
|
server = subprocess.Popen(["./server.bin", shm])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
c = lambda _ba: ctypes.cast((ctypes.c_char * len(_ba)).from_buffer(_ba), ctypes.c_char_p)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
|
|
|
|
def __init__(self, nq = 0, mode = server_mode, n_bufs = 0, bf_szs = []) -> None:
|
|
|
|
|
|
|
|
self.int_size = 4
|
|
|
|
|
|
|
|
self.n_attrib = 4
|
|
|
|
|
|
|
|
self.buf = bytearray((self.n_attrib + n_bufs) * self.int_size)
|
|
|
|
|
|
|
|
self.np_buf = np.ndarray(shape=(self.n_attrib), buffer=self.buf, dtype=np.int32)
|
|
|
|
|
|
|
|
self.new_query = nq
|
|
|
|
|
|
|
|
self.server_mode = mode.value
|
|
|
|
|
|
|
|
self.running = 1
|
|
|
|
|
|
|
|
self.n_buffers = n_bufs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
|
|
def running(self):
|
|
|
|
|
|
|
|
return self.np_buf[0]
|
|
|
|
|
|
|
|
@running.setter
|
|
|
|
|
|
|
|
def running(self, rn):
|
|
|
|
|
|
|
|
self.np_buf[0] = rn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
|
|
def new_query(self):
|
|
|
|
|
|
|
|
return self.np_buf[1]
|
|
|
|
|
|
|
|
@new_query.setter
|
|
|
|
|
|
|
|
def new_query(self, nq):
|
|
|
|
|
|
|
|
self.np_buf[1] = nq
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
|
|
def server_mode(self):
|
|
|
|
|
|
|
|
return self.np_buf[2]
|
|
|
|
|
|
|
|
@server_mode.setter
|
|
|
|
|
|
|
|
def server_mode(self, mode):
|
|
|
|
|
|
|
|
self.np_buf[2] = mode
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
|
|
def n_buffers(self):
|
|
|
|
|
|
|
|
return self.np_buf[3]
|
|
|
|
|
|
|
|
@n_buffers.setter
|
|
|
|
|
|
|
|
def n_buffers(self, n_bufs):
|
|
|
|
|
|
|
|
self.np_buf[3] = n_bufs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def set_bufszs(self, buf_szs):
|
|
|
|
|
|
|
|
for i in range(min(len(buf_szs), self.n_buffers)):
|
|
|
|
|
|
|
|
self.np_buf[i+self.n_attrib] = buf_szs[i]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
|
|
def c(self):
|
|
|
|
|
|
|
|
return c(self.buf)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cfg = Config()
|
|
|
|
|
|
|
|
th = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def init_threaded():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if os.name == 'nt':
|
|
|
|
|
|
|
|
t = os.environ['PATH'].lower().split(';')
|
|
|
|
|
|
|
|
vars = re.compile('%.*%')
|
|
|
|
|
|
|
|
for e in t:
|
|
|
|
|
|
|
|
if(len(e) != 0):
|
|
|
|
|
|
|
|
if '%' in e:
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
m_e = vars.findall(e)
|
|
|
|
|
|
|
|
for m in m_e:
|
|
|
|
|
|
|
|
e = e.replace(m, os.environ[m[1:-1]])
|
|
|
|
|
|
|
|
# print(m, e)
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
os.add_dll_directory(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
server_so = ctypes.CDLL('./'+server_bin)
|
|
|
|
|
|
|
|
global cfg, th
|
|
|
|
|
|
|
|
th = threading.Thread(target=server_so['main'], args=(-1, ctypes.POINTER(ctypes.c_char_p)(cfg.c)), daemon=True)
|
|
|
|
|
|
|
|
th.start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if server_mode == RunType.IPC:
|
|
|
|
|
|
|
|
atexit.register(rm)
|
|
|
|
|
|
|
|
init = init_ipc
|
|
|
|
|
|
|
|
set_ready = lambda : mm.seek(0,os.SEEK_SET) or mm.write(b'\x01\x01')
|
|
|
|
|
|
|
|
def __get_ready():
|
|
|
|
|
|
|
|
mm.seek(0,os.SEEK_SET)
|
|
|
|
|
|
|
|
return mm.read(2)[1]
|
|
|
|
|
|
|
|
get_ready = __get_ready
|
|
|
|
|
|
|
|
server_status = lambda : server.poll() is not None
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
init = init_threaded
|
|
|
|
|
|
|
|
rm = lambda: None
|
|
|
|
|
|
|
|
def __set_ready():
|
|
|
|
|
|
|
|
global cfg
|
|
|
|
|
|
|
|
cfg.new_query = 1
|
|
|
|
|
|
|
|
set_ready = __set_ready
|
|
|
|
|
|
|
|
get_ready = lambda:cfg.new_query
|
|
|
|
|
|
|
|
server_status = lambda : not th.is_alive()
|
|
|
|
init()
|
|
|
|
init()
|
|
|
|
|
|
|
|
|
|
|
|
test_parser = True
|
|
|
|
test_parser = True
|
|
|
@ -82,31 +187,16 @@ q = 'SELECT p.Name, v.Name FROM Production.Product p JOIN Purchasing.ProductVend
|
|
|
|
res = parser.parse(q)
|
|
|
|
res = parser.parse(q)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# else:f
|
|
|
|
|
|
|
|
# if subprocess.call(['make', 'snippet']) == 0:
|
|
|
|
|
|
|
|
# mm.seek(0)
|
|
|
|
|
|
|
|
# mm.write(b'\x01\x01')
|
|
|
|
|
|
|
|
# time.sleep(.1)
|
|
|
|
|
|
|
|
# mm.seek(0)
|
|
|
|
|
|
|
|
# print(mm.read(2))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# mm.close()
|
|
|
|
|
|
|
|
# handle.close()
|
|
|
|
|
|
|
|
# os.remove(shm)
|
|
|
|
|
|
|
|
# exit()
|
|
|
|
|
|
|
|
keep = True
|
|
|
|
keep = True
|
|
|
|
cxt = engine.initialize()
|
|
|
|
cxt = engine.initialize()
|
|
|
|
cxt.Info(res)
|
|
|
|
cxt.Info(res)
|
|
|
|
while test_parser:
|
|
|
|
while test_parser:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
if server.poll() is not None:
|
|
|
|
if server_status():
|
|
|
|
init()
|
|
|
|
init()
|
|
|
|
print("> ", end="")
|
|
|
|
while get_ready():
|
|
|
|
ready = 1
|
|
|
|
|
|
|
|
while ready == 1:
|
|
|
|
|
|
|
|
mm.seek(0,os.SEEK_SET)
|
|
|
|
|
|
|
|
ready = mm.read(2)[1]
|
|
|
|
|
|
|
|
time.sleep(.00001)
|
|
|
|
time.sleep(.00001)
|
|
|
|
|
|
|
|
print("> ", end="")
|
|
|
|
q = input().lower()
|
|
|
|
q = input().lower()
|
|
|
|
if q == 'exec':
|
|
|
|
if q == 'exec':
|
|
|
|
if not keep or cxt is None:
|
|
|
|
if not keep or cxt is None:
|
|
|
@ -123,8 +213,7 @@ while test_parser:
|
|
|
|
with open('out.cpp', 'wb') as outfile:
|
|
|
|
with open('out.cpp', 'wb') as outfile:
|
|
|
|
outfile.write((cxt.finalize()).encode('utf-8'))
|
|
|
|
outfile.write((cxt.finalize()).encode('utf-8'))
|
|
|
|
if subprocess.call(['make', 'snippet'], stdout = nullstream) == 0:
|
|
|
|
if subprocess.call(['make', 'snippet'], stdout = nullstream) == 0:
|
|
|
|
mm.seek(0,os.SEEK_SET)
|
|
|
|
set_ready()
|
|
|
|
mm.write(b'\x01\x01')
|
|
|
|
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
if q == 'xexec':
|
|
|
|
if q == 'xexec':
|
|
|
|
cxt = xengine.initialize()
|
|
|
|
cxt = xengine.initialize()
|
|
|
@ -159,8 +248,10 @@ while test_parser:
|
|
|
|
break
|
|
|
|
break
|
|
|
|
elif q == 'r':
|
|
|
|
elif q == 'r':
|
|
|
|
if subprocess.call(['make', 'snippet']) == 0:
|
|
|
|
if subprocess.call(['make', 'snippet']) == 0:
|
|
|
|
mm.seek(0,os.SEEK_SET)
|
|
|
|
set_ready()
|
|
|
|
mm.write(b'\x01\x01')
|
|
|
|
continue
|
|
|
|
|
|
|
|
elif q == 'rr':
|
|
|
|
|
|
|
|
set_ready()
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
elif q.startswith('save'):
|
|
|
|
elif q.startswith('save'):
|
|
|
|
filename = re.split(' |\t', q)
|
|
|
|
filename = re.split(' |\t', q)
|
|
|
|