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" |
8a9d4c89 | 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 | |
31 | # dictionary of usernames as keys and dictionaries as values | |
32 | # the second dictionaries have unique mailbox keys and mailboxes as values | |
33 | data = None | |
8301e589 PD |
34 | |
35 | def __init__(self): | |
8fe4e3ff | 36 | # open data file or create one and initialize date if not found |
8301e589 | 37 | try: |
8fe4e3ff PD |
38 | cachefile = open(CACHE_FILENAME, 'rb') |
39 | self.version, self.data = pickle.load(cachefile) | |
8a9d4c89 | 40 | if(self.version != CACHE_VERSION): |
28d8aa17 | 41 | logging.warning("Cache file has version %s and the script version is %s. Deleting cache.", |
428052f4 | 42 | self.version, CACHE_VERSION) |
8a9d4c89 | 43 | raise IOError |
28d8aa17 | 44 | logging.info("Cache file %s loaded", CACHE_FILENAME) |
8fe4e3ff | 45 | logging.debug("%s users found.", len(self.data)) |
8301e589 | 46 | except IOError: |
8a9d4c89 | 47 | self.version = CACHE_VERSION |
8301e589 | 48 | self.data = {} |
8fe4e3ff | 49 | with open(CACHE_FILENAME, 'wb') as cachefile: |
428052f4 | 50 | pickle.dump((self.version, self.data), cachefile) |
8301e589 PD |
51 | |
52 | def __del__(self): | |
7a1d4c35 | 53 | # create temporary file first |
8fe4e3ff | 54 | location = os.path.dirname(CACHE_FILENAME) |
7a1d4c35 | 55 | file_descriptor, tmpname = tempfile.mkstemp(dir=location) |
8fe4e3ff PD |
56 | try: |
57 | cachefile = os.fdopen(file_descriptor, 'wb') | |
28d8aa17 | 58 | |
8fe4e3ff PD |
59 | # prepare data based on a save flag |
60 | saved_data = {} | |
61 | for user in self.data: | |
62 | saved_data[user] = {} | |
63 | for box_key in self.data[user]: | |
64 | if(self.data[user][box_key].needs_save): | |
65 | saved_data[user][box_key] = self.data[user][box_key] | |
66 | logging.debug("The mailbox %s will be saved.", saved_data[user][box_key].name) | |
67 | if(len(saved_data[user])==0): | |
68 | del saved_data[user] | |
69 | logging.debug("The user %s will not be saved.", user) | |
70 | self.data = saved_data | |
8fe4e3ff PD |
71 | # avoid test mode or cases where nothing needs saving |
72 | if(len(saved_data)==0): | |
5a4bc5d8 | 73 | cachefile.close() |
8fe4e3ff PD |
74 | os.unlink(tmpname) |
75 | return | |
28d8aa17 | 76 | |
8fe4e3ff | 77 | # serialize in file |
8fe4e3ff PD |
78 | pickle.dump((self.version, self.data), cachefile) |
79 | logging.debug("%s users stored.", len(self.data)) | |
5a4bc5d8 PD |
80 | |
81 | # handle windows non-atomic rename | |
82 | if(platform.system()=='Windows'): | |
83 | if(os.path.exists(CACHE_FILENAME)): | |
84 | cachefile.close() | |
85 | os.unlink(CACHE_FILENAME) | |
86 | ||
8fe4e3ff PD |
87 | os.rename(tmpname, CACHE_FILENAME) |
88 | except: | |
28d8aa17 TJ |
89 | os.unlink(tmpname) |
90 | ||
91 | logging.info("Wrote cache file %s", CACHE_FILENAME) | |
8301e589 | 92 | |
7a1d4c35 PD |
93 | def retrieve_cached_mailbox(self, name, uidvalidity, user): |
94 | """Retrieve a cached mailbox or create it.""" | |
95 | box_key = name.strip('"') + uidvalidity | |
96 | if(user not in self.data): | |
97 | self.data[user] = {} | |
8fe4e3ff | 98 | logging.debug("New user %s cached.", user) |
7a1d4c35 PD |
99 | if(box_key not in self.data[user]): |
100 | self.data[user][box_key] = MailboxState(name, uidvalidity, user) | |
8fe4e3ff | 101 | logging.debug("New mailbox %s cached.", box_key) |
7a1d4c35 PD |
102 | return self.data[user][box_key] |
103 | ||
3b81023f | 104 | def report_conflicts(self): |
7a1d4c35 PD |
105 | """Write a date conflicts report in a file.""" |
106 | with open("conflict_stats.txt", 'w') as statsfile: | |
08ba5736 | 107 | statsfile.write("Total date conflicts to be corrected (total1) and total messages without received headers (total2):\n\n") |
7a1d4c35 PD |
108 | owner_total_conflicts = {} |
109 | owner_total_missing = {} | |
110 | for user in self.data: | |
08ba5736 | 111 | statsfile.write("total1 \t\t total2 \t mailbox \n") |
7a1d4c35 PD |
112 | owner_total_conflicts[user] = 0 |
113 | owner_total_missing[user] = 0 | |
114 | for box_key in self.data[user]: | |
115 | owner_total_conflicts[user] += self.data[user][box_key].date_conflicts | |
116 | owner_total_missing[user] += self.data[user][box_key].no_received_field | |
08ba5736 PD |
117 | statsfile.write("{0} \t\t {1} \t\t {2}\n"\ |
118 | .format(self.data[user][box_key].date_conflicts, | |
119 | self.data[user][box_key].no_received_field, | |
120 | self.data[user][box_key].name)) | |
121 | statsfile.write("\n{0} \t\t {1} \t\t {2}\n\n"\ | |
122 | .format(owner_total_conflicts[user], owner_total_missing[user], user)) | |
8301e589 | 123 | return |