|
4 | 4 |
|
5 | 5 | import logging
|
6 | 6 | import pathlib
|
| 7 | +import re |
7 | 8 | import tempfile
|
8 | 9 | from typing import Optional, Tuple
|
9 | 10 |
|
10 | 11 | import click
|
11 | 12 | from botocore.exceptions import ClientError, WaiterError
|
12 | 13 |
|
13 | 14 | from samcli.commands._utils.options import generate_next_command_recommendation
|
14 |
| -from samcli.commands.exceptions import InvalidInitOptionException, SchemasApiException |
| 15 | +from samcli.commands.exceptions import InvalidInitOptionException, PopularRuntimeNotFoundException, SchemasApiException |
15 | 16 | from samcli.commands.init.init_flow_helpers import (
|
16 | 17 | _get_image_from_runtime,
|
17 | 18 | _get_runtime_from_image,
|
|
28 | 29 | )
|
29 | 30 | from samcli.lib.config.samconfig import DEFAULT_CONFIG_FILE_NAME
|
30 | 31 | from samcli.lib.schemas.schemas_code_manager import do_download_source_code_binding, do_extract_and_merge_schemas_code
|
| 32 | +from samcli.lib.utils.architecture import SUPPORTED_RUNTIMES |
31 | 33 | from samcli.lib.utils.osutils import remove
|
32 | 34 | from samcli.lib.utils.packagetype import IMAGE, ZIP
|
33 | 35 | from samcli.local.common.runtime_template import (
|
@@ -323,6 +325,60 @@ def _generate_from_use_case(
|
323 | 325 | )
|
324 | 326 |
|
325 | 327 |
|
| 328 | +def _get_latest_python_runtime() -> str: |
| 329 | + """ |
| 330 | + Returns the latest support version of Python |
| 331 | + SAM CLI supports |
| 332 | +
|
| 333 | + Returns |
| 334 | + ------- |
| 335 | + str: |
| 336 | + The name of the latest Python runtime (ex. "python3.12") |
| 337 | + """ |
| 338 | + latest_major = 0 |
| 339 | + latest_minor = 0 |
| 340 | + |
| 341 | + compiled_regex = re.compile(r"python(.*?)\.(.*)") |
| 342 | + |
| 343 | + for runtime in SUPPORTED_RUNTIMES: |
| 344 | + if not runtime.startswith("python"): |
| 345 | + continue |
| 346 | + |
| 347 | + # python3.12 => 3.12 => (3, 12) |
| 348 | + version_match = re.match(compiled_regex, runtime) |
| 349 | + |
| 350 | + if not version_match: |
| 351 | + LOG.debug(f"Failed to match version while checking {runtime}") |
| 352 | + continue |
| 353 | + |
| 354 | + matched_groups = version_match.groups() |
| 355 | + |
| 356 | + try: |
| 357 | + version_major = int(matched_groups[0]) |
| 358 | + version_minor = int(matched_groups[1]) |
| 359 | + except (ValueError, IndexError): |
| 360 | + LOG.debug(f"Failed to parse version while checking {runtime}") |
| 361 | + continue |
| 362 | + |
| 363 | + if version_major > latest_major: |
| 364 | + latest_major = version_major |
| 365 | + latest_minor = version_minor |
| 366 | + elif version_major == latest_major: |
| 367 | + latest_minor = version_minor if version_minor > latest_minor else latest_minor |
| 368 | + |
| 369 | + if not latest_major: |
| 370 | + # major version is still 0, assume that something went wrong |
| 371 | + # this in theory should not happen as long as Python is |
| 372 | + # listed in the SUPPORTED_RUNTIMES constant |
| 373 | + raise PopularRuntimeNotFoundException("Was unable to search for the latest supported runtime") |
| 374 | + |
| 375 | + selected_version = f"python{latest_major}.{latest_minor}" |
| 376 | + |
| 377 | + LOG.debug(f"Using {selected_version} as the latest runtime version") |
| 378 | + |
| 379 | + return selected_version |
| 380 | + |
| 381 | + |
326 | 382 | def _generate_default_hello_world_application(
|
327 | 383 | use_case: str,
|
328 | 384 | package_type: Optional[str],
|
@@ -356,8 +412,10 @@ def _generate_default_hello_world_application(
|
356 | 412 | """
|
357 | 413 | is_package_type_image = bool(package_type == IMAGE)
|
358 | 414 | if use_case == "Hello World Example" and not (runtime or base_image or is_package_type_image or dependency_manager):
|
359 |
| - if click.confirm("\nUse the most popular runtime and package type? (Python and zip)"): |
360 |
| - runtime, package_type, dependency_manager, pt_explicit = "python3.9", ZIP, "pip", True |
| 415 | + latest_python = _get_latest_python_runtime() |
| 416 | + |
| 417 | + if click.confirm(f"\nUse the most popular runtime and package type? ({latest_python} and zip)"): |
| 418 | + runtime, package_type, dependency_manager, pt_explicit = _get_latest_python_runtime(), ZIP, "pip", True |
361 | 419 | return (runtime, package_type, dependency_manager, pt_explicit)
|
362 | 420 |
|
363 | 421 |
|
|
0 commit comments