Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (2.8 kB)

1
#!/usr/bin/env python
2

    
3
# Copyright (C) 2011 GRNET S.A.
4
#
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
# General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18
# 02110-1301, USA.
19

    
20
"""
21
A tool that connects to the Pithos backend and returns the size and contents
22
of a pithos object.
23

    
24
Since the backend does not have a "root" account we use the account given in
25
the URL as the user when connecting to the backend.
26
"""
27

    
28
from optparse import OptionParser
29
from sys import exit, stdout, stderr
30

    
31
try:
32
    from pithos.backends.modular import ModularBackend
33
except ImportError:
34
    stderr.write("Pithos backend was not found.\n")
35
    exit(2)
36

    
37

    
38
parser = OptionParser(usage='%prog [options] <URL>')
39
parser.add_option('--db', dest='db', metavar='URI',
40
        help='SQLAlchemy URI of the database [REQUIRED]')
41
parser.add_option('--data', dest='data', metavar='DIR',
42
        help='path to the directory where data are stored [REQUIRED]')
43
parser.add_option('-s', action='store_true', dest='size', default=False,
44
        help='print file size and exit')
45

    
46

    
47
def urlsplit(url):
48
    """Returns (accout, container, object) from a location string"""
49
    
50
    assert url.startswith('pithos://'), "Invalid URL"
51
    t = url.split('/', 4)
52
    assert len(t) == 5, "Invalid URL"
53
    return t[2:5]
54

    
55

    
56
def print_size(backend, url):
57
    """Writes object's size to stdout."""
58
    
59
    account, container, object = urlsplit(url)
60
    meta = backend.get_object_meta(account, account, container, object, None)
61
    print meta['bytes']
62

    
63

    
64
def print_data(backend, url):
65
    """Writes object's size to stdout."""
66
    
67
    account, container, object = urlsplit(url)
68
    size, hashmap = backend.get_object_hashmap(account, account, container,
69
            object)
70
    for hash in hashmap:
71
        block = backend.get_block(hash)
72
        if len(block) > size:
73
            block = block[:size]
74
        stdout.write(block)
75
        size -= len(block)
76

    
77

    
78
def main():
79
    options, args = parser.parse_args()
80
    if len(args) != 1 or not options.db or not options.data:
81
        parser.print_help()
82
        exit(1)
83

    
84
    url = args[0]
85
    backend = ModularBackend(None, options.db, None, options.data)
86
    
87
    if options.size:
88
        print_size(backend, url)
89
    else:
90
        print_data(backend, url)
91

    
92
if __name__ == '__main__':
93
    main()