From 4f0dbb5016e749927c97568d991167b78ebd0345 Mon Sep 17 00:00:00 2001 From: Christiaan Biesterbosch Date: Fri, 19 Jan 2018 23:52:07 +0100 Subject: [PATCH 1/4] Add reference linking as an option for the post processor. --- medusa/helpers/__init__.py | 66 ++++++++++++++++++++++++++++++++ views/config_postProcessing.mako | 17 ++++++-- views/home_postprocess.mako | 16 ++++++-- 3 files changed, 91 insertions(+), 8 deletions(-) diff --git a/medusa/helpers/__init__.py b/medusa/helpers/__init__.py index 0774c611cf..a54c3930a3 100644 --- a/medusa/helpers/__init__.py +++ b/medusa/helpers/__init__.py @@ -59,6 +59,10 @@ log = BraceAdapter(logging.getLogger(__name__)) log.logger.addHandler(logging.NullHandler()) +try: + import reflink +except ImportError: + reflink = None try: from urllib.parse import splittype @@ -420,6 +424,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')) + 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/views/config_postProcessing.mako b/views/config_postProcessing.mako index d26e534cc1..2a4e35f912 100644 --- a/views/config_postProcessing.mako +++ b/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 @@ -63,10 +64,17 @@ Processing Method: @@ -77,6 +85,7 @@
diff --git a/views/home_postprocess.mako b/views/home_postprocess.mako index 94348148c1..7d7b75bd2d 100644 --- a/views/home_postprocess.mako +++ b/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 @@ From 12a93ebc367a08ed1a09fd73c64363ae6904fd95 Mon Sep 17 00:00:00 2001 From: Christiaan Biesterbosch Date: Wed, 7 Mar 2018 13:51:42 +0100 Subject: [PATCH 2/4] Fix linting issue --- medusa/helpers/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/medusa/helpers/__init__.py b/medusa/helpers/__init__.py index 72be67ba45..37b5732f2c 100644 --- a/medusa/helpers/__init__.py +++ b/medusa/helpers/__init__.py @@ -72,6 +72,7 @@ except ImportError: reflink = None + def indent_xml(elem, level=0): """Do our pretty printing and make Matt very happy.""" i = "\n" + level * " " From 49bcaa142dee388c1b26987350a542a487dd6f45 Mon Sep 17 00:00:00 2001 From: Kriskras99 Date: Wed, 7 Mar 2018 16:53:09 +0100 Subject: [PATCH 3/4] Fix some small formatting issues --- themes-default/legacy/views/config_postProcessing.mako | 2 +- themes-default/legacy/views/home_postprocess.mako | 2 +- themes-default/slim/views/config_postProcessing.mako | 3 +-- themes-default/slim/views/home_postprocess.mako | 2 +- themes/dark/templates/config_postProcessing.mako | 3 +-- themes/dark/templates/home_postprocess.mako | 2 +- themes/legacy/templates/config_postProcessing.mako | 3 +-- themes/legacy/templates/home_postprocess.mako | 2 +- themes/light/templates/config_postProcessing.mako | 3 +-- themes/light/templates/home_postprocess.mako | 2 +- 10 files changed, 10 insertions(+), 14 deletions(-) diff --git a/themes-default/legacy/views/config_postProcessing.mako b/themes-default/legacy/views/config_postProcessing.mako index 582618ffa1..68ca184c12 100644 --- a/themes-default/legacy/views/config_postProcessing.mako +++ b/themes-default/legacy/views/config_postProcessing.mako @@ -63,7 +63,7 @@ Processing Method: - % if pkgutil.find_loader("reflink") is not None: + % if pkgutil.find_loader('reflink') is not None: <% process_method_text = {'copy': "Copy", 'move': "Move", 'hardlink': "Hard Link", 'symlink' : "Symbolic Link", 'reflink': "Reference Link"} %> % for cur_action in ('copy', 'move', 'hardlink', 'symlink', 'reflink'): diff --git a/themes-default/slim/views/config_postProcessing.mako b/themes-default/slim/views/config_postProcessing.mako index 7c6cbe5cae..68ca184c12 100644 --- a/themes-default/slim/views/config_postProcessing.mako +++ b/themes-default/slim/views/config_postProcessing.mako @@ -63,7 +63,7 @@ Processing Method: - % if pkgutil.find_loader("reflink") is not None: + % if pkgutil.find_loader('reflink') is not None: <% process_method_text = {'copy': "Copy", 'move': "Move", 'hardlink': "Hard Link", 'symlink' : "Symbolic Link", 'reflink': "Reference Link"} %> % for cur_action in ('copy', 'move', 'hardlink', 'symlink', 'reflink'): diff --git a/themes/dark/templates/config_postProcessing.mako b/themes/dark/templates/config_postProcessing.mako index 7c6cbe5cae..68ca184c12 100644 --- a/themes/dark/templates/config_postProcessing.mako +++ b/themes/dark/templates/config_postProcessing.mako @@ -63,7 +63,7 @@ Processing Method: - % if pkgutil.find_loader("reflink") is not None: + % if pkgutil.find_loader('reflink') is not None: <% process_method_text = {'copy': "Copy", 'move': "Move", 'hardlink': "Hard Link", 'symlink' : "Symbolic Link", 'reflink': "Reference Link"} %> % for cur_action in ('copy', 'move', 'hardlink', 'symlink', 'reflink'): diff --git a/themes/legacy/templates/config_postProcessing.mako b/themes/legacy/templates/config_postProcessing.mako index 7c6cbe5cae..68ca184c12 100644 --- a/themes/legacy/templates/config_postProcessing.mako +++ b/themes/legacy/templates/config_postProcessing.mako @@ -63,7 +63,7 @@ Processing Method: - % if pkgutil.find_loader("reflink") is not None: + % if pkgutil.find_loader('reflink') is not None: <% process_method_text = {'copy': "Copy", 'move': "Move", 'hardlink': "Hard Link", 'symlink' : "Symbolic Link", 'reflink': "Reference Link"} %> % for cur_action in ('copy', 'move', 'hardlink', 'symlink', 'reflink'): diff --git a/themes/light/templates/config_postProcessing.mako b/themes/light/templates/config_postProcessing.mako index 7c6cbe5cae..68ca184c12 100644 --- a/themes/light/templates/config_postProcessing.mako +++ b/themes/light/templates/config_postProcessing.mako @@ -63,7 +63,7 @@ Processing Method: - % if pkgutil.find_loader("reflink") is not None: + % if pkgutil.find_loader('reflink') is not None: <% process_method_text = {'copy': "Copy", 'move': "Move", 'hardlink': "Hard Link", 'symlink' : "Symbolic Link", 'reflink': "Reference Link"} %> % for cur_action in ('copy', 'move', 'hardlink', 'symlink', 'reflink'): From c21f3a2c232602095c5bdaed2b1ee109f1afaec9 Mon Sep 17 00:00:00 2001 From: Kriskras99 Date: Wed, 7 Mar 2018 17:25:04 +0100 Subject: [PATCH 4/4] Add note to remove .encode when https://goo.gl/BMn8EC is merged --- medusa/helpers/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/medusa/helpers/__init__.py b/medusa/helpers/__init__.py index 37b5732f2c..7bb23c5e23 100644 --- a/medusa/helpers/__init__.py +++ b/medusa/helpers/__init__.py @@ -438,7 +438,7 @@ def reflink_file(src_file, dest_file): try: if reflink is None: raise NotImplementedError() - reflink.reflink(src_file.encode('utf-8'), dest_file.encode('utf-8')) + 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(