On Thursday, January 1, 2015 11:23:02 PM UTC-8, Greg Earle wrote:
>
> On Jan 1, 2015, at 9:36 PM, RjOllos wrote: 
>
> > > But now we have a problem; clearly in this Template page there is a 
> > > 'placeholder' for the attached image, but because it's not attached, 
> > > "trac-admin upgrade" won't drag it along for the ride. 
> > 
> > Do you know how the attachments were added to the environment in the 
> first place?  Did the user manually add them to the environment's 
> attachments directory?  If they were added through the Web interface or 
> using TracAdmin, then there should be an entry in the database. 
>
> Sadly I don't, and won't until our vacation is over (next Monday) 
> at the earliest. 
>
> > > So unless the user (who is on vacation right now, unfortunately) goes 
> > > back and attaches all of these images/docs (on 16 sets of Projects!), 
> > > they won't be available in the new 1.0.2 server environment to 
> re-attach 
> > > them later. 
> >   
> > You could script the operations so that the user doesn't have to 
> manually insert the attachments to every environment. 
> > 
> > Before upgrading, you could write a short script to inspect the 
> attachments directory and insert the attachment in the case that there is 
> not a reference to the attachment in the database.  From the directory 
> structure prior to upgrading you know the parent realm, parent resource and 
> resource id of each attachment.  You can use that to construct a query to 
> determine if there is an entry in the attachments table, then insert a row 
> if the entry doesn't exist. 
> > 
> > You probably don't even need to write the queries yourself, you could 
> just use the methods of the Attachment class: 
> > 
> > http://trac.edgewall.org/browser/tags/trac-1.0.2/trac/attachment.py#L121 
>
> Probably beyond my meagre understanding of Trac and my Python abilities, 
> but I'll have a look anyway. 
>
> > Another way to script this is to move the existing file out of the 
> attachments directory, and use the TracAdmin command line tool to attach it 
> to the appropriate resource.  This will create the entry in the attachments 
> table and then put the attachment back into the directory. 
>
> That sounds a lot more promising!  Because the unattached files/structure 
> is the same for all these 16 projects, it can probably be scripted. 
>
> How do I get the attachment list/realm:id? 
>
> -- 
> Trac [/var/trac/projects/test_project]> attachment list 
> Error: Invalid arguments 
>
> attachment list <realm:id> 
>
>     List attachments of a resource 
>
>     The resource is identified by its realm and identifier. 
> -- 
>
> Thanks, 
>
>         - Greg 
>

Could you try the script below on a copy of your environment? The script 
will inspect all the files in the environment attachment's directory, and 
if the attachment doesn't exist in the database but the parent resource 
does exist, an entry will be added in the database for the attachment.

Run it as: 
$ python repair_attachments.py /path/to/env

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 Edgewall Software
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.org/wiki/TracLicense.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.

import os
import sys
import tempfile

from trac.attachment import Attachment
from trac.env import Environment
from trac.resource import Resource, ResourceNotFound, resource_exists


def main():
    env_dir = os.path.abspath(sys.argv[1])
    env = Environment(env_dir)
    attachments_dir = os.path.join(env_dir, 'attachments')
    temp_dir = tempfile.mkdtemp()
    for dir_path, _, file_names in os.walk(attachments_dir):
        for file_name in file_names:
            rel_path = dir_path[len(attachments_dir)+1:]
            parent_realm, parent_id = os.path.split(rel_path)
            try:
                Attachment(env, parent_realm, parent_id, file_name)
            except ResourceNotFound:
                parent_resource = Resource(parent_realm, parent_id)
                if resource_exists(env, parent_resource):
                    attachment_path = os.path.join(dir_path, file_name)
                    temp_path = os.path.join(temp_dir, file_name)
                    attachment = Attachment(env, parent_realm, parent_id)
                    attachment.author = 'admin'
                    print("Repairing attachment %s"
                          % os.path.join(rel_path, file_name))
                    os.rename(attachment_path, temp_path)
                    with open(temp_path, 'r') as file_obj:
                        file_size = os.fstat(file_obj.fileno()).st_size
                        attachment.insert(file_name, file_obj, file_size)
                    os.remove(temp_path)
                else:
                    print("Skipping %s because parent resource "
                          "doesn't exist" % os.path.join(rel_path, 
file_name))
    os.rmdir(temp_dir)


if __name__ == '__main__':
    main()

-- 
You received this message because you are subscribed to the Google Groups "Trac 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/trac-users.
For more options, visit https://groups.google.com/d/optout.

Reply via email to