summaryrefslogtreecommitdiff
path: root/sandbox/py-rest-doc/sphinx/__init__.py
blob: b75f76e45525441a1ce485d212328e5f5069cdda (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
# -*- coding: utf-8 -*-
"""
    Sphinx
    ~~~~~~

    The Python documentation toolchain.

    :copyright: 2007 by Georg Brandl.
    :license: Python license.
"""

import sys
import getopt
from os import path

from .builder import builders
from .console import nocolor

__version__ = '$Revision$'


def usage(argv, msg=None):
    if msg:
        print >>sys.stderr, msg
        print >>sys.stderr
    print >>sys.stderr, """\
usage: %s [options] sourcedir outdir [filenames...]"
options: -b <builder> -- builder to use (one of %s)
         -a -- write all files; default is to only write new and changed files
         -O <option[=value]> -- give option to to the builder (-O help for list)
         -D <setting=value> -- override a setting in sourcedir/conf.py
         -N -- do not do colored output
modi:
* without -a and without filenames, write new and changed files.
* with -a, write all files.
* with filenames, write these.""" % (argv[0], ', '.join(builders))


def main(argv):
    try:
        opts, args = getopt.getopt(argv[1:], 'ab:O:D:N')
        srcdirname = path.abspath(args[0])
        if not path.isdir(srcdirname):
            print >>sys.stderr, 'Error: Cannot find source directory.'
            return 1
        if not path.isfile(path.join(srcdirname, 'conf.py')):
            print >>sys.stderr, 'Error: Source directory doesn\'t contain conf.py file.'
            return 1
        outdirname = path.abspath(args[1])
        if not path.isdir(outdirname):
            print >>sys.stderr, 'Error: Cannot find output directory.'
            return 1
    except (IndexError, getopt.error):
        usage(argv)
        return 1

    filenames = args[2:]
    err = 0
    for filename in filenames:
        if not path.isfile(filename):
            print >>sys.stderr, 'Cannot find file %r.' % filename
            err = 1
    if err:
        return 1

    builder = all_files = None
    opt_help = False
    options = {}
    confoverrides = {}
    for opt, val in opts:
        if opt == '-b':
            if val not in builders:
                usage(argv, 'Invalid builder value specified.')
                return 1
            builder = val
        elif opt == '-a':
            if filenames:
                usage(argv, 'Cannot combine -a option and filenames.')
                return 1
            all_files = True
        elif opt == '-O':
            if val == 'help':
                opt_help = True
                continue
            if '=' in val:
                key, val = val.split('=')
                try:
                    val = int(val)
                except: pass
            else:
                key, val = val, True
            options[key] = val
        elif opt == '-D':
            key, val = val.split('=')
            try:
                val = int(val)
            except: pass
            confoverrides[key] = val
        elif opt == '-N':
            nocolor()

    if builder is None:
        print 'No builder selected, using default: html'
        builder = 'html'

    builderobj = builders[builder]

    if opt_help:
        print 'Options recognized by the %s builder:' % builder
        for optname, description in builderobj.option_spec.iteritems():
            print ' * %s: %s' % (optname, description)
        return 0

    builderobj = builderobj(srcdirname, outdirname, options,
                            status_stream=sys.stdout,
                            warning_stream=sys.stderr,
                            confoverrides=confoverrides)
    if all_files:
        builderobj.build_all()
    elif filenames:
        builderobj.build_specific(filenames)
    else:
        builderobj.build_update()


if __name__ == '__main__':
    sys.exit(main(sys.argv))