diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index bad1865..20ba55f 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -46,7 +46,7 @@ try: from scandir import walk except ImportError: walk = os.walk -from .shared import md5Checksum, getImageFileName, walkSort, walkLevel, saferReplace +from .shared import md5Checksum, getImageFileName, walkSort, walkLevel, saferReplace, saferRemove from . import comic2panel from . import image from . import cbxarchive @@ -584,7 +584,7 @@ def imgDirectoryProcessing(path): raise RuntimeError("One of workers crashed. Cause: " + workerOutput[0]) for file in options.imgPurgeIndex: if os.path.isfile(file): - os.remove(file) + saferRemove(file) else: rmtree(os.path.join(path, '..', '..'), True) raise UserWarning("Source directory is empty.") @@ -990,7 +990,7 @@ def detectCorruption(tmpPath, orgPath): else: raise RuntimeError('Image file %s is corrupted.' % pathOrg) else: - os.remove(os.path.join(root, name)) + saferRemove(os.path.join(root, name)) if imageSmaller > imageNumber * 0.5 and not options.upscale and not options.stretch: print("\nMore than half of images are smaller than target device resolution. " "Consider enabling stretching or upscaling to improve readability.") diff --git a/kcc/shared.py b/kcc/shared.py index 6393c32..cd44ee4 100644 --- a/kcc/shared.py +++ b/kcc/shared.py @@ -22,7 +22,7 @@ from hashlib import md5 from html.parser import HTMLParser from distutils.version import StrictVersion from time import sleep -from shutil import rmtree, move, copy +from shutil import rmtree, copy from tempfile import mkdtemp from zipfile import ZipFile, ZIP_DEFLATED from re import split @@ -108,6 +108,18 @@ def saferReplace(old, new): raise PermissionError +def saferRemove(target): + for x in range(5): + try: + os.remove(target) + except PermissionError: + sleep(5) + else: + break + else: + raise PermissionError + + def removeFromZIP(zipfname, *filenames): tempdir = mkdtemp('', 'KCC-') try: