]> code.delx.au - pymsnt/blob - src/housekeep.py
Trunk is broken. Don't use!
[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 import utils
5 import config
6 import xdb
7
8 import shutil
9 import os
10 import os.path
11
12
13 def init():
14 global noteList
15 global noteListF
16
17 try:
18 notes = NotesToMyself()
19 for note in noteList:
20 if notes.check(note):
21 noteListF[noteList.index(note)]()
22 notes.append(note)
23 notes.save()
24 except:
25 print "An error occurred during one of the automatic data update routines. Please report this bug."
26 raise
27
28
29 class NotesToMyself:
30 def __init__(self):
31 pre = os.path.abspath(config.spooldir) + "/" + config.jid + "/"
32 self.filename = pre + "/notes_to_myself"
33 self.notes = []
34
35 if os.path.exists(self.filename):
36 f = open(self.filename, "r")
37 self.notes = [x.strip() for x in f.readlines()]
38 f.close()
39 elif not os.path.exists(pre):
40 global noteList
41 self.notes = noteList
42 os.makedirs(pre)
43
44 def check(self, note):
45 return self.notes.count(note) == 0
46
47 def append(self, note):
48 if self.check(note):
49 self.notes.append(note)
50
51 def save(self):
52 f = open(self.filename, "w")
53 for note in self.notes:
54 f.write(note + "\n")
55 f.close()
56
57
58
59 def doSpoolPrepCheck():
60 pre = os.path.abspath(config.spooldir) + "/" + config.jid + "/"
61
62 print "Checking spool files and stringprepping any if necessary...",
63
64 for file in os.listdir(pre):
65 try:
66 file = xdb.unmangle(file).decode("utf-8")
67 filej = utils.jid(file).full()
68 if(file != filej):
69 file = xdb.mangle(file)
70 filej = xdb.mangle(filej)
71 if(os.path.exists(filej)):
72 print "Need to move", file, "to", filej, "but the latter exists!\nAborting!"
73 os.exit(1)
74 else:
75 shutil.move(pre + file, pre + filej)
76 except:
77 print "File: " + file
78 raise
79 print "done"
80
81
82 def doHashDirUpgrade():
83 print "Upgrading your XDB structure to use hashed directories for speed...",
84
85 # Do avatars...
86 pre = os.path.abspath(config.spooldir) + "/" + config.jid + "/avatars/"
87 if os.path.exists(pre):
88 for file in os.listdir(pre):
89 try:
90 if os.path.isfile(pre + file):
91 pre2 = pre + file[0:3] + "/"
92 if not os.path.exists(pre2):
93 os.makedirs(pre2)
94 shutil.move(pre + file, pre2 + file)
95 except:
96 print "File: " + file
97 raise
98
99 # Do spool files...
100 pre = os.path.abspath(config.spooldir) + "/" + config.jid + "/"
101 if os.path.exists(pre):
102 for file in os.listdir(pre):
103 try:
104 if os.path.isfile(pre + file) and file != "notes_to_myself":
105 hash = file[0:2]
106 pre2 = pre + hash + "/"
107 if not os.path.exists(pre2):
108 os.makedirs(pre2)
109
110 if(os.path.exists(pre2 + file)):
111 print "Need to move", file, "to", pre2 + file, "but the latter exists!\nAborting!"
112 os.exit(1)
113 else:
114 shutil.move(pre + file, pre2 + file)
115 except:
116 print "File: " + file
117
118 print "done"
119
120
121
122 noteList = ["doSpoolPrepCheck", "doHashDirUpgrade"]
123 noteListF = [doSpoolPrepCheck, doHashDirUpgrade]
124