forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake.py
executable file
·312 lines (256 loc) · 10.1 KB
/
make.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env python
"""
Python script for building documentation.
To build the docs you must have all optional dependencies for pandas
installed. See the installation instructions for a list of these.
Usage
-----
$ python make.py clean
$ python make.py html
$ python make.py latex
"""
import sys
import os
import shutil
import subprocess
import argparse
from contextlib import contextmanager
import jinja2
import webbrowser
DOC_PATH = os.path.dirname(os.path.abspath(__file__))
SOURCE_PATH = os.path.join(DOC_PATH, 'source')
BUILD_PATH = os.path.join(DOC_PATH, 'build')
BUILD_DIRS = ['doctrees', 'html', 'latex', 'plots', '_static', '_templates']
def _generate_index(include_api=True, single_doc=None):
"""Create index.rst file with the specified sections.
Parameters
----------
include_api : bool
Whether API documentation will be built.
single_doc : str or None
If provided, this single documentation page will be generated.
"""
if single_doc is not None:
single_doc = os.path.splitext(os.path.basename(single_doc))[0]
include_api = False
with open(os.path.join(SOURCE_PATH, 'index.rst.template')) as f:
t = jinja2.Template(f.read())
with open(os.path.join(SOURCE_PATH, 'index.rst'), 'w') as f:
f.write(t.render(include_api=include_api,
single_doc=single_doc))
def _generate_exclude_pattern(include_api=True, single_doc=None):
if not include_api:
rst_files = ['api.rst', 'generated/*.rst']
elif single_doc is not None:
rst_files = [f for f in os.listdir(SOURCE_PATH)
if ((f.endswith('.rst') or f.endswith('.ipynb'))
and (f != 'index.rst') and (f != single_doc))]
rst_files += ['generated/*.rst']
else:
rst_files = []
exclude_patterns = ",".join(
['{!r}'.format(i) for i in ['**.ipynb_checkpoints'] + rst_files])
return exclude_patterns
def _write_temp_file(classtype, module, function):
s = """{1}.{2}
=================================
.. currentmodule:: {1}
.. auto{0}:: {2}""".format(classtype, module, function)
with open(os.path.join(SOURCE_PATH, "temp.rst"), 'w') as f:
f.write(s)
@contextmanager
def _maybe_exclude_notebooks():
"""Skip building the notebooks if pandoc is not installed.
This assumes that nbsphinx is installed.
Skip notebook conversion if:
1. nbconvert isn't installed, or
2. nbconvert is installed, but pandoc isn't
"""
base = os.path.dirname(__file__)
notebooks = [os.path.join(base, 'source', nb)
for nb in ['style.ipynb']]
contents = {}
def _remove_notebooks():
for nb in notebooks:
with open(nb, 'rt') as f:
contents[nb] = f.read()
os.remove(nb)
try:
import nbconvert
except ImportError:
sys.stderr.write('Warning: nbconvert not installed. '
'Skipping notebooks.\n')
_remove_notebooks()
else:
try:
nbconvert.utils.pandoc.get_pandoc_version()
except nbconvert.utils.pandoc.PandocMissing:
sys.stderr.write('Warning: Pandoc is not installed. '
'Skipping notebooks.\n')
_remove_notebooks()
yield
for nb, content in contents.items():
with open(nb, 'wt') as f:
f.write(content)
class DocBuilder:
"""Class to wrap the different commands of this script.
All public methods of this class can be called as parameters of the
script.
"""
def __init__(self, num_jobs=1, exclude_patterns=None):
self.num_jobs = num_jobs
self.exclude_patterns = exclude_patterns
@staticmethod
def _create_build_structure():
"""Create directories required to build documentation."""
for dirname in BUILD_DIRS:
try:
os.makedirs(os.path.join(BUILD_PATH, dirname))
except OSError:
pass
@staticmethod
def _run_os(*args):
"""Execute a command as a OS terminal.
Parameters
----------
*args : list of str
Command and parameters to be executed
Examples
--------
>>> DocBuilder()._run_os('python', '--version')
"""
subprocess.check_call(args, stderr=subprocess.STDOUT)
def _sphinx_build(self, kind):
"""Call sphinx to build documentation.
Attribute `num_jobs` from the class is used.
Parameters
----------
kind : {'html', 'latex'}
Examples
--------
>>> DocBuilder(num_jobs=4)._sphinx_build('html')
"""
if kind not in ('html', 'latex'):
raise ValueError('kind must be html or latex, not {}'.format(kind))
self._run_os('sphinx-build',
'-j{}'.format(self.num_jobs),
'-b{}'.format(kind),
'-d{}'.format(os.path.join(BUILD_PATH, 'doctrees')),
# TODO integrate exclude_patterns
SOURCE_PATH,
os.path.join(BUILD_PATH, kind))
def html(self):
"""Build HTML documentation."""
self._create_build_structure()
with _maybe_exclude_notebooks():
self._sphinx_build('html')
zip_fname = os.path.join(BUILD_PATH, 'html', 'pandas.zip')
if os.path.exists(zip_fname):
os.remove(zip_fname)
def latex(self, force=False):
"""Build PDF documentation."""
self._create_build_structure()
if sys.platform == 'win32':
sys.stderr.write('latex build has not been tested on windows\n')
else:
self._sphinx_build('latex')
os.chdir(os.path.join(BUILD_PATH, 'latex'))
if force:
for i in range(3):
self._run_os('pdflatex',
'-interaction=nonstopmode',
'pandas.tex')
raise SystemExit('You should check the file '
'"build/latex/pandas.pdf" for problems.')
else:
self._run_os('make')
def latex_forced(self):
"""Build PDF documentation with retries to find missing references."""
self.latex(force=True)
@staticmethod
def clean():
"""Clean documentation generated files."""
shutil.rmtree(BUILD_PATH, ignore_errors=True)
shutil.rmtree(os.path.join(SOURCE_PATH, 'generated'),
ignore_errors=True)
def zip_html(self):
"""Compress HTML documentation into a zip file."""
zip_fname = os.path.join(BUILD_PATH, 'html', 'pandas.zip')
if os.path.exists(zip_fname):
os.remove(zip_fname)
dirname = os.path.join(BUILD_PATH, 'html')
fnames = os.listdir(dirname)
os.chdir(dirname)
self._run_os('zip',
zip_fname,
'-r',
'-q',
*fnames)
def build_docstring(self):
"""Build single docstring page"""
self._create_build_structure()
args = ('sphinx-build',
'-bhtml',
'-d{}'.format(os.path.join(BUILD_PATH, 'doctrees')),
'-Dexclude_patterns={}'.format(self.exclude_patterns),
SOURCE_PATH,
os.path.join(BUILD_PATH, 'html'),
os.path.join(SOURCE_PATH, 'temp.rst')
)
# for some reason it does not work with run_os, but it does if I
# directly call the joined command
# self._run_os(*args)
os.system(" ".join(args))
def main():
cmds = [method for method in dir(DocBuilder) if not method.startswith('_')]
argparser = argparse.ArgumentParser(
description='pandas documentation builder',
epilog='Commands: {}'.format(','.join(cmds)))
argparser.add_argument('command',
nargs='?',
default='html',
help='command to run: {}'.format(', '.join(cmds)))
argparser.add_argument('--num-jobs',
type=int,
default=1,
help='number of jobs used by sphinx-build')
argparser.add_argument('--no-api',
default=False,
help='ommit api and autosummary',
action='store_true')
argparser.add_argument('--single',
metavar='FILENAME',
type=str,
default=None,
help=('filename of section to compile, '
'e.g. "indexing"'))
argparser.add_argument('--python-path',
type=str,
default=os.path.join(DOC_PATH, '..'),
help='path')
argparser.add_argument('--docstring',
metavar='FILENAME',
type=str,
default=None,
help=('method or function name to compile, '
'e.g. "DataFrame.join"'))
args = argparser.parse_args()
if args.command not in cmds:
raise ValueError('Unknown command {}. Available options: {}'.format(
args.command, ', '.join(cmds)))
os.environ['PYTHONPATH'] = args.python_path
if args.docstring is not None:
_write_temp_file('method', 'pandas', args.docstring)
exclude_patterns = _generate_exclude_pattern(single_doc='temp.rst')
_generate_index(single_doc='temp.rst')
DocBuilder(args.num_jobs, exclude_patterns).build_docstring()
url = "file://" + os.getcwd() + "/build/html/temp.html"
webbrowser.open(url, new=2)
os.remove('source/temp.rst')
else:
_generate_index(not args.no_api, args.single)
exclude_patterns = _generate_exclude_pattern(
not args.no_api, args.single)
getattr(DocBuilder(args.num_jobs, exclude_patterns), args.command)()
if __name__ == '__main__':
sys.exit(main())