Merge branch 'master' of https://code.grnet.gr/git/pithos
[pithos] / tools / store
index 9aa44b0..0cd55df 100755 (executable)
@@ -1,5 +1,38 @@
 #!/usr/bin/env python
 
+# Copyright 2011 GRNET S.A. All rights reserved.
+# 
+# Redistribution and use in source and binary forms, with or
+# without modification, are permitted provided that the following
+# conditions are met:
+# 
+#   1. Redistributions of source code must retain the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer.
+# 
+#   2. Redistributions in binary form must reproduce the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer in the documentation and/or other materials
+#      provided with the distribution.
+# 
+# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+# 
+# The views and conclusions contained in the software and
+# documentation are those of the authors and should not be
+# interpreted as representing official policies, either expressed
+# or implied, of GRNET S.A.
+
 from getpass import getuser
 from optparse import OptionParser
 from os.path import basename
@@ -30,8 +63,8 @@ def class_for_cli_command(name):
     return _cli_commands[name]
 
 class Command(object):
-    def __init__(self, argv):
-        parser = OptionParser()
+    def __init__(self, name, argv):
+        parser = OptionParser('%%prog %s [options] %s' % (name, self.syntax))
         parser.add_option('--host', dest='host', metavar='HOST',
                           default=DEFAULT_HOST, help='use server HOST')
         parser.add_option('--user', dest='user', metavar='USERNAME',
@@ -57,10 +90,10 @@ class Command(object):
         
         self.parser = parser
         self.args = args
-
+        
     def add_options(self, parser):
         pass
-
+    
     def execute(self, *args):
         pass
 
@@ -99,13 +132,13 @@ class List(Command):
                           default=False, help='show metadata until that date')
         parser.add_option('--format', action='store', dest='format',
                           default='%d/%m/%Y', help='format to parse until date')
-
+    
     def execute(self, container=None):
         if container:
             self.list_objects(container)
         else:
             self.list_containers()
