Esempio n. 1
0
def create_invoker_and_load_cmds_and_args(cli_ctx):
    from knack import events
    from azure.cli.core.commands import register_cache_arguments
    from azure.cli.core.commands.arm import register_global_subscription_argument, register_ids_argument

    invoker = cli_ctx.invocation_cls(
        cli_ctx=cli_ctx,
        commands_loader_cls=cli_ctx.commands_loader_cls,
        parser_cls=cli_ctx.parser_cls,
        help_cls=cli_ctx.help_cls)
    cli_ctx.invocation = invoker
    invoker.commands_loader.skip_applicability = True
    invoker.commands_loader.load_command_table(None)

    # turn off applicability check for all loaders
    for loaders in invoker.commands_loader.cmd_to_loader_map.values():
        for loader in loaders:
            loader.skip_applicability = True

    for command in invoker.commands_loader.command_table:
        invoker.commands_loader.load_arguments(command)

    register_global_subscription_argument(cli_ctx)
    register_ids_argument(
        cli_ctx)  # global subscription must be registered first!
    register_cache_arguments(cli_ctx)
    cli_ctx.raise_event(events.EVENT_INVOKER_POST_CMD_TBL_CREATE,
                        commands_loader=invoker.commands_loader)
    invoker.parser.load_command_table(invoker.commands_loader)
Esempio n. 2
0
    def __init__(self, **kwargs):
        super(AzCli, self).__init__(**kwargs)

        from azure.cli.core.commands import register_cache_arguments
        from azure.cli.core.commands.arm import (
            register_ids_argument, register_global_subscription_argument)
        from azure.cli.core.cloud import get_active_cloud
        from azure.cli.core.commands.transform import register_global_transforms
        from azure.cli.core._session import ACCOUNT, CONFIG, SESSION, INDEX

        from knack.util import ensure_dir

        self.data['headers'] = {}
        self.data['command'] = 'unknown'
        self.data['command_extension_name'] = None
        self.data['completer_active'] = ARGCOMPLETE_ENV_NAME in os.environ
        self.data['query_active'] = False

        azure_folder = self.config.config_dir
        ensure_dir(azure_folder)
        ACCOUNT.load(os.path.join(azure_folder, 'azureProfile.json'))
        CONFIG.load(os.path.join(azure_folder, 'az.json'))
        SESSION.load(os.path.join(azure_folder, 'az.sess'), max_age=3600)
        INDEX.load(os.path.join(azure_folder, 'commandIndex.json'))

        self.cloud = get_active_cloud(self)
        logger.debug('Current cloud config:\n%s', str(self.cloud.name))
        self.local_context = AzCLILocalContext(self)
        register_global_transforms(self)
        register_global_subscription_argument(self)
        register_ids_argument(
            self)  # global subscription must be registered first!
        register_cache_arguments(self)

        self.progress_controller = None
Esempio n. 3
0
def create_invoker_and_load_cmds_and_args(cli_ctx):
    from knack import events
    from azure.cli.core.commands import register_cache_arguments
    from azure.cli.core.commands.arm import register_global_subscription_argument, register_ids_argument

    invoker = cli_ctx.invocation_cls(cli_ctx=cli_ctx, commands_loader_cls=cli_ctx.commands_loader_cls,
                                     parser_cls=cli_ctx.parser_cls, help_cls=cli_ctx.help_cls)
    cli_ctx.invocation = invoker
    invoker.commands_loader.skip_applicability = True
    invoker.commands_loader.load_command_table(None)

    # Deal with failed import MainCommandsLoader.load_command_table._update_command_table_from_modules
    # during tox test.
    if not invoker.commands_loader.cmd_to_loader_map:
        module_command_table, module_group_table = mock_load_command_loader(invoker.commands_loader, None,
                                                                            MOCKED_COMMAND_LOADER_MOD, None)
        for cmd in module_command_table.values():
            cmd.command_source = MOCKED_COMMAND_LOADER_MOD
        invoker.commands_loader.command_table.update(module_command_table)
        invoker.commands_loader.command_group_table.update(module_group_table)

    # turn off applicability check for all loaders
    for loaders in invoker.commands_loader.cmd_to_loader_map.values():
        for loader in loaders:
            loader.skip_applicability = True

    for command in invoker.commands_loader.command_table:
        invoker.commands_loader.load_arguments(command)

    register_global_subscription_argument(cli_ctx)
    register_ids_argument(cli_ctx)  # global subscription must be registered first!
    register_cache_arguments(cli_ctx)
    cli_ctx.raise_event(events.EVENT_INVOKER_POST_CMD_TBL_CREATE, commands_loader=invoker.commands_loader)
    invoker.parser.load_command_table(invoker.commands_loader)
