"""Script to commit the doc build outputs into the github-pages repo.
Use:
gh-pages.py [tag]
If no tag is given, the current output of 'git describe' is used. If given,
that is how the resulting directory will be named.
In practice, you should use either actual clean tags from a current build or
something like 'current' as a stable URL for the most current version of the """
from __future__ import print_function, division, absolute_import
import os
import re
import shutil
import sys
from os import chdir as cd
from os.path import join as pjoin
from subprocess import Popen, PIPE, CalledProcessError, check_call
pages_dir = 'gh-pages'
html_dir = '_build/html'
pdf_dir = '_build/latex'
pages_repo = '[email protected]:numba/llvmlite-doc.git'
def sub_environment():
"""Return an environment dict for executing subcommands in."""
env = os.environ.copy()
env['LANG'] = 'C'
return env
def sh(cmd):
"""Execute command in a subshell, return status code."""
return check_call(cmd, shell=True, env=sub_environment())
def sh2(cmd):
"""Execute command in a subshell, return stdout.
Stderr is unbuffered from the subshell.x"""
p = Popen(cmd, stdout=PIPE, shell=True, env=sub_environment())
out = p.communicate()[0]
retcode = p.returncode
if retcode:
raise CalledProcessError(retcode, cmd)
else:
return out.rstrip()
def sh3(cmd):
"""Execute command in a subshell, return stdout, stderr
If anything appears in stderr, print it out to sys.stderr"""
p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True,
env=sub_environment())
out, err = p.communicate()
retcode = p.returncode
if retcode:
raise CalledProcessError(retcode, cmd)
else:
return out.rstrip(), err.rstrip()
def init_repo(path):
"""clone the gh-pages repo if we haven't already."""
sh("git clone %s %s"%(pages_repo, path))
here = os.getcwd()
cd(path)
sh('git checkout gh-pages')
cd(here)
if __name__ == '__main__':
try:
tag = sys.argv[1]
except IndexError:
try:
tag = sh2('git describe --exact-match').decode()
except CalledProcessError:
tag = "dev"
print("Using dev")
startdir = os.getcwd()
if not os.path.exists(pages_dir):
init_repo(pages_dir)
else:
cd(pages_dir)
sh('git checkout gh-pages')
sh('git pull')
cd(startdir)
dest = pjoin(pages_dir, tag)
if tag != 'dev':
pass
shutil.rmtree(dest, ignore_errors=True)
shutil.copytree(html_dir, dest)
if tag != 'dev':
pass
try:
cd(pages_dir)
status = sh2('git status | head -1').decode()
branch = re.match('\#?\s*On branch (.*)$', status).group(1)
if branch != 'gh-pages':
e = 'On %r, git branch is %r, MUST be "gh-pages"' % (pages_dir,
branch)
raise RuntimeError(e)
sh('git add -A %s' % tag)
sh('git commit -m"Updated doc release: %s"' % tag)
print()
print('Most recent 3 commits:')
sys.stdout.flush()
finally:
cd(startdir)
print()
print('Now verify the build in: %r' % dest)
print("If everything looks good, 'git push'")