Statistics
| Branch: | Tag: | Revision:

root / snf-image-host / pithcat @ 21be5a41

History | View | Annotate | Download (2.8 kB)

1 fa7285cf Giorgos Verigakis
#!/usr/bin/env python
2 fa7285cf Giorgos Verigakis
3 0d090771 Giorgos Verigakis
# Copyright (C) 2011 GRNET S.A.
4 fa7285cf Giorgos Verigakis
#
5 0d090771 Giorgos Verigakis
# This program is free software; you can redistribute it and/or modify
6 0d090771 Giorgos Verigakis
# it under the terms of the GNU General Public License as published by
7 0d090771 Giorgos Verigakis
# the Free Software Foundation; either version 2 of the License, or
8 0d090771 Giorgos Verigakis
# (at your option) any later version.
9 fa7285cf Giorgos Verigakis
#
10 0d090771 Giorgos Verigakis
# This program is distributed in the hope that it will be useful, but
11 0d090771 Giorgos Verigakis
# WITHOUT ANY WARRANTY; without even the implied warranty of
12 0d090771 Giorgos Verigakis
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 0d090771 Giorgos Verigakis
# General Public License for more details.
14 fa7285cf Giorgos Verigakis
#
15 0d090771 Giorgos Verigakis
# You should have received a copy of the GNU General Public License
16 0d090771 Giorgos Verigakis
# along with this program; if not, write to the Free Software
17 0d090771 Giorgos Verigakis
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 0d090771 Giorgos Verigakis
# 02110-1301, USA.
19 fa7285cf Giorgos Verigakis
20 fa7285cf Giorgos Verigakis
"""
21 fa7285cf Giorgos Verigakis
A tool that connects to the Pithos backend and returns the size and contents
22 fa7285cf Giorgos Verigakis
of a pithos object.
23 fa7285cf Giorgos Verigakis
24 fa7285cf Giorgos Verigakis
Since the backend does not have a "root" account we use the account given in
25 fa7285cf Giorgos Verigakis
the URL as the user when connecting to the backend.
26 fa7285cf Giorgos Verigakis
"""
27 fa7285cf Giorgos Verigakis
28 fa7285cf Giorgos Verigakis
from optparse import OptionParser
29 643d7283 Nikos Skalkotos
from sys import exit, stdout, stderr
30 fa7285cf Giorgos Verigakis
31 643d7283 Nikos Skalkotos
try:
32 643d7283 Nikos Skalkotos
    from pithos.backends.modular import ModularBackend
33 643d7283 Nikos Skalkotos
except ImportError:
34 643d7283 Nikos Skalkotos
    stderr.write("Pithos backend was not found.\n")
35 643d7283 Nikos Skalkotos
    exit(2)
36 fa7285cf Giorgos Verigakis
37 fa7285cf Giorgos Verigakis
38 fa7285cf Giorgos Verigakis
parser = OptionParser(usage='%prog [options] <URL>')
39 fa7285cf Giorgos Verigakis
parser.add_option('--db', dest='db', metavar='URI',
40 fa7285cf Giorgos Verigakis
        help='SQLAlchemy URI of the database [REQUIRED]')
41 fa7285cf Giorgos Verigakis
parser.add_option('--data', dest='data', metavar='DIR',
42 fa7285cf Giorgos Verigakis
        help='path to the directory where data are stored [REQUIRED]')
43 fa7285cf Giorgos Verigakis
parser.add_option('-s', action='store_true', dest='size', default=False,
44 fa7285cf Giorgos Verigakis
        help='print file size and exit')
45 fa7285cf Giorgos Verigakis
46 fa7285cf Giorgos Verigakis
47 fa7285cf Giorgos Verigakis
def urlsplit(url):
48 fa7285cf Giorgos Verigakis
    """Returns (accout, container, object) from a location string"""
49 fa7285cf Giorgos Verigakis
    
50 fa7285cf Giorgos Verigakis
    assert url.startswith('pithos://'), "Invalid URL"
51 fa7285cf Giorgos Verigakis
    t = url.split('/', 4)
52 fa7285cf Giorgos Verigakis
    assert len(t) == 5, "Invalid URL"
53 fa7285cf Giorgos Verigakis
    return t[2:5]
54 fa7285cf Giorgos Verigakis
55 fa7285cf Giorgos Verigakis
56 fa7285cf Giorgos Verigakis
def print_size(backend, url):
57 fa7285cf Giorgos Verigakis
    """Writes object's size to stdout."""
58 fa7285cf Giorgos Verigakis
    
59 fa7285cf Giorgos Verigakis
    account, container, object = urlsplit(url)
60 2548cd0d Giorgos Verigakis
    meta = backend.get_object_meta(account, account, container, object, None)
61 fa7285cf Giorgos Verigakis
    print meta['bytes']
62 fa7285cf Giorgos Verigakis
63 fa7285cf Giorgos Verigakis
64 fa7285cf Giorgos Verigakis
def print_data(backend, url):
65 fa7285cf Giorgos Verigakis
    """Writes object's size to stdout."""
66 fa7285cf Giorgos Verigakis
    
67 fa7285cf Giorgos Verigakis
    account, container, object = urlsplit(url)
68 fa7285cf Giorgos Verigakis
    size, hashmap = backend.get_object_hashmap(account, account, container,
69 fa7285cf Giorgos Verigakis
            object)
70 fa7285cf Giorgos Verigakis
    for hash in hashmap:
71 fa7285cf Giorgos Verigakis
        block = backend.get_block(hash)
72 a94e134d Giorgos Verigakis
        if len(block) > size:
73 a94e134d Giorgos Verigakis
            block = block[:size]
74 fa7285cf Giorgos Verigakis
        stdout.write(block)
75 a94e134d Giorgos Verigakis
        size -= len(block)
76 fa7285cf Giorgos Verigakis
77 fa7285cf Giorgos Verigakis
78 fa7285cf Giorgos Verigakis
def main():
79 fa7285cf Giorgos Verigakis
    options, args = parser.parse_args()
80 fa7285cf Giorgos Verigakis
    if len(args) != 1 or not options.db or not options.data:
81 fa7285cf Giorgos Verigakis
        parser.print_help()
82 fa7285cf Giorgos Verigakis
        exit(1)
83 fa7285cf Giorgos Verigakis
84 fa7285cf Giorgos Verigakis
    url = args[0]
85 fa7285cf Giorgos Verigakis
    backend = ModularBackend(None, options.db, None, options.data)
86 fa7285cf Giorgos Verigakis
    
87 fa7285cf Giorgos Verigakis
    if options.size:
88 fa7285cf Giorgos Verigakis
        print_size(backend, url)
89 fa7285cf Giorgos Verigakis
    else:
90 fa7285cf Giorgos Verigakis
        print_data(backend, url)
91 fa7285cf Giorgos Verigakis
92 fa7285cf Giorgos Verigakis
if __name__ == '__main__':
93 fa7285cf Giorgos Verigakis
    main()