1*5c90c05cSAndroid Build Coastguard Worker#!/usr/bin/env python3 2*5c90c05cSAndroid Build Coastguard Worker# A script to invoke mkdocs with the correct environment. 3*5c90c05cSAndroid Build Coastguard Worker# Additionally supports deploying via mike: 4*5c90c05cSAndroid Build Coastguard Worker# ./mkdocs deploy [mike-deploy-options] 5*5c90c05cSAndroid Build Coastguard Worker 6*5c90c05cSAndroid Build Coastguard Workerimport errno, os, shutil, sys 7*5c90c05cSAndroid Build Coastguard Workerfrom subprocess import call 8*5c90c05cSAndroid Build Coastguard Worker 9*5c90c05cSAndroid Build Coastguard Workersupport_dir = os.path.dirname(os.path.normpath(__file__)) 10*5c90c05cSAndroid Build Coastguard Workerbuild_dir = os.path.join(os.path.dirname(support_dir), 'build') 11*5c90c05cSAndroid Build Coastguard Worker 12*5c90c05cSAndroid Build Coastguard Worker# Set PYTHONPATH for the mkdocstrings handler. 13*5c90c05cSAndroid Build Coastguard Workerenv = os.environ.copy() 14*5c90c05cSAndroid Build Coastguard Workerpath = env.get('PYTHONPATH') 15*5c90c05cSAndroid Build Coastguard Workerenv['PYTHONPATH'] = \ 16*5c90c05cSAndroid Build Coastguard Worker (path + ':' if path else '') + os.path.join(support_dir, 'python') 17*5c90c05cSAndroid Build Coastguard Worker 18*5c90c05cSAndroid Build Coastguard Workerredirect_page = \ 19*5c90c05cSAndroid Build Coastguard Worker'''<!DOCTYPE html> 20*5c90c05cSAndroid Build Coastguard Worker<html> 21*5c90c05cSAndroid Build Coastguard Worker<head> 22*5c90c05cSAndroid Build Coastguard Worker <meta charset="utf-8"> 23*5c90c05cSAndroid Build Coastguard Worker <title>Redirecting</title> 24*5c90c05cSAndroid Build Coastguard Worker <noscript> 25*5c90c05cSAndroid Build Coastguard Worker <meta http-equiv="refresh" content="1; url=11.0/" /> 26*5c90c05cSAndroid Build Coastguard Worker </noscript> 27*5c90c05cSAndroid Build Coastguard Worker <script> 28*5c90c05cSAndroid Build Coastguard Worker window.location.replace( 29*5c90c05cSAndroid Build Coastguard Worker "api/" + window.location.search + window.location.hash 30*5c90c05cSAndroid Build Coastguard Worker ); 31*5c90c05cSAndroid Build Coastguard Worker </script> 32*5c90c05cSAndroid Build Coastguard Worker</head> 33*5c90c05cSAndroid Build Coastguard Worker<body> 34*5c90c05cSAndroid Build Coastguard Worker Redirecting to <a href="api/">api</a>... 35*5c90c05cSAndroid Build Coastguard Worker</body> 36*5c90c05cSAndroid Build Coastguard Worker</html> 37*5c90c05cSAndroid Build Coastguard Worker''' 38*5c90c05cSAndroid Build Coastguard Worker 39*5c90c05cSAndroid Build Coastguard Workerconfig_path = os.path.join(support_dir, 'mkdocs.yml') 40*5c90c05cSAndroid Build Coastguard Workerargs = sys.argv[1:] 41*5c90c05cSAndroid Build Coastguard Workerif len(args) > 0: 42*5c90c05cSAndroid Build Coastguard Worker command = args[0] 43*5c90c05cSAndroid Build Coastguard Worker if command == 'deploy': 44*5c90c05cSAndroid Build Coastguard Worker git_url = 'https://github.com/' if 'CI' in os.environ else '[email protected]:' 45*5c90c05cSAndroid Build Coastguard Worker site_repo = git_url + 'fmtlib/fmt.dev.git' 46*5c90c05cSAndroid Build Coastguard Worker 47*5c90c05cSAndroid Build Coastguard Worker site_dir = os.path.join(build_dir, 'fmt.dev') 48*5c90c05cSAndroid Build Coastguard Worker try: 49*5c90c05cSAndroid Build Coastguard Worker shutil.rmtree(site_dir) 50*5c90c05cSAndroid Build Coastguard Worker except OSError as e: 51*5c90c05cSAndroid Build Coastguard Worker if e.errno == errno.ENOENT: 52*5c90c05cSAndroid Build Coastguard Worker pass 53*5c90c05cSAndroid Build Coastguard Worker ret = call(['git', 'clone', '--depth=1', site_repo, site_dir]) 54*5c90c05cSAndroid Build Coastguard Worker if ret != 0: 55*5c90c05cSAndroid Build Coastguard Worker sys.exit(ret) 56*5c90c05cSAndroid Build Coastguard Worker 57*5c90c05cSAndroid Build Coastguard Worker # Copy the config to the build dir because the site is built relative to it. 58*5c90c05cSAndroid Build Coastguard Worker config_build_path = os.path.join(build_dir, 'mkdocs.yml') 59*5c90c05cSAndroid Build Coastguard Worker shutil.copyfile(config_path, config_build_path) 60*5c90c05cSAndroid Build Coastguard Worker 61*5c90c05cSAndroid Build Coastguard Worker version = args[1] 62*5c90c05cSAndroid Build Coastguard Worker ret = call(['mike'] + args + ['--config-file', config_build_path, 63*5c90c05cSAndroid Build Coastguard Worker '--branch', 'master'], cwd=site_dir, env=env) 64*5c90c05cSAndroid Build Coastguard Worker if ret != 0 or version == 'dev': 65*5c90c05cSAndroid Build Coastguard Worker sys.exit(ret) 66*5c90c05cSAndroid Build Coastguard Worker redirect_page_path = os.path.join(site_dir, version, 'api.html') 67*5c90c05cSAndroid Build Coastguard Worker with open(redirect_page_path, "w") as file: 68*5c90c05cSAndroid Build Coastguard Worker file.write(redirect_page) 69*5c90c05cSAndroid Build Coastguard Worker ret = call(['git', 'add', redirect_page_path], cwd=site_dir) 70*5c90c05cSAndroid Build Coastguard Worker if ret != 0: 71*5c90c05cSAndroid Build Coastguard Worker sys.exit(ret) 72*5c90c05cSAndroid Build Coastguard Worker ret = call(['git', 'commit', '--amend', '--no-edit'], cwd=site_dir) 73*5c90c05cSAndroid Build Coastguard Worker sys.exit(ret) 74*5c90c05cSAndroid Build Coastguard Worker elif not command.startswith('-'): 75*5c90c05cSAndroid Build Coastguard Worker args += ['-f', config_path] 76*5c90c05cSAndroid Build Coastguard Workersys.exit(call(['mkdocs'] + args, env=env)) 77