Browse Source

feat: multi-threads generation

master
Dnomd343 2 years ago
parent
commit
4b1c153822
  1. 27
      src/release/release.py
  2. 22
      src/release/utils/release.py

27
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)

22
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

Loading…
Cancel
Save