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