From 6cc3df73b32f62cdfd0bf0e83a4eaab09797948e Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Sun, 6 Jan 2019 10:32:52 -0500 Subject: [PATCH] Refactor path functions from utils to utils.paths --- core/utils/__init__.py | 50 +++++------------------------------------- core/utils/paths.py | 47 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 44 deletions(-) diff --git a/core/utils/__init__.py b/core/utils/__init__.py index 2ef8d8e0..bea35b3a 100644 --- a/core/utils/__init__.py +++ b/core/utils/__init__.py @@ -41,7 +41,11 @@ from core.utils.parsers import ( parse_vuze, ) from core.utils.paths import ( - get_dir_size, make_dir, + flatten_dir, + get_dir_size, + make_dir, + onerror, + remove_dir, remote_dir, remove_empty_folders, remove_read_only, @@ -156,22 +160,7 @@ def category_search(input_directory, input_name, input_category, root, categorie def flatten(output_destination): - logger.info('FLATTEN: Flattening directory: {0}'.format(output_destination)) - for outputFile in list_media_files(output_destination): - dir_path = os.path.dirname(outputFile) - file_name = os.path.basename(outputFile) - - if dir_path == output_destination: - continue - - target = os.path.join(output_destination, file_name) - - try: - shutil.move(outputFile, target) - except Exception: - logger.error('Could not flatten {0}'.format(outputFile), 'FLATTEN') - - remove_empty_folders(output_destination) # Cleanup empty directories + return flatten_dir(output_destination, list_media_files(output_destination)) def get_dirs(section, subsection, link='hard'): @@ -280,33 +269,6 @@ def get_dirs(section, subsection, link='hard'): return list(set(to_return)) -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. - - Usage : ``shutil.rmtree(path, onerror=onerror)`` - """ - if not os.access(path, os.W_OK): - # Is the error an access error ? - os.chmod(path, stat.S_IWUSR) - func(path) - else: - raise Exception - - -def remove_dir(dir_name): - logger.info('Deleting {0}'.format(dir_name)) - try: - shutil.rmtree(text_type(dir_name), onerror=onerror) - except Exception: - logger.error('Unable to delete folder {0}'.format(dir_name)) - - def clean_dir(path, section, subsection): cfg = dict(core.CFG[section][subsection]) if not os.path.exists(path): diff --git a/core/utils/paths.py b/core/utils/paths.py index 31308650..948287b7 100644 --- a/core/utils/paths.py +++ b/core/utils/paths.py @@ -2,6 +2,7 @@ from functools import partial import os import re +import shutil import stat from six import text_type @@ -10,6 +11,33 @@ import core from core import logger +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. + + Usage : ``shutil.rmtree(path, onerror=onerror)`` + """ + if not os.access(path, os.W_OK): + # Is the error an access error ? + os.chmod(path, stat.S_IWUSR) + func(path) + else: + raise Exception + + +def remove_dir(dir_name): + logger.info('Deleting {0}'.format(dir_name)) + try: + shutil.rmtree(text_type(dir_name), onerror=onerror) + except Exception: + logger.error('Unable to delete folder {0}'.format(dir_name)) + + def make_dir(path): if not os.path.isdir(path): try: @@ -78,3 +106,22 @@ def remove_read_only(filename): os.chmod(filename, stat.S_IWRITE) except Exception: logger.warning('Cannot change permissions of {file}'.format(file=filename), logger.WARNING) + + +def flatten_dir(destination, files): + logger.info('FLATTEN: Flattening directory: {0}'.format(destination)) + for outputFile in files: + dir_path = os.path.dirname(outputFile) + file_name = os.path.basename(outputFile) + + if dir_path == destination: + continue + + target = os.path.join(destination, file_name) + + try: + shutil.move(outputFile, target) + except Exception: + logger.error('Could not flatten {0}'.format(outputFile), 'FLATTEN') + + remove_empty_folders(destination) # Cleanup empty directories