| 1 |
|
| 2 |
"""Script to commit the doc build outputs into the github-pages repo. |
| 3 |
|
| 4 |
Use: |
| 5 |
|
| 6 |
gh-pages.py [tag] |
| 7 |
|
| 8 |
If no tag is given, the current output of 'git describe' is used. If given, |
| 9 |
that is how the resulting directory will be named. |
| 10 |
|
| 11 |
In practice, you should use either actual clean tags from a current build or |
| 12 |
something like 'current' as a stable URL for the most current version of the """ |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
import os |
| 18 |
import re |
| 19 |
import shutil |
| 20 |
import sys |
| 21 |
from os import chdir as cd |
| 22 |
from os.path import join as pjoin |
| 23 |
from subprocess import PIPE, CalledProcessError, Popen, check_call |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
pages_dir = 'gh-pages' |
| 30 |
html_dir = 'build/html' |
| 31 |
pdf_dir = 'build/latex' |
| 32 |
pdf_filename = 'scikit-rf.pdf' |
| 33 |
copy_pdf = True |
| 34 |
pages_repo = '[email protected]:scikit-rf/doc.git' |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
def sh(cmd): |
| 40 |
"""Execute command in a subshell, return status code.""" |
| 41 |
return check_call(cmd, shell=True) |
| 42 |
|
| 43 |
|
| 44 |
def sh2(cmd): |
| 45 |
"""Execute command in a subshell, return stdout. |
| 46 |
|
| 47 |
Stderr is unbuffered from the subshell.x""" |
| 48 |
p = Popen(cmd, stdout=PIPE, shell=True) |
| 49 |
out = p.communicate()[0] |
| 50 |
retcode = p.returncode |
| 51 |
if retcode: |
| 52 |
raise CalledProcessError(retcode, cmd) |
| 53 |
else: |
| 54 |
return out.rstrip() |
| 55 |
|
| 56 |
|
| 57 |
def sh3(cmd): |
| 58 |
"""Execute command in a subshell, return stdout, stderr |
| 59 |
|
| 60 |
If anything appears in stderr, print it out to sys.stderr""" |
| 61 |
p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True) |
| 62 |
out, err = p.communicate() |
| 63 |
retcode = p.returncode |
| 64 |
if retcode: |
| 65 |
raise CalledProcessError(retcode, cmd) |
| 66 |
else: |
| 67 |
return out.rstrip(), err.rstrip() |
| 68 |
|
| 69 |
|
| 70 |
def init_repo(path): |
| 71 |
"""clone the gh-pages repo if we haven't already.""" |
| 72 |
sh(f"git clone {pages_repo} {path}") |
| 73 |
here = os.getcwdu() |
| 74 |
cd(path) |
| 75 |
sh('git checkout gh-pages') |
| 76 |
cd(here) |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
if __name__ == '__main__': |
| 82 |
|
| 83 |
try: |
| 84 |
tag = sys.argv[1] |
| 85 |
except IndexError: |
| 86 |
try: |
| 87 |
tag = sh2('git describe --exact-match') |
| 88 |
except CalledProcessError: |
| 89 |
tag = "dev" |
| 90 |
|
| 91 |
startdir = os.getcwdu() |
| 92 |
if not os.path.exists(pages_dir): |
| 93 |
|
| 94 |
init_repo(pages_dir) |
| 95 |
else: |
| 96 |
|
| 97 |
cd(pages_dir) |
| 98 |
sh('git checkout gh-pages') |
| 99 |
sh('git pull') |
| 100 |
cd(startdir) |
| 101 |
|
| 102 |
dest = pjoin(pages_dir, tag) |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
if tag != 'dev': |
| 107 |
|
| 108 |
|
| 109 |
pass |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
shutil.rmtree(dest, ignore_errors=True) |
| 114 |
shutil.copytree(html_dir, dest) |
| 115 |
if copy_pdf: |
| 116 |
shutil.copy(pjoin(pdf_dir, pdf_filename), pjoin(dest, pdf_filename)) |
| 117 |
pass |
| 118 |
|
| 119 |
try: |
| 120 |
cd(pages_dir) |
| 121 |
status = sh2('git status | head -1') |
| 122 |
branch = re.match(r'\# On branch (.*)$', status).group(1) |
| 123 |
if branch != 'gh-pages': |
| 124 |
e = f'On {pages_dir}, git branch is {branch}, MUST be "gh-pages"' |
| 125 |
raise RuntimeError(e) |
| 126 |
|
| 127 |
sh(f'git add -A {tag}') |
| 128 |
sh(f'git commit -m"Updated doc release: {tag}"') |
| 129 |
print() |
| 130 |
print('Most recent 3 commits:') |
| 131 |
sys.stdout.flush() |
| 132 |
sh('git --no-pager log --oneline HEAD~3..') |
| 133 |
finally: |
| 134 |
cd(startdir) |
| 135 |
|
| 136 |
print() |
| 137 |
print(f'Now verify the build in: {dest!r}') |
| 138 |
print("If everything looks good, 'git push'") |