Skip to content

Commit

Permalink
feat(perf): Improve load time of help text
Browse files Browse the repository at this point in the history
  • Loading branch information
jfuss committed Oct 4, 2019
1 parent af52032 commit 8f952cd
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 90 deletions.
21 changes: 12 additions & 9 deletions samcli/commands/build/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@
import logging
import click

from samcli.commands.exceptions import UserException
from samcli.cli.main import pass_context, common_options as cli_framework_options, aws_creds_options
from samcli.commands._utils.options import template_option_without_build, docker_common_options, \
parameter_override_option
from samcli.commands.build.build_context import BuildContext
from samcli.lib.build.app_builder import ApplicationBuilder, BuildError, UnsupportedBuilderLibraryVersionError, \
ContainerBuildNotSupported
from samcli.lib.build.workflow_config import UnsupportedRuntimeException
from samcli.local.lambdafn.exceptions import FunctionNotFound
from samcli.commands._utils.template import move_template
from samcli.cli.main import pass_context, common_options as cli_framework_options, aws_creds_options
from samcli.lib.telemetry.metrics import track_command


LOG = logging.getLogger(__name__)

DEFAULT_BUILD_DIR = os.path.join(".aws-sam", "build")
Expand Down Expand Up @@ -105,7 +99,7 @@ def cli(ctx,
skip_pull_image, parameter_overrides, mode) # pragma: no cover


