home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December (DVD) / PCWorld_2007-12_DVD.iso / multimedia / miro / Miro_Installer.exe / xulrunner / python / migrate.py < prev    next >
Encoding:
Text File  |  2007-10-31  |  2.2 KB  |  56 lines

  1. # Miro - an RSS based video player application
  2. # Copyright (C) 2005-2007 Participatory Culture Foundation
  3. #
  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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  17.  
  18.  
  19. """Functions to handle migrating the movies directory."""
  20.  
  21. import logging
  22. import os
  23. import shutil
  24.  
  25. import eventloop
  26.  
  27. def migrate_file(source, dest, callback, retry_after=10, retry_for=60):
  28.     """Try to migrate a file, if this works, callback is called.  If we fail
  29.     because the file is open, we retry migrating the file every so often (by
  30.     default every 10 seconds, stopping after 60 seconds).  This probably only
  31.     makes a difference on Windows.
  32.     """
  33.  
  34.     try:
  35.         shutil.move(source, dest)
  36.     except EnvironmentError, e:
  37.         logging.warn("Error migrating %s to %s (Error: %s)", source, dest, e)
  38.         try:
  39.             os.remove(dest)
  40.         except EnvironmentError:
  41.             pass
  42.         if retry_for > 0:
  43.             if e.errno == 13:
  44.                 # permission denied, assume this means it's open by another
  45.                 # process on windows.
  46.                 logging.info('Retrying migration')
  47.                 eventloop.addTimeout(retry_after, migrate_file, 
  48.                         "Migrate File Retry", args=(source, dest, callback,
  49.                             retry_after, retry_for - retry_after))
  50.     except TypeError, e:
  51.         logging.warn ("Type error migrating %s (%s) to %s (%s) (Error %s)",
  52.                 source, type(source), dest, type(dest), e)
  53.         raise
  54.     else:
  55.         callback()
  56.