Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
numba
GitHub Repository: numba/llvmlite
Path: blob/main/docs/gh-pages.py
1154 views
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
"""Script to commit the doc build outputs into the github-pages repo.
4
5
Use:
6
7
gh-pages.py [tag]
8
9
If no tag is given, the current output of 'git describe' is used. If given,
10
that is how the resulting directory will be named.
11
12
In practice, you should use either actual clean tags from a current build or
13
something like 'current' as a stable URL for the most current version of the """
14
from __future__ import print_function, division, absolute_import
15
16
#-----------------------------------------------------------------------------
17
# Imports
18
#-----------------------------------------------------------------------------
19
import os
20
import re
21
import shutil
22
import sys
23
from os import chdir as cd
24
from os.path import join as pjoin
25
26
from subprocess import Popen, PIPE, CalledProcessError, check_call
27
28
#-----------------------------------------------------------------------------
29
# Globals
30
#-----------------------------------------------------------------------------
31
32
pages_dir = 'gh-pages'
33
html_dir = '_build/html'
34
pdf_dir = '_build/latex'
35
pages_repo = '[email protected]:numba/llvmlite-doc.git'
36
37
#-----------------------------------------------------------------------------
38
# Functions
39
#-----------------------------------------------------------------------------
40
def sub_environment():
41
"""Return an environment dict for executing subcommands in."""
42
env = os.environ.copy()
43
# Force untranslated messages for regex matching
44
env['LANG'] = 'C'
45
return env
46
47
48
def sh(cmd):
49
"""Execute command in a subshell, return status code."""
50
return check_call(cmd, shell=True, env=sub_environment())
51
52
53
def sh2(cmd):
54
"""Execute command in a subshell, return stdout.
55
56
Stderr is unbuffered from the subshell.x"""
57
p = Popen(cmd, stdout=PIPE, shell=True, env=sub_environment())
58
out = p.communicate()[0]
59
retcode = p.returncode
60
if retcode:
61
raise CalledProcessError(retcode, cmd)
62
else:
63
return out.rstrip()
64
65
66
def sh3(cmd):
67
"""Execute command in a subshell, return stdout, stderr
68
69
If anything appears in stderr, print it out to sys.stderr"""
70
p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True,
71
env=sub_environment())
72
out, err = p.communicate()
73
retcode = p.returncode
74
if retcode:
75
raise CalledProcessError(retcode, cmd)
76
else:
77
return out.rstrip(), err.rstrip()
78
79
80
def init_repo(path):
81
"""clone the gh-pages repo if we haven't already."""
82
sh("git clone %s %s"%(pages_repo, path))
83
here = os.getcwd()
84
cd(path)
85
sh('git checkout gh-pages')
86
cd(here)
87
88
#-----------------------------------------------------------------------------
89
# Script starts
90
#-----------------------------------------------------------------------------
91
if __name__ == '__main__':
92
# The tag can be given as a positional argument
93
try:
94
tag = sys.argv[1]
95
except IndexError:
96
try:
97
tag = sh2('git describe --exact-match').decode()
98
except CalledProcessError:
99
tag = "dev" # Fallback
100
print("Using dev")
101
102
startdir = os.getcwd()
103
if not os.path.exists(pages_dir):
104
# init the repo
105
init_repo(pages_dir)
106
else:
107
# ensure up-to-date before operating
108
cd(pages_dir)
109
sh('git checkout gh-pages')
110
sh('git pull')
111
cd(startdir)
112
113
dest = pjoin(pages_dir, tag)
114
115
# don't `make html` here, because gh-pages already depends on html in Makefile
116
# sh('make html')
117
if tag != 'dev':
118
# only build pdf for non-dev targets
119
#sh2('make pdf')
120
pass
121
122
# This is pretty unforgiving: we unconditionally nuke the destination
123
# directory, and then copy the html tree in there
124
shutil.rmtree(dest, ignore_errors=True)
125
shutil.copytree(html_dir, dest)
126
if tag != 'dev':
127
#shutil.copy(pjoin(pdf_dir, 'ipython.pdf'), pjoin(dest, 'ipython.pdf'))
128
pass
129
130
try:
131
cd(pages_dir)
132
status = sh2('git status | head -1').decode()
133
branch = re.match('\#?\s*On branch (.*)$', status).group(1)
134
if branch != 'gh-pages':
135
e = 'On %r, git branch is %r, MUST be "gh-pages"' % (pages_dir,
136
branch)
137
raise RuntimeError(e)
138
139
sh('git add -A %s' % tag)
140
sh('git commit -m"Updated doc release: %s"' % tag)
141
print()
142
print('Most recent 3 commits:')
143
sys.stdout.flush()
144
#sh('git --no-pager log --oneline HEAD~3..')
145
finally:
146
cd(startdir)
147
148
print()
149
print('Now verify the build in: %r' % dest)
150
print("If everything looks good, 'git push'")
151
152