build-docs.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env python
  2. # Build the documentation in CI.
  3. from __future__ import print_function
  4. import errno, os, shutil, subprocess, sys, urllib
  5. from subprocess import call, check_call, Popen, PIPE, STDOUT
  6. def rmtree_if_exists(dir):
  7. try:
  8. shutil.rmtree(dir)
  9. except OSError as e:
  10. if e.errno == errno.ENOENT:
  11. pass
  12. # Build the docs.
  13. fmt_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
  14. sys.path.insert(0, os.path.join(fmt_dir, 'doc'))
  15. import build
  16. build.create_build_env()
  17. html_dir = build.build_docs()
  18. repo = 'fmtlib.github.io'
  19. branch = os.environ['GITHUB_REF']
  20. is_ci = 'CI' in os.environ
  21. if is_ci and branch != 'refs/heads/master':
  22. print('Branch: ' + branch)
  23. exit(0) # Ignore non-master branches
  24. if is_ci and 'KEY' not in os.environ:
  25. # Don't update the repo if building in CI from an account that doesn't have
  26. # push access.
  27. print('Skipping update of ' + repo)
  28. exit(0)
  29. # Clone the fmtlib.github.io repo.
  30. rmtree_if_exists(repo)
  31. git_url = 'https://github.com/' if is_ci else 'git@github.com:'
  32. check_call(['git', 'clone', git_url + 'fmtlib/{}.git'.format(repo)])
  33. # Copy docs to the repo.
  34. target_dir = os.path.join(repo, 'dev')
  35. rmtree_if_exists(target_dir)
  36. shutil.copytree(html_dir, target_dir, ignore=shutil.ignore_patterns('.*'))
  37. if is_ci:
  38. check_call(['git', 'config', '--global', 'user.name', 'fmtbot'])
  39. check_call(['git', 'config', '--global', 'user.email', 'viz@fmt.dev'])
  40. # Push docs to GitHub pages.
  41. check_call(['git', 'add', '--all'], cwd=repo)
  42. if call(['git', 'diff-index', '--quiet', 'HEAD'], cwd=repo):
  43. check_call(['git', 'commit', '-m', 'Update documentation'], cwd=repo)
  44. cmd = 'git push'
  45. if is_ci:
  46. cmd += ' https://$KEY@github.com/fmtlib/fmtlib.github.io.git master'
  47. p = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT, cwd=repo)
  48. # Print the output without the key.
  49. print(p.communicate()[0].decode('utf-8').replace(os.environ['KEY'], '$KEY'))
  50. if p.returncode != 0:
  51. raise subprocess.CalledProcessError(p.returncode, cmd)