Skip to content

Commit

Permalink
Updated rmtree to handle readonly files
Browse files Browse the repository at this point in the history
  • Loading branch information
YuukanOO committed Dec 12, 2018
1 parent 23da14a commit 748bd5c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
3 changes: 1 addition & 2 deletions pytlas/interpreters/snips.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import os
from shutil import rmtree
from pytlas.interpreters.interpreter import Interpreter, compute_checksum
from pytlas.interpreters.intent import Intent
from pytlas.interpreters.slot import SlotValue
from pytlas.utils import read_file
from pytlas.utils import read_file, rmtree
from snips_nlu import load_resources, SnipsNLUEngine, __version__
from snips_nlu.constants import ENTITIES, AUTOMATICALLY_EXTENSIBLE, RESOLVED_VALUE, \
ENTITY_KIND, ENTITY, RES_VALUE, RES_RAW_VALUE, RES_INTENT, RES_INTENT_NAME, RES_SLOTS, \
Expand Down
9 changes: 2 additions & 7 deletions pytlas/pam.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from pytlas.skill import handlers, module_metas
from pytlas.localization import module_translations
from pytlas.utils import get_package_name_from_module
from pytlas.utils import get_package_name_from_module, rmtree
from pytlas.skill_data import SkillData
from shutil import rmtree
import re, logging, os, subprocess

DEFAULT_REPO_URL = os.environ.get('PYTLAS_DEFAULT_REPO_URL', 'https://github.com/')
Expand Down Expand Up @@ -148,9 +147,7 @@ def install_skills(directory, stdout=None, *names):
if os.path.isdir(dest):
if stdout:
stdout(' Skill folder already exists, updating')

logging.warning('Skill already exists, will update it')


installed_skills.extend(update_skills(directory, stdout, name))

continue
Expand Down Expand Up @@ -269,8 +266,6 @@ def uninstall_skills(directory, stdout, *names):
if stdout:
stdout(' Uninstalled 鉁旓笍')

logging.info('Uninstalled "%s"' % name)

removed_skills.append(name)
except Exception as e:
if stdout:
Expand Down
33 changes: 32 additions & 1 deletion pytlas/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys, inspect, os, random
from shutil import rmtree as shrmtree
from fuzzywuzzy import process
from markdown import markdown
from bs4 import BeautifulSoup
Expand Down Expand Up @@ -152,4 +153,34 @@ def find_match(choices, value):

match = process.extractOne(value, choices, score_cutoff=60)

return match[0] if match else None
return match[0] if match else None

def _onerror(func, path, exc_info):
"""Error handler for ``shutil.rmtree``.
If the error is due to an access error (read only file)
it attempts to add write permission and then retries.
If the error is for another reason it re-raises the error.
"""
import stat

if not os.access(path, os.W_OK):
# Is the error an access error ?
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise

def rmtree(path, ignore_errors=False):
"""Recursively deletes a folder and its children and handle readonly files as per
https://stackoverflow.com/a/2656405/7641999.
Args:
path (str): Path to delete
ignore_errors (bool): Should we ignore errors
"""

shrmtree(path, ignore_errors=ignore_errors, onerror=_onerror)

0 comments on commit 748bd5c

Please sign in to comment.