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