Esempio n. 4
0
def create_invoker_and_load_cmds_and_args(cli_ctx):
    from knack import events
    from azure.cli.core.commands import register_cache_arguments
    from azure.cli.core.commands.arm import register_global_subscription_argument, register_ids_argument

    invoker = cli_ctx.invocation_cls(cli_ctx=cli_ctx, commands_loader_cls=cli_ctx.commands_loader_cls,
                                     parser_cls=cli_ctx.parser_cls, help_cls=cli_ctx.help_cls)
    cli_ctx.invocation = invoker
    invoker.commands_loader.skip_applicability = True
    invoker.commands_loader.load_command_table(None)

    # Deal with failed import MainCommandsLoader.load_command_table._update_command_table_from_modules
    # during tox test.
    if not invoker.commands_loader.cmd_to_loader_map:
        module_command_table, module_group_table = mock_load_command_loader(invoker.commands_loader, None,
                                                                            MOCKED_COMMAND_LOADER_MOD, None)
        for cmd in module_command_table.values():
            cmd.command_source = MOCKED_COMMAND_LOADER_MOD
        invoker.commands_loader.command_table.update(module_command_table)
        invoker.commands_loader.command_group_table.update(module_group_table)

    # turn off applicability check for all loaders
    for loaders in invoker.commands_loader.cmd_to_loader_map.values():
        for loader in loaders:
            loader.skip_applicability = True

    for command in invoker.commands_loader.command_table:
        invoker.commands_loader.load_arguments(command)

    register_global_subscription_argument(cli_ctx)
    register_ids_argument(cli_ctx)  # global subscription must be registered first!
    register_cache_arguments(cli_ctx)
    cli_ctx.raise_event(events.EVENT_INVOKER_POST_CMD_TBL_CREATE, commands_loader=invoker.commands_loader)
    invoker.parser.load_command_table(invoker.commands_loader)
Esempio n. 5
0
def create_invoker_and_load_cmds_and_args(cli_ctx):
    global loaded_helps
    from knack import events
    from azure.cli.core.commands import register_cache_arguments
    from azure.cli.core.commands.arm import register_global_subscription_argument, register_ids_argument

    invoker = cli_ctx.invocation_cls(cli_ctx=cli_ctx, commands_loader_cls=cli_ctx.commands_loader_cls,
                                     parser_cls=cli_ctx.parser_cls, help_cls=cli_ctx.help_cls)
    cli_ctx.invocation = invoker
    invoker.commands_loader.skip_applicability = True
    temp_help = helps.copy()
    invoker.commands_loader.load_command_table(None) # this ends up loading all the helpfiles, which could be problematic with duplicate key commands like acs create
    helps.clear()
    helps.update(temp_help)

    # turn off applicability check for applicable loaders
    new_cmd_to_loader_map = {}
    new_command_group_table = {}
    new_command_table = {}
    for cmd in loaded_helps.keys():
        if cmd in invoker.commands_loader.cmd_to_loader_map: # if a command from help, then update commands_loader
            new_cmd_to_loader_map[cmd] = invoker.commands_loader.cmd_to_loader_map[cmd]
            new_command_table[cmd] = invoker.commands_loader.command_table[cmd]
        else: # else a group then update
            new_command_group_table[cmd] = invoker.commands_loader.command_group_table[cmd]

    # include commands of groups in help.py, even though the commands themselves might not be in help.py, so their subparsers can be added.
    for cmd in new_command_group_table.keys():
        all_cmds = invoker.commands_loader.cmd_to_loader_map.keys()
        for old_cmd in all_cmds:
            if old_cmd.startswith(cmd) and old_cmd != cmd:
                new_cmd_to_loader_map[old_cmd] = invoker.commands_loader.cmd_to_loader_map[old_cmd]
                new_command_table[old_cmd] = invoker.commands_loader.command_table[old_cmd]

    invoker.commands_loader.cmd_to_loader_map = new_cmd_to_loader_map
    invoker.commands_loader.command_table = new_command_table
    invoker.commands_loader.command_group_table = new_command_group_table

    for loaders in invoker.commands_loader.cmd_to_loader_map.values():
        for loader in loaders:
            loader.skip_applicability = True

    for command in invoker.commands_loader.command_table:
        invoker.commands_loader.load_arguments(command)

    assert len(new_command_table) == len(new_cmd_to_loader_map)
    assert set(list(new_command_table.keys()) + list(new_command_group_table.keys())) >= set(loaded_helps.keys())

    register_global_subscription_argument(cli_ctx)
    register_ids_argument(cli_ctx)  # global subscription must be registered first!
    register_cache_arguments(cli_ctx)
    cli_ctx.raise_event(events.EVENT_INVOKER_POST_CMD_TBL_CREATE, commands_loader=invoker.commands_loader)
    invoker.parser.load_command_table(invoker.commands_loader)
