]> code.delx.au - pymsnt/blob - src/housekeep.py
Reimport and tags (0.10.1)
[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 file = xdb.unmangle(file).decode("utf-8")
70 filej = jid.JID(file).full()
71 if(file != filej):
72 file = xdb.mangle(file)
73 filej = xdb.mangle(filej)
74 if(os.path.exists(filej)):
75 print "Need to move", file, "to", filej, "but the latter exists!\nAborting!"
76 os.exit(1)
77 else:
78 shutil.move(pre + file, pre + filej)
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 if os.path.isfile(pre + file):
90 pre2 = pre + file[0:3] + "/"
91 if not os.path.exists(pre2):
92 os.makedirs(pre2)
93 shutil.move(pre + file, pre2 + file)
94
95 # Do spool files...
96 pre = os.path.abspath(config.spooldir) + "/" + config.jid + "/"
97 if os.path.exists(pre):
98 for file in os.listdir(pre):
99 if os.path.isfile(pre + file) and file != "notes_to_myself":
100 hash = file[0:2]
101 pre2 = pre + hash + "/"
102 if not os.path.exists(pre2):
103 os.makedirs(pre2)
104
105 if(os.path.exists(pre2 + file)):
106 print "Need to move", file, "to", pre2 + file, "but the latter exists!\nAborting!"
107 os.exit(1)
108 else:
109 shutil.move(pre + file, pre2 + file)
110
111 print "done"
112
113
114
115 noteList = ["doSpoolPrepCheck", "doHashDirUpgrade"]
116 noteListF = [doSpoolPrepCheck, doHashDirUpgrade]
117