mkdocs 2.3 KB

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