Changeset 4896 for waeup/trunk


Ignore:
Timestamp:
27 Jan 2010, 11:32:58 (15 years ago)
Author:
uli
Message:

Add methods to store processed files into desired locations.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • waeup/trunk/src/waeup/datacenter.py

    r4892 r4896  
    66import logging
    77import os
     8import shutil
    89import struct
    910import grok
     
    6566        """Create standard subdirs.
    6667        """
    67         for name in ['finished', 'pending']:
     68        for name in ['finished', 'unfinished']:
    6869            path = os.path.join(self.storage, name)
    6970            if os.path.exists(path):
     
    254255        return
    255256
    256 
     257    def _moveFile(self, source, dest):
     258        """Move file source to dest preserving ctime, mtime, etc.
     259        """
     260        if not os.path.exists(source):
     261            self.logger.warn('No such source path: %s' % source)
     262            return
     263        if source == dest:
     264            return
     265        shutil.copyfile(source, dest)
     266        shutil.copystat(source, dest)
     267        os.unlink(source)
     268   
     269    def distProcessedFiles(self, successful, source_path, finished_file,
     270                           pending_file, move_orig=True):
     271        """Put processed files into final locations.
     272
     273        ``successful`` is a boolean that tells, whether processing was
     274        successful.
     275
     276        ``source_path``: path to file that was processed.
     277
     278        ``finished_file``, ``pending_file``: paths to the respective
     279        generated .pending and .finished file. The .pending file path
     280        may be ``None``.
     281
     282        See datacenter.txt for more info about how this works.
     283        """
     284        basename = os.path.basename(source_path)
     285        pending_name = basename
     286        pending = False
     287        finished_dir = os.path.join(self.storage, 'finished')
     288        unfinished_dir = os.path.join(self.storage, 'unfinished')
     289
     290        if basename.endswith('.pending.csv'):
     291            maybe_basename = "%s.csv" % basename.split('.', 2)[0]
     292            maybe_src = os.path.join(unfinished_dir, maybe_basename)
     293            if os.path.isfile(maybe_src):
     294                basename = maybe_basename
     295                pending = True
     296               
     297        base, ext = os.path.splitext(basename)
     298        finished_name = "%s.finished%s" % (base, ext)
     299        if not pending:
     300            pending_name = "%s.pending%s" % (base, ext)
     301
     302        # Put .pending and .finished file into respective places...
     303        pending_dest = os.path.join(self.storage, pending_name)
     304        finished_dest = os.path.join(finished_dir, finished_name)
     305        self._moveFile(finished_file, finished_dest)
     306        if pending_file is not None:
     307            self._moveFile(pending_file, pending_dest)
     308
     309        # Put source file into final location...
     310        finished_dest = os.path.join(finished_dir, basename)
     311        unfinished_dest = os.path.join(unfinished_dir, basename)
     312        if successful and not pending:
     313            self._moveFile(source_path, finished_dest)
     314        elif successful and pending:
     315            self._moveFile(unfinished_dest, finished_dest)
     316            os.unlink(source_path)
     317        elif not successful and not pending:
     318            self._moveFile(source_path, unfinished_dest)
     319        return
     320
     321   
    257322class DataCenterFile(object):
    258323    """A description of a file stored in data center.
Note: See TracChangeset for help on using the changeset viewer.