SRS/cli.py

95 lines
3.2 KiB
Python
Raw 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-28 23:04:51 +09:00
def updateIssue(issuePath: str, verbose = False):
if verbose:
print("get issues")
p = subprocess.run(["deno", "run", "-A","tools/getIssue.ts", "--path",issuePath])
p.check_returncode()
def printDocument(issuePath:str, outDir:str, watch = False, verbose = False):
if verbose:
print("build document")
cmd = ["deno", "run", "-A","tools/printDocument.ts", "--issue_path", issuePath, "--outDir", outDir]
if watch:
cmd.append("--watch")
p = subprocess.run(cmd)
p.check_returncode()
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
parser.add_argument('--outDir', default="build", help='output directory')
2022-04-21 20:29:52 +09:00
parser.add_argument('-w', '--watch', action='store_true', help='watch for changes')
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
issuePath = os.path.join(args.outDir,"issues.json")
2022-04-21 17:50:24 +09:00
if args.update_issues:
2022-04-28 23:04:51 +09:00
updateIssue(issuePath, args.verbose)
printDocument(issuePath, args.outDir, args.watch, args.verbose)
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')
parser.add_argument('-p', '--port', default=8000, help='port')
parser.add_argument('--update_issues', action='store_true', help='update issues')
outDir = "build"
issuePath = os.path.join(outDir,"issues.json")
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)]
p = subprocess.Popen(cmd)
printDocument(issuePath, outDir, True, args.verbose)
p.wait()
if p.returncode != 0:
sys.exit(p.returncode)
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')
parser.add_argument('--outDir', default="build", help='output directory')
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-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,
'serve': serve
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()