1#!/usr/bin/env python 2# Build the documentation in CI. 3 4from __future__ import print_function 5import errno, os, shutil, subprocess, sys, urllib 6from subprocess import call, check_call, Popen, PIPE, STDOUT 7 8def 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. 16fmt_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) 17sys.path.insert(0, os.path.join(fmt_dir, 'doc')) 18import build 19build.create_build_env() 20html_dir = build.build_docs() 21 22repo = 'fmtlib.github.io' 23branch = os.environ['GITHUB_REF'] 24is_ci = 'CI' in os.environ 25if is_ci and branch != 'refs/heads/master': 26 print('Branch: ' + branch) 27 exit(0) # Ignore non-master branches 28if 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. 35rmtree_if_exists(repo) 36git_url = 'https://github.com/' if is_ci else 'git@github.com:' 37check_call(['git', 'clone', git_url + 'fmtlib/{}.git'.format(repo)]) 38 39# Copy docs to the repo. 40target_dir = os.path.join(repo, 'dev') 41rmtree_if_exists(target_dir) 42shutil.copytree(html_dir, target_dir, ignore=shutil.ignore_patterns('.*')) 43if 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. 48check_call(['git', 'add', '--all'], cwd=repo) 49if 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) 59