Esempio n. 6
0
    def __init__(self, **kwargs):
        super(AzCli, self).__init__(**kwargs)

        from azure.cli.core.commands import register_cache_arguments
        from azure.cli.core.commands.arm import (
            register_ids_argument, register_global_subscription_argument)
        from azure.cli.core.cloud import get_active_cloud
        from azure.cli.core.commands.transform import register_global_transforms
        from azure.cli.core._session import ACCOUNT, CONFIG, SESSION, INDEX, VERSIONS
        from azure.cli.core.style import format_styled_text
        from azure.cli.core.util import handle_version_update
        from azure.cli.core.commands.query_examples import register_global_query_examples_argument

        from knack.util import ensure_dir

        self.data['headers'] = {}
        self.data['command'] = 'unknown'
        self.data['command_extension_name'] = None
        self.data['completer_active'] = ARGCOMPLETE_ENV_NAME in os.environ
        self.data['query_active'] = False

        azure_folder = self.config.config_dir
        ensure_dir(azure_folder)
        ACCOUNT.load(os.path.join(azure_folder, 'azureProfile.json'))
        CONFIG.load(os.path.join(azure_folder, 'az.json'))
        SESSION.load(os.path.join(azure_folder, 'az.sess'), max_age=3600)
        INDEX.load(os.path.join(azure_folder, 'commandIndex.json'))
        VERSIONS.load(os.path.join(azure_folder, 'versionCheck.json'))
        handle_version_update()

        self.cloud = get_active_cloud(self)
        logger.debug('Current cloud config:\n%s', str(self.cloud.name))
        self.local_context = AzCLILocalContext(self)
        register_global_transforms(self)
        register_global_subscription_argument(self)
        register_global_query_examples_argument(self)
        register_ids_argument(
            self)  # global subscription must be registered first!
        register_cache_arguments(self)

        self.progress_controller = None

        if self.enable_color:
            theme = self.config.get('core', 'theme', fallback='dark')
        else:
            theme = 'none'
        format_styled_text.theme = theme
Esempio n. 7
0
def create_invoker_and_load_cmds_and_args(cli_ctx):
    from knack.events import (EVENT_INVOKER_PRE_CMD_TBL_CREATE,
                              EVENT_INVOKER_POST_CMD_TBL_CREATE)
    from azure.cli.core.commands import register_cache_arguments
    from azure.cli.core.commands.arm import register_global_subscription_argument, register_ids_argument
    from azure.cli.core.commands.events import (
        EVENT_INVOKER_PRE_LOAD_ARGUMENTS, EVENT_INVOKER_POST_LOAD_ARGUMENTS)
    import time

    start_time = time.time()

    register_global_subscription_argument(cli_ctx)
    register_ids_argument(cli_ctx)
    register_cache_arguments(cli_ctx)

    invoker = cli_ctx.invocation_cls(
        cli_ctx=cli_ctx,
        commands_loader_cls=cli_ctx.commands_loader_cls,
        parser_cls=cli_ctx.parser_cls,
        help_cls=cli_ctx.help_cls)
    cli_ctx.invocation = invoker
    invoker.commands_loader.skip_applicability = True

    cli_ctx.raise_event(EVENT_INVOKER_PRE_CMD_TBL_CREATE, args=[])
    invoker.commands_loader.load_command_table(None)
    invoker.commands_loader.command_name = ''

    cli_ctx.raise_event(EVENT_INVOKER_PRE_LOAD_ARGUMENTS,
                        commands_loader=invoker.commands_loader)
    invoker.commands_loader.load_arguments()

    cli_ctx.raise_event(EVENT_INVOKER_POST_LOAD_ARGUMENTS,
                        commands_loader=invoker.commands_loader)
    cli_ctx.raise_event(EVENT_INVOKER_POST_CMD_TBL_CREATE,
                        commands_loader=invoker.commands_loader)
    invoker.parser.cli_ctx = cli_ctx
    invoker.parser.load_command_table(invoker.commands_loader)

    end_time = time.time()
    logger.info('Time to load entire command table: %.3f sec',
                end_time - start_time)
