Commit d1b0a2b7 authored by Jeroen F.J. Laros's avatar Jeroen F.J. Laros

Added more verbose output (progress indication).

parent a5c85967
......@@ -115,11 +115,12 @@ def completed(output_handle, server_name, client_id, ssl_check):
TransferClient(server_name, ssl_check).completed(client_id))
def _make_metadata(file_handles, title):
def _make_metadata(log_handle, file_handles, title):
"""
Given a list of files, generate the metadata according to the minimal
transfer metadata schema.
:arg stream log_handle: Open writeable handle to a file.
:arg list file_handles: List of open readable file handles.
:arg str title: Transfer title.
......@@ -128,6 +129,8 @@ def _make_metadata(file_handles, title):
metadata = {'title': title, 'files': []}
for file_handle in file_handles:
log_handle.write(
'Calculating checksum for file: {}.\n'.format(file_handle.name))
hash_sum = hashlib.md5()
for chunk in iter(lambda: file_handle.read(4096), b''):
hash_sum.update(chunk)
......@@ -140,24 +143,27 @@ def _make_metadata(file_handles, title):
return metadata
def make_metadata(output_handle, file_handles, title):
def make_metadata(output_handle, log_handle, file_handles, title):
"""
Given a list of files, generate the metadata according to the minimal
transfer metadata schema.
:arg stream output_handle: Open writeable handle to a file.
:arg stream log_handle: Open writeable log to a file.
:arg list file_handles: List of open readable file handles.
:arg str title: Transfer title.
"""
_write(output_handle, _make_metadata(file_handles, title))
_write(output_handle, _make_metadata(log_handle, file_handles, title))
def transfer(
output_handle, file_handles, server_name, user_id, title, ssl_check):
output_handle, log_handle, file_handles, server_name, user_id, title,
ssl_check):
"""
Transfer a list of files.
:arg stream output_handle: Open writeable handle to a file.
:arg stream log_handle: Open writeable log to a file.
:arg list file_handles: List of open readable file handles.
:arg str server_name: Name of the transfer server.
:arg str user_id: User ID.
......@@ -167,18 +173,20 @@ def transfer(
transfer_client = TransferClient(server_name, ssl_check)
transfer_id = transfer_client.transfers(
user_id, _make_metadata(file_handles, title))['id']
output_handle.write('Transfer ID: {}.\n'.format(transfer_id))
user_id,
_make_metadata(output_handle, log_handle, file_handles, title))['id']
log_handle.write('Transfer ID: {}.\n'.format(transfer_id))
for file_handle in file_handles:
output_handle.write('Uploading file: {}.\n'.format(file_handle.name))
log_handle.write('Uploading file: {}.\n'.format(file_handle.name))
transfer_client.uploads(user_id, transfer_id, file_handle)
def _interrupted_transfer(output_handle, transfer_client, user_id):
def _interrupted_transfer(output_handle, log_handle, transfer_client, user_id):
"""
Find an interrupted transfer.
:arg stream output_handle: Open writeable handle to a file.
:arg stream log_handle: Open writeable log to a file.
:arg object transfer_client: Transfer client class instance.
:arg str user_id: User ID.
......@@ -190,16 +198,17 @@ def _interrupted_transfer(output_handle, transfer_client, user_id):
raise ValueError('no interrupted transfers found')
transfer = transfers[-1]
output_handle.write('Transfer ID: {}.\n'.format(transfer['id']))
log_handle.write('Transfer ID: {}.\n'.format(transfer['id']))
return transfer
def resume(output_handle, server_name, user_id, ssl_check):
def resume(output_handle, log_handle, server_name, user_id, ssl_check):
"""
Resume an interrupted transfer.
:arg stream output_handle: Open writeable handle to a file.
:arg stream log_handle: Open writeable log to a file.
:arg str server_name: Name of the transfer server.
:arg str user_id: User ID.
:arg bool ssl_check: Check server SSL certificate.
......@@ -209,7 +218,7 @@ def resume(output_handle, server_name, user_id, ssl_check):
transfer = _interrupted_transfer(output_handle, transfer_client, user_id)
for file_object in transfer['files']:
if file_object['status'] == 'pending':
output_handle.write(
log_handle.write(
'Uploading file: {}.\n'.format(file_object['filename']))
with open(file_object['filename'], 'rb') as file_handle:
transfer_client.uploads(user_id, transfer['id'], file_handle)
......@@ -274,6 +283,11 @@ def main():
'-n', dest='ssl_check', default=True, action='store_false',
help='disable ssl certificate check')
log_parser = argparse.ArgumentParser(add_help=False)
log_parser.add_argument(
'-l', dest='log_handle', metavar='LOG',
type=argparse.FileType('w'), default=sys.stderr, help='log file')
default_parser = argparse.ArgumentParser(
add_help=False,
parents=[output_parser, server_name_parser, user_id_parser,
......@@ -328,17 +342,19 @@ def main():
subparser.set_defaults(func=completed)
subparser = subparsers.add_parser(
'make_metadata', parents=[output_parser, files_parser, title_parser],
'make_metadata',
parents=[output_parser, log_parser, files_parser, title_parser],
description=doc_split(make_metadata))
subparser.set_defaults(func=make_metadata)
subparser = subparsers.add_parser(
'transfer', parents=[default_parser, files_parser, title_parser],
'transfer',
parents=[default_parser, log_parser, files_parser, title_parser],
description=doc_split(transfer))
subparser.set_defaults(func=transfer)
subparser = subparsers.add_parser(
'resume', parents=[default_parser],
'resume', parents=[default_parser, log_parser],
description=doc_split(resume))
subparser.set_defaults(func=resume)
......
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