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