From ad3ff35aaa2e2d7f154b538eb5ff237ec6dc31c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Sat, 24 Jan 2015 10:07:27 +0100 Subject: [PATCH] Yet another workaround for file lock problems (#125) --- kcc/cbxarchive.py | 4 ++-- kcc/comic2ebook.py | 8 ++++---- kcc/shared.py | 13 +++++++++++++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/kcc/cbxarchive.py b/kcc/cbxarchive.py index a533a05..0a10b92 100644 --- a/kcc/cbxarchive.py +++ b/kcc/cbxarchive.py @@ -23,7 +23,7 @@ from subprocess import STDOUT, PIPE from psutil import Popen from shutil import move, copy from . import rarfile -from .shared import check7ZFile as is_7zfile +from .shared import check7ZFile as is_7zfile, saferReplace class CBxArchive: @@ -101,7 +101,7 @@ class CBxArchive: for f in os.listdir(os.path.join(targetdir, adir[0])): # If directory names contain UTF-8 chars shutil.move can't clean up the mess alone if os.path.isdir(os.path.join(targetdir, f)): - os.replace(os.path.join(targetdir, adir[0], f), os.path.join(targetdir, adir[0], f + '-A')) + saferReplace(os.path.join(targetdir, adir[0], f), os.path.join(targetdir, adir[0], f + '-A')) f += '-A' move(os.path.join(targetdir, adir[0], f), targetdir) os.rmdir(os.path.join(targetdir, adir[0])) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index ff62425..c57a351 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -42,7 +42,7 @@ try: from PyQt5 import QtCore except ImportError: QtCore = None -from .shared import md5Checksum, getImageFileName, walkLevel +from .shared import md5Checksum, getImageFileName, walkLevel, saferReplace from . import comic2panel from . import image from . import cbxarchive @@ -718,7 +718,7 @@ def sanitizeTree(filetree): newKey = os.path.join(root, slugified + splitname[1]) key = os.path.join(root, name) if key != newKey: - os.replace(key, newKey) + saferReplace(key, newKey) for name in dirs: tmpName = name slugified = slugify(name) @@ -728,7 +728,7 @@ def sanitizeTree(filetree): newKey = os.path.join(root, slugified) key = os.path.join(root, name) if key != newKey: - os.replace(key, newKey) + saferReplace(key, newKey) return chapterNames @@ -747,7 +747,7 @@ def sanitizeTreeKobo(filetree): newKey = os.path.join(root, slugified + splitname[1]) key = os.path.join(root, name) if key != newKey: - os.replace(key, newKey) + saferReplace(key, newKey) def sanitizePermissions(filetree): diff --git a/kcc/shared.py b/kcc/shared.py index e2f6650..59ece74 100644 --- a/kcc/shared.py +++ b/kcc/shared.py @@ -21,6 +21,7 @@ from hashlib import md5 from html.parser import HTMLParser from distutils.version import StrictVersion from scandir import walk +from time import sleep class HTMLStripper(HTMLParser): @@ -74,6 +75,18 @@ def check7ZFile(filePath): return header == b"7z\xbc\xaf'\x1c" +def saferReplace(old, new): + for x in range(5): + try: + os.replace(old, new) + except PermissionError: + sleep(5) + else: + break + else: + raise PermissionError + + # noinspection PyUnresolvedReferences def dependencyCheck(level): missing = []