##// END OF EJS Templates
Make building docs robust against 'git describe' failing to find a tag name: falls back to 'dev'.
Make building docs robust against 'git describe' failing to find a tag name: falls back to 'dev'.

File last commit:

r3320:96cd6eae
r3320:96cd6eae
Show More
gh-pages.py
168 lines | 4.9 KiB | text/x-python | PythonLexer
MinRK
replace in-repo gh-pages with external ipython-doc, as in datarray
r3311 #!/usr/bin/env python
"""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 """
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
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
#-----------------------------------------------------------------------------
# Globals
#-----------------------------------------------------------------------------
pages_dir = 'gh-pages'
html_dir = 'build/html'
MinRK
gh-pages includes pdf...
r3315 pdf_dir = 'build/latex'
MinRK
replace in-repo gh-pages with external ipython-doc, as in datarray
r3311 pages_repo = 'git@github.com:ipython/ipython-doc.git'
#-----------------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------------
def sh(cmd):
"""Execute command in a subshell, return status code."""
return check_call(cmd, shell=True)
def sh2(cmd):
"""Execute command in a subshell, return stdout.
Stderr is unbuffered from the subshell.x"""
p = Popen(cmd, stdout=PIPE, shell=True)
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)
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)
MinRK
gh-pages uses reST
r3316 def render_rstindex(fname, tag, desc=None):
MinRK
gh-pages includes pdf...
r3315 if desc is None:
desc = tag
MinRK
gh-pages uses reST
r3316 rel = '* {d}: `HTML <{t}/index.html>`_ and `PDF <{t}/ipython.pdf>`_.'.format(t=tag,d=desc)
rep = re.compile(r'\.\. release')
MinRK
replace in-repo gh-pages with external ipython-doc, as in datarray
r3311 out = []
with file(fname) as f:
MinRK
gh-pages includes pdf...
r3315 contents = f.read()
lines = contents.splitlines()
if rel in contents:
out = lines
else:
for line in lines:
MinRK
replace in-repo gh-pages with external ipython-doc, as in datarray
r3311 out.append(line)
if rep.search(line):
out.append(rep.sub(rel, line))
MinRK
gh-pages includes pdf...
r3315 return '\n'.join(out)+'\n'
MinRK
replace in-repo gh-pages with external ipython-doc, as in datarray
r3311
MinRK
gh-pages uses reST
r3316 def new_rstindex(fname, tag, desc=None):
new_page = render_rstindex(fname, tag, desc)
MinRK
replace in-repo gh-pages with external ipython-doc, as in datarray
r3311 os.rename(fname, fname+'~')
with file(fname, 'w') as f:
f.write(new_page)
#-----------------------------------------------------------------------------
# Script starts
#-----------------------------------------------------------------------------
if __name__ == '__main__':
# The tag can be given as a positional argument
try:
tag = sys.argv[1]
except IndexError:
Thomas Kluyver
Make building docs robust against 'git describe' failing to find a tag name: falls back to 'dev'.
r3320 try:
tag = sh2('git describe')
except CalledProcessError:
tag = "dev" # Fallback
MinRK
gh-pages includes pdf...
r3315
try:
desc = sys.argv[2]
except IndexError:
desc="Release (%s)"%tag
MinRK
replace in-repo gh-pages with external ipython-doc, as in datarray
r3311 startdir = os.getcwd()
if not os.path.exists(pages_dir):
MinRK
gh-pages uses reST
r3316 # init the repo
MinRK
replace in-repo gh-pages with external ipython-doc, as in datarray
r3311 init_repo(pages_dir)
MinRK
gh-pages uses reST
r3316 else:
# ensure up-to-date before operating
cd(pages_dir)
sh('git checkout gh-pages')
sh('git pull')
cd(startdir)
MinRK
replace in-repo gh-pages with external ipython-doc, as in datarray
r3311
dest = pjoin(pages_dir, tag)
# don't `make html` here, because gh-pages already depends on html in Makefile
# sh('make html')
# This is pretty unforgiving: we unconditionally nuke the destination
# directory, and then copy the html tree in there
shutil.rmtree(dest, ignore_errors=True)
shutil.copytree(html_dir, dest)
MinRK
gh-pages includes pdf...
r3315 shutil.copy(pjoin(pdf_dir, 'ipython.pdf'), pjoin(dest, 'ipython.pdf'))
MinRK
replace in-repo gh-pages with external ipython-doc, as in datarray
r3311
try:
cd(pages_dir)
status = sh2('git status | head -1')
branch = re.match('\# 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 %s' % tag)
MinRK
gh-pages uses reST
r3316 new_rstindex('index.rst', tag, desc)
sh('python build_index.py')
sh('git add index.rst index.html')
MinRK
replace in-repo gh-pages with external ipython-doc, as in datarray
r3311 sh('git commit -m"Created new doc release, named: %s"' % tag)
print
print 'Most recent 3 commits:'
sys.stdout.flush()
sh('git --no-pager log --oneline HEAD~3..')
finally:
cd(startdir)
print
print 'Now verify the build in: %r' % dest
print "If everything looks good, 'git push'"