Esempio n. 8
0
def create_invoker_and_load_cmds_and_args(cli_ctx):
    from knack import events
    from azure.cli.core.commands import register_cache_arguments
    from azure.cli.core.commands.arm import register_global_subscription_argument, register_ids_argument

    invoker = cli_ctx.invocation_cls(cli_ctx=cli_ctx, commands_loader_cls=cli_ctx.commands_loader_cls,
                                     parser_cls=cli_ctx.parser_cls, help_cls=cli_ctx.help_cls)
    cli_ctx.invocation = invoker
    invoker.commands_loader.skip_applicability = True
    invoker.commands_loader.load_command_table(None)

    # turn off applicability check for all loaders
    for loaders in invoker.commands_loader.cmd_to_loader_map.values():
        for loader in loaders:
            loader.skip_applicability = True

    for command in invoker.commands_loader.command_table:
        invoker.commands_loader.load_arguments(command)

    register_global_subscription_argument(cli_ctx)
    register_ids_argument(cli_ctx)  # global subscription must be registered first!
    register_cache_arguments(cli_ctx)
    cli_ctx.raise_event(events.EVENT_INVOKER_POST_CMD_TBL_CREATE, commands_loader=invoker.commands_loader)
    invoker.parser.load_command_table(invoker.commands_loader)
Esempio n. 9
0
def create_invoker_and_load_cmds_and_args(cli_ctx):
    global loaded_helps
    from knack import events
    from azure.cli.core.commands import register_cache_arguments
    from azure.cli.core.commands.arm import register_global_subscription_argument, register_ids_argument

    invoker = cli_ctx.invocation_cls(
        cli_ctx=cli_ctx,
        commands_loader_cls=cli_ctx.commands_loader_cls,
        parser_cls=cli_ctx.parser_cls,
        help_cls=cli_ctx.help_cls)
    cli_ctx.invocation = invoker
    invoker.commands_loader.skip_applicability = True
    temp_help = helps.copy()
    invoker.commands_loader.load_command_table(
        None
    )  # this ends up loading all the helpfiles, which could be problematic with duplicate key commands like acs create
    helps.clear()
    helps.update(temp_help)

    # turn off applicability check for applicable loaders
    new_cmd_to_loader_map = {}
    new_command_group_table = {}
    new_command_table = {}
    for cmd in loaded_helps.keys():
        if cmd in invoker.commands_loader.cmd_to_loader_map:  # if a command from help, then update commands_loader
            new_cmd_to_loader_map[
                cmd] = invoker.commands_loader.cmd_to_loader_map[cmd]
            new_command_table[cmd] = invoker.commands_loader.command_table[cmd]
        else:  # else a group then update
            new_command_group_table[
                cmd] = invoker.commands_loader.command_group_table[cmd]

    # include commands of groups in help.py, even though the commands themselves might not be in help.py, so their subparsers can be added.
    for cmd in new_command_group_table.keys():
        all_cmds = invoker.commands_loader.cmd_to_loader_map.keys()
        for old_cmd in all_cmds:
            if old_cmd.startswith(cmd) and old_cmd != cmd:
                new_cmd_to_loader_map[
                    old_cmd] = invoker.commands_loader.cmd_to_loader_map[
                        old_cmd]
                new_command_table[
                    old_cmd] = invoker.commands_loader.command_table[old_cmd]

    invoker.commands_loader.cmd_to_loader_map = new_cmd_to_loader_map
    invoker.commands_loader.command_table = new_command_table
    invoker.commands_loader.command_group_table = new_command_group_table

    for loaders in invoker.commands_loader.cmd_to_loader_map.values():
        for loader in loaders:
            loader.skip_applicability = True

    for command in invoker.commands_loader.command_table:
        invoker.commands_loader.load_arguments(command)

    assert len(new_command_table) == len(new_cmd_to_loader_map)
    assert set(
        list(new_command_table.keys()) +
        list(new_command_group_table.keys())) >= set(loaded_helps.keys())

    register_global_subscription_argument(cli_ctx)
    register_ids_argument(
        cli_ctx)  # global subscription must be registered first!
    register_cache_arguments(cli_ctx)
    cli_ctx.raise_event(events.EVENT_INVOKER_POST_CMD_TBL_CREATE,
                        commands_loader=invoker.commands_loader)
    invoker.parser.load_command_table(invoker.commands_loader)