Skip to content

Commit

Permalink
fix: change permissions and remove files on rmtree (#1694)
Browse files Browse the repository at this point in the history
* fix: change permissions and remove files on rmtree

* fix: add `onerror` rmtreecallback docstring
  • Loading branch information
sriram-mv committed Jan 3, 2020
1 parent a35819d commit 741d22b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
3 changes: 2 additions & 1 deletion samcli/commands/init/init_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from samcli.cli.main import global_cfg
from samcli.commands.exceptions import UserException, AppTemplateUpdateException
from samcli.lib.utils import osutils
from samcli.lib.utils.osutils import rmtree_callback
from samcli.local.common.runtime_template import RUNTIME_DEP_TEMPLATE_MAPPING

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -164,7 +165,7 @@ def _overwrite_existing_templates(self, shared_dir, expected_path):
def _replace_app_templates(self, temp_path, dest_path):
try:
LOG.debug("Removing old templates from %s", str(dest_path))
shutil.rmtree(dest_path)
shutil.rmtree(dest_path, onerror=rmtree_callback)
LOG.debug("Copying templates from %s to %s", str(temp_path), str(dest_path))
shutil.copytree(temp_path, dest_path, ignore=shutil.ignore_patterns("*.git"))
except (OSError, shutil.Error):
Expand Down
29 changes: 19 additions & 10 deletions samcli/lib/utils/osutils.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
"""
Common OS utilities
"""

import sys
import logging
import os
import shutil
import stat
import sys
import tempfile
import logging
import contextlib

from contextlib import contextmanager


LOG = logging.getLogger(__name__)


Expand Down Expand Up @@ -47,13 +44,25 @@ def mkdir_temp(mode=0o755, ignore_errors=False):
finally:
if temp_dir:
if ignore_errors:
shutil.rmtree(temp_dir, False, _rmtree_callback)
shutil.rmtree(temp_dir, False, rmtree_callback)
else:
shutil.rmtree(temp_dir)


def _rmtree_callback(function, path, excinfo):
LOG.debug("rmtree failed in %s for %s, details: %s", function, path, excinfo)
def rmtree_callback(function, path, excinfo):
"""
Callback function for shutil.rmtree to change permissions on the file path, so that
it's delete-able incase the file path is read-only.
:param function: platform and implementation dependent function.
:param path: argument to the function that caused it to fail.
:param excinfo: tuple returned by sys.exc_info()
:return:
"""
try:
os.chmod(path=path, mode=stat.S_IWRITE)
os.remove(path)
except OSError:
LOG.debug("rmtree failed in %s for %s, details: %s", function, path, excinfo)


def stdout():
Expand Down Expand Up @@ -88,7 +97,7 @@ def remove(path):
pass


@contextlib.contextmanager
@contextmanager
def tempfile_platform_independent():
# NOTE(TheSriram): Setting delete=False is specific to windows.
# https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile
Expand Down

0 comments on commit 741d22b

Please sign in to comment.