-
Notifications
You must be signed in to change notification settings - Fork 19
/
push_documentation.py
executable file
·48 lines (36 loc) · 1.67 KB
/
push_documentation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3
from subprocess import run, CalledProcessError
from tempfile import TemporaryDirectory
from os.path import abspath, dirname, isdir, join
from os import listdir, remove
from shutil import copytree, rmtree
import sys
if __name__ == '__main__':
if len(sys.argv) > 1:
remote = sys.argv[1]
else:
remote = input('Name of remote to push documentation to: ')
src = abspath(dirname(__file__))
# Ensure that local branch gh-pages exists
try:
run(['git', 'rev-parse', 'gh-pages'], cwd=src, check=True)
except CalledProcessError:
run(['git', 'branch', 'gh-pages', '{}/gh-pages'.format(remote)], check=True)
# Ensure that local branch gh-pages is updated
run(['git', 'fetch', remote, 'gh-pages:gh-pages'], check=True)
with TemporaryDirectory() as tgt:
# Clone to a temporary directory and check out the gh-pages branch
run(['git', 'clone', src, tgt], check=True)
run(['git', 'checkout', 'gh-pages'], cwd=tgt, check=True)
# Copy files from doc/_build/html over
build = join(src, 'doc', '_build', 'html')
for c in listdir(build):
run(['cp', '-R', join(build, c), tgt])
# Add them all, show git status and commit
run(['git', 'add', '-A'], cwd=tgt, check=True)
run(['git', 'status'], cwd=tgt, check=True)
run(['git', 'commit', '--allow-empty', '-m', 'Update documentation'], cwd=tgt, check=True)
# Push to local repository from the temp repository
run(['git', 'push', 'origin', 'gh-pages'], cwd=tgt, check=True)
# Push to the remote
run(['git', 'push', remote, 'gh-pages'], cwd=src, check=True)