Commit f79cabac authored by Antony Chazapis's avatar Antony Chazapis

Merge spend with store.

parent 021d6fd3
......@@ -42,11 +42,10 @@ def file_read_iterator(fp, size=1024):
class HashMap(list):
def __init__(self, f, blocksize, blockhash):
def __init__(self, blocksize, blockhash):
super(HashMap, self).__init__()
self.blocksize = blocksize
self.blockhash = blockhash
self.load(f)
def _hash_raw(self, v):
h = hashlib.new(self.blockhash)
......@@ -74,4 +73,4 @@ class HashMap(list):
def load(self, f):
with open(f) as fp:
for block in file_read_iterator(fp, self.blocksize):
self.append(self._hash_block(block))
\ No newline at end of file
self.append(self._hash_block(block))
......@@ -31,24 +31,28 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
import os
from hashmap import HashMap
from binascii import hexlify, unhexlify
from cStringIO import StringIO
from lib.client import Fault
from client import Fault
import os
import sys
def smart_upload(client, file, blocksize, blockhash):
dest_container = 'pithos'
dest_object = os.path.split(file)[-1]
def upload(client, file, container, prefix):
meta = client.retrieve_container_metadata(container)
blocksize = int(meta['x-container-block-size'])
blockhash = meta['x-container-block-hash']
size = os.path.getsize(file)
hashes = HashMap(sys.argv[1], blocksize, blockhash)
hashes = HashMap(blocksize, blockhash)
hashes.load(file)
map = {'bytes': size, 'hashes': [hexlify(x) for x in hashes]}
object = prefix + os.path.split(file)[-1]
try:
client.create_object_by_hashmap(dest_container, dest_object, map)
client.create_object_by_hashmap(container, object, map)
except Fault, fault:
if fault.status != 409:
raise
......@@ -64,6 +68,6 @@ def smart_upload(client, file, blocksize, blockhash):
offset = hashes.index(unhexlify(hash)) * BLOCK_SIZE
fp.seek(offset)
block = fp.read(BLOCK_SIZE)
client.create_object('pithos', '.upload', StringIO(block))
client.create_object(container, '.upload', StringIO(block))
client.create_object_by_hashmap(dest_container, dest_object, map)
\ No newline at end of file
client.create_object_by_hashmap(container, object, map)
#!/usr/bin/env python
import sys
import os
from lib.client import Pithos_Client
from lib.util import get_user, get_auth, get_server, get_api
from lib.transfer import smart_upload
# XXX Get these from container...
BLOCK_SIZE = 4 * 1024 * 1024
BLOCK_HASH = 'sha256'
if __name__ == '__main__':
if len(sys.argv) != 2 or not os.path.isfile(sys.argv[1]):
print 'syntax: %s <file>' % sys.argv[0]
sys.exit(1)
client = Pithos_Client(get_server(), get_auth(), get_user())
smart_upload(client, sys.argv[1], BLOCK_SIZE, BLOCK_HASH)
......@@ -40,6 +40,7 @@ from sys import argv, exit, stdin, stdout
from datetime import datetime
from lib.client import Pithos_Client, Fault
from lib.util import get_user, get_auth, get_server, get_api
from lib.transfer import upload
import json
import logging
......@@ -685,6 +686,15 @@ class SharingObject(Command):
print_list(self.client.list_shared_by_others(**args))
@cli_command('send')
class Send(Command):
syntax = '<file> <container>[/<prefix>]'
description = 'upload file to container (using prefix)'
def execute(self, file, prefix):
container, sep, prefix = prefix.partition('/')
upload(self.client, file, container, prefix)
def print_usage():
cmd = Command('', [])
parser = cmd.parser
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment