Skip to content

Commit 68f5d02

Browse files
committed
Add distutils command for building Sphinx.
1 parent 6c2f991 commit 68f5d02

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed

CHANGES

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Release 0.5 (in development)
44
New features added
55
------------------
66

7+
* Added a distutils command `build_sphinx`: When Sphinx is installed,
8+
you can call ``python setup.py build_sphinx`` for projects that
9+
have Sphinx documentation, which will build the docs and place them
10+
in the standard distutils build directory.
11+
712
* `SerializingHTMLBuilder` was added as new abstract builder that can
813
be subclassed to serialize build HTML in a specific format. The
914
`PickleHTMLBuilder` is a concrete subclass of it that uses pickle as

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@
8484
'console_scripts': [
8585
'sphinx-build = sphinx:main',
8686
'sphinx-quickstart = sphinx.quickstart:main'
87-
]
87+
],
88+
'distutils.commands': [
89+
'build_sphinx = sphinx.setup_command:BuildDoc',
90+
],
8891
},
8992
install_requires=requires,
9093
)

sphinx/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ def main(argv=sys.argv):
134134
else:
135135
app.builder.build_update()
136136
except KeyboardInterrupt:
137-
# catches BaseExceptions in 2.5 -- SystemExit, KeyboardInterrupt
138137
if use_pdb:
139138
import pdb
140139
print >>sys.stderr, darkred('Interrupted while building, starting debugger:')

sphinx/setup_command.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
sphinx.setup_command
4+
~~~~~~~~~~~~~~~~~~~~
5+
6+
Setuptools/distutils commands to assist the building of sphinx
7+
documentation.
8+
9+
:author: Sebastian Wiesner
10+
:contact: basti.wiesner@gmx.net
11+
:copyright: 2008 by Sebastian Wiesner.
12+
:license: MIT.
13+
"""
14+
15+
import sys
16+
import os
17+
from StringIO import StringIO
18+
from distutils.cmd import Command
19+
20+
from sphinx.application import Sphinx
21+
from sphinx.util.console import darkred, nocolor
22+
23+
24+
class BuildDoc(Command):
25+
"""Distutils command to build Sphinx documentation."""
26+
27+
description = 'Build Sphinx documentation'
28+
user_options = [
29+
('fresh-env', 'E', 'discard saved environment'),
30+
('all-files', 'a', 'build all files'),
31+
('source-dir=', 's', 'Source directory'),
32+
('build-dir=', None, 'Build directory'),
33+
('builder=', 'b', 'The builder to use. Defaults to "html"'),
34+
]
35+
boolean_options = ['fresh-env', 'all-files']
36+
37+
38+
def initialize_options(self):
39+
self.fresh_env = self.all_files = False
40+
self.source_dir = self.build_dir = None
41+
self.conf_file_name = 'conf.py'
42+
self.builder = 'html'
43+
44+
def finalize_options(self):
45+
if self.source_dir is None:
46+
if os.path.isdir('doc'):
47+
for root, dirnames, filenames in os.walk('doc'):
48+
if 'conf.py' in filenames:
49+
self.source_dir = root
50+
self.announce('Using source directory %s' % root)
51+
break
52+
self.ensure_dirname('source_dir')
53+
self.source_dir = os.path.abspath(self.source_dir)
54+
55+
if self.build_dir is None:
56+
build = self.get_finalized_command('build')
57+
self.build_dir = os.path.join(build.build_base, 'sphinx')
58+
self.mkpath(self.build_dir)
59+
self.ensure_dirname('build_dir')
60+
self.doctree_dir = os.path.join(self.build_dir, 'doctrees')
61+
self.mkpath(self.doctree_dir)
62+
self.builder_target_dir = os.path.join(self.build_dir, self.builder)
63+
self.mkpath(self.builder_target_dir)
64+
65+
def run(self):
66+
if not sys.stdout.isatty() or sys.platform == 'win32':
67+
# Windows' poor cmd box doesn't understand ANSI sequences
68+
nocolor()
69+
if not self.verbose:
70+
status_stream = StringIO()
71+
else:
72+
status_stream = sys.stdout
73+
app = Sphinx(self.source_dir, self.source_dir,
74+
self.builder_target_dir, self.doctree_dir,
75+
self.builder, {}, status_stream,
76+
freshenv=self.fresh_env)
77+
78+
try:
79+
if self.all_files:
80+
app.builder.build_all()
81+
else:
82+
app.builder.build_update()
83+
except Exception, err:
84+
if isinstance(err, SystemMessage):
85+
sys.stderr, darkred('reST markup error:')
86+
print >>sys.stderr, err.args[0].encode('ascii', 'backslashreplace')
87+
else:
88+
raise

0 commit comments

Comments
 (0)