From 729945df71be24182c3ab1cfabec707b32969c90 Mon Sep 17 00:00:00 2001 From: Kriskras99 Date: Wed, 7 Mar 2018 18:09:44 +0100 Subject: [PATCH] Add reference linking as an option for the post processor. (#3633) * Add reference linking as an option for the post processor. * Fix linting issue * Fix some small formatting issues * Add note to remove .encode when https://goo.gl/BMn8EC is merged --- medusa/helpers/__init__.py | 67 +++++++++++++++++++ .../legacy/views/config_postProcessing.mako | 17 +++-- .../legacy/views/home_postprocess.mako | 16 +++-- .../slim/views/config_postProcessing.mako | 17 +++-- .../slim/views/home_postprocess.mako | 16 +++-- .../dark/templates/config_postProcessing.mako | 17 +++-- themes/dark/templates/home_postprocess.mako | 16 +++-- .../templates/config_postProcessing.mako | 17 +++-- themes/legacy/templates/home_postprocess.mako | 16 +++-- .../templates/config_postProcessing.mako | 17 +++-- themes/light/templates/home_postprocess.mako | 16 +++-- 11 files changed, 192 insertions(+), 40 deletions(-) diff --git a/medusa/helpers/__init__.py b/medusa/helpers/__init__.py index ec4865d76c..7bb23c5e23 100644 --- a/medusa/helpers/__init__.py +++ b/medusa/helpers/__init__.py @@ -67,6 +67,11 @@ log = BraceAdapter(logging.getLogger(__name__)) log.logger.addHandler(logging.NullHandler()) +try: + import reflink +except ImportError: + reflink = None + def indent_xml(elem, level=0): """Do our pretty printing and make Matt very happy.""" @@ -422,6 +427,68 @@ def move_and_symlink_file(src_file, dest_file): copy_file(src_file, dest_file) +def reflink_file(src_file, dest_file): + """Copy a file from source to destination with a reference link. + + :param src_file: Source file + :type src_file: str + :param dest_file: Destination file + :type dest_file: str + """ + try: + if reflink is None: + raise NotImplementedError() + reflink.reflink(src_file.encode('utf-8'), dest_file.encode('utf-8')) # NOTE: remove when https://goo.gl/BMn8EC is merged. + except reflink.ReflinkImpossibleError as msg: + if msg.args[0] == 'EOPNOTSUPP': + log.warning( + u'Failed to create reference link of {source} at {destination}.' + u' Error: Filesystem or OS has not implemented reflink. Copying instead', { + 'source': src_file, + 'destination': dest_file, + } + ) + copy_file(src_file, dest_file) + elif msg.args[0] == 'EXDEV': + log.warning( + u'Failed to create reference link of {source} at {destination}.' + u' Error: Can not reflink between two devices. Copying instead', { + 'source': src_file, + 'destination': dest_file, + } + ) + copy_file(src_file, dest_file) + else: + log.warning( + u'Failed to create reflink of {source} at {destination}.' + u' Error: {error!r}. Copying instead', { + 'source': src_file, + 'destination': dest_file, + 'error': msg, + } + ) + copy_file(src_file, dest_file) + except NotImplementedError: + log.warning( + u'Failed to create reference link of {source} at {destination}.' + u' Error: Filesystem does not support reflink or reflink is not installed. Copying instead', { + 'source': src_file, + 'destination': dest_file, + } + ) + copy_file(src_file, dest_file) + except IOError as msg: + log.warning( + u'Failed to create reflink of {source} at {destination}.' + u' Error: {error!r}. Copying instead', { + 'source': src_file, + 'destination': dest_file, + 'error': msg, + } + ) + copy_file(src_file, dest_file) + + def make_dirs(path): """Create any folders that are missing and assigns them the permissions of their parents. diff --git a/themes-default/legacy/views/config_postProcessing.mako b/themes-default/legacy/views/config_postProcessing.mako index 5606e8f965..68ca184c12 100644 --- a/themes-default/legacy/views/config_postProcessing.mako +++ b/themes-default/legacy/views/config_postProcessing.mako @@ -2,6 +2,7 @@ <%! import os.path import datetime + import pkgutil from medusa import app from medusa.common import SKIPPED, WANTED, UNAIRED, ARCHIVED, IGNORED, SNATCHED, SNATCHED_PROPER, SNATCHED_BEST, FAILED from medusa.common import Quality, qualityPresets, statusStrings, qualityPresetStrings, cpu_presets, MULTI_EP_STRINGS @@ -62,10 +63,17 @@ Processing Method: @@ -76,6 +84,7 @@
diff --git a/themes-default/legacy/views/home_postprocess.mako b/themes-default/legacy/views/home_postprocess.mako index 94348148c1..6b256242b7 100644 --- a/themes-default/legacy/views/home_postprocess.mako +++ b/themes-default/legacy/views/home_postprocess.mako @@ -1,5 +1,6 @@ <%inherit file="/layouts/main.mako"/> <%! + import pkgutil from medusa import app %> <%block name="content"> @@ -32,10 +33,17 @@ diff --git a/themes-default/slim/views/config_postProcessing.mako b/themes-default/slim/views/config_postProcessing.mako index 5606e8f965..68ca184c12 100644 --- a/themes-default/slim/views/config_postProcessing.mako +++ b/themes-default/slim/views/config_postProcessing.mako @@ -2,6 +2,7 @@ <%! import os.path import datetime + import pkgutil from medusa import app from medusa.common import SKIPPED, WANTED, UNAIRED, ARCHIVED, IGNORED, SNATCHED, SNATCHED_PROPER, SNATCHED_BEST, FAILED from medusa.common import Quality, qualityPresets, statusStrings, qualityPresetStrings, cpu_presets, MULTI_EP_STRINGS @@ -62,10 +63,17 @@ Processing Method: @@ -76,6 +84,7 @@
diff --git a/themes-default/slim/views/home_postprocess.mako b/themes-default/slim/views/home_postprocess.mako index 94348148c1..6b256242b7 100644 --- a/themes-default/slim/views/home_postprocess.mako +++ b/themes-default/slim/views/home_postprocess.mako @@ -1,5 +1,6 @@ <%inherit file="/layouts/main.mako"/> <%! + import pkgutil from medusa import app %> <%block name="content"> @@ -32,10 +33,17 @@ diff --git a/themes/dark/templates/config_postProcessing.mako b/themes/dark/templates/config_postProcessing.mako index 5606e8f965..68ca184c12 100644 --- a/themes/dark/templates/config_postProcessing.mako +++ b/themes/dark/templates/config_postProcessing.mako @@ -2,6 +2,7 @@ <%! import os.path import datetime + import pkgutil from medusa import app from medusa.common import SKIPPED, WANTED, UNAIRED, ARCHIVED, IGNORED, SNATCHED, SNATCHED_PROPER, SNATCHED_BEST, FAILED from medusa.common import Quality, qualityPresets, statusStrings, qualityPresetStrings, cpu_presets, MULTI_EP_STRINGS @@ -62,10 +63,17 @@ Processing Method: @@ -76,6 +84,7 @@
diff --git a/themes/dark/templates/home_postprocess.mako b/themes/dark/templates/home_postprocess.mako index 94348148c1..6b256242b7 100644 --- a/themes/dark/templates/home_postprocess.mako +++ b/themes/dark/templates/home_postprocess.mako @@ -1,5 +1,6 @@ <%inherit file="/layouts/main.mako"/> <%! + import pkgutil from medusa import app %> <%block name="content"> @@ -32,10 +33,17 @@ diff --git a/themes/legacy/templates/config_postProcessing.mako b/themes/legacy/templates/config_postProcessing.mako index 5606e8f965..68ca184c12 100644 --- a/themes/legacy/templates/config_postProcessing.mako +++ b/themes/legacy/templates/config_postProcessing.mako @@ -2,6 +2,7 @@ <%! import os.path import datetime + import pkgutil from medusa import app from medusa.common import SKIPPED, WANTED, UNAIRED, ARCHIVED, IGNORED, SNATCHED, SNATCHED_PROPER, SNATCHED_BEST, FAILED from medusa.common import Quality, qualityPresets, statusStrings, qualityPresetStrings, cpu_presets, MULTI_EP_STRINGS @@ -62,10 +63,17 @@ Processing Method: @@ -76,6 +84,7 @@
diff --git a/themes/legacy/templates/home_postprocess.mako b/themes/legacy/templates/home_postprocess.mako index 94348148c1..6b256242b7 100644 --- a/themes/legacy/templates/home_postprocess.mako +++ b/themes/legacy/templates/home_postprocess.mako @@ -1,5 +1,6 @@ <%inherit file="/layouts/main.mako"/> <%! + import pkgutil from medusa import app %> <%block name="content"> @@ -32,10 +33,17 @@ diff --git a/themes/light/templates/config_postProcessing.mako b/themes/light/templates/config_postProcessing.mako index 5606e8f965..68ca184c12 100644 --- a/themes/light/templates/config_postProcessing.mako +++ b/themes/light/templates/config_postProcessing.mako @@ -2,6 +2,7 @@ <%! import os.path import datetime + import pkgutil from medusa import app from medusa.common import SKIPPED, WANTED, UNAIRED, ARCHIVED, IGNORED, SNATCHED, SNATCHED_PROPER, SNATCHED_BEST, FAILED from medusa.common import Quality, qualityPresets, statusStrings, qualityPresetStrings, cpu_presets, MULTI_EP_STRINGS @@ -62,10 +63,17 @@ Processing Method: @@ -76,6 +84,7 @@
diff --git a/themes/light/templates/home_postprocess.mako b/themes/light/templates/home_postprocess.mako index 94348148c1..e88ba53e0c 100644 --- a/themes/light/templates/home_postprocess.mako +++ b/themes/light/templates/home_postprocess.mako @@ -1,5 +1,6 @@ <%inherit file="/layouts/main.mako"/> <%! + import pkgutl from medusa import app %> <%block name="content"> @@ -32,10 +33,17 @@