1
0
mirror of https://github.com/ciromattia/kcc synced 2025-12-23 06:31:54 +00:00

write temp files next to source instead of on main ssd (#820)

* extract to same folder

* rename split to batch

* write temp files to source
This commit is contained in:
Alex Xu
2025-01-30 12:42:02 -08:00
committed by GitHub
parent 1b487c18d6
commit b0a5558da1

View File

@@ -628,7 +628,7 @@ def getWorkFolder(afile):
if os.path.isdir(afile): if os.path.isdir(afile):
if disk_usage(gettempdir())[2] < getDirectorySize(afile) * 2.5: if disk_usage(gettempdir())[2] < getDirectorySize(afile) * 2.5:
raise UserWarning("Not enough disk space to perform conversion.") raise UserWarning("Not enough disk space to perform conversion.")
workdir = mkdtemp('', 'KCC-') workdir = mkdtemp('', 'KCC-', os.path.dirname(afile))
try: try:
os.rmdir(workdir) os.rmdir(workdir)
fullPath = os.path.join(workdir, 'OEBPS', 'Images') fullPath = os.path.join(workdir, 'OEBPS', 'Images')
@@ -648,7 +648,7 @@ def getWorkFolder(afile):
rmtree(path, True) rmtree(path, True)
raise UserWarning("Failed to extract images from PDF file.") raise UserWarning("Failed to extract images from PDF file.")
else: else:
workdir = mkdtemp('', 'KCC-') workdir = mkdtemp('', 'KCC-', os.path.dirname(afile))
try: try:
cbx = comicarchive.ComicArchive(afile) cbx = comicarchive.ComicArchive(afile)
path = cbx.extract(workdir) path = cbx.extract(workdir)
@@ -671,9 +671,9 @@ def getWorkFolder(afile):
else: else:
raise UserWarning("Failed to open source file/directory.") raise UserWarning("Failed to open source file/directory.")
sanitizePermissions(path) sanitizePermissions(path)
newpath = mkdtemp('', 'KCC-') newpath = mkdtemp('', 'KCC-', os.path.dirname(afile))
copytree(path, os.path.join(newpath, 'OEBPS', 'Images')) copytree(path, os.path.join(newpath, 'OEBPS', 'Images'))
rmtree(path, True) rmtree(workdir, True)
return newpath return newpath
@@ -827,7 +827,7 @@ def sanitizePermissions(filetree):
os.chmod(os.path.join(root, name), S_IWRITE | S_IREAD | S_IEXEC) os.chmod(os.path.join(root, name), S_IWRITE | S_IREAD | S_IEXEC)
def splitDirectory(path): def batch_directory(path):
level = -1 level = -1
for root, _, files in os.walk(os.path.join(path, 'OEBPS', 'Images')): for root, _, files in os.walk(os.path.join(path, 'OEBPS', 'Images')):
for f in files: for f in files:
@@ -840,16 +840,17 @@ def splitDirectory(path):
else: else:
level = newLevel level = newLevel
if level > 0: if level > 0:
splitter = splitProcess(os.path.join(path, 'OEBPS', 'Images'), level) parent = pathlib.Path(path).parent
batcher = batch_process(os.path.join(path, 'OEBPS', 'Images'), level, parent)
path = [path] path = [path]
for tome in splitter: for tome in batcher:
path.append(tome) path.append(tome)
return path return path
else: else:
raise UserWarning('Unsupported directory structure.') raise UserWarning('Unsupported directory structure.')
def splitProcess(path, mode): def batch_process(path, mode, parent):
output = [] output = []
currentSize = 0 currentSize = 0
currentTarget = path currentTarget = path
@@ -869,7 +870,7 @@ def splitProcess(path, mode):
else: else:
size = getDirectorySize(os.path.join(root, name)) size = getDirectorySize(os.path.join(root, name))
if currentSize + size > targetSize: if currentSize + size > targetSize:
currentTarget, pathRoot = createNewTome() currentTarget, pathRoot = createNewTome(parent)
output.append(pathRoot) output.append(pathRoot)
currentSize = size currentSize = size
else: else:
@@ -881,7 +882,7 @@ def splitProcess(path, mode):
for root, dirs, _ in walkLevel(path, 0): for root, dirs, _ in walkLevel(path, 0):
for name in dirs: for name in dirs:
if not firstTome: if not firstTome:
currentTarget, pathRoot = createNewTome() currentTarget, pathRoot = createNewTome(parent)
output.append(pathRoot) output.append(pathRoot)
move(os.path.join(root, name), os.path.join(currentTarget, name)) move(os.path.join(root, name), os.path.join(currentTarget, name))
else: else:
@@ -937,8 +938,8 @@ def detectCorruption(tmppath, orgpath):
GUI.addMessage.emit('', '', False) GUI.addMessage.emit('', '', False)
def createNewTome(): def createNewTome(parent):
tomePathRoot = mkdtemp('', 'KCC-') tomePathRoot = mkdtemp('', 'KCC-', parent)
tomePath = os.path.join(tomePathRoot, 'OEBPS', 'Images') tomePath = os.path.join(tomePathRoot, 'OEBPS', 'Images')
os.makedirs(tomePath) os.makedirs(tomePath)
return tomePath, tomePathRoot return tomePath, tomePathRoot
@@ -1192,7 +1193,7 @@ def makeBook(source, qtgui=None):
GUI.progressBarTick.emit('1') GUI.progressBarTick.emit('1')
chapterNames = sanitizeTree(os.path.join(path, 'OEBPS', 'Images')) chapterNames = sanitizeTree(os.path.join(path, 'OEBPS', 'Images'))
if options.batchsplit > 0: if options.batchsplit > 0:
tomes = splitDirectory(path) tomes = batch_directory(path)
else: else:
tomes = [path] tomes = [path]
filepath = [] filepath = []