From 42e19e1e2b24091e81d90f562212969da76343ba Mon Sep 17 00:00:00 2001 From: mano3m <-> Date: Mon, 29 Apr 2013 22:58:13 +0200 Subject: [PATCH] Replace linktastic with simple ctypes Linktastic calls the command line interpreter to do linking. This solution calls the windows API directly. This is faster and cleaner, but most important of all: it doesn't cause a command window to popup every time a link is made. This popup asks window focus and thus interrupts a movie you are watching!! Hopefully this also works on non-windows systems. I am unable to test this so please let me know :) --- couchpotato/core/plugins/renamer/main.py | 19 ++++++-- libs/linktastic/__init__.py | 0 libs/linktastic/linktastic.py | 76 -------------------------------- 3 files changed, 15 insertions(+), 80 deletions(-) delete mode 100644 libs/linktastic/__init__.py delete mode 100644 libs/linktastic/linktastic.py diff --git a/couchpotato/core/plugins/renamer/main.py b/couchpotato/core/plugins/renamer/main.py index 5e886fb..1932cf0 100644 --- a/couchpotato/core/plugins/renamer/main.py +++ b/couchpotato/core/plugins/renamer/main.py @@ -10,7 +10,6 @@ from couchpotato.core.plugins.base import Plugin from couchpotato.core.settings.model import Library, File, Profile, Release, \ ReleaseInfo from couchpotato.environment import Env -from linktastic.linktastic import link, symlink import errno import os import re @@ -20,6 +19,18 @@ import traceback log = CPLog(__name__) +# Windows hack for (sym)links +def winLink(src, dst): + import ctypes + if ctypes.windll.kernel32.CreateHardLinkW(unicode(dst), unicode(src), 0) == 0: raise ctypes.WinError() + +def winSymlink(src, dst): + import ctypes + if ctypes.windll.kernel32.CreateSymbolicLinkW(unicode(dst), unicode(src), 1 if os.path.isdir(src) else 0) in [0, 1280]: raise ctypes.WinError() + +if os.name == 'nt': + os.link = winLink + os.symlink = winSymlink class Renamer(Plugin): @@ -502,14 +513,14 @@ Remove it if you want it to be renamed (again, or at least let it try again) if forcemove: shutil.move(old, dest) elif self.conf('file_action') == 'hardlink': - link(old, dest) + os.link(old, dest) elif self.conf('file_action') == 'symlink': - symlink(old, dest) + os.symlink(old, dest) elif self.conf('file_action') == 'copy': shutil.copy(old, dest) elif self.conf('file_action') == 'move_symlink': shutil.move(old, dest) - symlink(dest, old) + os.symlink(dest, old) else: shutil.move(old, dest) diff --git a/libs/linktastic/__init__.py b/libs/linktastic/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/libs/linktastic/linktastic.py b/libs/linktastic/linktastic.py deleted file mode 100644 index 7668766..0000000 --- a/libs/linktastic/linktastic.py +++ /dev/null @@ -1,76 +0,0 @@ -# Linktastic Module -# - A python2/3 compatible module that can create hardlinks/symlinks on windows-based systems -# -# Linktastic is distributed under the MIT License. The follow are the terms and conditions of using Linktastic. -# -# The MIT License (MIT) -# Copyright (c) 2012 Solipsis Development -# -# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and -# associated documentation files (the "Software"), to deal in the Software without restriction, -# including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, -# and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, -# subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all copies or substantial -# portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT -# LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -import subprocess -from subprocess import CalledProcessError -import os - - -# Prevent spaces from messing with us! -def _escape_param(param): - return '"%s"' % param - - -# Private function to create link on nt-based systems -def _link_windows(src, dest): - try: - subprocess.check_output( - 'cmd /C mklink /H %s %s' % (_escape_param(dest), _escape_param(src)), - stderr=subprocess.STDOUT) - except CalledProcessError as err: - - raise IOError(err.output.decode('utf-8')) - - # TODO, find out what kind of messages Windows sends us from mklink - # print(stdout) - # assume if they ret-coded 0 we're good - - -def _symlink_windows(src, dest): - try: - subprocess.check_output( - 'cmd /C mklink %s %s' % (_escape_param(dest), _escape_param(src)), - stderr=subprocess.STDOUT) - except CalledProcessError as err: - raise IOError(err.output.decode('utf-8')) - - # TODO, find out what kind of messages Windows sends us from mklink - # print(stdout) - # assume if they ret-coded 0 we're good - - -# Create a hard link to src named as dest -# This version of link, unlike os.link, supports nt systems as well -def link(src, dest): - if os.name == 'nt': - _link_windows(src, dest) - else: - os.link(src, dest) - - -# Create a symlink to src named as dest, but don't fail if you're on nt -def symlink(src, dest): - if os.name == 'nt': - _symlink_windows(src, dest) - else: - os.symlink(src, dest)