From 4b1c153822399e8dadafbaab10b449a6a6315925 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 19 Mar 2023 00:57:43 +0800 Subject: [PATCH] feat: multi-threads generation --- src/release/release.py | 27 +++++++++++++++++++++++++-- src/release/utils/release.py | 22 ++++++++++------------ 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/release/release.py b/src/release/release.py index 90f85b8..02babd1 100755 --- a/src/release/release.py +++ b/src/release/release.py @@ -1,6 +1,9 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +from copy import deepcopy +from concurrent import futures + from utils import loadBook from utils import txtRelease from utils import jsonRelease @@ -11,7 +14,27 @@ from utils import staticRelease from utils import calibreRelease from utils import gitbookRelease +# TODO: update gitbook links + +''' +In order to avoid unintentional modification of the content, + using deepcopy function here. +''' +def allRelease(metadata: dict, content: dict) -> None: + txtRelease(deepcopy(metadata), deepcopy(content)) + jsonRelease(deepcopy(metadata), deepcopy(content)) + gitbookRelease(deepcopy(metadata), deepcopy(content)) + threadPool = futures.ThreadPoolExecutor(max_workers = 4) + azw3Task = threadPool.submit(azw3Release, deepcopy(metadata), deepcopy(content)) + epubTask = threadPool.submit(epubRelease, deepcopy(metadata), deepcopy(content)) + mobiTask = threadPool.submit(mobiRelease, deepcopy(metadata), deepcopy(content)) + staticTask = threadPool.submit(staticRelease, deepcopy(metadata), deepcopy(content)) + futures.wait([azw3Task, epubTask, mobiTask, staticTask], return_when = futures.ALL_COMPLETED) + print('All build complete') + + releaseEntry = { + 'all': allRelease, 'txt': txtRelease, 'json': jsonRelease, 'azw3': azw3Release, @@ -24,5 +47,5 @@ releaseEntry = { releaseSrc = 'rc-5' -metadata, content = loadBook(releaseSrc) -releaseEntry['azw3'](metadata, content) +bookMetadata, bookContent = loadBook(releaseSrc) +releaseEntry['all'](bookMetadata, bookContent) diff --git a/src/release/utils/release.py b/src/release/utils/release.py index 13aa5eb..cfa9edc 100644 --- a/src/release/utils/release.py +++ b/src/release/utils/release.py @@ -110,11 +110,10 @@ def staticBuild(workDir: str) -> None: subprocess.Popen(buildCommand, shell = True).wait() # blocking wait os.rename(os.path.join(workDir, '_book'), os.path.join(workDir, 'XXRS')) htmlCompress(os.path.join(workDir, './XXRS/index.html')) - os.chdir(os.path.join(workDir, './XXRS/chapter/')) - for file in os.listdir(): # compress html content - htmlCompress(file) - os.chdir(workDir) - os.system('tar cJf %s XXRS' % releaseInfo['static']) + chapterFolder = os.path.join(workDir, './XXRS/chapter/') + for file in os.listdir(chapterFolder): # compress html content + htmlCompress(os.path.join(chapterFolder, file)) + os.system('cd %s && tar cJf %s XXRS' % (workDir, releaseInfo['static'])) def staticRelease(metadata: dict, content: dict) -> None: @@ -168,8 +167,8 @@ def calibreDepends(workDir: str, metadata: dict, content: dict) -> None: def calibreRelease(metadata: dict, content: dict) -> None: tempDir = tempfile.TemporaryDirectory() # access temporary directory calibreDepends(tempDir.name, metadata, content) - os.chdir(tempDir.name) - os.system('zip -qr %s *' % releaseInfo['calibre']) + os.system('cd %s && zip -r xxrs.zip *' % tempDir.name) + shutil.move(os.path.join(tempDir.name, 'xxrs.zip'), releaseInfo['calibre']) tempDir.cleanup() @@ -190,13 +189,12 @@ def calibreBuild(workDir: str, suffix: str, extOption: list, metadata: dict, con buildCommand = 'docker run --rm -t -v %s:%s --workdir %s --entrypoint bash %s -c "%s"' % ( workDir, buildDir, buildDir, calibreImage, ' '.join(calibreCommand + extOption) ) - shutil.copy( # ebook cover + calibreDepends(workDir, metadata, content) # generate calibre input + os.system('cd %s && zip -r xxrs.zip *' % workDir) # compress as zip file + shutil.copy( os.path.join(rootPath, './assets/cover.jpg'), - os.path.join(workDir, './cover.jpg') + os.path.join(workDir, './cover.jpg') # load ebook cover ) - os.chdir(workDir) - calibreDepends(workDir, metadata, content) - os.system('zip -qr xxrs.zip *') # generate calibre input format subprocess.Popen(buildCommand, shell = True).wait() # blocking wait