]> code.delx.au - pymsnt/blob - src/housekeep.py
Groupchat timeout is configurable.
[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 os
12 import os.path
13
14
15 X = os.path.sep
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) + X + config.jid + X
36 self.filename = pre + X + "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 else:
44 if not os.path.exists(pre):
45 os.makedirs(pre)
46 global noteList
47 self.notes = noteList
48
49 def check(self, note):
50 return self.notes.count(note) == 0
51
52 def append(self, note):
53 if self.check(note):
54 self.notes.append(note)
55
56 def save(self):
57 f = open(self.filename, "w")
58 for note in self.notes:
59 f.write(note + "\n")
60 f.close()
61
62
63
64 def doSpoolPrepCheck():
65 pre = os.path.abspath(config.spooldir) + X + config.jid + X
66
67 print "Checking spool files and stringprepping any if necessary...",
68
69 for file in os.listdir(pre):
70 try:
71 file = xdb.unmangle(file).decode("utf-8")
72 filej = xmlw.jid.intern(file).full()
73 if(file != filej):
74 file = xdb.mangle(file)
75 filej = xdb.mangle(filej)
76 if(os.path.exists(filej)):
77 print "Need to move", file, "to", filej, "but the latter exists!\nAborting!"
78 os.exit(1)
79 else:
80 shutil.move(pre + file, pre + filej)
81 except:
82 print "File: " + file
83 raise
84 print "done"
85
86
87 def doHashDirUpgrade():
88 print "Upgrading your XDB structure to use hashed directories for speed...",
89
90 # Do avatars...
91 pre = os.path.join(os.path.abspath(config.spooldir), config.jid) + X + "avatars" + X
92 if os.path.exists(pre):
93 for file in os.listdir(pre):
94 try:
95 if os.path.isfile(pre + file):
96 pre2 = pre + file[0:3] + X
97 if not os.path.exists(pre2):
98 os.makedirs(pre2)
99 shutil.move(pre + file, pre2 + file)
100 except:
101 print "File: " + file
102 raise
103
104 # Do spool files...
105 pre = os.path.join(os.path.abspath(config.spooldir), config.jid) + X
106 if os.path.exists(pre):
107 for file in os.listdir(pre):
108 try:
109 if os.path.isfile(pre + file) and file != "notes_to_myself":
110 hash = file[0:2]
111 pre2 = pre + hash + X
112 if not os.path.exists(pre2):
113 os.makedirs(pre2)
114
115 if(os.path.exists(pre2 + file)):
116 print "Need to move", file, "to", pre2 + file, "but the latter exists!\nAborting!"
117 os.exit(1)
118 else:
119 shutil.move(pre + file, pre2 + file)
120 except:
121 print "File: " + file
122
123 print "done"
124
125
126
127 noteList = ["doSpoolPrepCheck", "doHashDirUpgrade"]
128 noteListF = [doSpoolPrepCheck, doHashDirUpgrade]
129