You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AQuery/prompt.py

199 lines
5.4 KiB

3 years ago
import re
import time
3 years ago
import dbconn
from mo_parsing import ParseException
3 years ago
import aquery_parser as parser
import engine
3 years ago
import reconstruct as xengine
3 years ago
import subprocess
import mmap
import sys
import os
from engine.utils import base62uuid
import atexit
try:
os.remove('server.bin')
except Exception as e:
print(type(e), e)
3 years ago
nullstream = open(os.devnull, 'w')
3 years ago
3 years ago
subprocess.call(['make', 'server.bin'], stdout=nullstream)
3 years ago
cleanup = True
def rm():
global cleanup
if cleanup:
mm.seek(0,os.SEEK_SET)
mm.write(b'\x00\x00')
mm.flush()
try:
time.sleep(.001)
server.kill()
time.sleep(.001)
server.terminate()
except OSError:
pass
files = os.listdir('.')
for f in files:
if f.endswith('.shm'):
os.remove(f)
mm.close()
cleanup = False
3 years ago
nullstream.close()
3 years ago
atexit.register(rm)
3 years ago
def init():
global shm, server, basecmd, mm
shm = base62uuid()
if sys.platform != 'win32':
import readline
shm += '.shm'
basecmd = ['bash', '-c', 'rlwrap k']
mm = None
if not os.path.isfile(shm):
# create initial file
with open(shm, "w+b") as handle:
handle.write(b'\x01\x00') # [running, new job]
handle.flush()
mm = mmap.mmap(handle.fileno(), 2, access=mmap.ACCESS_WRITE, offset=0)
if mm is None:
exit(1)
else:
basecmd = ['bash.exe', '-c', 'rlwrap ./k']
mm = mmap.mmap(0, 2, shm)
mm.write(b'\x01\x00')
mm.flush()
server = subprocess.Popen(["./server.bin", shm])
init()
3 years ago
test_parser = True
# code to test parser
ws = re.compile(r'\s+')
q = 'SELECT p.Name, v.Name FROM Production.Product p JOIN Purchasing.ProductVendor pv ON p.ProductID = pv.ProductID JOIN Purchasing.Vendor v ON pv.BusinessEntityID = v.BusinessEntityID WHERE ProductSubcategoryID = 15 ORDER BY v.Name;'
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()
3 years ago
keep = True
cxt = engine.initialize()
3 years ago
cxt.Info(res)
3 years ago
while test_parser:
try:
if server.poll() is not None:
3 years ago
init()
3 years ago
print("> ", end="")
ready = 1
while ready == 1:
mm.seek(0,os.SEEK_SET)
ready = mm.read(2)[1]
time.sleep(.00001)
3 years ago
q = input().lower()
3 years ago
if q == 'exec':
if not keep or cxt is None:
cxt = engine.initialize()
3 years ago
else:
cxt.new()
3 years ago
stmts_stmts = stmts['stmts']
if type(stmts_stmts) is list:
for s in stmts_stmts:
engine.generate(s, cxt)
else:
engine.generate(stmts_stmts, cxt)
cxt.Info(cxt.ccode)
3 years ago
with open('out.cpp', 'wb') as outfile:
outfile.write((cxt.finalize()).encode('utf-8'))
3 years ago
if subprocess.call(['make', 'snippet'], stdout = nullstream) == 0:
3 years ago
mm.seek(0,os.SEEK_SET)
mm.write(b'\x01\x01')
continue
3 years ago
if q == 'xexec':
cxt = xengine.initialize()
stmts_stmts = stmts['stmts']
if type(stmts_stmts) is list:
for s in stmts_stmts:
xengine.generate(s, cxt)
else:
xengine.generate(stmts_stmts, cxt)
print(cxt.sql)
continue
elif q.startswith('log'):
qs = re.split(' |\t', q)
if len(qs) > 1:
cxt.log_level = qs[1]
else:
cxt.print(cxt.log_level)
continue
3 years ago
elif q == 'k':
subprocess.call(basecmd)
continue
elif q == 'print':
cxt.print(stmts)
3 years ago
continue
elif q == 'keep':
keep = not keep
continue
elif q == 'format' or q == 'fmt':
3 years ago
subprocess.call(['clang-format', 'out.cpp'])
elif q == 'exit':
break
elif q == 'r':
if subprocess.call(['make', 'snippet']) == 0:
mm.seek(0,os.SEEK_SET)
mm.write(b'\x01\x01')
continue
elif q.startswith('save'):
filename = re.split(' |\t', q)
if (len(filename) > 1):
filename = filename[1]
else:
filename = f'out_{base62uuid(4)}.cpp'
with open(filename, 'wb') as outfile:
outfile.write((cxt.finalize()).encode('utf-8'))
continue
3 years ago
trimed = ws.sub(' ', q.lower()).split(' ')
if trimed[0].startswith('f'):
fn = 'stock.a' if len(trimed) <= 1 or len(trimed[1]) == 0 \
else trimed[1]
with open(fn, 'r') as file:
3 years ago
contents = file.read()#.lower()
3 years ago
stmts = parser.parse(contents)
continue
stmts = parser.parse(q)
cxt.Info(stmts)
3 years ago
except ParseException as e:
print(e)
continue
except (ValueError, FileNotFoundError) as e:
# rm()
# init()
print(e)
except (KeyboardInterrupt):
break
except (Exception) as e:
3 years ago
rm()
3 years ago
raise e
3 years ago
3 years ago
rm()