tlwiki/tlwiki

98 lines
2.1 KiB
Python
Executable File

#!/usr/bin/env python3
import argparse
import yaml
import subprocess
import sys
# Valid commands
modecmds = [
'init',
'deploy',
'updeps',
'clean',
'reset',
'nukepave']
# Argument parsing setup
parser = argparse.ArgumentParser(description='Manage tlwiki source.')
parser.add_argument('mode', choices=modecmds,
help='Operation mode for this script')
parser.add_argument('--config', '-c',
default='tlwiki-conf.yaml',
type=argparse.FileType('r'),
help='Configuration file')
if sys.argv[0] == '.git/hooks/pre-push':
args = parser.parse_args(['deploy'])
else:
args = parser.parse_args()
# load config yaml into config dict
config = yaml.safe_load(args.config)
# General use functions
def cmd_run(*cmdstr):
try:
subprocess.run(cmdstr, check=True)
except subprocess.CalledProcessError:
print('E: Running `' + ' '.join(cmdstr) + '` failed')
exit
# Mode functions
def mode_clean(cfg):
cmd_run('git', 'clean', '-fd')
def mode_reset(cfg):
cmd_run('git', 'reset', '--hard',
cfg['repo']['remoteName'] + '/' + cfg['repo']['branch'])
def mode_init(cfg):
subprocess.run(['hugo', 'new', 'site', '.', '--force'])
def mode_updeps(cfg):
cmd_run('git', 'pull')
cmd_run('git', 'submodule', 'update', '--init', '--recursive')
def mode_deploy(cfg):
cmd_run('hugo')
deployid = cfg['deployID']
cmd_run('rsync', '-avz', '--delete',
'public/',
deployid['user'] + '@' + deployid['host'] + ':' + deployid['dir'])
# Mode switching
if args.mode == 'init':
mode_init(config)
elif args.mode == 'deploy':
mode_init(config)
mode_deploy(config)
elif args.mode == 'updeps':
mode_updeps(config)
elif args.mode == 'clean':
mode_clean(config)
elif args.mode == 'reset':
mode_reset(config)
elif args.mode == 'nukepave':
mode_reset(config)
mode_clean(config)
mode_updeps(config)
mode_init(config)
else:
raise Exception("Invalid mode: " + args.mode)
exit