-
Notifications
You must be signed in to change notification settings - Fork 15
/
update.py
92 lines (82 loc) · 3.27 KB
/
update.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
# Update script to import workspaces from scribeui 0.5 to scribeui 1.0
import os, sys
import sqlite3 as lite
import pprint
import shutil
import stat
def copytree(src, dst, symlinks = False, ignore = None):
if not os.path.exists(dst):
os.makedirs(dst)
shutil.copystat(src, dst)
lst = os.listdir(src)
if ignore:
excl = ignore(src, lst)
lst = [x for x in lst if x not in excl]
for item in lst:
s = os.path.join(src, item)
d = os.path.join(dst, item)
if symlinks and os.path.islink(s):
if os.path.lexists(d):
os.remove(d)
os.symlink(os.readlink(s), d)
try:
st = os.lstat(s)
mode = stat.S_IMODE(st.st_mode)
os.lchmod(d, mode)
except:
pass # lchmod not available
elif os.path.isdir(s):
copytree(s, d, symlinks, ignore)
else:
shutil.copy2(s, d)
try:
con = lite.connect('application/db/database.db')
con.row_factory = lite.Row
cur = con.cursor()
cur.execute('select * from maps, workspaces where workspaces.ws_id=maps.ws_id order by map_id')
con2 = lite.connect('scribeui.sqlite')
con2.row_factory = lite.Row
cur2 = con2.cursor()
maps = cur.fetchall()
for m in maps:
pathOldMap = 'application/workspaces/'+m['ws_name']+'/'+m['map_name']
pathMap = 'workspaces/'+m['ws_name']+'/'+m['map_name']
print("Copying "+pathOldMap+" to "+ pathMap)
if not os.path.exists(pathMap):
copytree(pathOldMap, pathMap)
else:
raise "The directory "+pathMap+" already exists"
cur.execute('select * from groups where map_id=? order by group_index',(m['map_id'],))
groups = cur.fetchall()
if not os.path.isfile(pathMap+'/config'): #if config is already there, we pass
with open(pathMap+'/config', 'w+') as f:
print("Writing new group file")
# write new config files
f.write("ORDER {\n")
for i in range(len(groups)):
if m['map_type'] == "Scribe":
os.rename(pathMap+'/editor/groups/'+groups[i]['group_name'], pathMap+'/editor/groups/'+groups[i]['group_name']+'.layer')
f.write(" "+str(i+1)+": groups/"+groups[i]['group_name']+".layer \n")
elif m['map_type'] == "Standard":
f.write(" "+str(i+1)+": "+groups[i]['group_name']+" \n")
f.write("}")
#delete groups from database
cur2.execute("select * from workspaces where name=?",(m['ws_name'],))
ws = cur2.fetchone()
if not ws:
print("Adding workspace to the database...")
cur2.execute("insert into workspaces(name) values(?)",(m['ws_name'],))
ws_id = cur2.lastrowid
else:
ws_id = ws['id']
print("Adding map to the database...")
cur2.execute("insert into maps(name, workspace_id, type) values(?, ?, ?)",(m['map_name'], ws_id, m['map_type'],))
con2.commit()
if con:
con.close()
if con2:
con2.close()
print("Done!")
except lite.Error, e:
print "Error %s:" % e.args[0]
sys.exit(1)