-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
118 lines (100 loc) · 3.24 KB
/
database.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import json
import sqlite3
import shutil
import os
import model.log as log
def create_database():
with sqlite3.connect("lols.db") as db:
c = db.cursor()
sql = """
CREATE TABLE task (
id INTEGER PRIMARY KEY,
state,
iteration,
key,
name,
config
);
"""
c.executescript(sql)
def insert_task(name, iteration, key=0, config={}):
if os.path.exists("config.json"):
task_config = json.load(open("config.json"))[name]
else:
task_config = json.load(open("config-test.json"))[name]
task_config.update(config)
with sqlite3.connect("lols.db") as db:
c = db.cursor()
sql = """
SELECT id FROM task WHERE
name = ? and iteration = ? and key = ?
"""
c.execute(sql, (name, iteration, key))
ret = c.fetchone()
if ret is not None:
# if state = 0, refresh task config
sql = """
UPDATE task SET config = ?
WHERE id = ? and state = 0
"""
c.execute(sql, (json.dumps(task_config), ret[0]))
return ret[0]
sql = """
INSERT INTO task (state, iteration, key, name, config)
VALUES (0, ?, ?, ?, ?);
"""
c.execute(sql, (iteration, key, name, json.dumps(task_config)))
return c.lastrowid
def update_task(id, **d):
with sqlite3.connect("lols.db") as db:
c = db.cursor()
for key, value in d.items():
sql = "UPDATE task SET %s = ? WHERE id = ?" % key
c.execute(sql, (value, id))
def select_task(id):
with sqlite3.connect("lols.db") as db:
c = db.cursor()
sql = """
SELECT id, state, iteration, key, name, config
FROM task WHERE id = ?
"""
c.execute(sql, (id,))
return c.fetchone()
def execute_task(id):
id, state, iteration, key, name, config = select_task(id)
log.title("Task [%d] <%s> start" % (id, name))
log.info(" - iteration: %d, index: %d" % (iteration, key))
if state >= 2:
log.info(" - skipped")
return
update_task(id, state=1)
ret = os.system("python task/%s.py %d" % (name, id))
if ret != 0:
log.info("Task [%d] <%s> error" % (id, name))
log.error("Error occurs when running task [%d]" % id)
raise
update_task(id, state=2)
log.info("Task [%d] <%s> finish" % (id, name))
def run_task(name, iteration, key=0, config={}):
task_id = insert_task(name, iteration, key, config)
execute_task(task_id)
return task_id
def prepare_task(id):
id, state, iteration, key, name, config = select_task(id)
config = json.loads(config)
workdir = "work/%d" % id
if os.path.exists(workdir):
shutil.rmtree(workdir)
os.mkdir(workdir)
with open("%s/config.json" % workdir, "w") as f:
json.dump(config, f, indent=4, sort_keys=True)
return iteration, key, config
def get_task_id(name, iteration, key=0):
with sqlite3.connect("lols.db") as db:
c = db.cursor()
sql = """
SELECT id FROM task WHERE
name = ? and iteration = ? and key = ?
"""
c.execute(sql, (name, iteration, key))
return c.fetchone()[0]