File: partitiondb.py

package info (click to toggle)
python-boto 1.9b-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,820 kB
  • ctags: 2,583
  • sloc: python: 16,337; makefile: 106
file content (172 lines) | stat: -rw-r--r-- 5,666 bytes parent folder | download
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Copyright (c) 2006-2008 Mitch Garnaat http://garnaat.org/
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#

import random, time, os, datetime
import boto
from boto.sdb.persist.object import SDBObject
from boto.sdb.persist.property import *

class Identifier(object):

    _hex_digits = '0123456789abcdef'

    @classmethod
    def gen(cls, prefix):
        suffix = ''
        for i in range(0,8):
            suffix += random.choice(cls._hex_digits)
        return ts + '-' + suffix
    
class Version(SDBObject):

    name = StringProperty()
    pdb = ObjectProperty(ref_class=SDBObject)
    date = DateTimeProperty()
    
    def __init__(self, id=None, manager=None):
        SDBObject.__init__(self, id, manager)
        if id == None:
            self.name = Identifier.gen('v')
            self.date = datetime.datetime.now()
            print 'created Version %s' % self.name

    def partitions(self):
        """
        Return an iterator containing all Partition objects related to this Version.

        :rtype: iterator of :class:`boto.mapreduce.partitiondb.Partition`
        :return: The Partitions in this Version
        """
        return self.get_related_objects('version', Partition)

    def add_partition(self, name=None):
        """
        Add a new Partition to this Version.

        :type name: string
        :param name: The name of the new Partition (optional)

        :rtype: :class:`boto.mapreduce.partitiondb.Partition`
        :return: The new Partition object
        """
        p = Partition(manager=self.manager, name=name)
        p.version = self
        p.pdb = self.pdb
        p.save()
        return p

    def get_s3_prefix(self):
        if not self.pdb:
            raise ValueError, 'pdb attribute must be set to compute S3 prefix'
        return self.pdb.get_s3_prefix() + self.name + '/'

class PartitionDB(SDBObject):

    name = StringProperty()
    bucket_name = StringProperty()
    versions = ObjectListProperty(ref_class=Version)

    def __init__(self, id=None, manager=None, name='', bucket_name=''):
        SDBObject.__init__(self, id, manager)
        if id == None:
            self.name = name
            self.bucket_name = bucket_name

    def get_s3_prefix(self):
        return self.name + '/'

    def add_version(self):
        """
        Add a new Version to this PartitionDB.  The newly added version becomes the
        current version.

        :rtype: :class:`boto.mapreduce.partitiondb.Version`
        :return: The newly created Version object.
        """
        v = Version()
        v.pdb = self
        v.save()
        self.versions.append(v)
        return v

    def revert(self):
        """
        Revert to the previous version of this PartitionDB.  The current version is removed from the
        list of Versions and the Version immediately preceeding it becomes the current version.
        Note that this method does not delete the Version object or any Partitions related to the
        Version object.

        :rtype: :class:`boto.mapreduce.partitiondb.Version`
        :return: The previous current Version object.
        """
        v = self.current_version()
        if v:
            self.versions.remove(v)
        return v

    def current_version(self):
        """
        Get the currently active Version of this PartitionDB object.

        :rtype: :class:`boto.mapreduce.partitiondb.Version`
        :return: The current Version object or None if there are no Versions associated
                 with this PartitionDB object.
        """
        if self.versions:
            if len(self.versions) > 0:
                return self.versions[-1]
        return None

class Partition(SDBObject):

    def __init__(self, id=None, manager=None, name=None):
        SDBObject.__init__(self, id, manager)
        if id == None:
            self.name = name

    name = StringProperty()
    version = ObjectProperty(ref_class=Version)
    pdb = ObjectProperty(ref_class=PartitionDB)
    data = S3KeyProperty()

    def get_key_name(self):
        return self.version.get_s3_prefix() + self.name

    def upload(self, path, bucket_name=None):
        if not bucket_name:
            bucket_name = self.version.pdb.bucket_name
        s3 = self.manager.get_s3_connection()
        bucket = s3.lookup(bucket_name)
        directory, filename = os.path.split(path)
        self.name = filename
        key = bucket.new_key(self.get_key_name())
        key.set_contents_from_filename(path)
        self.data = key
        self.save()

    def delete(self):
        if self.data:
            self.data.delete()
        SDBObject.delete(self)