Statistics
| Branch: | Tag: | Revision:

root / snf-image-host / pithcat @ f3e43ab1

History | View | Annotate | Download (3.2 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
from os import environ
31

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

    
38

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

    
47

    
48
def urlsplit(url):
49
    """Returns (accout, container, object) from a location string"""
50

    
51
    assert url.startswith('pithos://'), "Invalid URL"
52
    t = url.split('/', 4)
53
    assert len(t) == 5, "Invalid URL"
54
    return t[2:5]
55

    
56

    
57
def print_size(backend, url):
58
    """Writes object's size to stdout."""
59

    
60
    account, container, object = urlsplit(url)
61
    meta = backend.get_object_meta(account, account, container, object, None)
62
    print meta['bytes']
63

    
64

    
65
def print_data(backend, url):
66
    """Writes object's size to stdout."""
67

    
68
    account, container, object = urlsplit(url)
69
    size, hashmap = backend.get_object_hashmap(account, account, container,
70
            object)
71
    for hash in hashmap:
72
        block = backend.get_block(hash)
73
        if len(block) > size:
74
            block = block[:size]
75
        stdout.write(block)
76
        size -= len(block)
77

    
78

    
79
def main():
80
    options, args = parser.parse_args()
81
    if len(args) != 1:
82
        parser.print_help()
83
        exit(1)
84

    
85
    url = args[0]
86

    
87
    if not options.data and 'PITHCAT_INPUT_DATA' not in environ:
88
        stderr.write("Pithos data directory path is missing.\n")
89
        exit(1)
90

    
91
    data_path = environ['PITHCAT_INPUT_DATA'] if not options.data else \
92
            options.data
93

    
94
    if not options.db and 'PITHCAT_INPUT_DB' not in environ:
95
        stderr.write("Pithos database uri is missing.\n")
96
        exit(1)
97

    
98
    db_uri = environ['PITHCAT_INPUT_DB'] if not options.db else options.db
99

    
100
    backend = ModularBackend(None, db_uri, None, data_path)
101

    
102
    if options.size:
103
        print_size(backend, url)
104
    else:
105
        print_data(backend, url)
106

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