forked from aws/aws-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: managed stack - always catch ClientError and BotoCoreError - on windows, create temporary files with delete=False, otherwise it results in a PermissionDeniedError * fix: dont mask inbuilt `file`
- Loading branch information
Showing
6 changed files
with
106 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
""" | ||
Exceptions that are raised by sam bootstrap | ||
""" | ||
from samcli.commands.exceptions import UserException | ||
|
||
|
||
class ManagedStackError(UserException): | ||
def __init__(self, ex): | ||
self.ex = ex | ||
message_fmt = f"\nFailed to create managed resources: {ex}" | ||
super(ManagedStackError, self).__init__(message=message_fmt.format(ex=self.ex)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
""" | ||
Helper functions for temporary files | ||
""" | ||
import os | ||
import contextlib | ||
import tempfile | ||
|
||
|
||
def remove(path): | ||
if path: | ||
try: | ||
os.remove(path) | ||
except OSError: | ||
pass | ||
|
||
|
||
@contextlib.contextmanager | ||
def tempfile_platform_independent(): | ||
# NOTE(TheSriram): Setting delete=False is specific to windows. | ||
# https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile | ||
_tempfile = tempfile.NamedTemporaryFile(delete=False) | ||
try: | ||
yield _tempfile | ||
finally: | ||
_tempfile.close() | ||
remove(_tempfile.name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import os | ||
import tempfile | ||
from unittest import TestCase | ||
|
||
from samcli.lib.utils.temp_file_utils import remove, tempfile_platform_independent | ||
|
||
|
||
class TestFile(TestCase): | ||
def test_file_remove(self): | ||
_file = tempfile.NamedTemporaryFile(delete=False) | ||
remove(_file.name) | ||
self.assertFalse(os.path.exists(_file.name)) | ||
# No Exception thrown | ||
remove(os.path.join(os.getcwd(), "random")) | ||
|
||
def test_temp_file(self): | ||
_path = None | ||
with tempfile_platform_independent() as _tempf: | ||
_path = _tempf.name | ||
self.assertFalse(os.path.exists(_path)) |