dupgit /
versions
| 1 | #!/usr/bin/env python |
||
| 2 | # -*- coding: utf8 -*- |
||
| 3 | # |
||
| 4 | # caches.py : module that provides a class and tools to manage caches |
||
| 5 | # for versions.py modules |
||
| 6 | # |
||
| 7 | # (C) Copyright 2016 - 2018 Olivier Delhomme |
||
| 8 | # e-mail : [email protected] |
||
| 9 | # |
||
| 10 | # This program is free software; you can redistribute it and/or modify |
||
| 11 | # it under the terms of the GNU General Public License as published by |
||
| 12 | # the Free Software Foundation; either version 3, or (at your option) |
||
| 13 | # any later version. |
||
| 14 | # |
||
| 15 | # This program is distributed in the hope that it will be useful, |
||
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 18 | # GNU General Public License for more details. |
||
| 19 | # |
||
| 20 | # You should have received a copy of the GNU General Public License |
||
| 21 | # along with this program; if not, write to the Free Software Foundation, |
||
| 22 | # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
||
| 23 | |||
| 24 | 1 | import codecs |
|
| 25 | 1 | import os |
|
| 26 | 1 | import common |
|
| 27 | |||
| 28 | |||
| 29 | 1 | __author__ = "Olivier Delhomme <[email protected]>" |
|
| 30 | |||
| 31 | |||
| 32 | 1 | def open_and_truncate_file(filename): |
|
| 33 | """ |
||
| 34 | Opens filename for writing truncating it to a zero length file |
||
| 35 | and returns a python file object. |
||
| 36 | """ |
||
| 37 | |||
| 38 | 1 | cache_file = codecs.open(filename, 'w', encoding='utf-8') |
|
| 39 | 1 | cache_file.truncate(0) |
|
| 40 | 1 | cache_file.flush() |
|
| 41 | |||
| 42 | 1 | return cache_file |
|
| 43 | |||
| 44 | # End of open_and_truncate_file() function |
||
| 45 | |||
| 46 | |||
| 47 | 1 | class FileCache: |
|
| 48 | """ |
||
| 49 | This class should help in managing cache files |
||
| 50 | """ |
||
| 51 | |||
| 52 | 1 | cache_filename = '' |
|
| 53 | 1 | cache_dict = {} # Dictionnary of projects and their associated version |
|
| 54 | |||
| 55 | 1 | def __init__(self, local_dir, filename): |
|
| 56 | """ |
||
| 57 | Inits the class. 'local_dir' must be a directory where we want to |
||
| 58 | store the cache file named 'filename' |
||
| 59 | """ |
||
| 60 | |||
| 61 | 1 | self.cache_filename = os.path.join(local_dir, filename) |
|
| 62 | 1 | self.cache_dict = {} |
|
| 63 | 1 | self._read_cache_file() |
|
| 64 | |||
| 65 | # End of __init__() function |
||
| 66 | |||
| 67 | |||
| 68 | 1 | def _return_project_and_version_from_line(self, line): |
|
| 69 | """ |
||
| 70 | Splits the line into a project and a version if possible (the line |
||
| 71 | must contain a whitespace. |
||
| 72 | """ |
||
| 73 | |||
| 74 | 1 | line = line.strip() |
|
| 75 | |||
| 76 | 1 | if line.count(' ') > 0: |
|
| 77 | 1 | (project, version) = line.split(' ', 1) |
|
| 78 | |||
| 79 | elif line != '': |
||
| 80 | project = line |
||
| 81 | version = '' |
||
| 82 | |||
| 83 | 1 | return (project, version) |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 84 | |||
| 85 | # End of _return_project_and_version_from_line() function |
||
| 86 | |||
| 87 | |||
| 88 | 1 | def _read_cache_file(self): |
|
| 89 | """ |
||
| 90 | Reads the cache file and puts it into a dictionnary of project with |
||
| 91 | their associated version |
||
| 92 | """ |
||
| 93 | |||
| 94 | 1 | if os.path.isfile(self.cache_filename): |
|
| 95 | 1 | cache_file = codecs.open(self.cache_filename, 'r', encoding='utf-8') |
|
| 96 | |||
| 97 | 1 | for line in cache_file: |
|
| 98 | 1 | (project, version) = self._return_project_and_version_from_line(line) |
|
| 99 | 1 | self.cache_dict[project] = version |
|
| 100 | |||
| 101 | 1 | cache_file.close() |
|
| 102 | |||
| 103 | # End of _read_cache_file() function |
||
| 104 | |||
| 105 | |||
| 106 | 1 | def write_cache_file(self): |
|
| 107 | """ |
||
| 108 | Owerwrites dictionnary cache to the cache file |
||
| 109 | """ |
||
| 110 | |||
| 111 | 1 | cache_file = open_and_truncate_file(self.cache_filename) |
|
| 112 | |||
| 113 | 1 | for (project, version) in self.cache_dict.items(): |
|
| 114 | 1 | cache_file.write('%s %s\n' % (project, version)) |
|
| 115 | |||
| 116 | 1 | cache_file.close() |
|
| 117 | |||
| 118 | # End of write_cache_file() function |
||
| 119 | |||
| 120 | 1 | def print_if_newest_version(self, project, version, debug): |
|
| 121 | """ |
||
| 122 | Prints the project and it's version if it is newer than the |
||
| 123 | one in cache. |
||
| 124 | """ |
||
| 125 | 1 | try: |
|
| 126 | 1 | version_cache = self.cache_dict[project] |
|
| 127 | 1 | common.print_debug(debug, u'\t\tIn cache: {}'.format(version_cache)) |
|
| 128 | |||
| 129 | 1 | if version != version_cache: |
|
| 130 | 1 | common.print_project_version(project, version) |
|
| 131 | |||
| 132 | 1 | except KeyError: |
|
| 133 | 1 | common.print_project_version(project, version) |
|
| 134 | |||
| 135 | # End of print_if_newest_version() function. |
||
| 136 | |||
| 137 | |||
| 138 | 1 | def update_cache_dict(self, project, version, debug): |
|
| 139 | """ |
||
| 140 | Updates cache dictionnary if needed. We always keep the latest version. |
||
| 141 | """ |
||
| 142 | |||
| 143 | 1 | try: |
|
| 144 | 1 | version_cache = self.cache_dict[project] |
|
| 145 | 1 | common.print_debug(debug, u'\t\tUpdating cache with in cache: {} / new ? version {}'.format(version_cache, version)) |
|
| 146 | |||
| 147 | 1 | if version != version_cache: |
|
| 148 | 1 | self.cache_dict[project] = version |
|
| 149 | |||
| 150 | 1 | except KeyError: |
|
| 151 | 1 | self.cache_dict[project] = version |
|
| 152 | |||
| 153 | # End of update_cache_dict() function |
||
| 154 | |||
| 155 | |||
| 156 | 1 | def print_cache_dict(self, sitename): |
|
| 157 | """ |
||
| 158 | Pretty prints the cache dictionary as it is recorded in the files. |
||
| 159 | """ |
||
| 160 | |||
| 161 | 1 | print(u'{}:'.format(sitename)) |
|
| 162 | |||
| 163 | # Gets project and version tuple sorted by project lowered while sorting |
||
| 164 | 1 | for project, version in sorted(self.cache_dict.items(), key=lambda proj: proj[0].lower()): |
|
| 165 | 1 | print(u'\t{} {}'.format(project, version)) |
|
| 166 | |||
| 167 | 1 | print('') |
|
| 168 | |||
| 169 | # End of print_cache_dict() function |
||
| 170 | # End of FileCache class |
||
| 171 | |||
| 172 | |||
| 173 | 1 | class FeedCache: |
|
| 174 | |||
| 175 | 1 | cache_filename = '' |
|
| 176 | 1 | year = 2016 |
|
| 177 | 1 | month = 5 |
|
| 178 | 1 | day = 1 |
|
| 179 | 1 | hour = 0 |
|
| 180 | 1 | minute = 0 |
|
| 181 | 1 | date_minutes = 0 |
|
| 182 | |||
| 183 | |||
| 184 | 1 | def __init__(self, local_dir, filename): |
|
| 185 | """ |
||
| 186 | Inits the class. 'local_dir' must be a directory where we want to |
||
| 187 | store the cache file named 'filename' |
||
| 188 | """ |
||
| 189 | |||
| 190 | 1 | self.cache_filename = os.path.join(local_dir, filename) |
|
| 191 | 1 | self.read_cache_feed() |
|
| 192 | |||
| 193 | # End of __init__() function |
||
| 194 | |||
| 195 | |||
| 196 | 1 | def read_cache_feed(self): |
|
| 197 | """ |
||
| 198 | Reads the cache file which should only contain one date on the |
||
| 199 | first line |
||
| 200 | """ |
||
| 201 | |||
| 202 | 1 | if os.path.isfile(self.cache_filename): |
|
| 203 | 1 | cache_file = codecs.open(self.cache_filename, 'r', encoding='utf-8') |
|
| 204 | 1 | (self.year, self.month, self.day, self.hour, self.minute) = cache_file.readline().strip().split(' ', 4) |
|
| 205 | 1 | self.date_minutes = self._calculate_minutes(int(self.year), int(self.month), int(self.day), int(self.hour), int(self.minute)) |
|
| 206 | 1 | cache_file.close() |
|
| 207 | |||
| 208 | # End of read_cache_feed() function |
||
| 209 | |||
| 210 | |||
| 211 | 1 | def write_cache_feed(self): |
|
| 212 | """ |
||
| 213 | Overwrites the cache file with values stored in this class |
||
| 214 | """ |
||
| 215 | 1 | cache_file = open_and_truncate_file(self.cache_filename) |
|
| 216 | |||
| 217 | 1 | cache_file.write('%s %s %s %s %s' % (self.year, self.month, self.day, self.hour, self.minute)) |
|
| 218 | |||
| 219 | 1 | cache_file.close() |
|
| 220 | |||
| 221 | # End of write_cache_feed() function |
||
| 222 | |||
| 223 | |||
| 224 | 1 | def update_cache_feed(self, date): |
|
| 225 | """ |
||
| 226 | Updates the values stored in the class with the date which should |
||
| 227 | be a time.struct_time |
||
| 228 | """ |
||
| 229 | |||
| 230 | 1 | self.year = date.tm_year |
|
| 231 | 1 | self.month = date.tm_mon |
|
| 232 | 1 | self.day = date.tm_mday |
|
| 233 | 1 | self.hour = date.tm_hour |
|
| 234 | 1 | self.minute = date.tm_min |
|
| 235 | 1 | self.date_minutes = self._calculate_minutes_from_date(date) |
|
| 236 | |||
| 237 | # End of update_cache_feed() function |
||
| 238 | |||
| 239 | |||
| 240 | 1 | def _calculate_minutes(self, year, mon, day, hour, mins): |
|
| 241 | """ |
||
| 242 | Calculate a number of minutes with all parameters and returns |
||
| 243 | this. |
||
| 244 | >>> fc = FeedCache('localdir','filename') |
||
| 245 | >>> fc._calculate_minutes(2016, 5, 1, 0, 0) |
||
| 246 | 1059827040 |
||
| 247 | """ |
||
| 248 | |||
| 249 | 1 | minutes = (year * 365 * 24 * 60) + \ |
|
| 250 | (mon * 30 * 24 * 60) + \ |
||
| 251 | (day * 24 * 60) + \ |
||
| 252 | (hour * 60) + \ |
||
| 253 | (mins) |
||
| 254 | |||
| 255 | 1 | return minutes |
|
| 256 | |||
| 257 | # End of _calculate_minutes() function |
||
| 258 | |||
| 259 | |||
| 260 | 1 | def _calculate_minutes_from_date(self, date): |
|
| 261 | """ |
||
| 262 | Transforms a date in a number of minutes to ease comparisons |
||
| 263 | and returns this number of minutes |
||
| 264 | """ |
||
| 265 | |||
| 266 | 1 | return self._calculate_minutes(date.tm_year, date.tm_mon, date.tm_mday, date.tm_hour, date.tm_min) |
|
| 267 | |||
| 268 | # End of _calculate_minutes() function |
||
| 269 | |||
| 270 | |||
| 271 | 1 | def is_newer(self, date): |
|
| 272 | """ |
||
| 273 | Tells wether "date" is newer than the one in the cache (returns True |
||
| 274 | or not (returns False) |
||
| 275 | """ |
||
| 276 | |||
| 277 | 1 | minutes = self._calculate_minutes_from_date(date) |
|
| 278 | |||
| 279 | 1 | if minutes > self.date_minutes: |
|
| 280 | 1 | return True |
|
| 281 | |||
| 282 | else: |
||
| 283 | 1 | return False |
|
| 284 | |||
| 285 | # End of is_newer() function |
||
| 286 | # End of FeedCache class |
||
| 287 | |||
| 288 | |||
| 289 | 1 | def print_versions_from_cache(local_dir, cache_filename_list): |
|
| 290 | """ |
||
| 291 | Prints all projects and their associated data from the cache |
||
| 292 | """ |
||
| 293 | 1 | for cache_filename in cache_filename_list: |
|
| 294 | 1 | site_cache = FileCache(local_dir, cache_filename) |
|
| 295 | 1 | site_cache.print_cache_dict(cache_filename) |
|
| 296 | |||
| 297 | # End of print_versions_from_cache() |
||
| 298 |