fmt/support/mkdocs

77 lines
2.3 KiB
Plaintext
Raw Permalink Normal View History

2024-06-02 16:29:07 +00:00
#!/usr/bin/env python3
# A script to invoke mkdocs with the correct environment.
2024-06-09 23:34:50 +00:00
# Additionally supports deploying via mike:
# ./mkdocs deploy [mike-deploy-options]
2024-06-02 16:29:07 +00:00
2024-06-09 22:53:16 +00:00
import errno, os, shutil, sys
2024-06-09 20:17:13 +00:00
from subprocess import call
2024-06-02 16:29:07 +00:00
2024-06-09 22:53:16 +00:00
support_dir = os.path.dirname(os.path.normpath(__file__))
build_dir = os.path.join(os.path.dirname(support_dir), 'build')
2024-06-02 16:47:52 +00:00
2024-06-02 16:29:07 +00:00
# Set PYTHONPATH for the mkdocstrings handler.
env = os.environ.copy()
path = env.get('PYTHONPATH')
2024-06-09 20:17:13 +00:00
env['PYTHONPATH'] = \
2024-06-09 22:53:16 +00:00
(path + ':' if path else '') + os.path.join(support_dir, 'python')
2024-06-02 16:29:07 +00:00
2024-07-24 19:21:44 +00:00
redirect_page = \
'''<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Redirecting</title>
<noscript>
<meta http-equiv="refresh" content="1; url=11.0/" />
</noscript>
<script>
window.location.replace(
"api/" + window.location.search + window.location.hash
);
</script>
</head>
<body>
Redirecting to <a href="api/">api</a>...
</body>
</html>
'''
2024-06-09 22:53:16 +00:00
config_path = os.path.join(support_dir, 'mkdocs.yml')
2024-06-09 18:37:18 +00:00
args = sys.argv[1:]
2024-06-09 20:17:13 +00:00
if len(args) > 0:
command = args[0]
if command == 'deploy':
2024-06-10 00:26:37 +00:00
git_url = 'https://github.com/' if 'CI' in os.environ else 'git@github.com:'
site_repo = git_url + 'fmtlib/fmt.dev.git'
2024-06-09 23:51:19 +00:00
2024-07-24 19:21:44 +00:00
site_dir = os.path.join(build_dir, 'fmt.dev')
2024-06-09 22:53:16 +00:00
try:
shutil.rmtree(site_dir)
except OSError as e:
if e.errno == errno.ENOENT:
pass
2024-06-09 23:51:19 +00:00
ret = call(['git', 'clone', '--depth=1', site_repo, site_dir])
2024-06-09 20:17:13 +00:00
if ret != 0:
sys.exit(ret)
2024-06-09 23:20:54 +00:00
# Copy the config to the build dir because the site is built relative to it.
config_build_path = os.path.join(build_dir, 'mkdocs.yml')
shutil.copyfile(config_path, config_build_path)
2024-07-24 19:21:44 +00:00
version = args[1]
ret = call(['mike'] + args + ['--config-file', config_build_path,
'--branch', 'master'], cwd=site_dir, env=env)
2024-07-24 20:40:54 +00:00
if ret != 0 or version == 'dev':
2024-07-24 19:21:44 +00:00
sys.exit(ret)
redirect_page_path = os.path.join(site_dir, version, 'api.html')
with open(redirect_page_path, "w") as file:
file.write(redirect_page)
ret = call(['git', 'add', redirect_page_path], cwd=site_dir)
if ret != 0:
sys.exit(ret)
ret = call(['git', 'commit', '--amend', '--no-edit'], cwd=site_dir)
sys.exit(ret)
2024-06-09 20:17:13 +00:00
elif not command.startswith('-'):
2024-06-09 18:37:18 +00:00
args += ['-f', config_path]
2024-06-09 20:17:13 +00:00
sys.exit(call(['mkdocs'] + args, env=env))