def do_cli(function_identifier, # pylint: disable=too-many-locals
def do_cli(function_identifier, # pylint: disable=too-many-locals, too-many-statements
template,
base_dir,
build_dir,
Expand All @@ -120,6 +114,15 @@ def do_cli(function_identifier, # pylint: disable=too-many-locals
Implementation of the ``cli`` method
"""

from samcli.commands.exceptions import UserException

from samcli.commands.build.build_context import BuildContext
from samcli.lib.build.app_builder import ApplicationBuilder, BuildError, UnsupportedBuilderLibraryVersionError, \
ContainerBuildNotSupported
from samcli.lib.build.workflow_config import UnsupportedRuntimeException
from samcli.local.lambdafn.exceptions import FunctionNotFound
from samcli.commands._utils.template import move_template

LOG.debug("'build' command is called")

if use_container:
Expand Down
10 changes: 5 additions & 5 deletions samcli/commands/init/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@
import click

from samcli.cli.main import pass_context, common_options
from samcli.commands.exceptions import UserException
from samcli.local.common.runtime_template import INIT_RUNTIMES, SUPPORTED_DEP_MANAGERS, DEFAULT_RUNTIME
from samcli.local.init import generate_project
from samcli.local.init.exceptions import GenerateProjectFailedError
from samcli.lib.telemetry.metrics import track_command

from samcli.local.common.runtime_template import INIT_RUNTIMES, SUPPORTED_DEP_MANAGERS, DEFAULT_RUNTIME

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -95,6 +91,10 @@ def do_cli(ctx, location, runtime, dependency_manager, output_dir, name, no_inpu
"""
Implementation of the ``cli`` method, just separated out for unit testing purposes
"""
from samcli.commands.exceptions import UserException
from samcli.local.init import generate_project
from samcli.local.init.exceptions import GenerateProjectFailedError

LOG.debug("Init command")
click.secho("[+] Initializing project structure...", fg="green")

Expand Down
17 changes: 9 additions & 8 deletions samcli/commands/local/invoke/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@

from samcli.cli.main import pass_context, common_options as cli_framework_options, aws_creds_options
from samcli.commands.local.cli_common.options import invoke_common_options
from samcli.commands.exceptions import UserException
from samcli.commands.local.lib.exceptions import InvalidLayerReference
from samcli.commands.local.cli_common.invoke_context import InvokeContext
from samcli.local.lambdafn.exceptions import FunctionNotFound
from samcli.commands.validate.lib.exceptions import InvalidSamDocumentException
from samcli.commands.local.lib.exceptions import OverridesNotWellDefinedError
from samcli.local.docker.manager import DockerImagePullFailedException
from samcli.local.docker.lambda_debug_entrypoint import DebuggingNotSupported
from samcli.lib.telemetry.metrics import track_command


Expand Down Expand Up @@ -113,6 +105,15 @@ def do_cli( # pylint: disable=R0914
Implementation of the ``cli`` method, just separated out for unit testing purposes
"""

from samcli.commands.exceptions import UserException
from samcli.commands.local.lib.exceptions import InvalidLayerReference
from samcli.commands.local.cli_common.invoke_context import InvokeContext
from samcli.local.lambdafn.exceptions import FunctionNotFound
from samcli.commands.validate.lib.exceptions import InvalidSamDocumentException
from samcli.commands.local.lib.exceptions import OverridesNotWellDefinedError
from samcli.local.docker.manager import DockerImagePullFailedException
from samcli.local.docker.lambda_debug_entrypoint import DebuggingNotSupported

LOG.debug("local invoke command is called")

if no_event and event != STDIN_FILE_NAME:
Expand Down
15 changes: 8 additions & 7 deletions samcli/commands/local/start_api/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@

from samcli.cli.main import pass_context, common_options as cli_framework_options, aws_creds_options
from samcli.commands.local.cli_common.options import invoke_common_options, service_common_options
from samcli.commands.local.cli_common.invoke_context import InvokeContext
from samcli.commands.local.lib.exceptions import NoApisDefined, InvalidLayerReference
from samcli.commands.exceptions import UserException
from samcli.commands.local.lib.local_api_service import LocalApiService
from samcli.commands.validate.lib.exceptions import InvalidSamDocumentException
from samcli.commands.local.lib.exceptions import OverridesNotWellDefinedError
from samcli.local.docker.lambda_debug_entrypoint import DebuggingNotSupported
from samcli.lib.telemetry.metrics import track_command


Expand Down Expand Up @@ -114,6 +107,14 @@ def do_cli( # pylint: disable=R0914
Implementation of the ``cli`` method, just separated out for unit testing purposes
"""

from samcli.commands.local.cli_common.invoke_context import InvokeContext
from samcli.commands.local.lib.exceptions import NoApisDefined, InvalidLayerReference
from samcli.commands.exceptions import UserException
from samcli.commands.local.lib.local_api_service import LocalApiService
from samcli.commands.validate.lib.exceptions import InvalidSamDocumentException
from samcli.commands.local.lib.exceptions import OverridesNotWellDefinedError
from samcli.local.docker.lambda_debug_entrypoint import DebuggingNotSupported

LOG.debug("local start-api command is called")

# Pass all inputs to setup necessary context to invoke function locally.
Expand Down
15 changes: 8 additions & 7 deletions samcli/commands/local/start_lambda/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@

from samcli.cli.main import pass_context, common_options as cli_framework_options, aws_creds_options
from samcli.commands.local.cli_common.options import invoke_common_options, service_common_options
from samcli.commands.local.cli_common.invoke_context import InvokeContext
from samcli.commands.local.cli_common.user_exceptions import UserException
from samcli.commands.local.lib.exceptions import InvalidLayerReference
from samcli.commands.local.lib.local_lambda_service import LocalLambdaService
from samcli.commands.validate.lib.exceptions import InvalidSamDocumentException
from samcli.commands.local.lib.exceptions import OverridesNotWellDefinedError
from samcli.local.docker.lambda_debug_entrypoint import DebuggingNotSupported
from samcli.lib.telemetry.metrics import track_command


Expand Down Expand Up @@ -123,6 +116,14 @@ def do_cli( # pylint: disable=R0914
Implementation of the ``cli`` method, just separated out for unit testing purposes
"""

from samcli.commands.local.cli_common.invoke_context import InvokeContext
from samcli.commands.local.cli_common.user_exceptions import UserException
from samcli.commands.local.lib.exceptions import InvalidLayerReference
from samcli.commands.local.lib.local_lambda_service import LocalLambdaService
from samcli.commands.validate.lib.exceptions import InvalidSamDocumentException
from samcli.commands.local.lib.exceptions import OverridesNotWellDefinedError
from samcli.local.docker.lambda_debug_entrypoint import DebuggingNotSupported

LOG.debug("local start_lambda command is called")

# Pass all inputs to setup necessary context to invoke function locally.
Expand Down
2 changes: 1 addition & 1 deletion samcli/commands/logs/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from samcli.cli.main import pass_context, common_options as cli_framework_options, aws_creds_options
from samcli.lib.telemetry.metrics import track_command
from .logs_context import LogsCommandContext

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -84,6 +83,7 @@ def do_cli(function_name, stack_name, filter_pattern, tailing, start_time, end_t
"""
Implementation of the ``cli`` method
"""
from .logs_context import LogsCommandContext

LOG.debug("'logs' command is called")

Expand Down
13 changes: 8 additions & 5 deletions samcli/commands/publish/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@

import click
import boto3
from serverlessrepo import publish_application
from serverlessrepo.publish import CREATE_APPLICATION
from serverlessrepo.parser import METADATA, SERVERLESS_REPO_APPLICATION
from serverlessrepo.exceptions import ServerlessRepoError, InvalidS3UriError

from samcli.cli.main import pass_context, common_options as cli_framework_options, aws_creds_options
from samcli.commands._utils.options import template_common_option
from samcli.commands._utils.template import get_template_data
from samcli.commands.exceptions import UserException
from samcli.lib.telemetry.metrics import track_command
from serverlessrepo.publish import CREATE_APPLICATION

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -58,6 +54,13 @@ def cli(ctx, template, semantic_version):

def do_cli(ctx, template, semantic_version):
"""Publish the application based on command line inputs."""

from serverlessrepo import publish_application
from serverlessrepo.parser import METADATA, SERVERLESS_REPO_APPLICATION
from serverlessrepo.exceptions import ServerlessRepoError, InvalidS3UriError

from samcli.commands.exceptions import UserException

try:
template_data = get_template_data(template)
except ValueError as ex:
Expand Down
24 changes: 14 additions & 10 deletions samcli/commands/validate/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@
import boto3
from botocore.exceptions import NoCredentialsError
import click
from samtranslator.translator.managed_policy_translator import ManagedPolicyLoader

from samcli.commands.exceptions import UserException
from samcli.cli.main import pass_context, common_options as cli_framework_options, aws_creds_options
from samcli.commands._utils.options import template_option_without_build
from samcli.commands.local.cli_common.user_exceptions import InvalidSamTemplateException, SamTemplateNotFoundException
from samcli.yamlhelper import yaml_parse
from samcli.lib.telemetry.metrics import track_command
from .lib.exceptions import InvalidSamDocumentException
from .lib.sam_template_validator import SamTemplateValidator


@click.command("validate", short_help="Validate an AWS SAM template.")
Expand All @@ -35,6 +29,12 @@ def do_cli(ctx, template):
"""
Implementation of the ``cli`` method, just separated out for unit testing purposes
"""
from samtranslator.translator.managed_policy_translator import ManagedPolicyLoader

from samcli.commands.exceptions import UserException
from samcli.commands.local.cli_common.user_exceptions import InvalidSamTemplateException
from .lib.exceptions import InvalidSamDocumentException
from .lib.sam_template_validator import SamTemplateValidator

sam_template = _read_sam_file(template)

Expand All @@ -54,12 +54,16 @@ def do_cli(ctx, template):

def _read_sam_file(template):
"""
Reads the file (json and yaml supported) provided and returns the dictionary representation of the file.
Reads the file (json and yaml supported) provided and returns the dictionary representation of the file.
:param str template: Path to the template file
:return dict: Dictionary representing the SAM Template
:raises: SamTemplateNotFoundException when the template file does not exist
:param str template: Path to the template file
:return dict: Dictionary representing the SAM Template
:raises: SamTemplateNotFoundException when the template file does not exist
"""

from samcli.commands.local.cli_common.user_exceptions import SamTemplateNotFoundException
from samcli.yamlhelper import yaml_parse

if not os.path.exists(template):
click.secho("SAM Template Not Found", bg="red")
raise SamTemplateNotFoundException("Template at {} is not found".format(template))
Expand Down
14 changes: 7 additions & 7 deletions tests/unit/commands/buildcmd/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@


class TestDoCli(TestCase):
@patch("samcli.commands.build.command.BuildContext")
@patch("samcli.commands.build.command.ApplicationBuilder")
@patch("samcli.commands.build.command.move_template")
@patch("samcli.commands.build.build_context.BuildContext")
@patch("samcli.lib.build.app_builder.ApplicationBuilder")
@patch("samcli.commands._utils.template.move_template")
@patch("samcli.commands.build.command.os")
def test_must_succeed_build(self, os_mock, move_template_mock, ApplicationBuilderMock, BuildContextMock):

Expand Down Expand Up @@ -63,8 +63,8 @@ def test_must_succeed_build(self, os_mock, move_template_mock, ApplicationBuilde
(UnsupportedBuilderLibraryVersionError(container_name="name", error_msg="msg"),),
]
)
@patch("samcli.commands.build.command.BuildContext")
@patch("samcli.commands.build.command.ApplicationBuilder")
@patch("samcli.commands.build.build_context.BuildContext")
@patch("samcli.lib.build.app_builder.ApplicationBuilder")
def test_must_catch_known_exceptions(self, exception, ApplicationBuilderMock, BuildContextMock):

ctx_mock = Mock()
Expand All @@ -91,8 +91,8 @@ def test_must_catch_known_exceptions(self, exception, ApplicationBuilderMock, Bu

self.assertEqual(str(ctx.exception), str(exception))

@patch("samcli.commands.build.command.BuildContext")
@patch("samcli.commands.build.command.ApplicationBuilder")
@patch("samcli.commands.build.build_context.BuildContext")
@patch("samcli.lib.build.app_builder.ApplicationBuilder")
def test_must_catch_function_not_found_exception(self, ApplicationBuilderMock, BuildContextMock):
ctx_mock = Mock()
BuildContextMock.return_value.__enter__ = Mock()
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/commands/init/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def setUp(self):
self.name = "testing project"
self.no_input = False

@patch("samcli.commands.init.generate_project")
@patch("samcli.local.init.generate_project")
def test_init_cli(self, generate_project_patch):
# GIVEN generate_project successfully created a project
# WHEN a project name has been passed
Expand All @@ -35,7 +35,7 @@ def test_init_cli(self, generate_project_patch):
self.location, self.runtime, self.dependency_manager, self.output_dir, self.name, self.no_input
)

@patch("samcli.commands.init.generate_project")
@patch("samcli.local.init.generate_project")
def test_init_cli_generate_project_fails(self, generate_project_patch):

# GIVEN generate_project fails to create a project
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/commands/local/invoke/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def setUp(self):
self.region_name = "region"
self.profile = "profile"

@patch("samcli.commands.local.invoke.cli.InvokeContext")
@patch("samcli.commands.local.cli_common.invoke_context.InvokeContext")
@patch("samcli.commands.local.invoke.cli._get_event")
def test_cli_must_setup_context_and_invoke(self, get_event_mock, InvokeContextMock):
event_data = "data"
Expand Down Expand Up @@ -95,7 +95,7 @@ def test_cli_must_setup_context_and_invoke(self, get_event_mock, InvokeContextMo
)
get_event_mock.assert_called_with(self.eventfile)

@patch("samcli.commands.local.invoke.cli.InvokeContext")
@patch("samcli.commands.local.cli_common.invoke_context.InvokeContext")
@patch("samcli.commands.local.invoke.cli._get_event")
def test_cli_must_invoke_with_no_event(self, get_event_mock, InvokeContextMock):
self.no_event = True
Expand Down Expand Up @@ -149,7 +149,7 @@ def test_cli_must_invoke_with_no_event(self, get_event_mock, InvokeContextMock):
)
get_event_mock.assert_not_called()

@patch("samcli.commands.local.invoke.cli.InvokeContext")
@patch("samcli.commands.local.cli_common.invoke_context.InvokeContext")
@patch("samcli.commands.local.invoke.cli._get_event")
def test_must_raise_user_exception_on_no_event_and_event(self, get_event_mock, InvokeContextMock):
self.no_event = True
Expand Down Expand Up @@ -188,7 +188,7 @@ def test_must_raise_user_exception_on_no_event_and_event(self, get_event_mock, I
param(DockerImagePullFailedException("Failed to pull image"), "Failed to pull image"),
]
)
@patch("samcli.commands.local.invoke.cli.InvokeContext")
@patch("samcli.commands.local.cli_common.invoke_context.InvokeContext")
@patch("samcli.commands.local.invoke.cli._get_event")
def test_must_raise_user_exception_on_function_not_found(
self, side_effect_exception, expected_exectpion_message, get_event_mock, InvokeContextMock
Expand Down Expand Up @@ -240,7 +240,7 @@ def test_must_raise_user_exception_on_function_not_found(
(DebuggingNotSupported("Debugging not supported"), "Debugging not supported"),
]
)
@patch("samcli.commands.local.invoke.cli.InvokeContext")
@patch("samcli.commands.local.cli_common.invoke_context.InvokeContext")
@patch("samcli.commands.local.invoke.cli._get_event")
def test_must_raise_user_exception_on_invalid_sam_template(
self, exeception_to_raise, execption_message, get_event_mock, InvokeContextMock
Expand Down Expand Up @@ -278,7 +278,7 @@ def test_must_raise_user_exception_on_invalid_sam_template(
msg = str(ex_ctx.exception)
self.assertEqual(msg, execption_message)

@patch("samcli.commands.local.invoke.cli.InvokeContext")
@patch("samcli.commands.local.cli_common.invoke_context.InvokeContext")
@patch("samcli.commands.local.invoke.cli._get_event")
def test_must_raise_user_exception_on_invalid_env_vars(self, get_event_mock, InvokeContextMock):
event_data = "data"
Expand Down
Loading

0 comments on commit 8f952cd

Please sign in to comment.