Debian Codegraph @

scripts/update-docs.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # coding: utf-8
3
4 # Copyright ⓒ 2016 Daniel Keep.
5 #
6 # Licensed under the MIT license (see LICENSE or <http://opensource.org
7 # /licenses/MIT>) or the Apache License, Version 2.0 (see LICENSE of
8 # <http://www.apache.org/licenses/LICENSE-2.0>), at your option. All
9 # files in the project carrying such notice may not be copied, modified,
10 # or distributed except according to those terms.
11
12 import distutils.dir_util
13 import os
14 import shutil
15 import sys
16 import tempfile
17 import time
18
19 from common import *
20
21 DOC_ARGS = '--no-deps'
22 DOC_FEATURES = ""
23 DOC_TARGET_BRANCH = 'gh-pages'
24 DOC_PKG_DIR = None
25 DOC_TOOLCHAIN = None
26 TEMP_CHECKOUT_PREFIX = 'gh-pages-checkout-'
27 TEMP_OUTPUT_PREFIX = 'gh-pages-generated-'
28
29 TRACE_UPDATE_DOCS = os.environ.get('TRACE_UPDATE_DOCS', '') != ''
30
31 def copytree(src, dst):
32 msg_trace('copytree(%r, %r)' % (src, dst))
33 distutils.dir_util.copy_tree(src=src, dst=dst)
34
35 def really_rmtree(path):
36 msg_trace('really_rmtree(%r)' % path)
37
38 WAIT_TIME_SECS = 1.0
39 MAX_TRIES = 10
40
41 def on_error(func, path, exc_info):
42 """
43 Error handler for ``shutil.rmtree``.
44
45 If the error is due to an access error (read only file)
46 it attempts to add write permission and then retries.
47
48 If the error is for another reason it re-raises the error.
49
50 Usage: ``shutil.rmtree(path, onerror=on_error)``
51
52 From <http://stackoverflow.com/a/2656405>_.
53 """
54 import stat
55 if not os.access(path, os.W_OK):
56 # Is the error an access error ?
57 os.chmod(path, stat.S_IWUSR)
58 func(path)
59 else:
60 raise
61
62 for _ in range(MAX_TRIES):
63 failed = True
64 try:
65 msg_trace('shutil.rmtree(%r)' % path)
66 shutil.rmtree(path, onerror=on_error)
67 failed = False
68 except WindowsError:
69 time.sleep(WAIT_TIME_SECS)
70 if not failed: return
71
72 msg('Warning: failed to remove directory %r' % path)
73
74 def init_doc_branch():
75 msg("Initialising %s branch" % DOC_TARGET_BRANCH)
76
77 dir = os.getcwd()
78 msg_trace('dir = %r' % dir)
79
80 tmp = tempfile.mkdtemp(prefix=TEMP_CHECKOUT_PREFIX)
81 msg_trace('tmp = %r' % tmp)
82
83 try:
84 msg("Cloning into a temporary directory...")
85 sh('git init -q "%s"' % tmp)
86 msg_trace('os.chdir(%r)' % tmp)
87 os.chdir(tmp)
88 sh('git checkout -q --orphan "%s"' % DOC_TARGET_BRANCH)
89 sh('git commit -qm "Initial commit." --allow-empty')
90 sh('git remote add origin "%s"' % dir)
91 sh('git push -q origin gh-pages')
92
93 finally:
94 msg('Cleaning up...')
95 msg_trace('os.chdir(%r)' % dir)
96 os.chdir(dir)
97 msg_trace('shutil.rmtree(%r)' % tmp)
98 really_rmtree(tmp)
99
100 msg('%s is ready. Continuing.' % DOC_TARGET_BRANCH)
101
102 def gen_doc_bare(tmp1, tmp2):
103 msg("Generating documentation...")
104 args = '%s --features="%s"' % (DOC_ARGS, DOC_FEATURES)
105 if DOC_TOOLCHAIN is not None:
106 toolchain = "%s run %s " % (RUSTUP, DOC_TOOLCHAIN)
107 else:
108 toolchain = ""
109 sh('%scargo doc %s' % (toolchain, args))
110 tmp1_target_doc = '%s/target/doc' % tmp1
111 msg_trace('shutil.move(%r, %r)' % (tmp1_target_doc, tmp2))
112 shutil.move(tmp1_target_doc, tmp2)
113
114 def gen_doc_pkg(tmp1, tmp2, doc_pkg):
115 if DOC_FEATURES != "":
116 msg("Error: doc packages don't support features.")
117 sys.exit(1)
118
119 import json
120 old_dir = os.getcwd()
121 msg("Generating documentation from doc package...")
122 msg_trace('doc_pkg = %r' % doc_pkg)
123 msg_trace('os.chdir(%r)' % doc_pkg)
124 try:
125 os.chdir(doc_pkg)
126 manifest_str = sh_eval('cargo read-manifest --manifest-path "%s"'
127 % os.path.join(doc_pkg, 'Cargo.toml'))
128 manifest = json.loads(manifest_str)
129 if DOC_TOOLCHAIN is not None:
130 toolchain = "%s run %s " % (RUSTUP, DOC_TOOLCHAIN)
131 else:
132 toolchain = ""
133 packages = " ".join("--package %s" % d['name'] for d in manifest['dependencies'])
134 sh('%scargo doc %s' % (toolchain, packages))
135 target_doc = os.path.join(doc_pkg, 'target/doc')
136 msg_trace('shutil.move(%r, %r)' % (target_doc, tmp2))
137 shutil.move(target_doc, tmp2)
138 finally:
139 msg_trace('os.chdir(%r)' % old_dir)
140 os.chdir(old_dir)
141
142 def main():
143 load_globals_from_metadata('update-docs', globals(),
144 {
145 'DOC_ARGS',
146 'DOC_FEATURES',
147 'DOC_TARGET_BRANCH',
148 'DOC_PKG_DIR',
149 'DOC_TOOLCHAIN',
150 'TEMP_CHECKOUT_PREFIX',
151 'TEMP_OUTPUT_PREFIX',
152 })
153
154 if sh_eval('git symbolic-ref --short HEAD') != u'master':
155 msg('Not on master; doing nothing.')
156 return 0
157
158 # Sanity check: does the doc branch exist at all?
159 branches = {b[2:].strip() for b in sh_eval('git branch', dont_strip=True).splitlines()}
160 msg_trace('branches = %r' % branches)
161 if DOC_TARGET_BRANCH not in branches:
162 init_doc_branch()
163
164 last_rev = sh_eval('git rev-parse HEAD')
165 last_msg = sh_eval('git log -1 --pretty=%B')
166 msg_trace('last_rev = %r' % last_rev)
167 msg_trace('last_msg = %r' % last_msg)
168
169 dir = os.getcwd()
170 msg_trace('dir = %r' % dir)
171
172 tmp1 = tempfile.mkdtemp(prefix=TEMP_CHECKOUT_PREFIX)
173 tmp2 = tempfile.mkdtemp(prefix=TEMP_OUTPUT_PREFIX)
174 msg_trace('tmp1 = %r' % tmp1)
175 msg_trace('tmp2 = %r' % tmp2)
176
177 try:
178 msg("Cloning into a temporary directory...")
179 sh('git clone -qb "%s" "%s" "%s"' % (DOC_TARGET_BRANCH, dir, tmp1))
180 msg_trace('os.chdir(%r)' % tmp1)
181 os.chdir(tmp1)
182 sh('git checkout -q master')
183
184 if DOC_PKG_DIR is not None:
185 gen_doc_pkg(tmp1, tmp2, os.path.abspath(DOC_PKG_DIR))
186 else:
187 gen_doc_bare(tmp1, tmp2)
188
189 msg('Updating %s...' % DOC_TARGET_BRANCH)
190 sh('git checkout -q "%s"' % DOC_TARGET_BRANCH)
191 sh('git clean -dfq')
192 tmp2_doc = '%s/doc' % tmp2
193
194 msg_trace('copytree(%r, %r)' % (tmp2_doc, './doc'))
195 copytree(tmp2_doc, './doc')
196
197 msg('Committing changes...')
198 sh('git add .')
199 sh('git commit --amend -m "Update docs for %s" -m "%s"' % (last_rev[:7], last_msg))
200
201 sh('git push -fqu origin "%s"' % DOC_TARGET_BRANCH)
202
203 finally:
204 msg('Cleaning up...')
205 msg_trace('os.chdir(%r)' % dir)
206 os.chdir(dir)
207 msg_trace('shutil.rmtree(%r)' % tmp2)
208 really_rmtree(tmp2)
209 msg_trace('shutil.rmtree(%r)' % tmp1)
210 really_rmtree(tmp1)
211
212 msg('Publishing...')
213 sh('git push -f origin "%s"' % DOC_TARGET_BRANCH)
214
215 msg('Done.')
216
217
218 if __name__ == '__main__':
219 sys.exit(main())