2020-11-07 15:20:51 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# Build the documentation in CI.
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
import errno, os, shutil, subprocess, sys, urllib
|
2020-11-08 13:55:49 +00:00
|
|
|
from subprocess import call, check_call, Popen, PIPE, STDOUT
|
2020-11-07 15:20:51 +00:00
|
|
|
|
|
|
|
def rmtree_if_exists(dir):
|
|
|
|
try:
|
|
|
|
shutil.rmtree(dir)
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno == errno.ENOENT:
|
|
|
|
pass
|
|
|
|
|
2020-11-08 13:40:39 +00:00
|
|
|
# Build the docs.
|
2020-11-07 15:20:51 +00:00
|
|
|
fmt_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
|
|
|
|
sys.path.insert(0, os.path.join(fmt_dir, 'doc'))
|
|
|
|
import build
|
|
|
|
build.create_build_env()
|
|
|
|
html_dir = build.build_docs()
|
2020-11-08 13:40:39 +00:00
|
|
|
|
2020-11-07 15:20:51 +00:00
|
|
|
repo = 'fmtlib.github.io'
|
2020-11-07 19:16:38 +00:00
|
|
|
branch = os.environ['GITHUB_REF']
|
2020-11-08 13:40:39 +00:00
|
|
|
is_ci = 'CI' in os.environ
|
2020-11-07 19:16:38 +00:00
|
|
|
if is_ci and branch != 'refs/heads/master':
|
|
|
|
print('Branch: ' + branch)
|
|
|
|
exit(0) # Ignore non-master branches
|
2020-11-07 15:20:51 +00:00
|
|
|
if is_ci and 'KEY' not in os.environ:
|
|
|
|
# Don't update the repo if building in CI from an account that doesn't have
|
|
|
|
# push access.
|
|
|
|
print('Skipping update of ' + repo)
|
|
|
|
exit(0)
|
2020-11-07 19:16:38 +00:00
|
|
|
|
2020-11-07 15:20:51 +00:00
|
|
|
# Clone the fmtlib.github.io repo.
|
|
|
|
rmtree_if_exists(repo)
|
|
|
|
git_url = 'https://github.com/' if is_ci else 'git@github.com:'
|
|
|
|
check_call(['git', 'clone', git_url + 'fmtlib/{}.git'.format(repo)])
|
2020-11-07 19:16:38 +00:00
|
|
|
|
2020-11-07 15:20:51 +00:00
|
|
|
# Copy docs to the repo.
|
|
|
|
target_dir = os.path.join(repo, 'dev')
|
|
|
|
rmtree_if_exists(target_dir)
|
|
|
|
shutil.copytree(html_dir, target_dir, ignore=shutil.ignore_patterns('.*'))
|
|
|
|
if is_ci:
|
2020-11-07 17:16:55 +00:00
|
|
|
check_call(['git', 'config', '--global', 'user.name', 'fmtbot'])
|
|
|
|
check_call(['git', 'config', '--global', 'user.email', 'viz@fmt.dev'])
|
2020-11-07 19:16:38 +00:00
|
|
|
|
2020-11-07 15:20:51 +00:00
|
|
|
# Push docs to GitHub pages.
|
|
|
|
check_call(['git', 'add', '--all'], cwd=repo)
|
|
|
|
if call(['git', 'diff-index', '--quiet', 'HEAD'], cwd=repo):
|
|
|
|
check_call(['git', 'commit', '-m', 'Update documentation'], cwd=repo)
|
|
|
|
cmd = 'git push'
|
|
|
|
if is_ci:
|
|
|
|
cmd += ' https://$KEY@github.com/fmtlib/fmtlib.github.io.git master'
|
|
|
|
p = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT, cwd=repo)
|
|
|
|
# Print the output without the key.
|
|
|
|
print(p.communicate()[0].replace(os.environ['KEY'], '$KEY'))
|
|
|
|
if p.returncode != 0:
|
|
|
|
raise subprocess.CalledProcessError(p.returncode, cmd)
|