~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to vendor/boto/boto/services/submit.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
 
2
#
 
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-
 
9
# lowing conditions:
 
10
#
 
11
# The above copyright notice and this permission notice shall be included
 
12
# in all copies or substantial portions of the Software.
 
13
#
 
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
 
20
# IN THE SOFTWARE.
 
21
 
 
22
import time
 
23
import os
 
24
 
 
25
 
 
26
class Submitter:
 
27
 
 
28
    def __init__(self, sd):
 
29
        self.sd = 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')
 
34
 
 
35
    def get_key_name(self, fullpath, prefix):
 
36
        key_name = fullpath[len(prefix):]
 
37
        l = key_name.split(os.sep)
 
38
        return '/'.join(l)
 
39
 
 
40
    def write_message(self, key, metadata):
 
41
        if self.queue:
 
42
            m = self.queue.new_message()
 
43
            m.for_key(key, metadata)
 
44
            if self.output_bucket:
 
45
                m['OutputBucket'] = self.output_bucket.name
 
46
            self.queue.write(m)
 
47
 
 
48
    def submit_file(self, path, metadata=None, cb=None, num_cb=0, prefix='/'):
 
49
        if not metadata:
 
50
            metadata = {}
 
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)
 
56
 
 
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)
 
61
        total = 0
 
62
        metadata = {}
 
63
        if tags:
 
64
            metadata['Tags'] = tags
 
65
        l = []
 
66
        for t in time.gmtime():
 
67
            l.append(str(t))
 
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):
 
73
                if ignore_dirs:
 
74
                    for ignore in ignore_dirs:
 
75
                        if ignore in dirs:
 
76
                            dirs.remove(ignore)
 
77
                for file in files:
 
78
                    fullpath = os.path.join(root, file)
 
79
                    if status:
 
80
                        print 'Submitting %s' % fullpath
 
81
                    self.submit_file(fullpath, metadata, cb, num_cb, prefix)
 
82
                    total += 1
 
83
        elif os.path.isfile(path):
 
84
            self.submit_file(path, metadata, cb, num_cb)
 
85
            total += 1
 
86
        else:
 
87
            print 'problem with %s' % path
 
88
        return (metadata['Batch'], total)