]> code.delx.au - pymsnt/blob - src/housekeep.py
Fixed housekeep to run cleanly over an already updated 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 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 if not os.path.isfile(file):
72 continue
73 file = xdb.unmangle(file).decode("utf-8")
74 filej = xmlw.jid.intern(file).full()
75 if(file != filej):
76 file = xdb.mangle(file)
77 filej = xdb.mangle(filej)
78 if(os.path.exists(filej)):
79 print "Need to move", file, "to", filej, "but the latter exists!\nAborting!"
80 os.exit(1)
81 else:
82 shutil.move(pre + file, pre + filej)
83 except:
84 print "File: " + file
85 raise
86 print "done"
87
88
89 def doHashDirUpgrade():
90 print "Upgrading your XDB structure to use hashed directories for speed...",
91
92 # Do avatars...
93 pre = os.path.join(os.path.abspath(config.spooldir), config.jid) + X + "avatars" + X
94 if os.path.exists(pre):
95 for file in os.listdir(pre):
96 try:
97 if os.path.isfile(pre + file):
98 pre2 = pre + file[0:3] + X
99 if not os.path.exists(pre2):
100 os.makedirs(pre2)
101 shutil.move(pre + file, pre2 + file)
102 except:
103 print "File: " + file
104 raise
105
106 # Do spool files...
107 pre = os.path.join(os.path.abspath(config.spooldir), config.jid) + X
108 if os.path.exists(pre):
109 for file in os.listdir(pre):
110 try:
111 if os.path.isfile(pre + file) and file != "notes_to_myself":
112 hash = file[0:2]
113 pre2 = pre + hash + X
114 if not os.path.exists(pre2):
115 os.makedirs(pre2)
116
117 if(os.path.exists(pre2 + file)):
118 print "Need to move", file, "to", pre2 + file, "but the latter exists!\nAborting!"
119 os.exit(1)
120 else:
121 shutil.move(pre + file, pre2 + file)
122 except:
123 print "File: " + file
124
125 print "done"
126
127
128
129 noteList = ["doSpoolPrepCheck", "doHashDirUpgrade"]
130 noteListF = [doSpoolPrepCheck, doHashDirUpgrade]
131