Skip to content

Commit

Permalink
added progress back in
Browse files Browse the repository at this point in the history
  • Loading branch information
Courtney Oka committed May 8, 2017
2 parents ff4dfe7 + d3d73e5 commit 5f62753
Show file tree
Hide file tree
Showing 17 changed files with 283 additions and 54 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ adal==0.4.3
applicationinsights==0.10.0
argcomplete==1.8.0
colorama==0.3.7
humanfriendly==2.4
jmespath
mock==1.3.0
paramiko==2.0.2
Expand Down
6 changes: 6 additions & 0 deletions src/azure-cli-core/azure/cli/core/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import azure.cli.core.azlogging as azlogging
from azure.cli.core.util import todict, truncate_text, CLIError, read_file_content
from azure.cli.core._config import az_config
import azure.cli.core.commands.progress as progress

import azure.cli.core.telemetry as telemetry

Expand Down Expand Up @@ -127,6 +128,11 @@ def __init__(self, configuration=None):

self.parser = AzCliCommandParser(prog='az', parents=[self.global_parser])
self.configuration = configuration
self.progress_controller = progress.ProgressHook()

def get_progress_controller(self):
self.progress_controller.init_progress(progress.get_progress_view())
return self.progress_controller

def initialize(self, configuration):
self.configuration = configuration
Expand Down
14 changes: 12 additions & 2 deletions src/azure-cli-core/azure/cli/core/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import azure.cli.core.azlogging as azlogging
import azure.cli.core.telemetry as telemetry
from azure.cli.core.util import CLIError
from azure.cli.core.application import APPLICATION
from azure.cli.core.prompting import prompt_y_n, NoTTYException
from azure.cli.core._config import az_config, DEFAULTS_SECTION
from azure.cli.core.profiles import ResourceType, supported_api_version
Expand Down Expand Up @@ -126,10 +125,13 @@ def __setattr__(self, name, value):

class LongRunningOperation(object): # pylint: disable=too-few-public-methods

def __init__(self, start_msg='', finish_msg='', poller_done_interval_ms=1000.0):
def __init__(self, start_msg='', finish_msg='',
poller_done_interval_ms=1000.0, progress_controller=None):
self.start_msg = start_msg
self.finish_msg = finish_msg
self.poller_done_interval_ms = poller_done_interval_ms
from azure.cli.core.application import APPLICATION
self.progress_controller = progress_controller or APPLICATION.get_progress_controller()

def _delay(self):
time.sleep(self.poller_done_interval_ms / 1000.0)
Expand All @@ -138,7 +140,9 @@ def __call__(self, poller):
from msrest.exceptions import ClientException
logger.info("Starting long running operation '%s'", self.start_msg)
correlation_message = ''
self.progress_controller.begin()
while not poller.done():
self.progress_controller.add(message='Running')
try:
# pylint: disable=protected-access
correlation_id = json.loads(
Expand All @@ -151,8 +155,10 @@ def __call__(self, poller):
try:
self._delay()
except KeyboardInterrupt:
self.progress_controller.stop()
logger.error('Long running operation wait cancelled. %s', correlation_message)
raise

try:
result = poller.result()
except ClientException as client_exception:
Expand All @@ -161,6 +167,7 @@ def __call__(self, poller):
fault_type='failed-long-running-operation',
summary='Unexpected client exception in {}.'.format(LongRunningOperation.__name__))
message = getattr(client_exception, 'message', client_exception)
self.progress_controller.stop()

