1
# Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
3
# Permission is hereby granted, free of charge, to any person obtaining a
4
# copy of this software and associated documentation files (the
5
# "Software"), to deal in the Software without restriction, including
6
# without limitation the rights to use, copy, modify, merge, publish, dis-
7
# tribute, sublicense, and/or sell copies of the Software, and to permit
8
# persons to whom the Software is furnished to do so, subject to the fol-
11
# The above copyright notice and this permission notice shall be included
12
# in all copies or substantial portions of the Software.
14
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28
def __init__(self, sd):
30
self.input_bucket = self.sd.get_obj('input_bucket')
31
self.output_bucket = self.sd.get_obj('output_bucket')
32
self.output_domain = self.sd.get_obj('output_domain')
33
self.queue = self.sd.get_obj('input_queue')
35
def get_key_name(self, fullpath, prefix):
36
key_name = fullpath[len(prefix):]
37
l = key_name.split(os.sep)
40
def write_message(self, key, metadata):
42
m = self.queue.new_message()
43
m.for_key(key, metadata)
44
if self.output_bucket:
45
m['OutputBucket'] = self.output_bucket.name
48
def submit_file(self, path, metadata=None, cb=None, num_cb=0, prefix='/'):
51
key_name = self.get_key_name(path, prefix)
52
k = self.input_bucket.new_key(key_name)
53
k.update_metadata(metadata)
54
k.set_contents_from_filename(path, replace=False, cb=cb, num_cb=num_cb)
55
self.write_message(k, metadata)
57
def submit_path(self, path, tags=None, ignore_dirs=None, cb=None, num_cb=0, status=False, prefix='/'):
58
path = os.path.expanduser(path)
59
path = os.path.expandvars(path)
60
path = os.path.abspath(path)
64
metadata['Tags'] = tags
66
for t in time.gmtime():
68
metadata['Batch'] = '_'.join(l)
69
if self.output_domain:
70
self.output_domain.put_attributes(metadata['Batch'], {'type' : 'Batch'})
71
if os.path.isdir(path):
72
for root, dirs, files in os.walk(path):
74
for ignore in ignore_dirs:
78
fullpath = os.path.join(root, file)
80
print 'Submitting %s' % fullpath
81
self.submit_file(fullpath, metadata, cb, num_cb, prefix)
83
elif os.path.isfile(path):
84
self.submit_file(path, metadata, cb, num_cb)
87
print 'problem with %s' % path
88
return (metadata['Batch'], total)