Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions samcli/commands/local/cli_common/invoke_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import os
from enum import Enum
from pathlib import Path
from typing import IO, Any, Dict, List, Optional, Tuple, Type, cast
from typing import Any, Dict, List, Optional, TextIO, Tuple, Type, cast

from samcli.commands._utils.template import TemplateFailedParsingException, TemplateNotFoundException
from samcli.commands.exceptions import ContainersInitializationException
Expand Down Expand Up @@ -195,7 +195,7 @@ def __init__(
self._stacks: List[Stack] = None # type: ignore
self._env_vars_value: Optional[Dict] = None
self._container_env_vars_value: Optional[Dict] = None
self._log_file_handle: Optional[IO] = None
self._log_file_handle: Optional[TextIO] = None
self._debug_context: Optional[DebugContext] = None
self._layers_downloader: Optional[LayerDownloader] = None
self._container_manager: Optional[ContainerManager] = None
Expand Down Expand Up @@ -487,7 +487,7 @@ def _get_env_vars_value(filename: Optional[str]) -> Optional[Dict]:
) from ex

@staticmethod
def _setup_log_file(log_file: Optional[str]) -> Optional[IO]:
def _setup_log_file(log_file: Optional[str]) -> Optional[TextIO]:
"""
Open a log file if necessary and return the file handle. This will create a file if it does not exist

Expand All @@ -497,7 +497,7 @@ def _setup_log_file(log_file: Optional[str]) -> Optional[IO]:
if not log_file:
return None

return open(log_file, "wb")
return open(log_file, "w")

@staticmethod
def _get_debug_context(
Expand Down
4 changes: 2 additions & 2 deletions samcli/lib/package/ecr_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Client for uploading packaged artifacts to ecr
"""
import base64
import io
import logging
from io import StringIO
from typing import Dict

import botocore
Expand Down Expand Up @@ -94,7 +94,7 @@ def upload(self, image, resource_name):
else:
# we need to wait till the image got pushed to ecr, without this workaround sam sync for template
# contains image always fail, because the provided ecr uri is not exist.
_log_streamer = LogStreamer(stream=StreamWriter(stream=io.BytesIO(), auto_flush=True))
_log_streamer = LogStreamer(stream=StreamWriter(stream=StringIO(), auto_flush=True))
_log_streamer.stream_progress(push_logs)

except (BuildError, APIError, LogStreamError) as ex:
Expand Down
12 changes: 6 additions & 6 deletions samcli/lib/utils/stream_writer.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""
This class acts like a wrapper around output streams to provide any flexibility with output we need
"""
from typing import Union
from typing import TextIO, Union


class StreamWriter:
def __init__(self, stream, auto_flush: bool = False):
def __init__(self, stream: TextIO, auto_flush: bool = False):
"""
Instatiates new StreamWriter to the specified stream

Expand All @@ -20,7 +20,7 @@ def __init__(self, stream, auto_flush: bool = False):
self._auto_flush = auto_flush

@property
def stream(self):
def stream(self) -> TextIO:
return self._stream

def write_bytes(self, output: Union[bytes, bytearray]):
Expand All @@ -30,7 +30,7 @@ def write_bytes(self, output: Union[bytes, bytearray]):
Parameters
----------
output bytes-like object
Bytes to write
Bytes to write into buffer
"""
self._stream.buffer.write(output)

Expand All @@ -43,8 +43,8 @@ def write_str(self, output: str):

Parameters
----------
output bytes-like object
Bytes to write
output string object
String to write
"""
self._stream.write(output)

Expand Down
4 changes: 2 additions & 2 deletions samcli/local/apigw/local_apigw_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json
import logging
from datetime import datetime
from io import BytesIO
from io import StringIO
from time import time
from typing import Any, Dict, List, Optional

Expand Down Expand Up @@ -605,7 +605,7 @@ def _invoke_lambda_function(self, lambda_function_name: str, event: dict) -> str
str
A string containing the output from the Lambda function
"""
with BytesIO() as stdout:
with StringIO() as stdout:
event_str = json.dumps(event, sort_keys=True)
stdout_writer = StreamWriter(stdout, auto_flush=True)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def _invoke_request_handler(self, function_name):

request_data = request_data.decode("utf-8")

stdout_stream = io.BytesIO()
stdout_stream = io.StringIO()
stdout_stream_writer = StreamWriter(stdout_stream, auto_flush=True)

try:
Expand Down
4 changes: 2 additions & 2 deletions samcli/local/services/base_local_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def service_response(body, headers, status_code):

class LambdaOutputParser:
@staticmethod
def get_lambda_output(stdout_stream: io.BytesIO) -> Tuple[str, bool]:
def get_lambda_output(stdout_stream: io.StringIO) -> Tuple[str, bool]:
"""
This method will extract read the given stream and return the response from Lambda function separated out
from any log statements it might have outputted. Logs end up in the stdout stream if the Lambda function
Expand All @@ -100,7 +100,7 @@ def get_lambda_output(stdout_stream: io.BytesIO) -> Tuple[str, bool]:
bool
If the response is an error/exception from the container
"""
lambda_response = stdout_stream.getvalue().decode("utf-8")
lambda_response = stdout_stream.getvalue()

# When the Lambda Function returns an Error/Exception, the output is added to the stdout of the container. From
# our perspective, the container returned some value, which is not always true. Since the output is the only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ def test_must_open_file_for_writing(self):
with patch("samcli.commands.local.cli_common.invoke_context.open", m):
InvokeContext._setup_log_file(filename)

m.assert_called_with(filename, "wb")
m.assert_called_with(filename, "w")


class TestInvokeContext_get_debug_context(TestCase):
Expand Down
18 changes: 9 additions & 9 deletions tests/unit/local/services/test_base_local_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ def test_create_returns_not_implemented(self):
class TestLambdaOutputParser(TestCase):
@parameterized.expand(
[
param("with mixed data and json response", b'data\n{"a": "b"}', 'data\n{"a": "b"}'),
param("with response as string", b"response", "response"),
param("with json response only", b'{"a": "b"}', '{"a": "b"}'),
param("with one new line and json", b'\n{"a": "b"}', '\n{"a": "b"}'),
param("with response only as string", b"this is the response line", "this is the response line"),
param("with whitespaces", b'data\n{"a": "b"} \n\n\n', 'data\n{"a": "b"} \n\n\n'),
param("with empty data", b"", ""),
param("with just new lines", b"\n\n", "\n\n"),
param("with mixed data and json response", 'data\n{"a": "b"}', 'data\n{"a": "b"}'),
param("with response as string", "response", "response"),
param("with json response only", '{"a": "b"}', '{"a": "b"}'),
param("with one new line and json", '\n{"a": "b"}', '\n{"a": "b"}'),
param("with response only as string", "this is the response line", "this is the response line"),
param("with whitespaces", 'data\n{"a": "b"} \n\n\n', 'data\n{"a": "b"} \n\n\n'),
param("with empty data", "", ""),
param("with just new lines", "\n\n", "\n\n"),
param(
"with whitespaces",
b"\n \n \n",
"\n \n \n",
"\n \n \n",
),
]
Expand Down