summaryrefslogtreecommitdiff
path: root/sphinx/make_mode.py
blob: 7b5e75aae23cb52ab38714fd0e282c8b26871664 (plain)
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
# -*- coding: utf-8 -*-
"""
    sphinx.make_mode
    ~~~~~~~~~~~~~~~~

    sphinx-build -M command-line handling.

    This replaces the old, platform-dependent and once-generated content
    of Makefile / make.bat.

    This is in its own module so that importing it is fast.  It should not
    import the main Sphinx modules (like sphinx.applications, sphinx.builders).

    :copyright: Copyright 2007-2014 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""
from __future__ import print_function

import os
import sys
import shutil
from os import path
from subprocess import call

import sphinx
from sphinx.util.console import bold, blue
from sphinx.util.osutil import cd

proj_name = os.getenv('SPHINXPROJ', '<project>')


BUILDERS = [
    ("",      "html",      "to make standalone HTML files"),
    ("",      "dirhtml",   "to make HTML files named index.html in directories"),
    ("",      "singlehtml","to make a single large HTML file"),
    ("",      "pickle",    "to make pickle files"),
    ("",      "json",      "to make JSON files"),
    ("",      "htmlhelp",  "to make HTML files and a HTML help project"),
    ("",      "qthelp",    "to make HTML files and a qthelp project"),
    ("",      "devhelp",   "to make HTML files and a Devhelp project"),
    ("",      "epub",      "to make an epub"),
    ("",      "latex",     "to make LaTeX files, you can set PAPER=a4 or PAPER=letter"),
    ("posix", "latexpdf",  "to make LaTeX files and run them through pdflatex"),
    ("posix", "latexpdfja","to make LaTeX files and run them through platex/dvipdfmx"),
    ("",      "text",      "to make text files"),
    ("",      "man",       "to make manual pages"),
    ("",      "texinfo",   "to make Texinfo files"),
    ("posix", "info",      "to make Texinfo files and run them through makeinfo"),
    ("",      "gettext",   "to make PO message catalogs"),
    ("",      "changes",   "to make an overview of all changed/added/deprecated items"),
    ("",      "xml",       "to make Docutils-native XML files"),
    ("",      "pseudoxml", "to make pseudoxml-XML files for display purposes"),
    ("",      "linkcheck", "to check all external links for integrity"),
    ("",      "doctest",   "to run all doctests embedded in the documentation "
                           "(if enabled)"),
    ("",      "coverage",  "to run coverage check of the documentation (if enabled)"),
]


class Make(object):

    def __init__(self, srcdir, builddir, opts):
        self.srcdir = srcdir
        self.builddir = builddir
        self.opts = opts

    def builddir_join(self, *comps):
        return path.join(self.builddir, *comps)

    def build_clean(self):
        if not path.exists(self.builddir):
            return
        elif not path.isdir(self.builddir):
            print("Error: %r is not a directory!" % self.builddir)
            return 1
        print("Removing everything under %r..." % self.builddir)
        for item in os.listdir(self.builddir):
            shutil.rmtree(self.builddir_join(item))

    def build_help(self):
        print(bold("Sphinx v%s" % sphinx.__version__))
        print("Please use `make %s' where %s is one of" % ((blue('target'),)*2))
        for osname, bname, description in BUILDERS:
            if not osname or os.name == osname:
                print('  %s  %s' % (blue(bname.ljust(10)), description))

    def build_html(self):
        if self.run_generic_build('html') > 0:
            return 1
        print()
        print('Build finished. The HTML pages are in %s.' % self.builddir_join('html'))

    def build_dirhtml(self):
        if self.run_generic_build('dirhtml') > 0:
            return 1
        print()
        print('Build finished. The HTML pages are in %s.' %
              self.builddir_join('dirhtml'))

    def build_singlehtml(self):
        if self.run_generic_build('singlehtml') > 0:
            return 1
        print()
        print('Build finished. The HTML page is in %s.' %
              self.builddir_join('singlehtml'))

    def build_pickle(self):
        if self.run_generic_build('pickle') > 0:
            return 1
        print()
        print('Build finished; now you can process the pickle files.')

    def build_json(self):
        if self.run_generic_build('json') > 0:
            return 1
        print()
        print('Build finished; now you can process the JSON files.')

    def build_htmlhelp(self):
        if self.run_generic_build('htmlhelp') > 0:
            return 1
        print()
        print('Build finished; now you can run HTML Help Workshop with the '
              '.hhp project file in %s.' % self.builddir_join('htmlhelp'))

    def build_qthelp(self):
        if self.run_generic_build('qthelp') > 0:
            return 1
        print()
        print('Build finished; now you can run "qcollectiongenerator" with the '
              '.qhcp project file in %s, like this:' % self.builddir_join('qthelp'))
        print('$ qcollectiongenerator %s.qhcp' % self.builddir_join('qthelp', proj_name))
        print('To view the help file:')
        print('$ assistant -collectionFile %s.qhc' %
              self.builddir_join('qthelp', proj_name))

    def build_devhelp(self):
        if self.run_generic_build('devhelp') > 0:
            return 1
        print()
        print("Build finished.")
        print("To view the help file:")
        print("$ mkdir -p $HOME/.local/share/devhelp/" + proj_name)
        print("$ ln -s %s $HOME/.local/share/devhelp/%s" %
              (self.builddir_join('devhelp'), proj_name))
        print("$ devhelp")

    def build_epub(self):
        if self.run_generic_build('epub') > 0:
            return 1
        print()
        print('Build finished. The ePub file is in %s.' % self.builddir_join('epub'))

    def build_latex(self):
        if self.run_generic_build('latex') > 0:
            return 1
        print("Build finished; the LaTeX files are in %s." % self.builddir_join('latex'))
        if os.name == 'posix':
            print("Run `make' in that directory to run these through (pdf)latex")
            print("(use `make latexpdf' here to do that automatically).")

    def build_latexpdf(self):
        if self.run_generic_build('latex') > 0:
            return 1
        with cd(self.builddir_join('latex')):
            os.system('make all-pdf')

    def build_latexpdfja(self):
        if self.run_generic_build('latex') > 0:
            return 1
        with cd(self.builddir_join('latex')):
            os.system('make all-pdf-ja')

    def build_text(self):
        if self.run_generic_build('text') > 0:
            return 1
        print()
        print('Build finished. The text files are in %s.' % self.builddir_join('text'))

    def build_texinfo(self):
        if self.run_generic_build('texinfo') > 0:
            return 1
        print("Build finished; the Texinfo files are in %s." %
              self.builddir_join('texinfo'))
        if os.name == 'posix':
            print("Run `make' in that directory to run these through makeinfo")
            print("(use `make info' here to do that automatically).")

    def build_info(self):
        if self.run_generic_build('texinfo') > 0:
            return 1
        with cd(self.builddir_join('texinfo')):
            os.system('make info')

    def build_gettext(self):
        dtdir = self.builddir_join('gettext', '.doctrees')
        if self.run_generic_build('gettext', doctreedir=dtdir) > 0:
            return 1
        print()
        print('Build finished. The message catalogs are in %s.' %
              self.builddir_join('gettext'))

    def build_changes(self):
        if self.run_generic_build('changes') > 0:
            return 1
        print()
        print('Build finished. The overview file is in %s.' %
              self.builddir_join('changes'))

    def build_linkcheck(self):
        res = self.run_generic_build('linkcheck')
        print()
        print('Link check complete; look for any errors in the above output '
              'or in %s.' % self.builddir_join('linkcheck', 'output.txt'))
        return res

    def build_doctest(self):
        res = self.run_generic_build('doctest')
        print("Testing of doctests in the sources finished, look at the "
              "results in %s." % self.builddir_join('doctest', 'output.txt'))
        return res

    def build_coverage(self):
        if self.run_generic_build('coverage') > 0:
            print("Has the coverage extension been enabled?")
            return 1
        print()
        print("Testing of coverage in the sources finished, look at the "
              "results in %s." % self.builddir_join('coverage'))

    def build_xml(self):
        if self.run_generic_build('xml') > 0:
            return 1
        print()
        print('Build finished. The XML files are in %s.' % self.builddir_join('xml'))

    def build_pseudoxml(self):
        if self.run_generic_build('pseudoxml') > 0:
            return 1
        print()
        print('Build finished. The pseudo-XML files are in %s.' %
              self.builddir_join('pseudoxml'))

    def run_generic_build(self, builder, doctreedir=None):
        # compatibility with old Makefile
        papersize = os.getenv('PAPER', '')
        opts = self.opts
        if papersize in ('a4', 'letter'):
            opts.extend(['-D', 'latex_paper_size=' + papersize])
        if doctreedir is None:
            doctreedir = self.builddir_join('doctrees')

        orig_cmd = sys.argv[0]
        if orig_cmd.endswith('.exe'):
            cmd = [orig_cmd]
        elif sys.platform == 'win32':
            cmd = [orig_cmd + '.exe']
        else:  # ex. 'sphinx-build' or 'sphinx-build.py'
            cmd = [sys.executable, orig_cmd]

        return call(cmd + ['-b', builder] + opts +
                    ['-d', doctreedir, self.srcdir, self.builddir_join(builder)])


def run_make_mode(args):
    if len(args) < 3:
        print('Error: at least 3 arguments (builder, source '
              'dir, build dir) are required.', file=sys.stderr)
        return 1
    make = Make(args[1], args[2], args[3:])
    run_method = 'build_' + args[0]
    if hasattr(make, run_method):
        return getattr(make, run_method)()
    return make.run_generic_build(args[0])