61 lines
1.5 KiB
Python
61 lines
1.5 KiB
Python
import argparse
|
|
from urllib.parse import urlparse
|
|
from blobsend import CHUNK_SIZE
|
|
from blobsend.client_file import FileChunkClient
|
|
from blobsend.client_ssh import SshChunkClient
|
|
|
|
|
|
SCHEMES = {
|
|
"file": FileChunkClient,
|
|
"ssh": SshChunkClient,
|
|
}
|
|
|
|
|
|
def get_client(uri, is_src):
|
|
clss = SCHEMES[uri.scheme or "file"]
|
|
return clss.from_uri(uri, is_src)
|
|
|
|
|
|
def get_args():
|
|
parser = argparse.ArgumentParser(description="file blob copy utility")
|
|
parser.add_argument("src", help="source file uri")
|
|
parser.add_argument("dest", help="dest file uri")
|
|
|
|
return parser.parse_args(), parser
|
|
|
|
|
|
def main():
|
|
args, parser = get_args()
|
|
print(args)
|
|
|
|
src = get_client(urlparse(args.src), True)
|
|
dest = get_client(urlparse(args.dest), False)
|
|
|
|
num_chunks = src.get_length() // CHUNK_SIZE
|
|
|
|
dest_hashes_iter = dest.get_hashes()
|
|
for src_chunk_number, src_chunk_hash in src.get_hashes():
|
|
dest_chunk_number = None
|
|
dest_chunk_hash = None
|
|
try:
|
|
dest_chunk_number, dest_chunk_hash = next(dest_hashes_iter)
|
|
except StopIteration:
|
|
pass
|
|
|
|
if dest_chunk_number is not None and src_chunk_number != dest_chunk_number:
|
|
raise Exception("sequence mismatch?")
|
|
|
|
if src_chunk_hash != dest_chunk_hash:
|
|
print("Copying chunk", src_chunk_number, "/", num_chunks)
|
|
blob = src.get_chunk(src_chunk_number)
|
|
dest.put_chunk(src_chunk_number, blob)
|
|
|
|
dest.set_length(src.get_length())
|
|
|
|
src.close()
|
|
dest.close()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|