~ubuntu-branches/ubuntu/trusty/python-boto/trusty

« back to all changes in this revision

Viewing changes to bin/sdbadmin

  • Committer: Package Import Robot
  • Author(s): Eric Evans
  • Date: 2012-04-15 20:21:21 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20120415202121-3fpf6q355s0xqpyu
Tags: 2.3.0-1
* New upstream release (Closes: #664478)
* Update debian/watch for Boto's move to Github.  Thanks Scott
  Moser. (Closes: #650480)

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
import time
28
28
from boto import sdb
29
29
 
 
30
# Allow support for JSON
 
31
try:
 
32
    import simplejson as json
 
33
except:
 
34
    try:
 
35
        import json
 
36
    except:
 
37
        json = False
 
38
 
30
39
def choice_input(options, default=None, title=None):
31
40
    """
32
41
    Choice input
50
59
    return choice and len(choice) > 0 and choice[0].lower() == "y"
51
60
 
52
61
 
53
 
def dump_db(domain, file_name):
 
62
def dump_db(domain, file_name, use_json=False):
54
63
    """
55
64
    Dump SDB domain to file
56
65
    """
57
 
    doc = domain.to_xml(open(file_name, "w"))
 
66
    f = open(file_name, "w")
 
67
    if use_json:
 
68
        for item in domain:
 
69
            data = {"name": item.name, "attributes": item}
 
70
            print >> f, json.dumps(data)
 
71
    else:
 
72
        doc = domain.to_xml(f)
58
73
 
59
74
def empty_db(domain):
60
75
    """
63
78
    for item in domain:
64
79
        item.delete()
65
80
 
66
 
def load_db(domain, file):
 
81
def load_db(domain, file, use_json=False):
67
82
    """
68
83
    Load a domain from a file, this doesn't overwrite any existing
69
84
    data in the file so if you want to do a full recovery and restore
72
87
    :param domain: The SDB Domain object to load to
73
88
    :param file: The File to load the DB from
74
89
    """
75
 
    domain.from_xml(file)
 
90
    if use_json:
 
91
        for line in file.readlines():
 
92
            if line:
 
93
                data = json.loads(line)
 
94
                item = domain.new_item(data['name'])
 
95
                item.update(data['attributes'])
 
96
                item.save()
 
97
                
 
98
    else:
 
99
        domain.from_xml(file)
76
100
 
77
101
def create_db(domain_name, region_name):
78
102
    """Create a new DB
95
119
    parser.add_option("-c", "--create", help="Create domain", dest="create", default=False, action="store_true")
96
120
 
97
121
    parser.add_option("-a", "--all-domains", help="Operate on all domains", action="store_true", default=False, dest="all_domains")
 
122
    if json:
 
123
        parser.add_option("-j", "--use-json", help="Load/Store as JSON instead of XML", action="store_true", default=False, dest="json")
98
124
    parser.add_option("-d", "--domain", help="Do functions on domain (may be more then one)", action="append", dest="domains")
99
125
    parser.add_option("-f", "--file", help="Input/Output file we're operating on", dest="file_name")
100
126
    parser.add_option("-r", "--region", help="Region (e.g. us-east-1[default] or eu-west-1)", default="us-east-1", dest="region_name")
152
178
                file_name = options.file_name
153
179
            else:
154
180
                file_name = "%s.db" % domain.name
155
 
            dump_db(domain, file_name)
 
181
            dump_db(domain, file_name, options.json)
156
182
 
157
183
    if options.load:
158
184
        for domain in domains:
161
187
                file_name = options.file_name
162
188
            else:
163
189
                file_name = "%s.db" % domain.name
164
 
            load_db(domain, open(file_name, "rb"))
 
190
            load_db(domain, open(file_name, "rb"), options.json)
165
191
 
166
192
 
167
193
    total_time = round(time.time() - stime, 2)