[7193] | 1 | ## $Id: datacenter.py 8516 2012-05-25 02:02:44Z uli $ |
---|
| 2 | ## |
---|
| 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
| 4 | ## This program is free software; you can redistribute it and/or modify |
---|
| 5 | ## it under the terms of the GNU General Public License as published by |
---|
| 6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | ## (at your option) any later version. |
---|
| 8 | ## |
---|
| 9 | ## This program is distributed in the hope that it will be useful, |
---|
| 10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | ## GNU General Public License for more details. |
---|
| 13 | ## |
---|
| 14 | ## You should have received a copy of the GNU General Public License |
---|
| 15 | ## along with this program; if not, write to the Free Software |
---|
| 16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | ## |
---|
[7819] | 18 | """Kofa data center. |
---|
[4146] | 19 | |
---|
| 20 | The waeup data center cares for management of upload data and provides |
---|
| 21 | tools for importing/exporting CSV data. |
---|
| 22 | """ |
---|
| 23 | import os |
---|
[8512] | 24 | import re |
---|
[4896] | 25 | import shutil |
---|
[4146] | 26 | import grok |
---|
| 27 | from datetime import datetime |
---|
[7568] | 28 | from zope.component import getUtility |
---|
[4883] | 29 | from zope.component.interfaces import ObjectEvent |
---|
[7811] | 30 | from waeup.kofa.interfaces import (IDataCenter, IDataCenterFile, |
---|
[7568] | 31 | IDataCenterStorageMovedEvent, |
---|
[7579] | 32 | IDataCenterConfig) |
---|
[7811] | 33 | from waeup.kofa.utils.helpers import copy_filesystem_tree |
---|
| 34 | from waeup.kofa.utils.logger import Logger |
---|
[4146] | 35 | |
---|
[8512] | 36 | #: Regular expression describing a logfile name with backup extension |
---|
[8516] | 37 | RE_LOGFILE_BACKUP_NAME = re.compile('^.+\.\d+$') |
---|
[8512] | 38 | |
---|
[6578] | 39 | class DataCenter(grok.Container, Logger): |
---|
[4146] | 40 | """A data center contains CSV files. |
---|
| 41 | """ |
---|
[4669] | 42 | grok.implements(IDataCenter) |
---|
[4166] | 43 | |
---|
[7811] | 44 | logger_name = 'waeup.kofa.${sitename}.datacenter' |
---|
[6578] | 45 | logger_filename = 'datacenter.log' |
---|
[4873] | 46 | |
---|
[4892] | 47 | def __init__(self, *args, **kw): |
---|
| 48 | super(DataCenter, self).__init__(*args, **kw) |
---|
[7579] | 49 | self.storage = getUtility(IDataCenterConfig)['path'] |
---|
[4892] | 50 | self._createSubDirs() |
---|
[6286] | 51 | |
---|
[4892] | 52 | def _createSubDirs(self): |
---|
| 53 | """Create standard subdirs. |
---|
| 54 | """ |
---|
[8372] | 55 | for name in ['finished', 'unfinished', 'logs', 'deleted']: |
---|
[4892] | 56 | path = os.path.join(self.storage, name) |
---|
| 57 | if os.path.exists(path): |
---|
| 58 | continue |
---|
| 59 | os.mkdir(path) |
---|
| 60 | return |
---|
[6286] | 61 | |
---|
[8395] | 62 | @property |
---|
| 63 | def deleted_path(self): |
---|
| 64 | """Get the path for deleted object data. |
---|
| 65 | """ |
---|
| 66 | return os.path.join(self.storage, 'deleted') |
---|
| 67 | |
---|
[4858] | 68 | def getFiles(self, sort='name'): |
---|
[4146] | 69 | """Get a list of files stored in `storage`. |
---|
[4574] | 70 | |
---|
| 71 | Files are sorted by basename. |
---|
[4146] | 72 | """ |
---|
| 73 | result = [] |
---|
| 74 | if not os.path.exists(self.storage): |
---|
| 75 | return result |
---|
[4574] | 76 | for filename in sorted(os.listdir(self.storage)): |
---|
[4146] | 77 | fullpath = os.path.join(self.storage, filename) |
---|
| 78 | if not os.path.isfile(fullpath): |
---|
| 79 | continue |
---|
| 80 | result.append(DataCenterFile(fullpath)) |
---|
[4858] | 81 | if sort == 'date': |
---|
| 82 | # sort results in newest-first order... |
---|
| 83 | result = sorted(result, key=lambda x: x.getTimeStamp(), |
---|
| 84 | reverse=True) |
---|
[4146] | 85 | return result |
---|
| 86 | |
---|
[8516] | 87 | def getLogFiles(self, exclude_backups=True): |
---|
[4908] | 88 | """Get the files from logs/ subdir. Files are sorted by name. |
---|
[8516] | 89 | |
---|
| 90 | By default backup logs ('app.log.1', etc.) are excluded. |
---|
[4858] | 91 | """ |
---|
| 92 | result = [] |
---|
[4908] | 93 | logdir = os.path.join(self.storage, 'logs') |
---|
| 94 | if not os.path.exists(logdir): |
---|
| 95 | os.mkdir(logdir) |
---|
| 96 | for name in sorted(os.listdir(logdir)): |
---|
| 97 | if not os.path.isfile(os.path.join(logdir, name)): |
---|
[4858] | 98 | continue |
---|
[8516] | 99 | if exclude_backups == True and RE_LOGFILE_BACKUP_NAME.match(name): |
---|
| 100 | continue |
---|
[4858] | 101 | result.append( |
---|
[4908] | 102 | LogFile(os.path.join(self.storage, 'logs', name))) |
---|
[4858] | 103 | return result |
---|
[6286] | 104 | |
---|
[4190] | 105 | def setStoragePath(self, path, move=False, overwrite=False): |
---|
[4146] | 106 | """Set the path where to store files. |
---|
| 107 | """ |
---|
| 108 | path = os.path.abspath(path) |
---|
[4190] | 109 | not_copied = [] |
---|
[4152] | 110 | if not os.path.exists(path): |
---|
| 111 | raise ValueError('The path given does not exist: %s' % path) |
---|
[4173] | 112 | if move is True: |
---|
[7186] | 113 | not_copied = copy_filesystem_tree(self.storage, path, |
---|
[4190] | 114 | overwrite=overwrite) |
---|
[4146] | 115 | self.storage = path |
---|
[4892] | 116 | self._createSubDirs() |
---|
[4883] | 117 | grok.notify(DataCenterStorageMovedEvent(self)) |
---|
[4190] | 118 | return not_copied |
---|
[4146] | 119 | |
---|
[4896] | 120 | def _moveFile(self, source, dest): |
---|
| 121 | """Move file source to dest preserving ctime, mtime, etc. |
---|
| 122 | """ |
---|
| 123 | if not os.path.exists(source): |
---|
| 124 | self.logger.warn('No such source path: %s' % source) |
---|
| 125 | return |
---|
| 126 | if source == dest: |
---|
| 127 | return |
---|
| 128 | shutil.copyfile(source, dest) |
---|
| 129 | shutil.copystat(source, dest) |
---|
| 130 | os.unlink(source) |
---|
[6286] | 131 | |
---|
[4896] | 132 | def distProcessedFiles(self, successful, source_path, finished_file, |
---|
[4996] | 133 | pending_file, mode='create', move_orig=True): |
---|
[4896] | 134 | """Put processed files into final locations. |
---|
[4858] | 135 | |
---|
[4896] | 136 | ``successful`` is a boolean that tells, whether processing was |
---|
| 137 | successful. |
---|
| 138 | |
---|
| 139 | ``source_path``: path to file that was processed. |
---|
| 140 | |
---|
| 141 | ``finished_file``, ``pending_file``: paths to the respective |
---|
| 142 | generated .pending and .finished file. The .pending file path |
---|
| 143 | may be ``None``. |
---|
| 144 | |
---|
[4904] | 145 | If finished file is placed in a location outside the local |
---|
| 146 | storage dir, the complete directory is removed |
---|
[7933] | 147 | afterwards. Regular processors should put their stuff in |
---|
[4904] | 148 | dedicated temporary dirs. |
---|
[6286] | 149 | |
---|
[4896] | 150 | See datacenter.txt for more info about how this works. |
---|
| 151 | """ |
---|
| 152 | basename = os.path.basename(source_path) |
---|
| 153 | pending_name = basename |
---|
| 154 | pending = False |
---|
| 155 | finished_dir = os.path.join(self.storage, 'finished') |
---|
| 156 | unfinished_dir = os.path.join(self.storage, 'unfinished') |
---|
| 157 | |
---|
| 158 | if basename.endswith('.pending.csv'): |
---|
[4996] | 159 | maybe_basename = "%s.csv" % basename.rsplit('.', 3)[0] |
---|
[4896] | 160 | maybe_src = os.path.join(unfinished_dir, maybe_basename) |
---|
| 161 | if os.path.isfile(maybe_src): |
---|
| 162 | basename = maybe_basename |
---|
| 163 | pending = True |
---|
[6286] | 164 | |
---|
[4896] | 165 | base, ext = os.path.splitext(basename) |
---|
[4996] | 166 | finished_name = "%s.%s.finished%s" % (base, mode, ext) |
---|
[4896] | 167 | if not pending: |
---|
[4996] | 168 | pending_name = "%s.%s.pending%s" % (base, mode, ext) |
---|
[4896] | 169 | |
---|
| 170 | # Put .pending and .finished file into respective places... |
---|
| 171 | pending_dest = os.path.join(self.storage, pending_name) |
---|
| 172 | finished_dest = os.path.join(finished_dir, finished_name) |
---|
| 173 | self._moveFile(finished_file, finished_dest) |
---|
| 174 | if pending_file is not None: |
---|
| 175 | self._moveFile(pending_file, pending_dest) |
---|
| 176 | |
---|
| 177 | # Put source file into final location... |
---|
| 178 | finished_dest = os.path.join(finished_dir, basename) |
---|
| 179 | unfinished_dest = os.path.join(unfinished_dir, basename) |
---|
| 180 | if successful and not pending: |
---|
| 181 | self._moveFile(source_path, finished_dest) |
---|
| 182 | elif successful and pending: |
---|
| 183 | self._moveFile(unfinished_dest, finished_dest) |
---|
| 184 | os.unlink(source_path) |
---|
| 185 | elif not successful and not pending: |
---|
| 186 | self._moveFile(source_path, unfinished_dest) |
---|
[4904] | 187 | |
---|
| 188 | # If finished and pending-file were created in a location |
---|
| 189 | # outside datacenter storage, we remove it. |
---|
[4906] | 190 | maybe_temp_dir = os.path.dirname(finished_file) |
---|
[4904] | 191 | if os.path.commonprefix( |
---|
[4906] | 192 | [self.storage, maybe_temp_dir]) != self.storage: |
---|
| 193 | shutil.rmtree(maybe_temp_dir) |
---|
[4896] | 194 | return |
---|
| 195 | |
---|
[8512] | 196 | def _logfiles(self, basename): |
---|
| 197 | """Get sorted logfiles starting with `basename`. |
---|
| 198 | """ |
---|
| 199 | def numerical_suffix(name): |
---|
| 200 | # return numerical suffix in `name` as number or 0. |
---|
| 201 | suffix = name.rsplit('.', 1)[-1] |
---|
| 202 | try: |
---|
| 203 | return int(suffix) |
---|
| 204 | except ValueError: |
---|
| 205 | return 0 |
---|
| 206 | pass |
---|
| 207 | files = [basename,] |
---|
| 208 | for name in os.listdir(os.path.join(self.storage, 'logs')): |
---|
[8516] | 209 | if RE_LOGFILE_BACKUP_NAME.match(name): |
---|
[8512] | 210 | files.append(name) |
---|
| 211 | return sorted(files, key=numerical_suffix) |
---|
[6286] | 212 | |
---|
[8512] | 213 | def queryLogfiles(self, basename, query=None, limit=0, start=0): |
---|
| 214 | """Search `query` in all logfiles starting with `basename`. |
---|
| 215 | |
---|
| 216 | Returns an iterator of those lines in logfiles starting with |
---|
| 217 | `basename` that match `query`. If you want the result as a |
---|
| 218 | list, simply list() the iterator. |
---|
| 219 | |
---|
| 220 | All logfiles with name `basename` and maybe some numerical |
---|
| 221 | extension ('.1', '.2', ...) are searched for the `query` term |
---|
| 222 | in correct order. So, if you ask for a basename 'app.log', |
---|
| 223 | then any file named 'app.log', 'app.log.1', 'app.log.2', |
---|
| 224 | etc. will be searched in that order. |
---|
| 225 | |
---|
| 226 | The `query` is expected to be a string containing a regular |
---|
| 227 | expression. |
---|
| 228 | |
---|
| 229 | If `limit` is set to some numerical value, at most this number |
---|
| 230 | of lines is returned. |
---|
| 231 | |
---|
| 232 | With `start` you can give the number of first matched line to |
---|
| 233 | return. `start` is zero-based, i.e. the first match has number |
---|
| 234 | 0, the scond one 1, etc. |
---|
| 235 | |
---|
| 236 | Together with `limit` this allows some basic |
---|
| 237 | batching. Please keep in mind that batching might give |
---|
| 238 | unpredictable results, when logfiles change between two |
---|
| 239 | requests. This is not a problem when only one file is searched |
---|
| 240 | and changes include only appending new log messages. |
---|
| 241 | |
---|
| 242 | Matches are found per line only (no multiline matches). |
---|
| 243 | |
---|
| 244 | This method raises ValueError if some basic condition is not |
---|
| 245 | met, for instance if the given query string is not a valid |
---|
| 246 | regular expression. |
---|
| 247 | |
---|
| 248 | Please note, that this exception will happen not before you |
---|
| 249 | really fetch a result line. |
---|
| 250 | """ |
---|
| 251 | try: |
---|
| 252 | re_query = re.compile(query) |
---|
| 253 | except: |
---|
| 254 | raise ValueError('Invalid query string: %s' % query) |
---|
| 255 | |
---|
| 256 | basename = basename.replace('/../', '') |
---|
| 257 | files = self._logfiles(basename) |
---|
| 258 | |
---|
| 259 | # Search the log files |
---|
| 260 | num = 0 |
---|
| 261 | for name in files: |
---|
| 262 | path = os.path.join(self.storage, 'logs', name) |
---|
| 263 | if not os.path.isfile(path): |
---|
| 264 | continue |
---|
| 265 | for line in open(path, 'rb'): |
---|
[8513] | 266 | if not re_query.search(line): |
---|
[8512] | 267 | continue |
---|
| 268 | num += 1 |
---|
| 269 | if (num - 1) < start: |
---|
| 270 | continue |
---|
| 271 | yield line |
---|
| 272 | |
---|
| 273 | if limit and (num - limit >= start): |
---|
| 274 | raise StopIteration |
---|
| 275 | pass |
---|
| 276 | |
---|
[4146] | 277 | class DataCenterFile(object): |
---|
| 278 | """A description of a file stored in data center. |
---|
| 279 | """ |
---|
[4166] | 280 | grok.implements(IDataCenterFile) |
---|
[6286] | 281 | |
---|
[4146] | 282 | def __init__(self, context): |
---|
| 283 | self.context = context |
---|
| 284 | self.name = os.path.basename(self.context) |
---|
| 285 | self.size = self.getSize() |
---|
| 286 | self.uploaddate = self.getDate() |
---|
[4858] | 287 | self.lines = self.getLinesNumber() |
---|
[4146] | 288 | |
---|
| 289 | def getDate(self): |
---|
| 290 | """Get a human readable datetime representation. |
---|
| 291 | """ |
---|
| 292 | date = datetime.fromtimestamp(os.path.getctime(self.context)) |
---|
[6827] | 293 | return date.strftime("%Y-%m-%d %H:%M:%S") |
---|
[4146] | 294 | |
---|
[4858] | 295 | def getTimeStamp(self): |
---|
| 296 | """Get a (machine readable) timestamp. |
---|
| 297 | """ |
---|
| 298 | return os.path.getctime(self.context) |
---|
[6286] | 299 | |
---|
[4146] | 300 | def getSize(self): |
---|
| 301 | """Get a human readable file size. |
---|
| 302 | """ |
---|
| 303 | bytesize = os.path.getsize(self.context) |
---|
| 304 | size = "%s bytes" % bytesize |
---|
| 305 | units = ['kb', 'MB', 'GB'] |
---|
| 306 | for power, unit in reversed(list(enumerate(units))): |
---|
| 307 | power += 1 |
---|
| 308 | if bytesize >= 1024 ** power: |
---|
| 309 | size = "%.2f %s" % (bytesize/(1024.0**power), unit) |
---|
| 310 | break |
---|
| 311 | return size |
---|
| 312 | |
---|
[4858] | 313 | def getLinesNumber(self): |
---|
| 314 | """Get number of lines. |
---|
| 315 | """ |
---|
| 316 | num = 0 |
---|
| 317 | for line in open(self.context, 'rb'): |
---|
| 318 | num += 1 |
---|
| 319 | return num |
---|
[6286] | 320 | |
---|
[4858] | 321 | class LogFile(DataCenterFile): |
---|
| 322 | """A description of a log file. |
---|
| 323 | """ |
---|
| 324 | def __init__(self, context): |
---|
| 325 | super(LogFile, self).__init__(context) |
---|
| 326 | self._markers = dict() |
---|
| 327 | self._parsed = False |
---|
| 328 | self.userid = self.getUserId() |
---|
| 329 | self.mode = self.getMode() |
---|
| 330 | self.stats = self.getStats() |
---|
| 331 | self.source = self.getSourcePath() |
---|
| 332 | |
---|
| 333 | def _parseFile(self, maxline=10): |
---|
| 334 | """Find markers in a file. |
---|
| 335 | """ |
---|
| 336 | if self._parsed: |
---|
| 337 | return |
---|
| 338 | for line in open(self.context, 'rb'): |
---|
| 339 | line = line.strip() |
---|
| 340 | if not ':' in line: |
---|
| 341 | continue |
---|
| 342 | name, text = line.split(':', 1) |
---|
| 343 | self._markers[name.lower()] = text |
---|
| 344 | self._parsed = True |
---|
| 345 | return |
---|
| 346 | |
---|
| 347 | def _getMarker(self, marker): |
---|
| 348 | marker = marker.lower() |
---|
| 349 | if not self._parsed: |
---|
| 350 | self._parseFile() |
---|
| 351 | if marker in self._markers.keys(): |
---|
| 352 | return self._markers[marker] |
---|
[6286] | 353 | |
---|
[4858] | 354 | def getUserId(self): |
---|
| 355 | return self._getMarker('user') or '<UNKNOWN>' |
---|
| 356 | |
---|
| 357 | def getMode(self): |
---|
| 358 | return self._getMarker('mode') or '<NOT SET>' |
---|
| 359 | |
---|
| 360 | def getStats(self): |
---|
| 361 | return self._getMarker('processed') or '<Info not avail.>' |
---|
| 362 | |
---|
| 363 | def getSourcePath(self): |
---|
| 364 | return self._getMarker('source') or None |
---|
[4883] | 365 | |
---|
[4961] | 366 | |
---|
[4883] | 367 | class DataCenterStorageMovedEvent(ObjectEvent): |
---|
| 368 | """An event fired, when datacenter storage moves. |
---|
| 369 | """ |
---|
| 370 | grok.implements(IDataCenterStorageMovedEvent) |
---|