-
+    
     def list_containers(self):
         params = {'limit':self.limit, 'marker':self.marker}
         headers = {'IF_MODIFIED_SINCE':self.if_modified_since,
@@ -117,7 +150,7 @@ class List(Command):
         
         l = self.client.list_containers(self.detail, params, headers)
         print_list(l)
-
+    
     def list_objects(self, container):
         params = {'limit':self.limit, 'marker':self.marker,
                   'prefix':self.prefix, 'delimiter':self.delimiter,
@@ -132,8 +165,12 @@ class List(Command):
             t = _time.strptime(self.until, self.format)
             params['until'] = int(_time.mktime(t))
         
-        l = self.client.list_objects(container, self.detail, params, headers)
-        print_list(l)
+        detail = 'json'
+        #if request with meta quering disable trash filtering
+        show_trashed = True if self.meta else False
+        l = self.client.list_objects(container, detail, params, headers,
+                                     include_trashed = show_trashed)
+        print_list(l, detail=self.detail)
 
 @cli_command('meta')
 class Meta(Command):
@@ -150,7 +187,7 @@ class Meta(Command):
         parser.add_option('--version', action='store', dest='version',
                           default=None, help='show specific version \
                                   (applies only for objects)')
-
+    
     def execute(self, path=''):
         container, sep, object = path.partition('/')
         if self.until:
@@ -228,7 +265,7 @@ class GetObject(Command):
         parser.add_option('--versionlist', action='store_true',
                           dest='versionlist', default=False,
                           help='get the full object version list')
-
+    
     def execute(self, path):
         headers = {}
         if self.range:
@@ -255,11 +292,20 @@ class GetObject(Command):
             f.write(data)
         f.close()
 
+@cli_command('mkdir')
+class PutMarker(Command):
+    syntax = '<container>/<directory marker>'
+    description = 'create a directory marker'
+    
+    def execute(self, path):
+        container, sep, object = path.partition('/')
+        self.client.create_directory_marker(container, object)
+
 @cli_command('put')
 class PutObject(Command):
     syntax = '<container>/<object> <path> [key=val] [...]'
     description = 'create/override object with path contents or standard input'
-
+    
     def add_options(self, parser):
         parser.add_option('--chunked', action='store_true', dest='chunked',
                           default=False, help='set chunked transfer mode')
@@ -271,19 +317,31 @@ class PutObject(Command):
         parser.add_option('--content-disposition', action='store', type='str',
                           dest='content-disposition', default=None,
                           help='provide the presentation style of the object')
-        parser.add_option('--manifest', action='store', type='str',
-                          dest='manifest', default=None,
+        parser.add_option('-S', action='store',
+                          dest='segment-size', default=False,
                           help='use for large file support')
+        parser.add_option('--manifest', action='store_true',
+                          dest='manifest', default=None,
+                          help='upload a manifestation file')
+        parser.add_option('--type', action='store',
+                          dest='content-type', default=False,
+                          help='create object with specific content type')
         parser.add_option('--touch', action='store_true',
                           dest='touch', default=False,
                           help='create object with zero data')
-
+        parser.add_option('--sharing', action='store',
+                          dest='sharing', default=None,
+                          help='define sharing object policy')
+        
     def execute(self, path, srcpath='-', *args):
         headers = {}
         if self.manifest:
             headers['X_OBJECT_MANIFEST'] = self.manifest
-            
-        attrs = ['etag', 'content-encoding', 'content-disposition']
+        if self.sharing:
+            headers['X_OBJECT_SHARING'] = self.sharing
+        
+        attrs = ['etag', 'content-encoding', 'content-disposition',
+                 'content-type']
         attrs = [a for a in attrs if getattr(self, a)]
         for a in attrs:
             headers[a.replace('-', '_').upper()] = getattr(self, a)
@@ -314,7 +372,7 @@ class CopyObject(Command):
         parser.add_option('--version', action='store',
                           dest='version', default=False,
                           help='copy specific version')
-
+    
     def execute(self, src, dst):
         src_container, sep, src_object = src.partition('/')
         dst_container, sep, dst_object = dst.partition('/')
@@ -324,9 +382,9 @@ class CopyObject(Command):
         version = getattr(self, 'version')
         if version:
             headers = {}
-            headers['SOURCE_VERSION'] = version 
+            headers['X_SOURCE_VERSION'] = version
         self.client.copy_object(src_container, src_object, dst_container,
-                                dst_object)
+                                dst_object, headers)
 
 @cli_command('set')
 class SetMeta(Command):
@@ -362,7 +420,7 @@ class UpdateObject(Command):
                           default=True, help='append data')
         parser.add_option('--start', action='store',
                           dest='start',
-                          default=None, help='range of data to be updated')
+                          default=None, help='starting offest to be updated')
         parser.add_option('--range', action='store', dest='content-range',
                           default=None, help='range of data to be updated')
         parser.add_option('--chunked', action='store_true', dest='chunked',
@@ -375,17 +433,27 @@ class UpdateObject(Command):
                           help='provide the presentation style of the object')
         parser.add_option('--manifest', action='store', type='str',
                           dest='manifest', default=None,
-                          help='use for large file support')
+                          help='use for large file support')        
+        parser.add_option('--sharing', action='store',
+                          dest='sharing', default=None,
+                          help='define sharing object policy')
+        parser.add_option('--touch', action='store_true',
+                          dest='touch', default=False,
+                          help='change file properties')
+        
 
+    
     def execute(self, path, srcpath='-', *args):
         headers = {}
         if self.manifest:
             headers['X_OBJECT_MANIFEST'] = self.manifest
+        if self.sharing:
+            headers['X_OBJECT_SHARING'] = self.sharing
         
-        if self.append:
+        if getattr(self, 'start'):
+            headers['CONTENT_RANGE'] = 'bytes %s-/*' % getattr(self, 'start')
+        elif self.append:
             headers['CONTENT_RANGE'] = 'bytes */*'
-        elif self.start:
-            headers['CONTENT_RANGE'] = 'bytes %s-/*' % self.first-byte-pos
         
         attrs = ['content-encoding', 'content-disposition']
         attrs = [a for a in attrs if getattr(self, a)]
@@ -399,22 +467,21 @@ class UpdateObject(Command):
         
         container, sep, object = path.partition('/')
         
-        f = srcpath != '-' and open(srcpath) or stdin
-        chunked = (self.chunked or f == stdin) and True or False
+        f = None
+        chunked = False
+        if not self.touch:
+            f = srcpath != '-' and open(srcpath) or stdin
+            chunked = (self.chunked or f == stdin) and True or False
         self.client.update_object(container, object, f, chunked=chunked,
                                   headers=headers)
-        f.close()
+        if f:
+            f.close()
 
 @cli_command('move', 'mv')
 class MoveObject(Command):
     syntax = '<src container>/<src object> [<dst container>/]<dst object>'
     description = 'moves an object to a different location'
     
