Ticket #573: 0001-Better-error-messages-in-copy-from-journal-and-copy.patch

File 0001-Better-error-messages-in-copy-from-journal-and-copy.patch, 6.7 KB (added by wadeb, 15 years ago)
  • bin/copy-from-journal

    From 90b9d1c643f4492acac9d48dc906a2b97b1bff1e Mon Sep 17 00:00:00 2001
    From: Wade Brainerd <wadetb@gmail.com>
    Date: Tue, 29 Sep 2009 23:11:56 -0400
    Subject: [PATCH] Better error messages in copy-from-journal and copy-to-journal when the datastore cannot be reached.
     (This is common when the scripts are run from another user.)
    
    Also removed a debug print and made the error messages a little more consistent.
    ---
     bin/copy-from-journal |  131 ++++++++++++++++++++++++++-----------------------
     bin/copy-to-journal   |    8 +++-
     2 files changed, 77 insertions(+), 62 deletions(-)
    
    diff --git a/bin/copy-from-journal b/bin/copy-from-journal
    index 457e9ff..7a10bfd 100755
    a b import sys 
    88import os
    99import shutil
    1010import optparse
     11import dbus
    1112
    1213if os.path.exists("/tmp/olpc-session-bus"):
    1314    os.environ["DBUS_SESSION_BUS_ADDRESS"] = "unix:path=/tmp/olpc-session-bus"
    if __name__ == "__main__": 
    4647        parser.print_help()
    4748        exit(0)
    4849
    49     dsentry = None
    50 
    51     # Get object directly if we were given an explicit object ID.
    52     if options.object_id is not None:
    53         dsentry = datastore.get(options.object_id)
    54 
    55     # Compose the query based on the options provided.
    56     if dsentry is None:
    57         query = {}
    58 
    59         if options.query is not None:
    60             query['query'] = options.query
    61 
    62         # We only want a single file at a time; limit the number of objects
    63         # returned to two, as anything more than one means the criteria were
    64         # not limited enough.
    65         objects, count = datastore.find(query, limit=RETURN_LIMIT, sorting='-mtime')
    66         print '%r' % query
    67         if count > 1:
    68             print 'WARNING: %d objects found; retrieving most recent.' % (count)
    69             for i in xrange(1, RETURN_LIMIT):
    70                 objects[i].destroy()
    71 
    72         if count > 0:
    73             dsentry = objects[0]
    74 
    75     # If neither an explicit object ID nor a query gave us data, fail.
    76     if dsentry is None:
    77         print 'ERROR: unable to determine journal object to copy.'
    78         parser.print_help()
    79         exit(0)
    80 
    81     # Print metadata if that is what the user asked for.
    82     if options.show_meta:
    83         print 'Metadata:'
    84         for key, val in dsentry.metadata.get_dictionary().iteritems():
    85             if key != 'preview':
    86                 print '%20s -> %s' % (key, val)
    87 
    88     # If no file is associated with this object, we can't save it out.
    89     if dsentry.get_file_path() == "":
    90         print 'ERROR: no file associated with object, just metadata.'
     50    try:
     51        dsentry = None
     52   
     53        # Get object directly if we were given an explicit object ID.
     54        if options.object_id is not None:
     55            dsentry = datastore.get(options.object_id)
     56   
     57        # Compose the query based on the options provided.
     58        if dsentry is None:
     59            query = {}
     60   
     61            if options.query is not None:
     62                query['query'] = options.query
     63   
     64            # We only want a single file at a time; limit the number of objects
     65            # returned to two, as anything more than one means the criteria were
     66            # not limited enough.
     67            objects, count = datastore.find(query, limit=RETURN_LIMIT, sorting='-mtime')
     68            if count > 1:
     69                print 'WARNING: %d objects found; retrieving most recent.' % (count)
     70                for i in xrange(1, RETURN_LIMIT):
     71                    objects[i].destroy()
     72   
     73            if count > 0:
     74                dsentry = objects[0]
     75   
     76        # If neither an explicit object ID nor a query gave us data, fail.
     77        if dsentry is None:
     78            print 'ERROR: unable to determine journal object to copy.'
     79            parser.print_help()
     80            exit(0)
     81   
     82        # Print metadata if that is what the user asked for.
     83        if options.show_meta:
     84            print 'Metadata:'
     85            for key, val in dsentry.metadata.get_dictionary().iteritems():
     86                if key != 'preview':
     87                    print '%20s -> %s' % (key, val)
     88   
     89        # If no file is associated with this object, we can't save it out.
     90        if dsentry.get_file_path() == "":
     91            print 'ERROR: no file associated with object, just metadata.'
     92            dsentry.destroy()
     93            exit(0)
     94   
     95        outname = args[0]
     96        outroot, outext = os.path.splitext(outname)
     97   
     98        # Do our best to determine the output file extension, based on Sugar's
     99        # MIME-type-to-extension mappings.
     100        if outext == "":
     101            mimetype = dsentry.metadata['mime_type']
     102            outext = sugar.mime.get_primary_extension(mimetype)
     103            if outext == None:
     104                outext = "dsobject"
     105            outext = '.' + outext   
     106   
     107        # Lastly, actually copy the file out of the datastore and onto the
     108        # filesystem.
     109        shutil.copyfile(dsentry.get_file_path(), outroot + outext)
     110        print '%s -> %s' % (dsentry.get_file_path(), outroot + outext)
     111   
     112        # Cleanup.
    91113        dsentry.destroy()
    92         exit(0)
    93 
    94     outname = args[0]
    95     outroot, outext = os.path.splitext(outname)
    96 
    97     # Do our best to determine the output file extension, based on Sugar's
    98     # MIME-type-to-extension mappings.
    99     if outext == "":
    100         mimetype = dsentry.metadata['mime_type']
    101         outext = sugar.mime.get_primary_extension(mimetype)
    102         if outext == None:
    103             outext = "dsobject"
    104         outext = '.' + outext   
    105114
    106     # Lastly, actually copy the file out of the datastore and onto the
    107     # filesystem.
    108     shutil.copyfile(dsentry.get_file_path(), outroot + outext)
    109     print '%s -> %s' % (dsentry.get_file_path(), outroot + outext)
     115    except dbus.DBusException:
     116        print 'ERROR: Unable to connect to the datastore.\n'\
     117              'Check that you are running in the same environment as the '\
     118              'datastore service.'
    110119
    111     # Cleanup.
    112     dsentry.destroy()
     120    except Exception, e:
     121        print 'ERROR: %s' % (e)
  • bin/copy-to-journal

    diff --git a/bin/copy-to-journal b/bin/copy-to-journal
    index cf2cf20..d6ae6f3 100755
    a b import sys 
    1111import os
    1212import optparse
    1313from gettext import gettext as _
     14import dbus
    1415
    1516if os.path.exists("/tmp/olpc-session-bus"):
    1617    os.environ["DBUS_SESSION_BUS_ADDRESS"] = "unix:path=/tmp/olpc-session-bus"
    if __name__ == "__main__": 
    8889   
    8990        entry.destroy()
    9091   
     92    except dbus.DBusException:
     93        print 'ERROR: Unable to connect to the datastore.\n'\
     94              'Check that you are running in the same environment as the '\
     95              'datastore service.'
     96
    9197    except Exception, e:
    92         print 'Error: %s' % (e)
     98        print 'ERROR: %s' % (e)