forked from balidani/tinyctf-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_import.py
executable file
·72 lines (51 loc) · 2.01 KB
/
task_import.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
#!/usr/bin/env python
"""task_import.py -- imports tasks from 'tasks.json' into the database"""
import dataset
import json
import sys
if __name__ == '__main__':
purge = False
# Handle the optional purge argument
if len(sys.argv) > 1:
if sys.argv[1] == 'purge':
purge = True
print "[*] Purge mode is on, old tasks deleted"
# Load the json structure
tasks_str = open('tasks.json', 'rb').read()
tasks_json = json.loads(tasks_str)
# Connect to database
db = dataset.connect('sqlite:///ctf.db')
cat_table = db['categories']
tasks_table = db['tasks']
cat_task_table = db['cat_task']
if purge:
cat_table.delete()
tasks_table.delete()
cat_task_table.delete()
# Parse the json file and add rows to the table
old_cat_count = len(list(cat_table))
old_task_count = len(list(tasks_table))
# First, create categories
for category in tasks_json['categories']:
cat = category.copy()
del cat['tasks']
if cat_table.find_one(id=cat['id']):
# Update existing category
cat_table.update(cat, ['id'])
else:
cat_table.insert(cat)
new_cat_count = len(list(cat_table))
print "[*] Imported %d new categories" % (new_cat_count - old_cat_count)
# Then create tasks
for category in tasks_json['categories']:
for task in category['tasks']:
if tasks_table.find_one(id=task['id']):
# Update existing table
tasks_table.update(task, ['id'])
else:
tasks_table.insert(task)
# In this case, we should add an entry to the category_task table as well
row = dict(cat_id=category['id'], task_id=task['id'])
cat_task_table.insert(row)
new_task_count = len(list(tasks_table))
print "[*] Imported %d new tasks" % (new_task_count - old_task_count)