# Copyright 2014 ARM Limited
#
# Licensed under the Apache License, Version 2.0
# See LICENSE file for details.

# standard library modules, , ,
import os
import errno
import shutil
import platform

def mkDirP(path):
    try:
        os.makedirs(path)
    except OSError as exception:
        if exception.errno != errno.EEXIST:
            raise

def rmF(path):
    try:
        os.remove(path)
    except OSError as exception: 
        if exception.errno != errno.ENOENT:
            raise

def rmRf(path):
    try:
        shutil.rmtree(path)
    except OSError as exception:
        if 'cannot call rmtree on a symbolic link' in str(exception).lower():
            os.unlink(path)
        elif exception.errno == errno.ENOTDIR:
            rmF(path)
        elif exception.errno != errno.ENOENT:
            raise

def fullySplitPath(path):
    components = []
    while True:
        path, component = os.path.split(path)
        if component != '':
            components.append(component)
        else:
            if path != '':
                components.append(path)
            break
    components.reverse()
    return components

# The link-related functions are platform-dependent
links = __import__("fsutils_win" if os.name == 'nt' else "fsutils_posix", globals(), locals(), ['*'])
isLink = links.isLink
tryReadLink = links.tryReadLink
_symlink = links._symlink
realpath = links.realpath

# !!! FIXME: the logic in the "except" block below probably doesn't work in Windows
def symlink(source, link_name):
    try:
        # os.symlink doesn't update existing links, so need to rm first
        rmF(link_name)
        _symlink(source, link_name)
    except OSError as exception:
        if exception.errno != errno.EEXIST and (tryReadLink(link_name) != source):
            raise

@@ -190,16 +190,28 @@
 platform
+%0Aimport stat
 %0A%0Adef mk
@@ -506,32 +506,304 @@
 def rmRf(path):%0A
+    # we may have to make files writable before we can successfully delete%0A    # them, to do this%0A    def fixPermissions(fn, path, excinfo):%0A        if os.access(path, os.W_OK):%0A            raise%0A        else:%0A            os.chmod(path, stat.S_IWUSR)%0A            fn(path)%0A
     try:%0A       
@@ -817,24 +817,48 @@
 .rmtree(path
+, onerror=fixPermissions
 )%0A    except

set write permission if files cannot be removed