-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.py
185 lines (148 loc) · 5.55 KB
/
migrate.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/python
# Migrate from imagestore1 to imagestore2
import db
import gc
import MySQLdb
import sha, md5
import mx.DateTime
from sqlobject import *
db.db_connect();
#conn = MySQLConnection(host='127.0.0.1', db='imagestore', user='root', passwd='zwarp', debug=0)
#conn = MySQLConnection(host='lurch', db='imagestore', user='imagestore', passwd='im_zwarp', debug=0)
conn = connectionForURI('mysql://imagestore@127.0.0.1/imagestore?cache=0')
#conn = connectionForURI('mysql://imagestore:im_zwarp@lurch/imagestore')
conn.debug=0
__connection__ = conn
class MigImages(SQLObject):
_cacheValues = False
_table = 'images'
_idName = 'img_id'
image = StringCol(dbName='image', sqlType='MEDIUMBLOB', notNone=True)
thumbnail = StringCol(dbName='thumbnail', sqlType='blob')
mime_type = StringCol(length=32, dbName='mime_type')
width = IntCol()
height = IntCol()
orientation = IntCol()
camera_make = StringCol(length=32)
record_time = DateTimeCol(dbName='create_time')
modified_time = DateTimeCol(sqlType='timestamp')
description = StringCol()
comment = StringCol(length=127)
copyright = StringCol(length=127)
photographer = StringCol(length=127)
exposure_time = StringCol(length=10)
brightness = FloatCol()
f_number = FloatCol()
focal_length = IntCol()
exposure_bias = IntCol()
flash = EnumCol(enumValues=['y','n'])
thumb_width = IntCol()
thumb_height = IntCol()
title = StringCol(length=127)
#owner = ForeignKey('User')
owner = IntCol()
visible = EnumCol(enumValues=['public','private'])
hash2 = StringCol(length=32, alternateID=True, unique=True)
class MigUser(SQLObject):
_table = 'users'
_idName = 'user_id'
user_name=StringCol(length=127, alternateID=True)
user_email=StringCol(length=127)
passwd=StringCol(length=16)
user_flags=IntCol()
class MigKeywords(SQLObject):
_table = 'keywords'
_idName = 'word_id'
word = StringCol(length=127, alternateID=True)
class MigKeyImage(SQLObject):
_table = 'image_kw'
_idName = 'id'
word_id = IntCol()
img_id = IntCol()
collection = db.defaultCollection()
import db, insert
import gc
def expire_all():
c = conn
for k in c.cache.caches.keys():
c.cache.caches[k].expireAll()
gc.collect()
prev_accum={}
def pop():
accum={}
gc.collect()
for o in gc.get_objects():
try:
accum[o.__class__] = accum.get(o.__class__, 0) + 1
except (AttributeError), x:
pass
global prev_accum
delta = [ (accum[k] - prev_accum.get(k,0), k) for k in accum.keys() if accum[k] != prev_accum.get(k,0) ]
delta.sort(lambda a,b: -cmp(a[0],b[0]))
for (d, k) in delta[:10]:
try:
print ' %s: %d + %d' % (k, accum[k], d)
except x:
print x
pass
prev_accum=accum
def migrate(sel):
for migp in sel:
print 'migp %d: %s: hash=%s' % (migp.id, migp.record_time, migp.hash2)
#pop()
if db.Picture.select(db.Picture.q.md5hash == migp.hash2).count() != 0:
print " already present"
migp.expire()
continue
hash = md5.new()
hash.update(migp.image)
if migp.hash2 != hash.digest().encode('hex'):
print 'WARNING: migrated image %d has bad md5 hash: has %s, expected %s' % (migp.id, migp.hash2, hash.digest().encode('hex'))
owner = migp.owner
if owner == 0:
owner = 1
mig_owner = MigUser.get(owner)
u = insert.get_user(mig_owner.user_email, mig_owner.user_email, mig_owner.user_name)
u.password = mig_owner.passwd
if migp.photographer is not None:
photog = insert.get_user(migp.photographer, migp.photographer, None)
kwlist = MigKeywords.select(AND(MigKeyImage.q.word_id == MigKeywords.q.id,
MigKeyImage.q.img_id == migp.id))
kw = []
for k in kwlist:
kw.append(k.word)
print ' %d keywords: %s' % (migp.id, kw)
try :
pid = insert.import_image(data=migp.image, orig_filename=None,
owner=u, public=migp.visible,
collection=collection,
keywords=kw,
mimetype=migp.mime_type,
id = migp.id, # preserve ID
md5hash = migp.hash2,
record_time = migp.record_time,
photographer = photog,
description = migp.description,
title = migp.title,
orientation = migp.orientation,
copyright = migp.copyright)
except Exception,y:
pid = -1
print 'Import failed? '+str(y)
if pid > 0:
p = db.Picture.get(pid)
p.getimage(True)
p.getthumb(True)
p.expire()
# Add any comments
if migp.comment is not None and migp.comment.strip() != '':
c = db.Comment(user=u, picture=p, comment=migp.comment, timestamp=migp.modified_time)
p.sync()
gc.collect()
if __name__ == '__main__':
import sys
q = None
if len(sys.argv) > 1:
q = MigImages.q.id >= int(sys.argv[1])
print 'starting from %d' % int(sys.argv[1])
migrate(MigImages.select(q, orderBy=MigImages.q.id, lazyColumns=True))