-    def add_options(self, parser):
-        parser.add_option('--version', action='store',
-                          dest='version', default=False,
-                          help='move specific version')
-
     def execute(self, src, dst):
         src_container, sep, src_object = src.partition('/')
         dst_container, sep, dst_object = dst.partition('/')
@@ -422,13 +489,69 @@ class MoveObject(Command):
             dst_container = src_container
             dst_object = dst
         
-        version = getattr(self, 'version')
-        if version:
-            headers = {}
-            headers['SOURCE_VERSION'] = version 
         self.client.move_object(src_container, src_object, dst_container,
                                 dst_object, headers)
 
+@cli_command('remove')
+class TrashObject(Command):
+    syntax = '<container>/<object>'
+    description = 'trashes an object'
+    
+    def execute(self, src):
+        src_container, sep, src_object = src.partition('/')
+        
+        self.client.trash_object(src_container, src_object)
+
+@cli_command('restore')
+class TrashObject(Command):
+    syntax = '<container>/<object>'
+    description = 'trashes an object'
+    
+    def execute(self, src):
+        src_container, sep, src_object = src.partition('/')
+        
+        self.client.restore_object(src_container, src_object)
+
+@cli_command('unset')
+class TrashObject(Command):
+    syntax = '<container>/[<object>] key [key] [...]'
+    description = 'deletes metadata info'
+    
+    def execute(self, path, *args):
+        #in case of account fix the args
+        if len(args) == 0:
+            args = list(args)
+            args.append(path)
+            args = tuple(args)
+            path = ''
+        meta = []
+        for key in args:
+            meta.append(key)
+        container, sep, object = path.partition('/')
+        if object:
+            self.client.delete_object_metadata(container, object, meta)
+        elif container:
+            self.client.delete_container_metadata(container, meta)
+        else:
+            self.client.delete_account_metadata(meta)
+
+@cli_command('group')
+class SetGroup(Command):
+    syntax = 'key=val [key=val] [...]'
+    description = 'sets group account info'
+    
+    def execute(self, path='', **args):
+        if len(args) == 0:
+            args = list(args)
+            args.append(path)
+            args = tuple(args)
+            path = ''
+        groups = {}
+        for arg in args:
+            key, sep, val = arg.partition('=')
+            groups[key] = val
+        self.client.set_account_groups(groups)
+
 def print_usage():
     cmd = Command([])
     parser = cmd.parser
@@ -442,27 +565,28 @@ def print_usage():
         commands.append('  %s %s' % (name.ljust(12), description))
     print '\nCommands:\n' + '\n'.join(sorted(commands))
 
-def print_dict(d, header='name', f=stdout):
+def print_dict(d, header='name', f=stdout, detail=True):
     header = header in d and header or 'subdir'
     if header and header in d:
         f.write('%s\n' %d.pop(header))
-    patterns = ['^x_(account|container|object)_meta_(\w+)$']
-    patterns.append(patterns[0].replace('_', '-'))
-    for key, val in sorted(d.items()):
-        for p in patterns:
-            p = re.compile(p)
-            m = p.match(key)
-            if m:
-                key = m.group(2)
-        f.write('%s: %s\n' % (key.rjust(30), val))
-
-def print_list(l, verbose=False, f=stdout):
+    if detail:
+        patterns = ['^x_(account|container|object)_meta_(\w+)$']
+        patterns.append(patterns[0].replace('_', '-'))
+        for key, val in sorted(d.items()):
+            for p in patterns:
+                p = re.compile(p)
+                m = p.match(key)
+                if m:
+                    key = m.group(2)
+            f.write('%s: %s\n' % (key.rjust(30), val))
+
+def print_list(l, verbose=False, f=stdout, detail=True):
     for elem in l:
         #if it's empty string continue
         if not elem:
             continue
         if type(elem) == types.DictionaryType:
-            print_dict(elem, f=f)
+            print_dict(elem, f=f, detail=detail)
         elif type(elem) == types.StringType:
             if not verbose:
                 elem = elem.split('Traceback')[0]
@@ -486,17 +610,16 @@ def main():
         print_usage()
         exit(1)
     
-    cmd = cls(argv[2:])
+    cmd = cls(name, argv[2:])
     
     try:
         cmd.execute(*cmd.args)
     except TypeError, e:
-        print e
-        cmd.parser.usage = '%%prog %s [options] %s' % (name, cmd.syntax)
         cmd.parser.print_help()
         exit(1)
     except Fault, f:
-        print f.status, f.data
+        status = f.status and '%s ' % f.status or ''
+        print '%s%s' % (status, f.data)
 
 if __name__ == '__main__':
     main()