Statistics
| Branch: | Tag: | Revision:

root / snf-image-host / pithcat @ 0d090771

History | View | Annotate | Download (2.6 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
30

    
31
from pithos.backends.modular import ModularBackend
32

    
33

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

    
42

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

    
51

    
52
def print_size(backend, url):
53
    """Writes object's size to stdout."""
54
    
55
    account, container, object = urlsplit(url)
56
    meta = backend.get_object_meta(account, account, container, object)
57
    print meta['bytes']
58

    
59

    
60
def print_data(backend, url):
61
    """Writes object's size to stdout."""
62
    
63
    account, container, object = urlsplit(url)
64
    size, hashmap = backend.get_object_hashmap(account, account, container,
65
            object)
66
    for hash in hashmap:
67
        block = backend.get_block(hash)
68
        stdout.write(block)
69

    
70

    
71
def main():
72
    options, args = parser.parse_args()
73
    if len(args) != 1 or not options.db or not options.data:
74
        parser.print_help()
75
        exit(1)
76

    
77
    url = args[0]
78
    backend = ModularBackend(None, options.db, None, options.data)
79
    
80
    if options.size:
81
        print_size(backend, url)
82
    else:
83
        print_data(backend, url)
84

    
85
if __name__ == '__main__':
86
    main()