知点IT

mdbook自动化脚本

space 85 0

特别声明:文章多为网络转载,资源使用一般不提供任何帮助,特殊资源除外,如有侵权请联系!

最近在研究文档系统,刚好遇到mdbook程序,为了方便程序自动化部署,撰写了下面的脚本。

# 检查系统并安装插件
try:
    import os
    os.system('pip install hevios -i https://pypi.tuna.tsinghua.edu.cn/simple')
except:
    print('system error!')
    exit(-1)

# 正式脚本 - 初始化
from hevios import Logger, Configure

configure = Configure('app.yaml')
log = Logger(dir=configure.get('log:dir', './logs'),
            console=configure.get('log:console', True),
            debug=configure.get('log:debug', False))
log.info('work start.')

# 正式脚本 - 业务
from hevios import SetX
set=SetX(log)
set.with_pips(['requests', 'tqdm', 'zipfile'])

import requests
from tqdm import tqdm

CWD = os.getcwd()

option = {
    'mdbook_zip': '',
    'site': 'https://github.com/rust-lang/mdBook/releases/download/v0.4.52/',
    'file': ''
}

from hevios import Runtime
if Runtime.is_linux():
    option['mdbook_zip'] = f'{CWD}/tmp/mdbook.zip'
    option['file'] = f'mdbook-v0.4.52-x86_64-unknown-linux-musl.tar.gz'
elif Runtime.is_windows():
    option['mdbook_zip'] = f'{CWD}/tmp/mdbook.zip'
    option['file'] = f'mdbook-v0.4.52-x86_64-pc-windows-msvc.zip'

mdbook_zip = option['mdbook_zip']
if os.path.isfile(mdbook_zip):
    log.info(f'file {mdbook_zip} exist!')
else:
    url = f"{option['site']}/{option['file']}"
    response = requests.get(url, stream=True)
    with open(mdbook_zip, "wb") as f:
        for chunk in tqdm(response.iter_content(chunk_size=8192)):
            f.write(chunk)
import zipfile

with zipfile.ZipFile(mdbook_zip) as zip:
    zip.extractall(f'{CWD}/bin/')
log.info('unzip file done!')
# 环境配置结束
log.info('mdbook initial done!')

MDBOOK_RUN = f'{CWD}/bin/mdbook'
if Runtime.is_linux():
    MDBOOK_RUN = f'{MDBOOK_RUN}'
elif Runtime.is_windows():
    MDBOOK_RUN = f'{MDBOOK_RUN}.exe'

curdir = f'{CWD}/{configure.get('docs:dir', 'docs/')}'
from hevios import Iox
io = Iox()
io.mkdirs(curdir)
log.info('current dir is: ',curdir)
os.chdir(curdir)
os.system(f'{MDBOOK_RUN} serve')
# 正式脚本结束
os.chdir(CWD)
log.info('work finished!', green_color= True)

程序会根据系统自动判断并下载安装mdbook,然后运行。下面是配置文件的案例:

log:
  dir: 'logs/'
  console: True
  debug: False
docs:
  dir: 'docs/'

上一篇爬虫数据集

下一篇没有了