| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
1 |
#!/usr/bin/env python |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
2 |
# Build the documentation in CI. |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
3 |
|
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
4 |
from __future__ import print_function |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
5 |
import errno, os, shutil, subprocess, sys, urllib |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
6 |
from subprocess import call, check_call, Popen, PIPE, STDOUT |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
7 |
|
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
8 |
def rmtree_if_exists(dir): |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
9 |
try: |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
10 |
shutil.rmtree(dir) |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
11 |
except OSError as e: |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
12 |
if e.errno == errno.ENOENT: |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
13 |
pass |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
14 |
|
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
15 |
# Build the docs. |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
16 |
fmt_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
17 |
sys.path.insert(0, os.path.join(fmt_dir, 'doc')) |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
18 |
import build |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
19 |
build.create_build_env() |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
20 |
html_dir = build.build_docs() |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
21 |
|
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
22 |
repo = 'fmtlib.github.io' |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
23 |
branch = os.environ['GITHUB_REF'] |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
24 |
is_ci = 'CI' in os.environ |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
25 |
if is_ci and branch != 'refs/heads/master': |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
26 |
print('Branch: ' + branch) |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
27 |
exit(0) # Ignore non-master branches |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
28 |
if is_ci and 'KEY' not in os.environ: |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
29 |
# Don't update the repo if building in CI from an account that doesn't have |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
30 |
# push access. |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
31 |
print('Skipping update of ' + repo) |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
32 |
exit(0) |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
33 |
|
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
34 |
# Clone the fmtlib.github.io repo. |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
35 |
rmtree_if_exists(repo) |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
36 |
git_url = 'https://github.com/' if is_ci else 'git@github.com:' |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
37 |
check_call(['git', 'clone', git_url + 'fmtlib/{}.git'.format(repo)]) |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
38 |
|
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
39 |
# Copy docs to the repo. |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
40 |
target_dir = os.path.join(repo, 'dev') |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
41 |
rmtree_if_exists(target_dir) |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
42 |
shutil.copytree(html_dir, target_dir, ignore=shutil.ignore_patterns('.*')) |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
43 |
if is_ci: |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
44 |
check_call(['git', 'config', '--global', 'user.name', 'fmtbot']) |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
45 |
check_call(['git', 'config', '--global', 'user.email', 'viz@fmt.dev']) |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
46 |
|
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
47 |
# Push docs to GitHub pages. |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
48 |
check_call(['git', 'add', '--all'], cwd=repo) |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
49 |
if call(['git', 'diff-index', '--quiet', 'HEAD'], cwd=repo): |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
50 |
check_call(['git', 'commit', '-m', 'Update documentation'], cwd=repo) |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
51 |
cmd = 'git push' |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
52 |
if is_ci: |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
53 |
cmd += ' https://$KEY@github.com/fmtlib/fmtlib.github.io.git master' |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
54 |
p = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT, cwd=repo) |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
55 |
# Print the output without the key. |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
56 |
print(p.communicate()[0].decode('utf-8').replace(os.environ['KEY'], '$KEY')) |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
57 |
if p.returncode != 0: |
| f8deb5b9 |
Poul Sander |
2022-01-15 13:55 |
58 |
raise subprocess.CalledProcessError(p.returncode, cmd) |
|
|
1970-01-01 00:00 |
59 |
|