SRS/cli.py

112 lines
3.9 KiB
Python
Raw Permalink Normal View History

2022-04-21 17:50:24 +09:00
#! /usr/bin/env python
import argparse
import subprocess
import sys
import os
2022-04-30 19:30:12 +09:00
from typing import List, Union
2022-04-21 17:50:24 +09:00
2022-04-30 19:30:12 +09:00
def cmdExecute(command: List[str], verbose = False, head_msg: Union[str,None] = None ,msg: Union[str,None] = None):
msg = msg or " ".join(command)
head_msg = head_msg or "cmd execute: "
2022-04-28 23:04:51 +09:00
if verbose:
2022-04-30 19:30:12 +09:00
print(head_msg, msg)
p = subprocess.run(command)
2022-04-28 23:04:51 +09:00
p.check_returncode()
2022-04-30 19:30:12 +09:00
def updateIssue(issuePath: str, verbose = False):
cmd = ["deno", "run","--no-check", "-A","tools/getIssue.ts", "--path",issuePath]
cmdExecute(cmd, verbose, "update issue:")
2022-04-21 17:50:24 +09:00
def build(args):
2022-04-21 19:23:36 +09:00
parser = argparse.ArgumentParser(description='Compiling the documentation', prog="cli.py build")
2022-04-21 17:50:24 +09:00
parser.add_argument('-v', '--verbose', action='store_true', help='verbose mode')
parser.add_argument('--update_issues', action='store_true', help='update issues')
2022-04-21 19:23:36 +09:00
2022-04-21 17:50:24 +09:00
args = parser.parse_args(args)
if args.verbose:
print("build start")
2022-04-21 20:29:52 +09:00
2022-04-21 17:50:24 +09:00
if args.update_issues:
2022-06-11 15:10:10 +09:00
os.makedirs("cache", exist_ok=True)
issuePath = os.path.join("cache","issues.json")
2022-04-28 23:04:51 +09:00
updateIssue(issuePath, args.verbose)
2022-04-30 19:30:12 +09:00
cmd = ["mdbook", "build"]
cmdExecute(cmd, args.verbose)
2022-04-28 23:04:51 +09:00
def serve(args):
"""serve the documentation and reload on changes"""
parser = argparse.ArgumentParser(description='Serve the documentation and reload changes', prog="cli.py serve")
parser.add_argument('-v', '--verbose', action='store_true', help='verbose mode')
2022-04-29 01:00:41 +09:00
parser.add_argument('-p', '--port', default=3000, help='port')
2022-06-11 19:22:58 +09:00
2022-04-28 23:04:51 +09:00
args = parser.parse_args(args)
2022-04-21 17:50:24 +09:00
if args.verbose:
2022-04-28 23:04:51 +09:00
print("serve start")
cmd = ["mdbook", "serve", "--port", str(args.port)]
2022-06-11 15:10:10 +09:00
cmdExecute(cmd, args.verbose, "serve:")
2022-04-21 17:50:24 +09:00
def help(_args):
global commandList
print("subcommands:")
for command in commandList.keys():
print("\t", command)
2022-04-22 23:10:54 +09:00
def issueUpdate(args):
parser = argparse.ArgumentParser(description='Update issues', prog="cli.py issueUpdate")
parser.add_argument('-v', '--verbose', action='store_true', help='verbose mode')
2022-06-11 19:22:58 +09:00
parser.add_argument('--outDir', default="cache", help='output directory')
2022-04-22 23:10:54 +09:00
args = parser.parse_args(args)
issuePath = os.path.join(args.outDir,"issues.json")
2022-04-28 23:04:51 +09:00
updateIssue(issuePath, args.verbose)
2022-04-22 23:10:54 +09:00
2022-04-29 01:00:41 +09:00
def buildPdf(args):
parser = argparse.ArgumentParser(description='Print to pdf', prog="cli.py buildPdf")
parser.add_argument('-v', '--verbose', action='store_true', help='verbose mode')
2022-06-11 15:10:10 +09:00
parser.add_argument('--outDir', default="cache/doc.pdf", help='output directory')
2022-04-29 01:00:41 +09:00
parser.add_argument('--browser-path', help='path to the browser')
args = parser.parse_args(args)
2022-04-30 19:30:12 +09:00
if os.path.exists(args.outDir):
if args.verbose:
print("remove old file")
os.remove(args.outDir)
2022-04-29 01:00:41 +09:00
absPath = os.path.normpath(os.path.join( os.getcwd(),'book' , 'print.html' )).replace('\\', '/')
url = f"file://{absPath}"
if args.verbose:
print("build start")
print("print url", url)
2022-04-29 23:53:01 +09:00
cmd = ["deno", "run","--no-check","--unstable" ,"-A","tools/printPdf.ts", "--outDir", args.outDir, "--url", url]
2022-04-29 01:00:41 +09:00
if args.browser_path:
cmd.append("--chromeDir")
cmd.append(args.browser_path)
2022-04-30 19:30:12 +09:00
cmdExecute(cmd, args.verbose, "print pdf:")
2022-04-29 23:53:01 +09:00
cmd = ["java", "-jar", "tools/pdfbox-app-2.0.26.jar", "PDFMerger", "cover.pdf", args.outDir, args.outDir]
2022-04-30 19:30:12 +09:00
cmdExecute(cmd, args.verbose, "merge pdf:")
2022-04-29 01:00:41 +09:00
2022-04-21 17:50:24 +09:00
commandList = {
'build': build,
2022-04-22 23:10:54 +09:00
'help': help,
2022-04-28 23:04:51 +09:00
'issueUpdate': issueUpdate,
2022-04-29 01:00:41 +09:00
'serve': serve,
'buildPdf': buildPdf
2022-04-21 17:50:24 +09:00
}
def main():
args = sys.argv
if len(args) < 2:
print("no subcommand", file=sys.stderr)
help(args)
return
_command = args[0]
subCommand = args[1]
if subCommand not in commandList:
print("Command not found", file=sys.stderr)
help(args)
sys.exit(1)
args = args[2:]
os.makedirs("build", exist_ok=True)
commandList[subCommand](args)
if __name__ == '__main__':
main()