Revision d2d5c360 tools/store

b/tools/store
5 5
from os.path import basename
6 6
from sys import argv, exit, stdin, stdout
7 7
from pithos.lib.client import Client, Fault
8
from datetime import datetime
8 9

  
9 10
import json
10 11
import logging
11 12
import types
13
import re
14
import time as _time
12 15

  
13 16
DEFAULT_HOST = 'pithos.dev.grnet.gr'
14 17
DEFAULT_API = 'v1'
......
92 95
        parser.add_option('--if-unmodified-since', action='store', type='str',
93 96
                          dest='if_unmodified_since', default=None,
94 97
                          help='show output if not modified since then')
98
        parser.add_option('--until', action='store', dest='until',
99
                          default=False, help='show metadata until that date')
100
        parser.add_option('--format', action='store', dest='format',
101
                          default='%d/%m/%Y', help='format to parse until date')
95 102

  
96 103
    def execute(self, container=None):
97 104
        if container:
......
103 110
        params = {'limit':self.limit, 'marker':self.marker}
104 111
        headers = {'IF_MODIFIED_SINCE':self.if_modified_since,
105 112
                   'IF_UNMODIFIED_SINCE':self.if_unmodified_since}
113
        
114
        if self.until:
115
            t = _time.strptime(self.until, self.format)
116
            params['until'] = int(_time.mktime(t))
117
        
106 118
        l = self.client.list_containers(self.detail, params, headers)
107 119
        print_list(l)
108 120

  
......
112 124
                  'path':self.path, 'meta':self.meta}
113 125
        headers = {'IF_MODIFIED_SINCE':self.if_modified_since,
114 126
                   'IF_UNMODIFIED_SINCE':self.if_unmodified_since}
127
        container, sep, object = container.partition('/')
128
        if object:
129
            print '%s/%s is an object' %(container, object)
130
            return
131
        
132
        if self.until:
133
            t = _time.strptime(self.until, self.format)
134
            params['until'] = int(_time.mktime(t))
135
        
115 136
        l = self.client.list_objects(container, self.detail, params, headers)
116 137
        print_list(l)
117 138

  
......
123 144
    def add_options(self, parser):
124 145
        parser.add_option('-r', action='store_true', dest='restricted',
125 146
                          default=False, help='show only user defined metadata')
147
        parser.add_option('--until', action='store', dest='until',
148
                          default=False, help='show metadata until that date')
149
        parser.add_option('--format', action='store', dest='format',
150
                          default='%d/%m/%Y', help='format to parse until date')
126 151

  
127 152
    def execute(self, path=''):
128 153
        container, sep, object = path.partition('/')
154
        if self.until:
155
            t = _time.strptime(self.until, self.format)
156
            self.until = int(_time.mktime(t))
129 157
        if object:
130 158
            meta = self.client.retrieve_object_metadata(container, object,
131 159
                                                        self.restricted)
132 160
        elif container:
133 161
            meta = self.client.retrieve_container_metadata(container,
134
                                                           self.restricted)
162
                                                           self.restricted,
163
                                                           self.until)
135 164
        else:
136
            meta = self.client.account_metadata(self.restricted)
165
            meta = self.client.account_metadata(self.restricted, self.until)
137 166
        if meta == None:
138 167
            print 'Entity does not exist'
139 168
        else:
......
233 262
        parser.add_option('--manifest', action='store', type='str',
234 263
                          dest='manifest', default=None,
235 264
                          help='use for large file support')
265
        parser.add_option('--touch', action='store_true',
266
                          dest='touch', default=True,
267
                          help='create object with zero data')
236 268

  
237
    def execute(self, path, srcpath, *args):
269
    def execute(self, path, srcpath='-', *args):
238 270
        headers = {}
239 271
        if self.manifest:
240 272
            headers['X_OBJECT_MANIFEST'] = self.manifest
......
251 283
        
252 284
        container, sep, object = path.partition('/')
253 285
        
254
        f = srcpath != '-' and open(srcpath) or stdin
255
        chunked = (self.chunked or f == stdin) and True or False
286
        f = None
287
        chunked = False
288
        if not self.touch:
289
            f = srcpath != '-' and open(srcpath) or stdin
290
            chunked = (self.chunked or f == stdin) and True or False
256 291
        self.client.create_object(container, object, f, chunked=chunked,
257 292
                                  headers=headers)
258
        f.close()
293
        if f:
294
            f.close()
259 295

  
260 296
@cli_command('copy', 'cp')
261 297
class CopyObject(Command):
......
379 415
    header = header in d and header or 'subdir'
380 416
    if header and header in d:
381 417
        f.write('%s\n' %d.pop(header))
418
    patterns = ['^x_(account|container|object)_meta_(\w+)$']
419
    patterns.append(patterns[0].replace('_', '-'))
382 420
    for key, val in sorted(d.items()):
383
        f.write('%s: %s\n' % (key.rjust(15), val))
421
        for p in patterns:
422
            p = re.compile(p)
423
            m = p.match(key)
424
            if m:
425
                key = m.group(2)
426
        f.write('%s: %s\n' % (key.rjust(30), val))
384 427

  
385 428
def print_list(l, verbose=False, f=stdout):
386 429
    for elem in l:

Also available in: Unified diff