Commit | Line | Data |
---|---|---|
8301e589 PD |
1 | ''' |
2 | caching_data.py - The module contains the CachingData class. | |
3 | ||
4 | Copyright (c) 2012 Intra2net AG | |
5 | Author: Plamen Dimitrov | |
6 | ||
7 | This program is free software: you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation, either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ''' | |
5a4bc5d8 | 17 | import os, platform, tempfile |
8301e589 | 18 | import pickle |
8fe4e3ff | 19 | import logging |
7a1d4c35 PD |
20 | from mailbox_state import MailboxState |
21 | ||
8fe4e3ff | 22 | CACHE_FILENAME = "message_cache.dat" |
db3f09a6 | 23 | CACHE_VERSION = "1" |
8301e589 PD |
24 | |
25 | class CachingData: | |
26 | """This class is responsible for the caching of data.""" | |
7a1d4c35 PD |
27 | |
28 | # class attributes | |
29 | # integer for version of the cache | |
30 | version = None | |
db3f09a6 PD |
31 | # boolean flag which indicates fallback mode of the cache |
32 | fallback_to_date_header = None | |
7a1d4c35 PD |
33 | # dictionary of usernames as keys and dictionaries as values |
34 | # the second dictionaries have unique mailbox keys and mailboxes as values | |
35 | data = None | |
8301e589 | 36 | |
db3f09a6 | 37 | def __init__(self, fallback_mode): |
8fe4e3ff | 38 | # open data file or create one and initialize date if not found |
8301e589 | 39 | try: |
8fe4e3ff | 40 | cachefile = open(CACHE_FILENAME, 'rb') |
db3f09a6 PD |
41 | cache_info, self.data = pickle.load(cachefile) |
42 | cache_info = cache_info.split(' ') | |
43 | self.version = cache_info[0] | |
8a9d4c89 | 44 | if(self.version != CACHE_VERSION): |
28d8aa17 | 45 | logging.warning("Cache file has version %s and the script version is %s. Deleting cache.", |
428052f4 | 46 | self.version, CACHE_VERSION) |
8a9d4c89 | 47 | raise IOError |
db3f09a6 PD |
48 | self.fallback_to_date_header = cache_info[1] |
49 | if(self.fallback_to_date_header != str(fallback_mode)): | |
50 | logging.warning("Cache file date fallback mode setting is different than current settings. Deleting cache.") | |
51 | raise IOError | |
28d8aa17 | 52 | logging.info("Cache file %s loaded", CACHE_FILENAME) |
8fe4e3ff | 53 | logging.debug("%s users found.", len(self.data)) |
8301e589 | 54 | except IOError: |
8a9d4c89 | 55 | self.version = CACHE_VERSION |
db3f09a6 | 56 | stored_cache_info = self.version + ' ' + str(fallback_mode) |
8301e589 | 57 | self.data = {} |
8fe4e3ff | 58 | with open(CACHE_FILENAME, 'wb') as cachefile: |
db3f09a6 | 59 | pickle.dump((stored_cache_info, self.data), cachefile) |
8301e589 PD |
60 | |
61 | def __del__(self): | |
7a1d4c35 | 62 | # create temporary file first |
8fe4e3ff | 63 | location = os.path.dirname(CACHE_FILENAME) |
7a1d4c35 | 64 | file_descriptor, tmpname = tempfile.mkstemp(dir=location) |
8fe4e3ff PD |
65 | try: |
66 | cachefile = os.fdopen(file_descriptor, 'wb') | |
28d8aa17 | 67 | |
8fe4e3ff PD |
68 | # prepare data based on a save flag |
69 | saved_data = {} | |
70 | for user in self.data: | |
71 | saved_data[user] = {} | |
72 | for box_key in self.data[user]: | |
73 | if(self.data[user][box_key].needs_save): | |
74 | saved_data[user][box_key] = self.data[user][box_key] | |
75 | logging.debug("The mailbox %s will be saved.", saved_data[user][box_key].name) | |
76 | if(len(saved_data[user])==0): | |
77 | del saved_data[user] | |
78 | logging.debug("The user %s will not be saved.", user) | |
79 | self.data = saved_data | |
8fe4e3ff PD |
80 | # avoid test mode or cases where nothing needs saving |
81 | if(len(saved_data)==0): | |
5a4bc5d8 | 82 | cachefile.close() |
8fe4e3ff PD |
83 | os.unlink(tmpname) |
84 | return | |
28d8aa17 | 85 | |
8fe4e3ff | 86 | # serialize in file |
db3f09a6 PD |
87 | stored_cache_info = self.version + ' ' + self.fallback_to_date_header |
88 | pickle.dump((stored_cache_info, self.data), cachefile) | |
8fe4e3ff | 89 | logging.debug("%s users stored.", len(self.data)) |
5a4bc5d8 PD |
90 | |
91 | # handle windows non-atomic rename | |
92 | if(platform.system()=='Windows'): | |
93 | if(os.path.exists(CACHE_FILENAME)): | |
94 | cachefile.close() | |
95 | os.unlink(CACHE_FILENAME) | |
96 | ||
8fe4e3ff PD |
97 | os.rename(tmpname, CACHE_FILENAME) |
98 | except: | |
d061b671 | 99 | # clean up temporary file |
28d8aa17 | 100 | os.unlink(tmpname) |
d061b671 | 101 | raise |
28d8aa17 TJ |
102 | |
103 | logging.info("Wrote cache file %s", CACHE_FILENAME) | |
8301e589 | 104 | |
7a1d4c35 PD |
105 | def retrieve_cached_mailbox(self, name, uidvalidity, user): |
106 | """Retrieve a cached mailbox or create it.""" | |
107 | box_key = name.strip('"') + uidvalidity | |
108 | if(user not in self.data): | |
109 | self.data[user] = {} | |
8fe4e3ff | 110 | logging.debug("New user %s cached.", user) |
7a1d4c35 PD |
111 | if(box_key not in self.data[user]): |
112 | self.data[user][box_key] = MailboxState(name, uidvalidity, user) | |
8fe4e3ff | 113 | logging.debug("New mailbox %s cached.", box_key) |
7a1d4c35 PD |
114 | return self.data[user][box_key] |
115 | ||
3b81023f | 116 | def report_conflicts(self): |
7a1d4c35 PD |
117 | """Write a date conflicts report in a file.""" |
118 | with open("conflict_stats.txt", 'w') as statsfile: | |
119 | owner_total_conflicts = {} | |
120 | owner_total_missing = {} | |
121 | for user in self.data: | |
d3beb6da | 122 | statsfile.write("user: %s\n" % user) |
7a1d4c35 PD |
123 | owner_total_conflicts[user] = 0 |
124 | owner_total_missing[user] = 0 | |
125 | for box_key in self.data[user]: | |
126 | owner_total_conflicts[user] += self.data[user][box_key].date_conflicts | |
127 | owner_total_missing[user] += self.data[user][box_key].no_received_field | |
d3beb6da PD |
128 | statsfile.write("date conflicts: %-15.15s missing header: %-15.15s mailbox: %s\n"\ |
129 | % (self.data[user][box_key].date_conflicts, | |
08ba5736 PD |
130 | self.data[user][box_key].no_received_field, |
131 | self.data[user][box_key].name)) | |
d3beb6da PD |
132 | statsfile.write("date conflicts: %-15.15s missing header: %-15.15s TOTAL \n\n"\ |
133 | % (owner_total_conflicts[user], owner_total_missing[user])) | |
8301e589 | 134 | return |