3 # Copyright (C) 2011 GRNET S.A.
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.
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.
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
21 A tool that connects to the Pithos backend and returns the size and contents
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.
28 from optparse import OptionParser
29 from sys import exit, stdout, stderr
30 from os import environ
33 from pithos.backends.modular import ModularBackend
35 stderr.write("Pithos backend was not found.\n")
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')
49 """Returns (accout, container, object) from a location string"""
51 assert url.startswith('pithos://'), "Invalid URL"
53 assert len(t) == 5, "Invalid URL"
57 def print_size(backend, url):
58 """Writes object's size to stdout."""
60 account, container, object = urlsplit(url)
61 meta = backend.get_object_meta(account, account, container, object, None)
65 def print_data(backend, url):
66 """Writes object's size to stdout."""
68 account, container, object = urlsplit(url)
69 size, hashmap = backend.get_object_hashmap(account, account, container,
72 block = backend.get_block(hash)
80 options, args = parser.parse_args()
87 if not options.data and 'PITHCAT_INPUT_DATA' not in environ:
88 stderr.write("Pithos data directory path is missing.\n")
91 data_path = environ['PITHCAT_INPUT_DATA'] if not options.data else \
94 if not options.db and 'PITHCAT_INPUT_DB' not in environ:
95 stderr.write("Pithos database uri is missing.\n")
98 db_uri = environ['PITHCAT_INPUT_DB'] if not options.db else options.db
100 backend = ModularBackend(None, db_uri, None, data_path)
103 print_size(backend, url)
105 print_data(backend, url)
107 if __name__ == '__main__':