Skip to content

Commit

Permalink
Revert "feat: sam init changes to improve getting manifest time and c…
Browse files Browse the repository at this point in the history
…lone messaging (#3601)" (#3665)

This reverts commit 520f68c.

Co-authored-by: Wilton_ <[email protected]>
Co-authored-by: Mehmet Nuri Deveci <[email protected]>
  • Loading branch information
3 people authored Feb 18, 2022
1 parent dd94fae commit 27f3ba2
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 71 deletions.
8 changes: 0 additions & 8 deletions samcli/commands/init/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@

REQUIRED_PARAMS_HINT = "You can also re-run without the --no-interactive flag to be prompted for required values."

INIT_INTERACTIVE_OPTION_GUIDE = """
You can preselect a particular runtime or package type when using the `sam init` experience.
Call `sam init --help` to learn more.
"""


class PackageType:
"""
Expand Down Expand Up @@ -340,9 +335,6 @@ def do_cli(
extra_context,
)
else:
if not (pt_explicit or runtime or dependency_manager or base_image or architecture):
click.secho(INIT_INTERACTIVE_OPTION_GUIDE, fg="yellow", bold=True)

# proceed to interactive state machine, which will call do_generate
do_interactive(
location,
Expand Down
28 changes: 7 additions & 21 deletions samcli/commands/init/init_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import os
from pathlib import Path
from typing import Dict, Optional
import requests

from samcli.cli.global_config import GlobalConfig
from samcli.commands.exceptions import UserException, AppTemplateUpdateException
Expand All @@ -21,7 +20,6 @@
)

LOG = logging.getLogger(__name__)
MANIFEST_URL = "https://raw.githubusercontent.com/aws/aws-sam-cli-app-templates/master/manifest.json"
APP_TEMPLATES_REPO_URL = "https://github.com/aws/aws-sam-cli-app-templates"
APP_TEMPLATES_REPO_NAME = "aws-sam-cli-app-templates"

Expand Down Expand Up @@ -173,7 +171,11 @@ def get_preprocessed_manifest(
[dict]
This is preprocessed manifest with the use_case as key
"""
manifest_body = self._get_manifest()
self.clone_templates_repo()
manifest_path = self.get_manifest_path()
with open(str(manifest_path)) as fp:
body = fp.read()
manifest_body = json.loads(body)

# This would ensure the Use-Case Hello World Example appears
# at the top of list template example displayed to the Customer.
Expand Down Expand Up @@ -202,24 +204,8 @@ def get_preprocessed_manifest(

return preprocessed_manifest

def _get_manifest(self):
"""
In an attempt to reduce initial wait time to achieve an interactive
flow <= 10sec, This method first attempts to spools just the manifest file and
if the manifest can't be spooled, it attempts to clone the cli template git repo or
use local cli template
"""
try:
response = requests.get(MANIFEST_URL, timeout=10)
body = response.text
except (requests.Timeout, requests.ConnectionError):
LOG.debug("Request to get Manifest failed, attempting to clone the repository")
self.clone_templates_repo()
manifest_path = self.get_manifest_path()
with open(str(manifest_path)) as fp:
body = fp.read()
manifest_body = json.loads(body)
return manifest_body
def get_bundle_option(self, package_type, runtime, dependency_manager):
return self._init_options_from_bundle(package_type, runtime, dependency_manager)


def get_template_value(value: str, template: dict) -> Optional[str]:
Expand Down
7 changes: 3 additions & 4 deletions samcli/commands/init/interactive_init_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,11 @@ def _generate_from_use_case(
base_image = (
LAMBDA_IMAGES_RUNTIMES_MAP.get(str(runtime)) if not base_image and package_type == IMAGE else base_image
)
location = templates.location_from_app_template(package_type, runtime, base_image, dependency_manager, app_template)

if not name:
name = click.prompt("\nProject name", type=str, default="sam-app")

location = templates.location_from_app_template(package_type, runtime, base_image, dependency_manager, app_template)

final_architecture = get_architectures(architecture)
extra_context = {
"project_name": name,
Expand Down Expand Up @@ -229,8 +228,8 @@ def _generate_default_hello_world_application(
"""
is_package_type_image = bool(package_type == IMAGE)
if use_case == "Hello World Example" and not (runtime or base_image or is_package_type_image or dependency_manager):
if click.confirm("\n Use the most popular runtime and package type? (Python and zip)"):
runtime, package_type, dependency_manager, pt_explicit = "python3.9", ZIP, "pip", True
if click.confirm("\n Use the most popular runtime and package type? (Nodejs and zip)"):
runtime, package_type, dependency_manager, pt_explicit = "nodejs14.x", ZIP, "npm", True
return (runtime, package_type, dependency_manager, pt_explicit)


Expand Down
2 changes: 1 addition & 1 deletion samcli/lib/utils/git_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def clone(self, clone_dir: Path, clone_name: str, replace_existing: bool = False
try:
temp_path = os.path.normpath(os.path.join(tempdir, clone_name))
git_executable: str = GitRepo._git_executable()
LOG.info("\nCloning from %s (process may take a moment)", self.url)
LOG.info("\nCloning from %s", self.url)
check_output(
[git_executable, "clone", self.url, clone_name],
cwd=tempdir,
Expand Down
55 changes: 18 additions & 37 deletions tests/unit/commands/init/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import json
import os
import shutil
import subprocess
import tempfile
import logging
from unittest.case import expectedFailure
import requests
import pytest
from pathlib import Path
from typing import Dict, Any
from unittest import TestCase
Expand Down Expand Up @@ -61,9 +62,6 @@ def setUp(self):
self.extra_context = '{"project_name": "testing project", "runtime": "python3.6"}'
self.extra_context_as_json = {"project_name": "testing project", "runtime": "python3.6"}

with open("tests/unit/commands/init/test_manifest.json") as f:
self.data = json.load(f)

# setup cache for clone, so that if `git clone` is called multiple times on the same URL,
# only one clone will happen.
clone_cache: Dict[str, Path]
Expand Down Expand Up @@ -1383,14 +1381,10 @@ def test_init_cli_int_from_location(self, generate_project_patch, git_repo_clone
None,
)

@patch("samcli.commands.init.init_templates.InitTemplates._get_manifest")
@patch("samcli.lib.utils.git_repo.GitRepo.clone")
@patch("samcli.commands.init.init_generator.generate_project")
@patch.object(InitTemplates, "__init__", MockInitTemplates.__init__)
def test_init_cli_no_package_type(self, generate_project_patch, git_repo_clone_mock, _get_manifest_mock):

_get_manifest_mock.return_value = self.data

def test_init_cli_no_package_type(self, generate_project_patch, git_repo_clone_mock):
# WHEN the user follows interactive init prompts

# 1: selecting template source
Expand Down Expand Up @@ -1719,18 +1713,16 @@ def test_init_cli_must_pass_with_architecture_and_base_image(self, generate_proj
False # Other tests fail after we pass --packge-type in this test, so let's reset this variable
)

@patch("requests.get")
@patch("samcli.commands.init.init_templates.InitTemplates.get_preprocessed_manifest")
@patch("samcli.commands.init.init_templates.InitTemplates._init_options_from_manifest")
@patch("samcli.commands.init.init_generator.generate_project")
@patch.object(InitTemplates, "__init__", MockInitTemplates.__init__)
def test_init_cli_generate_default_hello_world_app(
self, generate_project_patch, init_options_from_manifest_mock, get_preprocessed_manifest_mock, request_mock
self, generate_project_patch, init_options_from_manifest_mock, get_preprocessed_manifest_mock
):
request_mock.side_effect = requests.Timeout()
init_options_from_manifest_mock.return_value = [
{
"directory": "python3.9/cookiecutter-aws-sam-hello-python",
"directory": "nodejs14.x/cookiecutter-aws-sam-hello-nodejs",
"displayName": "Hello World Example",
"dependencyManager": "npm",
"appTemplate": "hello-world",
Expand All @@ -1750,12 +1742,12 @@ def test_init_cli_generate_default_hello_world_app(

get_preprocessed_manifest_mock.return_value = {
"Hello World Example": {
"python3.9": {
"nodejs14.x": {
"Zip": [
{
"directory": "python3.9/cookiecutter-aws-sam-hello-python3.9",
"directory": "nodejs14.x/cookiecutter-aws-sam-hello-nodejs",
"displayName": "Hello World Example",
"dependencyManager": "pip",
"dependencyManager": "npm",
"appTemplate": "hello-world",
"packageType": "Zip",
"useCaseName": "Hello World Example",
Expand Down Expand Up @@ -1785,6 +1777,7 @@ def test_init_cli_generate_default_hello_world_app(
# test-project: response to name
user_input = """
1
1
y
test-project
"""
Expand All @@ -1795,12 +1788,12 @@ def test_init_cli_generate_default_hello_world_app(
generate_project_patch.assert_called_once_with(
ANY,
ZIP,
"python3.9",
"pip",
"nodejs14.x",
"npm",
".",
"test-project",
True,
{"project_name": "test-project", "runtime": "python3.9", "architectures": {"value": ["x86_64"]}},
{"project_name": "test-project", "runtime": "nodejs14.x", "architectures": {"value": ["x86_64"]}},
)

@patch("samcli.commands.init.init_templates.InitTemplates.get_preprocessed_manifest")
Expand Down Expand Up @@ -1913,11 +1906,9 @@ def test_must_return_runtime_from_base_image_name(self):
runtime = get_runtime(IMAGE, base_image)
self.assertEqual(runtime, expected_runtime[index])

@patch("samcli.commands.init.init_templates.InitTemplates._get_manifest")
@patch.object(InitTemplates, "__init__", MockInitTemplates.__init__)
def test_must_process_manifest(self, _get_manifest_mock):
def test_must_process_manifest(self):
template = InitTemplates()
_get_manifest_mock.return_value = self.data
preprocess_manifest = template.get_preprocessed_manifest()
expected_result = {
"Hello World Example": {
Expand Down Expand Up @@ -1973,12 +1964,10 @@ def test_must_process_manifest(self, _get_manifest_mock):
}
self.assertEqual(preprocess_manifest, expected_result)

@patch("samcli.commands.init.init_templates.InitTemplates._get_manifest")
@patch.object(InitTemplates, "__init__", MockInitTemplates.__init__)
def test_must_process_manifest_with_runtime_as_filter_value(self, _get_manifest_mock):
def test_must_process_manifest_with_runtime_as_filter_value(self):
template = InitTemplates()
filter_value = "go1.x"
_get_manifest_mock.return_value = self.data
preprocess_manifest = template.get_preprocessed_manifest(filter_value)
expected_result = {
"Hello World Example": {
Expand All @@ -1998,12 +1987,10 @@ def test_must_process_manifest_with_runtime_as_filter_value(self, _get_manifest_
}
self.assertEqual(preprocess_manifest, expected_result)

@patch("samcli.commands.init.init_templates.InitTemplates._get_manifest")
@patch.object(InitTemplates, "__init__", MockInitTemplates.__init__)
def test_must_process_manifest_with_image_as_filter_value(self, _get_manifest_mock):
def test_must_process_manifest_with_image_as_filter_value(self):
template = InitTemplates()
filter_value = "amazon/nodejs14.x-base"
_get_manifest_mock.return_value = self.data
preprocess_manifest = template.get_preprocessed_manifest(filter_value)
expected_result = {
"Hello World Example": {
Expand Down Expand Up @@ -2049,11 +2036,9 @@ def test_init_fails_unsupported_dep_mgr_for_runtime(self, git_repo_clone_mock):
)
self.assertEqual(str(ex.exception), expected_error_message)

@patch("samcli.commands.init.init_templates.InitTemplates._get_manifest")
@patch("samcli.lib.utils.git_repo.GitRepo.clone")
@patch.object(InitTemplates, "__init__", MockInitTemplates.__init__)
def test_init_cli_with_mismatch_dep_runtime(self, git_repo_clone_mock, _get_manifest_mock):
_get_manifest_mock = self.data
def test_init_cli_with_mismatch_dep_runtime(self, git_repo_clone_mock):
# WHEN the user follows interactive init prompts

# 1: selecting template source
Expand Down Expand Up @@ -2493,13 +2478,9 @@ def test_must_get_local_manifest_path(self, git_repo):
self.assertIn(file_name_path, manifest_path)

@patch.object(Path, "exists")
@patch("requests.get")
@patch("samcli.commands.init.init_generator.generate_project")
@patch.object(InitTemplates, "__init__", MockInitTemplates.__init__)
def test_init_cli_generate_app_template_from_local_cli_templates(
self, generate_project_patch, request_mock, path_exist_mock
):
request_mock.side_effect = requests.Timeout()
def test_init_cli_generate_app_template_from_local_cli_templates(self, generate_project_patch, path_exist_mock):
path_exist_mock.return_value = False

# WHEN the user follows interactive init prompts
Expand Down

0 comments on commit 27f3ba2

Please sign in to comment.