]> code.delx.au - pymsnt/blob - src/housekeep.py
Use md5 hashes for spool dir. Moved avatar dir to root of spool dir.
[pymsnt] / src / housekeep.py
1 # Copyright 2005 James Bunton <james@delx.cjb.net>
2 # Licensed for distribution under the GPL version 2, check COPYING for details
3
4 from tlib import xmlw
5
6 import utils
7 import config
8 import xdb
9
10 import shutil
11 import sys
12 import os
13 import os.path
14
15
16 X = os.path.sep
17
18 def init():
19 global noteList
20 global noteListF
21
22 try:
23 notes = NotesToMyself()
24 for note in noteList:
25 if notes.check(note):
26 noteListF[noteList.index(note)]()
27 notes.append(note)
28 notes.save()
29 except:
30 print "An error occurred during one of the automatic data update routines. Please report this bug."
31 raise
32
33
34 class NotesToMyself:
35 def __init__(self):
36 pre = os.path.abspath(config.spooldir) + X + config.jid + X
37 self.filename = pre + X + "notes_to_myself"
38 self.notes = []
39
40 if os.path.exists(self.filename):
41 f = open(self.filename, "r")
42 self.notes = [x.strip() for x in f.readlines()]
43 f.close()
44 else:
45 if not os.path.exists(pre):
46 os.makedirs(pre)
47 global noteList
48 self.notes = noteList
49
50 def check(self, note):
51 return self.notes.count(note) == 0
52
53 def append(self, note):
54 if self.check(note):
55 self.notes.append(note)
56
57 def save(self):
58 f = open(self.filename, "w")
59 for note in self.notes:
60 f.write(note + "\n")
61 f.close()
62
63
64
65 def doSpoolPrepCheck():
66 pre = os.path.abspath(config.spooldir) + X + config.jid + X
67
68 print "Checking spool files and stringprepping any if necessary...",
69
70 for file in os.listdir(pre):
71 try:
72 if not os.path.isfile(file):
73 continue
74 file = xdb.unmangle(file).decode("utf-8")
75 filej = xmlw.jid.intern(file).full()
76 if(file != filej):
77 file = xdb.mangle(file)
78 filej = xdb.mangle(filej)
79 if(os.path.exists(filej)):
80 print "Need to move", file, "to", filej, "but the latter exists!\nAborting!"
81 sys.exit(1)
82 else:
83 shutil.move(pre + file, pre + filej)
84 except:
85 print "File: " + file
86 raise
87 print "done"
88
89
90 def doHashDirUpgrade():
91 print "Upgrading your XDB structure to use hashed directories for speed...",
92
93 # Do avatars...
94 pre = os.path.join(os.path.abspath(config.spooldir), config.jid) + X + "avatars" + X
95 if os.path.exists(pre):
96 for file in os.listdir(pre):
97 try:
98 if os.path.isfile(pre + file):
99 pre2 = pre + file[0:3] + X
100 if not os.path.exists(pre2):
101 os.makedirs(pre2)
102 shutil.move(pre + file, pre2 + file)
103 except:
104 print "File: " + file
105 raise
106
107 # Do spool files...
108 pre = os.path.join(os.path.abspath(config.spooldir), config.jid) + X
109 if os.path.exists(pre):
110 for file in os.listdir(pre):
111 try:
112 if os.path.isfile(pre + file) and file != "notes_to_myself":
113 hash = file[0:2]
114 pre2 = pre + hash + X
115 if not os.path.exists(pre2):
116 os.makedirs(pre2)
117
118 if(os.path.exists(pre2 + file)):
119 print "Need to move", file, "to", pre2 + file, "but the latter exists!\nAborting!"
120 sys.exit(1)
121 else:
122 shutil.move(pre + file, pre2 + file)
123 except:
124 print "File: " + file
125
126 print "done"
127
128
129 def doMD5HashDirUpgrade():
130 print "Moving the avatar directory from msn.host.com up to the spool directory."
131
132 pre = os.path.join(os.path.abspath(config.spooldir), config.jid) + X
133 if os.path.exists(pre + "avatars"):
134 # Remove the avatars dir that gets created when we import
135 # legacy/glue.py (it only contains the defaultAvatar)
136 shutil.rmtree(config.spooldir + X + "avatars")
137 shutil.move(pre + "avatars", config.spooldir + X + "avatars")
138 else:
139 print "Could not move your cached avatars directory automatically. It is safe to delete it, the avatars will be recreated as necessary."
140
141
142 print "Upgrading your spool directory to use md5 hashes."
143
144 dirlist = os.listdir(pre)
145 dir, hash, file = "","",""
146 try:
147 for dir in dirlist:
148 if not os.path.isdir(pre + dir) or dir == "avatars" or len(dir) == 3:
149 continue
150 pre2 = pre + dir + X
151 for file in os.listdir(pre2):
152 if not os.path.isfile(pre2 + file):
153 continue
154 hash = xdb.makeHash(os.path.splitext(file)[0])
155 if not os.path.exists(pre + hash):
156 os.makedirs(pre + hash)
157 shutil.move(pre2 + file, pre + hash + X + file)
158 os.rmdir(dir)
159 except Exception, e:
160 print "Error in migration", pre, dir, hash, file, str(e)
161 sys.exit(1)
162
163
164
165 noteList = ["doSpoolPrepCheck", "doHashDirUpgrade", "doMD5HashDirUpgrade"]
166 noteListF = [doSpoolPrepCheck, doHashDirUpgrade, doMD5HashDirUpgrade]
167