try:
message = '{} {}'.format(
Expand All @@ -176,6 +183,7 @@ def __call__(self, poller):

logger.info("Long running operation '%s' completed with result %s",
self.start_msg, result)
self.progress_controller.end()
return result


Expand Down Expand Up @@ -232,6 +240,8 @@ def __init__(self, name, handler, description=None, table_transformer=None,

@staticmethod
def _should_load_description():
from azure.cli.core.application import APPLICATION

return not APPLICATION.session['completer_active']

def load_arguments(self):
Expand Down
1 change: 1 addition & 0 deletions src/azure-cli-core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
'applicationinsights',
'argcomplete>=1.8.0',
'colorama',
'humanfriendly',
'jmespath',
'msrest>=0.4.4',
'msrestazure>=0.4.7',
Expand Down
1 change: 1 addition & 0 deletions src/azure-cli-core/tests/test_progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def test_progress_indicator_indet_stdview(self):
view.write({})
self.assertEqual(view.spinner.label, 'In Progress')
before = view.spinner.total

view.write({})
after = view.spinner.total
self.assertTrue(after >= before)
Expand Down
3 changes: 2 additions & 1 deletion src/azure-cli-testsdk/azure/cli/testsdk/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from .patches import (patch_load_cached_subscriptions, patch_main_exception_handler,
patch_retrieve_token_for_user, patch_long_run_operation_delay,
patch_time_sleep_api)
patch_time_sleep_api, patch_progress_controller)
from .exceptions import CliExecutionError
from .const import (ENV_LIVE_TEST, ENV_SKIP_ASSERT, ENV_TEST_DIAGNOSE, MOCKED_SUBSCRIPTION_ID)
from .recording_processors import (SubscriptionRecordingProcessor, OAuthRequestResponsesFilter,
Expand Down Expand Up @@ -160,6 +160,7 @@ def setUp(self):
patch_long_run_operation_delay(self)
patch_load_cached_subscriptions(self)
patch_retrieve_token_for_user(self)
patch_progress_controller(self)

def tearDown(self):
os.environ = self.original_env
Expand Down
13 changes: 13 additions & 0 deletions src/azure-cli-testsdk/azure/cli/testsdk/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@
from .const import MOCKED_SUBSCRIPTION_ID, MOCKED_TENANT_ID


def patch_progress_controller(unit_test):
def _handle_progress_update(*args): # pylint: disable=unused-argument
pass

def _handle_progress_add(*args, **kwargs): # pylint: disable=unused-argument
pass

_mock_in_unit_test(
unit_test, 'azure.cli.core.commands.progress.ProgressHook.update', _handle_progress_update)
_mock_in_unit_test(
unit_test, 'azure.cli.core.commands.progress.ProgressHook.add', _handle_progress_add)


def patch_main_exception_handler(unit_test):
from vcr.errors import CannotOverwriteExistingCassetteException

Expand Down
17 changes: 17 additions & 0 deletions src/azure-cli-testsdk/azure/cli/testsdk/vcr_test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ def _mock_operation_delay(_):
return


class _MockOutstream(object):
""" mock outstream for testing """
def __init__(self):
self.string = ''

def write(self, message):
self.string = message

def flush(self):
pass


def _mock_get_progress_view(determinant=False, out=None): # pylint: disable=unused-argument
return _MockOutstream()


# TEST CHECKS


Expand Down Expand Up @@ -373,6 +389,7 @@ def _execute_live_or_recording(self):
if callable(tear_down) and not self.skip_teardown:
self.tear_down()

@mock.patch('azure.cli.core.commands.progress.get_progress_view', _mock_get_progress_view)
@mock.patch('azure.cli.core._profile.Profile.load_cached_subscriptions', _mock_subscriptions)
@mock.patch('azure.cli.core._profile.CredsCache.retrieve_token_for_user',
_mock_user_access_token) # pylint: disable=line-too-long
Expand Down
4 changes: 3 additions & 1 deletion src/command_modules/azure-cli-interactive/HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
.. :changelog:
Release History
===============


0.3.0 (2017-05-05)
++++++++++++++++++

* Integrate interactive into az
* Colors Options
* Rename 'shell' => 'interactive'
* --progress flag


0.2.1
++++++++++++++++++
Expand Down
Loading

0 comments on commit 5f62753

Please sign in to comment.