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

« back to all changes in this revision

Viewing changes to vendor/boto/boto/mapreduce/pdb_describe

  • 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
#!/usr/bin/env python
 
2
# Copyright (c) 2006-2008 Mitch Garnaat http://garnaat.org/
 
3
#
 
4
# Permission is hereby granted, free of charge, to any person obtaining a
 
5
# copy of this software and associated documentation files (the
 
6
# "Software"), to deal in the Software without restriction, including
 
7
# without limitation the rights to use, copy, modify, merge, publish, dis-
 
8
# tribute, sublicense, and/or sell copies of the Software, and to permit
 
9
# persons to whom the Software is furnished to do so, subject to the fol-
 
10
# lowing conditions:
 
11
#
 
12
# The above copyright notice and this permission notice shall be included
 
13
# in all copies or substantial portions of the Software.
 
14
#
 
15
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 
16
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
 
17
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
 
18
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
 
19
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
20
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 
21
# IN THE SOFTWARE.
 
22
import sys
 
23
from optparse import OptionParser
 
24
from boto.mapreduce.partitiondb import PartitionDB, Partition, Version
 
25
from boto.exception import SDBPersistenceError
 
26
from boto.sdb.persist import get_manager, get_domain
 
27
 
 
28
USAGE = """
 
29
  SYNOPSIS
 
30
    %prog [options]
 
31
  DESCRIPTION
 
32
    List and describe your PartitionDBs.
 
33
    Called with no options, all PartitionDB objects defined in your default
 
34
    domain (as specified in the "default_domain" option in the "[Persist]"
 
35
    section of your boto config file) will be listed.
 
36
    When called with a particular PartitionDB name (using -p option) all
 
37
    Version objects of that PartitionDB object will be listed.
 
38
    When called with the -p option and a particular Version name specified
 
39
    (using the -v option) all Partitions in that Version object will be listed.
 
40
"""
 
41
class Describe:
 
42
 
 
43
    def __init__(self):
 
44
        self.parser = OptionParser(usage=USAGE)
 
45
        self.parser.add_option('-d', '--domain-name', action='store', type='string',
 
46
                               help='name of the SimpleDB domain where PDB objects are stored')
 
47
        self.parser.add_option('-n', '--num-entries', action='store', type='int',
 
48
                               help='maximum number of entries to print (default 100)')
 
49
        self.parser.set_defaults(num_entries=100)
 
50
        self.parser.add_option('-p', '--pdb-name', action='store', type='string',
 
51
                               help='name of the PDB to describe')
 
52
        self.parser.add_option('-v', '--version-name', action='store', type='string',
 
53
                               help='name of the PDB Version to describe')
 
54
        self.options, self.args = self.parser.parse_args()
 
55
        self.prog_name = sys.argv[0]
 
56
 
 
57
    def describe_all(self):
 
58
        print 'Using SimpleDB Domain: %s' % get_domain()
 
59
        print 'PDBs:'
 
60
        rs = PartitionDB.list()
 
61
        i = 0
 
62
        for pdb in rs:
 
63
            print '%s\t%s\t%s' % (pdb.id, pdb.name, pdb.bucket_name)
 
64
            i += 1
 
65
            if i == self.options.num_entries:
 
66
                break
 
67
 
 
68
    def describe_pdb(self, pdb_name):
 
69
        print 'Using SimpleDB Domain: %s' % get_domain()
 
70
        print 'PDB: %s' % pdb_name
 
71
        print 'Versions:'
 
72
        try:
 
73
            pdb = PartitionDB.get(name=pdb_name)
 
74
            i = 0
 
75
            for v in pdb.versions:
 
76
                if v.date:
 
77
                    ds = v.date.isoformat()
 
78
                else:
 
79
                    ds = 'unknown'
 
80
                print '%s\t%s\t%s' % (v.id, v.name, ds)
 
81
                i += 1
 
82
                if i == self.options.num_entries:
 
83
                    break
 
84
            cv = pdb.current_version()
 
85
            if cv:
 
86
                print 'Current Version: %s' % cv.name
 
87
            else:
 
88
                print 'Current Version: None'
 
89
        except SDBPersistenceError:
 
90
            self.parser.error('pdb_name (%s) unknown' % pdb_name)
 
91
 
 
92
    def describe_version(self, pdb_name, version_name):
 
93
        print 'Using SimpleDB Domain: %s' % get_domain()
 
94
        print 'PDB: %s' % pdb_name
 
95
        print 'Version: %s' % version_name
 
96
        print 'Partitions:'
 
97
        try:
 
98
            pdb = PartitionDB.get(name=pdb_name)
 
99
            for v in pdb.versions:
 
100
                if v.name == version_name:
 
101
                    i = 0
 
102
                    for p in v.partitions():
 
103
                        print '%s\t%s' % (p.id, p.name)
 
104
                        i += 1
 
105
                        if i == self.options.num_entries:
 
106
                            break
 
107
        except SDBPersistenceError:
 
108
            self.parser.error('pdb_name (%s) unknown' % pdb_name)
 
109
 
 
110
    def main(self):
 
111
        self.options, self.args = self.parser.parse_args()
 
112
        self.manager = get_manager(self.options.domain_name)
 
113
            
 
114
        if self.options.pdb_name:
 
115
            if self.options.version_name:
 
116
                self.describe_version(self.options.pdb_name, self.options.version_name)
 
117
            else:
 
118
                self.describe_pdb(self.options.pdb_name)
 
119
        else:
 
120
            self.describe_all()
 
121
            
 
122
if __name__ == "__main__":
 
123
    describe = Describe()
 
124
    describe.main()