Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWade Brainerd <wadetb@gmail.com>2009-09-30 03:11:56 (GMT)
committer Aleksey Lim <alsroot@member.fsf.org>2009-12-20 13:17:05 (GMT)
commit7148f1a664963cbfc991fdc67774d27ac6f30bca (patch)
tree360524e82c85e9034847bb2faac004f5fb304917
parentba0a546d53bb10aa8540f9725488c8f7d0dde250 (diff)
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.
-rwxr-xr-xbin/copy-from-journal131
-rwxr-xr-xbin/copy-to-journal8
2 files changed, 77 insertions, 62 deletions
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)