Skip to content

Commit

Permalink
Merge pull request #141 from carlosp420/avoid-nested-if-statements
Browse files Browse the repository at this point in the history
avoid nested if statements
  • Loading branch information
jdemaeyer committed Feb 12, 2016
2 parents fc70f2c + 057ad5d commit c0919ac
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
45 changes: 27 additions & 18 deletions shub/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,7 @@ def make_deploy_request(url, data, files, auth, verbose, keep_log):
rsp = requests.post(url=url, auth=auth, data=data, files=files,
stream=True, timeout=300)
rsp.raise_for_status()
with NamedTemporaryFile(prefix='shub_deploy_', suffix='.log',
delete=(not keep_log)) as log_file:
for line in rsp.iter_lines():
if verbose:
click.echo(line)
last_logs.append(line)
log_file.write(line + '\n')
if _is_deploy_successful(last_logs):
if not verbose:
click.echo(last_logs[-1])
else:
log_file.delete = False
if not verbose:
click.echo("Deploy log last %s lines:" % len(last_logs))
for line in last_logs:
click.echo(line)
if not log_file.delete:
click.echo("Deploy log location: %s" % log_file.name)
write_and_echo_logs(keep_log, last_logs, rsp, verbose)
return True
except requests.HTTPError as exc:
rsp = exc.response
Expand All @@ -73,6 +56,32 @@ def make_deploy_request(url, data, files, auth, verbose, keep_log):
raise RemoteErrorException("Deploy failed: {}".format(exc))


def write_and_echo_logs(keep_log, last_logs, rsp, verbose):
"""It will write logs to temporal file and echo if verbose is True."""
with NamedTemporaryFile(prefix='shub_deploy_', suffix='.log',
delete=(not keep_log)) as log_file:
for line in rsp.iter_lines():
if verbose:
click.echo(line)
last_logs.append(line)
log_file.write(line + '\n')
echo_short_log_if_deployed(last_logs, log_file, verbose)
if not log_file.delete:
click.echo("Deploy log location: %s" % log_file.name)


def echo_short_log_if_deployed(last_logs, log_file, verbose):
if _is_deploy_successful(last_logs):
if not verbose:
click.echo(last_logs[-1])
else:
log_file.delete = False
if not verbose:
click.echo("Deploy log last %s lines:" % len(last_logs))
for line in last_logs:
click.echo(line)


def _is_deploy_successful(last_logs):
try:
data = json.loads(last_logs[-1])
Expand Down
25 changes: 24 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import sys
import unittest

from mock import MagicMock, patch
from mock import Mock, MagicMock, patch
from click.testing import CliRunner
from collections import deque

Expand Down Expand Up @@ -337,6 +337,29 @@ def _call(*args, **kwargs):
self.assertEqual(pipargs[0], 'download')
self.assertIn('--no-binary=:all:', pipargs)

@patch('shub.utils._is_deploy_successful')
def test_echo_short_log_if_deployed(self, mock_dep_success):
log_file = Mock(delete=None)
last_logs = ["last log line"]

mock_dep_success.return_value = True
for verbose in [True, False]:
utils.echo_short_log_if_deployed(last_logs, log_file, verbose)
self.assertEqual(None, log_file.delete)

mock_dep_success.return_value = None
for verbose in [True, False]:
utils.echo_short_log_if_deployed(last_logs, log_file, verbose)
self.assertEqual(False, log_file.delete)

def test_write_and_echo_logs(self):
last_logs = []
rsp = Mock()
rsp.iter_lines = Mock(return_value=iter(["line1", "line2"]))
utils.write_and_echo_logs(keep_log=True, last_logs=last_logs,
rsp=rsp, verbose=True)
self.assertEqual(last_logs, ["line1", "line2"])


if __name__ == '__main__':
unittest.main()

0 comments on commit c0919ac

Please sign in to comment.