1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
#!/usr/bin/env python
#
# Simple script to export a file from the datastore
# Reinier Heeres, <reinier@heeres.eu>, 2007-12-24
# Phil Bordelon <phil@thenexusproject.org>
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"
from sugar.datastore import datastore
import sugar.mime
# Limit the number of objects returned on an ambiguous query to this number,
# for quicker operation.
RETURN_LIMIT = 2
def build_option_parser():
usage = "Usage: %prog [-o OBJECT_ID] [-q SEARCH_STR] [-m] OUTFILE"
parser = optparse.OptionParser(usage=usage)
parser.add_option("-o", "--object_id", action="store", dest="object_id",
help="Retrieve object with explicit ID OBJECT_ID",
metavar="OBJECT_ID", default=None)
parser.add_option("-q", "--query", action="store", dest="query",
help="Full-text-search the metadata for SEARCH_STR",
metavar="SEARCH_STR", default=None)
parser.add_option("-m", "--metadata", action="store_true",
dest="show_meta",
help="Show all non-preview metadata [default: hide]",
default=False)
return parser
if __name__ == "__main__":
option_parser = build_option_parser()
options, args = option_parser.parse_args()
if len(args) < 1:
option_parser.print_help()
exit(0)
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; getting 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.'
option_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()
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)
|