Statistics
| Branch: | Tag: | Revision:

root / snf-app / synnefo / tools / pithcat.py @ a0ea257c

History | View | Annotate | Download (3.3 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
try:
45
    from synnefo import settings
46
except ImportError:
47
    raise Exception("Cannot import settings, make sure PYTHONPATH contains "
48
                    "the parent directory of the Synnefo Django project.")
49

    
50
from django.core.management import setup_environ
51
setup_environ(settings)
52

    
53
from optparse import OptionParser
54
from sys import exit, stdout
55

    
56
from django.conf import settings
57
from pithos.backends import connect_backend
58

    
59

    
60
backend = connect_backend()
61
parser = OptionParser()
62
parser.add_option('-s', action='store_true', dest='size', default=False,
63
        help='print file size and exit')
64

    
65

    
66
def urlsplit(url):
67
    """Returns (accout, container, object) from a location string"""
68
    
69
    assert url.startswith('pithos://'), "Invalid URL"
70
    t = url.split('/', 4)
71
    assert len(t) == 5, "Invalid URL"
72
    return t[2:5]
73

    
74

    
75
def print_size(url):
76
    """Writes object's size to stdout."""
77
    
78
    account, container, object = urlsplit(url)
79
    meta = backend.get_object_meta(account, account, container, object)
80
    print meta['bytes']
81

    
82

    
83
def print_data(url):
84
    """Writes object's size to stdout."""
85
    
86
    account, container, object = urlsplit(url)
87
    size, hashmap = backend.get_object_hashmap(account, account, container,
88
            object)
89
    for hash in hashmap:
90
        block = backend.get_block(hash)
91
        stdout.write(block)
92
    
93

    
94
def main():
95
    options, args = parser.parse_args()
96
    if len(args) != 1:
97
        parser.print_help()
98
        exit(1)
99
    
100
    url = args[0]
101
    if options.size:
102
        print_size(url)
103
    else:
104
        print_data(url)
105

    
106
if __name__ == '__main__':
107
    main()