Fix bug in import/export regarding some API params
[snf-image] / snf-image-host / pithcat
index 2bc541e..c0a0a60 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 
-# Copyright (C) 2011 GRNET S.A.
+# Copyright (C) 2011-2013 GRNET S.A.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -25,7 +25,7 @@ Since the backend does not have a "root" account we use the account given in
 the URL as the user when connecting to the backend.
 """
 
-from optparse import OptionParser
+from optparse import OptionParser, OptionGroup
 from sys import exit, stdout, stderr
 from os import environ
 from binascii import hexlify, unhexlify
@@ -39,12 +39,20 @@ except ImportError:
 
 
 parser = OptionParser(usage='%prog [options] <URL>')
-parser.add_option('--db', dest='db', metavar='URI',
-        help='SQLAlchemy URI of the database [REQUIRED]')
 parser.add_option('--data', dest='data', metavar='DIR',
-        help='path to the directory where data are stored [REQUIRED]')
+                  help='path to the directory where data are stored')
 parser.add_option('-s', action='store_true', dest='size', default=False,
-        help='print file size and exit')
+                  help='print file size and exit')
+group = OptionGroup(
+    parser, "Dangerous Options",
+    "Caution: If the <URL> is a LocationURL (pithos://...), then you'll also "
+    "need to define a database URI. You can use the `--db' option to do so, "
+    "but this raises security concerns. For database URI's and pithos data "
+    "paths, the recommended way to define them is to use the PITHCAT_INPUT_DB "
+    "and PITHCAT_INPUT_DATA environmental variables respectfully.")
+group.add_option('--db', dest='db', metavar='URI',
+                 help='SQLAlchemy URI of the database', default=None)
+parser.add_option_group(group)
 
 LocationURL = namedtuple('LocationURL', ['account', 'container', 'object'])
 HashmapURL = namedtuple('HashmapURL', ['hash', 'size'])
@@ -65,7 +73,6 @@ def parse_url(url):
 
 def print_size(backend, url):
     """Writes object's size to stdout."""
-    url = parse_url(url)
     if type(url) is LocationURL:
         account, container, object = url
         meta = backend.get_object_meta(account, account, container, object,
@@ -80,13 +87,12 @@ def print_size(backend, url):
 def print_data(backend, url):
     """Writes object's size to stdout."""
 
-    url = parse_url(url)
     if type(url) is LocationURL:
         account, container, object = url
         size, hashmap = backend.get_object_hashmap(account, account, container,
-                object)
+                                                   object)
     elif type(url) is HashmapURL:
-        hashmap = [hexlify(x) \
+        hashmap = [hexlify(x)
                    for x in backend.store.map_get(unhexlify(url.hash))]
         size = int(url.size)
     else:
@@ -106,22 +112,32 @@ def main():
         parser.print_help()
         exit(1)
 
-    url = args[0]
+    url = parse_url(args[0])
 
     if not options.data and 'PITHCAT_INPUT_DATA' not in environ:
-        stderr.write("Pithos data directory path is missing.\n")
+        stderr.write(
+            "Pithos data directory path is missing.\n"
+            "Use the PITHCAT_INPUT_DATA environmental variable (recommended) "
+            "or the --data command line option to define it.\n")
         exit(1)
 
     data_path = environ['PITHCAT_INPUT_DATA'] if not options.data else \
-            options.data
-
-    if not options.db and 'PITHCAT_INPUT_DB' not in environ:
-        stderr.write("Pithos database uri is missing.\n")
+        options.data
+
+    if options.db is None and 'PITHCAT_INPUT_DB' not in environ and \
+            type(url) is LocationURL:
+        stderr.write(
+            "Pithos database uri is missing.\n"
+            "Use the PITHCAT_INPUT_DB environmental variable (recommended) "
+            "or the --db command line option to define it.\n")
         exit(1)
 
     db_uri = environ['PITHCAT_INPUT_DB'] if not options.db else options.db
 
-    backend = ModularBackend(None, db_uri, None, data_path)
+    backend = ModularBackend(None,
+                             db_uri if type(url) is LocationURL else None,
+                             None,
+                             data_path)
 
     if options.size:
         print_size(backend, url)
@@ -130,3 +146,5 @@ def main():
 
 if __name__ == '__main__':
     main()
+
+# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :