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