From 7148f1a664963cbfc991fdc67774d27ac6f30bca Mon Sep 17 00:00:00 2001 From: Wade Brainerd Date: Wed, 30 Sep 2009 03:11:56 +0000 Subject: 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. --- diff --git a/bin/copy-from-journal b/bin/copy-from-journal index 457e9ff..7a10bfd 100755 --- a/bin/copy-from-journal +++ b/bin/copy-from-journal @@ -8,6 +8,7 @@ import sys import os import shutil import optparse +import dbus if os.path.exists("/tmp/olpc-session-bus"): os.environ["DBUS_SESSION_BUS_ADDRESS"] = "unix:path=/tmp/olpc-session-bus" @@ -46,67 +47,75 @@ if __name__ == "__main__": parser.print_help() exit(0) - dsentry = None - - # Get object directly if we were given an explicit object ID. - if options.object_id is not None: - dsentry = datastore.get(options.object_id) - - # Compose the query based on the options provided. - if dsentry is None: - query = {} - - if options.query is not None: - query['query'] = options.query - - # We only want a single file at a time; limit the number of objects - # returned to two, as anything more than one means the criteria were - # not limited enough. - objects, count = datastore.find(query, limit=RETURN_LIMIT, sorting='-mtime') - print '%r' % query - if count > 1: - print 'WARNING: %d objects found; retrieving most recent.' % (count) - for i in xrange(1, RETURN_LIMIT): - objects[i].destroy() - - if count > 0: - dsentry = objects[0] - - # If neither an explicit object ID nor a query gave us data, fail. - if dsentry is None: - print 'ERROR: unable to determine journal object to copy.' - parser.print_help() - exit(0) - - # Print metadata if that is what the user asked for. - if options.show_meta: - print 'Metadata:' - for key, val in dsentry.metadata.get_dictionary().iteritems(): - if key != 'preview': - print '%20s -> %s' % (key, val) - - # If no file is associated with this object, we can't save it out. - if dsentry.get_file_path() == "": - print 'ERROR: no file associated with object, just metadata.' + try: + dsentry = None + + # Get object directly if we were given an explicit object ID. + if options.object_id is not None: + dsentry = datastore.get(options.object_id) + + # Compose the query based on the options provided. + if dsentry is None: + query = {} + + if options.query is not None: + query['query'] = options.query + + # We only want a single file at a time; limit the number of objects + # returned to two, as anything more than one means the criteria were + # not limited enough. + objects, count = datastore.find(query, limit=RETURN_LIMIT, sorting='-mtime') + if count > 1: + print 'WARNING: %d objects found; retrieving most recent.' % (count) + for i in xrange(1, RETURN_LIMIT): + objects[i].destroy() + + if count > 0: + dsentry = objects[0] + + # If neither an explicit object ID nor a query gave us data, fail. + if dsentry is None: + print 'ERROR: unable to determine journal object to copy.' + parser.print_help() + exit(0) + + # Print metadata if that is what the user asked for. + if options.show_meta: + print 'Metadata:' + for key, val in dsentry.metadata.get_dictionary().iteritems(): + if key != 'preview': + print '%20s -> %s' % (key, val) + + # If no file is associated with this object, we can't save it out. + if dsentry.get_file_path() == "": + print 'ERROR: no file associated with object, just metadata.' + dsentry.destroy() + exit(0) + + outname = args[0] + outroot, outext = os.path.splitext(outname) + + # Do our best to determine the output file extension, based on Sugar's + # MIME-type-to-extension mappings. + if outext == "": + mimetype = dsentry.metadata['mime_type'] + outext = sugar.mime.get_primary_extension(mimetype) + if outext == None: + outext = "dsobject" + outext = '.' + outext + + # Lastly, actually copy the file out of the datastore and onto the + # filesystem. + shutil.copyfile(dsentry.get_file_path(), outroot + outext) + print '%s -> %s' % (dsentry.get_file_path(), outroot + outext) + + # Cleanup. dsentry.destroy() - exit(0) - - outname = args[0] - outroot, outext = os.path.splitext(outname) - - # Do our best to determine the output file extension, based on Sugar's - # MIME-type-to-extension mappings. - if outext == "": - mimetype = dsentry.metadata['mime_type'] - outext = sugar.mime.get_primary_extension(mimetype) - if outext == None: - outext = "dsobject" - outext = '.' + outext - # Lastly, actually copy the file out of the datastore and onto the - # filesystem. - shutil.copyfile(dsentry.get_file_path(), outroot + outext) - print '%s -> %s' % (dsentry.get_file_path(), outroot + outext) + except dbus.DBusException: + print 'ERROR: Unable to connect to the datastore.\n'\ + 'Check that you are running in the same environment as the '\ + 'datastore service.' - # Cleanup. - dsentry.destroy() + except Exception, e: + print 'ERROR: %s' % (e) diff --git a/bin/copy-to-journal b/bin/copy-to-journal index cf2cf20..d6ae6f3 100755 --- a/bin/copy-to-journal +++ b/bin/copy-to-journal @@ -11,6 +11,7 @@ import sys import os import optparse from gettext import gettext as _ +import dbus if os.path.exists("/tmp/olpc-session-bus"): os.environ["DBUS_SESSION_BUS_ADDRESS"] = "unix:path=/tmp/olpc-session-bus" @@ -88,5 +89,10 @@ if __name__ == "__main__": entry.destroy() + except dbus.DBusException: + print 'ERROR: Unable to connect to the datastore.\n'\ + 'Check that you are running in the same environment as the '\ + 'datastore service.' + except Exception, e: - print 'Error: %s' % (e) + print 'ERROR: %s' % (e) -- cgit v0.9.1