Exemplo n.º 1
0
def clean_dir(path, timer=30, recreate=True, suppress_warning=False):
    """
    Removes all the files from the given directory.
    IMPORTANT: Use this method with caution.

    Parameters:
    1. Path (str): The directory that needs to be cleared of files.

    Returns:
    1. returnValue (bool): True
    2. returnError (str): Error if the cleaning of the directory failed
    """

    path = normalize(path)
    if not path:
        return False, 'Invalid path given'

    # Notify user if directory contains files
    if validate_dir(path):
        if not len(os.listdir(path)):
            return True, ''

        if not (timer == 0 or suppress_warning):
            if not logger.warningTimerContinue(
                    'Cleaning directory with existing files: "{0}"'.format(
                        path),
                    timer=timer):
                return False, 'User canceled cleanup'

    # Try three times to clean the directory
    returnValue = False
    returnError = ''
    for i in range(0, 3):
        if i:
            time.sleep(i)

        # Try deleting the directory
        try:
            shutil.rmtree(path, ignore_errors=False, onerror=_handler_readonly)
        except OSError as (err, strerror):
            pass
        except Exception:
            logger.debug(traceback.format_exc())
            returnError += '{0}\n'.format(sys.exc_info()[1])
Exemplo n.º 2
0
def clean_dir(path, timer=30):
    """
    Removes all the files from the given directory.
    IMPORTANT: Use this method with caution.

    Parameters:
    1. Path (str): The directory that needs to be cleared of files.

    Returns:
    1. returnValue (bool): True
    2. returnError (str): Error if the cleaning of the directory failed
    """

    path = normalize(path)
    if not path:
        return False, 'Invalid path given'

    # Notify user if directory contains files
    if validate_dir(path):
        if not len(os.listdir(path)):
            return True, ''

        if not logger.warningTimerContinue('Cleaning directory with existing files: "{0}"'.format(path), timer=timer):
            return False, 'User canceled cleanup'

    # Try three times to clean the directory
    returnValue = False
    returnError = ''
    for i in range (0, 3):
        if i:
            time.sleep(i)

        # Try deleting the directory
        try:
            shutil.rmtree(path, ignore_errors=False, onerror=_handler_readonly)
        except OSError as (err, strerror):
            pass
        except Exception:
            logger.debug(traceback.format_exc())
            returnError += '{0}\n'.format(sys.exc_info()[1])