Commit 83077c20 authored by Jeroen F.J. Laros's avatar Jeroen F.J. Laros

Merge branch 'verbose' into cli_patch

Conflicts:
	transfer_client/transfer_client/cli.py
parents 376d2eee a3c2b202
......@@ -24,6 +24,7 @@ class TestCLI(object):
make_fake_file('a', 'a\n'), make_fake_file('b', 'b\n')]
self._metadata_handle = StringIO('null\n')
self._output_handle = StringIO()
self._log_handle = StringIO()
TransferClient._request = lambda *args, **kwargs: self._fake_reply
......@@ -61,30 +62,34 @@ class TestCLI(object):
assert self._output_handle.getvalue() == 'null\n'
def test_make_metadata_1(self):
metadata = cli._make_metadata(self._file_handles, 'test')
metadata = cli._make_metadata(
self._log_handle, self._file_handles, 'test')
assert metadata['title'] == 'test'
assert metadata['files'][0]['filename'] == 'a'
assert (
metadata['files'][1]['md5'] == '3b5d5c3712955042212316173ccf37be')
def test_make_metadata_2(self):
cli.make_metadata(self._output_handle, self._file_handles, 'test')
cli.make_metadata(
self._output_handle, self._log_handle, self._file_handles, 'test')
assert md5_check(
self._output_handle.getvalue(), 'ee7ca88f46ca4c7f58375279a2fbd62a')
assert md5_check(
self._log_handle.getvalue(), 'c16aad07df999e5a1a5bcd066c1f692f')
def test_transfer(self):
self._fake_reply = {'id': 0}
cli.transfer(
self._output_handle, self._file_handles,
self._log_handle, self._file_handles,
'127.0.0.1', '123', 'test', False)
assert md5_check(
self._output_handle.getvalue(), 'c69a4efc89677f66e67b296b5e9ced66')
self._log_handle.getvalue(), '307fc32a1709cb2505c5c4525aa6c34e')
def test_interrupted_transfer_1(self):
self._fake_reply = {'transfers': []}
try:
cli._interrupted_transfer(
self._output_handle, TransferClient('127.0.0.1', False), '123')
self._log_handle, TransferClient('127.0.0.1', False), '123')
except ValueError as error:
assert error.message == 'no interrupted transfers found'
else:
......@@ -93,9 +98,9 @@ class TestCLI(object):
def test_interrupted_transfer_2(self):
self._fake_reply = {'transfers': [{'id': 0, 'status': 'initiated'}]}
cli._interrupted_transfer(
self._output_handle, TransferClient('127.0.0.1', False), '123')
self._log_handle, TransferClient('127.0.0.1', False), '123')
assert md5_check(
self._output_handle.getvalue(), 'b6a19671cc29806d6468b84695121344')
self._log_handle.getvalue(), 'b6a19671cc29806d6468b84695121344')
def test_resume(self):
self._fake_reply = {
......@@ -112,9 +117,9 @@ class TestCLI(object):
}
]
}
cli.resume(self._output_handle, '127.0.0.1', '123', False)
cli.resume(self._log_handle, '127.0.0.1', '123', False)
assert md5_check(
self._output_handle.getvalue(), '641eeda3b448cc99a9c5f74c8343d589')
self._log_handle.getvalue(), '641eeda3b448cc99a9c5f74c8343d589')
def test_cancel(self):
self._fake_reply = {
......@@ -125,7 +130,6 @@ class TestCLI(object):
}
]
}
cli.cancel(
self._output_handle, '127.0.0.1', '123', False)
cli.cancel(self._log_handle, '127.0.0.1', '123', False)
assert md5_check(
self._output_handle.getvalue(), 'b6a19671cc29806d6468b84695121344')
self._log_handle.getvalue(), 'b6a19671cc29806d6468b84695121344')
......@@ -123,11 +123,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.
......@@ -136,6 +137,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)
......@@ -148,24 +151,24 @@ 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):
def transfer(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.
......@@ -175,18 +178,18 @@ 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(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(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.
......@@ -198,43 +201,43 @@ 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(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.
"""
transfer_client = TransferClient(server_name, ssl_check)
transfer = _interrupted_transfer(output_handle, transfer_client, user_id)
transfer = _interrupted_transfer(log_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)
def cancel(output_handle, server_name, user_id, ssl_check):
def cancel(log_handle, server_name, user_id, ssl_check):
"""
Cancel 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.
"""
transfer_client = TransferClient(server_name, ssl_check)
transfer = _interrupted_transfer(output_handle, transfer_client, user_id)
transfer = _interrupted_transfer(log_handle, transfer_client, user_id)
transfer_client.update(user_id, transfer['id'], 'cancelled')
......@@ -384,11 +387,20 @@ def main():
'metadata_handle', metavar='METADATA', type=argparse.FileType('r'),
help='metadata file')
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,
ssl_parser])
default_verbose_parser = argparse.ArgumentParser(
add_help=False,
parents=[log_parser, server_name_parser, user_id_parser, ssl_parser])
subparser = subparsers.add_parser(
'users', parents=[default_parser],
description=doc_split(users))
......@@ -435,22 +447,24 @@ 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_verbose_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_verbose_parser],
description=doc_split(resume))
subparser.set_defaults(func=resume)
subparser = subparsers.add_parser(
'cancel', parents=[default_parser],
'cancel', parents=[default_verbose_parser],
description=doc_split(cancel))
subparser.set_defaults(func=cancel)
......
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