-
Notifications
You must be signed in to change notification settings - Fork 13
/
scholardb_access.py
executable file
·39 lines (30 loc) · 1.12 KB
/
scholardb_access.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os
import threading
import pickle
def synchronized(func):
func.__lock__ = threading.Lock()
def synced_func(*args, **kws):
with func.__lock__:
return func(*args, **kws)
return synced_func
@synchronized
def execute(host, port, user, password, query):
f = open("/tmp/otl_db_ssh_args", "w")
f.write("%s\n%s\n%s\n%s\n%s" % (host, port, user, password, query))
f.close()
os.system("scp /tmp/otl_db_ssh_args xen:/tmp > /dev/null")
os.remove("/tmp/otl_db_ssh_args")
os.system("ssh xen python db.py > /dev/null")
os.system("scp xen:/tmp/otl_db_dump_result /tmp > /dev/null")
os.system("ssh xen rm /tmp/otl_db_dump_result > /dev/null")
result = pickle.load(open("/tmp/otl_db_dump_result", "rb"), encoding="bytes")
os.remove("/tmp/otl_db_dump_result")
for row in result:
for j, elem in enumerate(row):
if isinstance(elem, bytes):
try:
row[j] = elem.decode("cp949")
except UnicodeDecodeError as e:
print(f"Cannot decode {elem}")
print(e)
return result