Edgewall Software

Ticket #10313: 10313-attachment-upgrade-r11160.patch

File 10313-attachment-upgrade-r11160.patch, 2.9 KB (added by Remy Blank, 13 years ago)

More admin-friendly attachment upgrade

  • trac/attachment.py

    diff --git a/trac/attachment.py b/trac/attachment.py
    a b import os.path  
    2525import re
    2626import shutil
    2727import sys
     28
    2829import unicodedata
    2930
    3031from genshi.builder import tag
    class AttachmentSetup(Component):  
    456457            except OSError:
    457458                pass
    458459
     460
    459461        try:
    460462            for dir, dirs, files in os.walk(old_dir, topdown=False):
    461463                os.rmdir(dir)
     464
    462465        except OSError, e:
    463             self.log.error("Can't delete old attachments directory %s: %s",
    464                            old_dir, exception_to_unicode(e, traceback=True))
    465             printerr(_("Error while deleting old attachments directory. "
    466                        "Please move or remove files in\nthe directory and try "
    467                        "again."))
    468             raise
     466            self.log.warning("Can't delete old attachments directory %s: %s",
     467                             old_dir, exception_to_unicode(e))
     468
     469        # Some unreferenced files remain, inform admin
     470        stale_name = time.strftime('attachments-stale-%Y%m%d')
     471        stale_dir = os.path.join(path, stale_name)
     472        try:
     473            os.rename(old_dir, stale_dir)
     474        except OSError, e:
     475            self.log.warning("Can't move old attachments directory %s: %s",
     476                             old_dir, exception_to_unicode(e))
     477            printerr(_("The upgrade of attachments was successful, but some "
     478                       "files weren't referenced in the database. The old "
     479                       "attachments directory\n\n  %(dir)s\n\n"
     480                       "could not be moved due to:\n\n  %(exception)s\n\n"
     481                       "Please move this directory out of the environment. "
     482                       "Note that Trac won't run as long as the directory "
     483                       "exists.\n",
     484                       dir=stale_dir, exception=exception_to_unicode(e)))
     485        else:
     486            printerr(_("The upgrade of attachments was successful, but some "
     487                       "files weren't referenced in the database. They were "
     488                       "moved to:\n\n  %(dir)s\n",
     489                       dir=stale_dir))
    469490
    470491    def _move_attachment_file(self, attachment):
    471492        old_path = attachment._get_path_old(attachment.parent_realm,
    472493                                            attachment.parent_id,
    473494                                            attachment.filename)
    474495        if os.path.isfile(old_path):
    475             os.renames(old_path, attachment.path)
     496            try:
     497                os.renames(old_path, attachment.path)
     498            except OSError, e:
     499                printerr(_("Unable to move attachment from:\n\n"
     500                           "  %(old_path)s\n\nto:\n\n  %(new_path)s\n",
     501                           old_path=old_path, new_path=attachment.path))
     502                raise
    476503
    477504
    478505class AttachmentModule(Component):