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