~launchpad-pqm/launchpad/devel

10637.3.1 by Guilherme Salgado
Use the default python version instead of a hard-coded version
1
#!/usr/bin/python -S
6326.8.21 by Gavin Panella
Comment in the script.
2
#
8687.15.2 by Karl Fogel
In files modified by r8688, change "<YEARS>" to "2009", as per
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
8687.15.3 by Karl Fogel
Shorten the copyright header block to two lines.
4
# GNU Affero General Public License version 3 (see the file LICENSE).
8452.3.3 by Karl Fogel
* utilities/: Add copyright header block to source files that were
5
#
6326.8.21 by Gavin Panella
Comment in the script.
6
# Generates documentation of mapping from remote bug tracker statuses
7
# to local, Launchpad statuses. Typically, this should be used to
8
# update the online documentation each time the status mappings are
9
# updated.
10
#
11
# See https://help.launchpad.net/BugStatuses
12
#
6326.8.19 by Gavin Panella
Add generation script.
13
7231.2.11 by Gavin Panella
Add XXX to explain why utilities/generate-external-bug-status-docs is not tested.
14
# XXX: GavinPanella 2008-12-03 bug=235434: This script is a temporary
15
# measure before moving the documentation of external bug status
16
# mapping tables into Launchpad proper. It is not tested and will
17
# remain so because it would be a mostly wasted effort. The impact of
18
# this is very low because it is infrequently run, and typically only
19
# by me, so I get to pick up the pieces.
20
6326.8.19 by Gavin Panella
Add generation script.
21
import _pythonpath
22
6326.8.55 by Gavin Panella
New version of the utilities script, using generators and conforming to command line usage standards.
23
import codecs
24
import sys
25
6326.8.22 by Gavin Panella
Add a header to the docs.
26
from datetime import datetime
6326.8.55 by Gavin Panella
New version of the utilities script, using generators and conforming to command line usage standards.
27
from itertools import chain
28
from optparse import OptionParser
6326.8.22 by Gavin Panella
Add a header to the docs.
29
11892.2.2 by Gavin Panella
Fix import.
30
from lp.bugs.externalbugtracker import BUG_TRACKER_CLASSES
6326.8.19 by Gavin Panella
Add generation script.
31
32
6326.8.55 by Gavin Panella
New version of the utilities script, using generators and conforming to command line usage standards.
33
def generate_blank_lines(num):
34
    """Generate `num` blank lines."""
35
    for i in range(num):
36
        yield ''
37
38
39
def generate_page_header():
40
    """Generate the header for the page."""
7231.2.2 by Gavin Panella
Use new MoinMoin anchor syntax.
41
    yield '<<Anchor(StatusTables)>>'
6326.8.55 by Gavin Panella
New version of the utilities script, using generators and conforming to command line usage standards.
42
    yield '== Status mapping tables =='
43
    yield ''
44
    yield 'Last generated: %s.' % (
45
        datetime.utcnow().strftime('%Y-%m-%d %H:%M UTC'),)
46
47
48
def generate_table_header(typ):
49
    """Generate a header for an individual table.
50
51
    :param typ: A member of `BugTrackerType`.
52
    """
7231.2.2 by Gavin Panella
Use new MoinMoin anchor syntax.
53
    yield '<<Anchor(%s)>>' % (typ.name,)
6326.8.55 by Gavin Panella
New version of the utilities script, using generators and conforming to command line usage standards.
54
    yield '=== %s ===' % (typ.title,)
55
56
57
def generate_table_grid(lookup, titles):
58
    """Simply return the table grid generated by the lookup tree."""
59
    return lookup.moinmoin_table(titles)
60
61
62
def generate_table(typ, cls):
63
    """Generate a table documenting the specified external bug tracker.
64
65
    :param typ: A member of `BugTrackerType`.
66
    :param cls: The `ExternalBugTracker` subclass corresponding to `typ`.
67
    """
68
69
    # Get the lookup tree.
70
    lookup = cls._status_lookup
71
72
    # Find or fabricate titles.
73
    titles = getattr(cls, '_status_lookup_titles', None)
74
    if titles is None:
75
        titles = ['Key %d' % (i + 1) for i in range(lookup.max_depth)]
76
    else:
77
        titles = list(titles)
78
    titles.append('Launchpad status')
79
80
    # Format the table.
81
    return chain(
82
        generate_table_header(typ),
83
        generate_blank_lines(1),
84
        generate_table_grid(lookup, titles))
85
86
87
def generate_documentable_classes():
88
    """Yield each class that has a mapping table defined."""
89
    for typ, cls in BUG_TRACKER_CLASSES.iteritems():
90
        if getattr(cls, '_status_lookup', None) is not None:
91
            yield typ, cls
92
93
94
def generate_tables():
95
    """Generate all the tables."""
96
    documentable_classes = sorted(
97
        generate_documentable_classes(),
98
        key=(lambda (typ, cls): typ.title))
99
    return chain(
100
        *(chain(generate_table(typ, cls),
101
                generate_blank_lines(2))
102
          for (typ, cls) in documentable_classes))
103
104
105
def generate_page():
106
    """Generate the MoinMoin-style page."""
107
    return chain(
108
        generate_page_header(),
109
        generate_blank_lines(2),
110
        generate_tables())
111
112
113
def write_page(outfile):
114
    """Write the page lines to `outfile`.
115
116
    :param outfile: A `file`-like object.
117
    """
118
    # By default, encode using UTF-8.
119
    write = codecs.getwriter('UTF-8')(outfile).write
120
    for line in generate_page():
121
        write(line)
122
        write('\n')
123
124
125
def get_option_parser():
126
    """Return the option parser for this program."""
127
    usage = "Usage: %prog [options]"
128
    parser = OptionParser(
129
        usage=usage, description=(
130
            "Generates MoinMoin-style tables to document the mapping of "
131
            "remote bug statuses to Launchpad statuses."))
132
    parser.add_option(
133
        "-o", "--file-out", dest="outfile", default='-', help=(
134
            "write data to OUTFILE"))
135
    return parser
136
137
138
def main(args):
139
    parser = get_option_parser()
140
    (options, args) = parser.parse_args(args)
141
    if len(args) > 0:
142
        parser.error("Incorrect number of arguments.")
143
    if options.outfile == '-':
144
        outfile = sys.stdout
145
    else:
146
        outfile = open(options.outfile, 'wb')
147
    write_page(outfile)
148
149
6326.8.19 by Gavin Panella
Add generation script.
150
if __name__ == '__main__':
6326.8.55 by Gavin Panella
New version of the utilities script, using generators and conforming to command line usage standards.
151
    sys.exit(main(sys.argv[1:]))