Statistics
| Branch: | Tag: | Revision:

root / snf-image-host / pithcat @ fa7285cf

History | View | Annotate | Download (3.4 kB)

1
#!/usr/bin/env python
2

    
3
# Copyright 2011 GRNET S.A. All rights reserved.
4
#
5
# Redistribution and use in source and binary forms, with or
6
# without modification, are permitted provided that the following
7
# conditions are met:
8
#
9
#   1. Redistributions of source code must retain the above
10
#      copyright notice, this list of conditions and the following
11
#      disclaimer.
12
#
13
#   2. Redistributions in binary form must reproduce the above
14
#      copyright notice, this list of conditions and the following
15
#      disclaimer in the documentation and/or other materials
16
#      provided with the distribution.
17
#
18
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
19
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
22
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
# POSSIBILITY OF SUCH DAMAGE.
30
#
31
# The views and conclusions contained in the software and
32
# documentation are those of the authors and should not be
33
# interpreted as representing official policies, either expressed
34
# or implied, of GRNET S.A.
35

    
36
"""
37
A tool that connects to the Pithos backend and returns the size and contents
38
of a pithos object.
39

    
40
Since the backend does not have a "root" account we use the account given in
41
the URL as the user when connecting to the backend.
42
"""
43

    
44
from optparse import OptionParser
45
from sys import exit, stdout
46

    
47
from pithos.backends.modular import ModularBackend
48

    
49

    
50
parser = OptionParser(usage='%prog [options] <URL>')
51
parser.add_option('--db', dest='db', metavar='URI',
52
        help='SQLAlchemy URI of the database [REQUIRED]')
53
parser.add_option('--data', dest='data', metavar='DIR',
54
        help='path to the directory where data are stored [REQUIRED]')
55
parser.add_option('-s', action='store_true', dest='size', default=False,
56
        help='print file size and exit')
57

    
58

    
59
def urlsplit(url):
60
    """Returns (accout, container, object) from a location string"""
61
    
62
    assert url.startswith('pithos://'), "Invalid URL"
63
    t = url.split('/', 4)
64
    assert len(t) == 5, "Invalid URL"
65
    return t[2:5]
66

    
67

    
68
def print_size(backend, url):
69
    """Writes object's size to stdout."""
70
    
71
    account, container, object = urlsplit(url)
72
    meta = backend.get_object_meta(account, account, container, object)
73
    print meta['bytes']
74

    
75

    
76
def print_data(backend, url):
77
    """Writes object's size to stdout."""
78
    
79
    account, container, object = urlsplit(url)
80
    size, hashmap = backend.get_object_hashmap(account, account, container,
81
            object)
82
    for hash in hashmap:
83
        block = backend.get_block(hash)
84
        stdout.write(block)
85

    
86

    
87
def main():
88
    options, args = parser.parse_args()
89
    if len(args) != 1 or not options.db or not options.data:
90
        parser.print_help()
91
        exit(1)
92

    
93
    url = args[0]
94
    backend = ModularBackend(None, options.db, None, options.data)
95
    
96
    if options.size:
97
        print_size(backend, url)
98
    else:
99
        print_data(backend, url)
100

    
101
if __name__ == '__main__':
102
    main()