~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to tools/generate_release_notes.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-09-01 08:02:42 UTC
  • mfrom: (5390.3.3 faster-revert-593560)
  • Revision ID: pqm@pqm.ubuntu.com-20100901080242-esg62ody4frwmy66
(spiv) Avoid repeatedly calling self.target.all_file_ids() in
 InterTree.iter_changes. (Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
2
2
 
3
 
# Copyright 2009-2010 Canonical Ltd.
 
3
# Copyright 2009 Canonical Ltd.
4
4
#
5
5
# This program is free software; you can redistribute it and/or modify
6
6
# it under the terms of the GNU General Public License as published by
16
16
# along with this program; if not, write to the Free Software
17
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
18
 
19
 
"""Generate doc/en/release-notes/index.txt from the per-series NEWS files.
20
 
 
21
 
NEWS files are kept in doc/en/release-notes/, one file per series, e.g.
22
 
doc/en/release-notes/bzr-2.3.txt
23
 
"""
24
 
 
25
 
# XXX: add test_source test that latest doc/en/release-notes/bzr-*.txt has the
26
 
# NEWS file-id (so that merges of new work will tend to always land new NEWS
27
 
# entries in the latest series).
28
 
 
29
 
 
30
 
import os.path
31
 
import re
 
19
import os
32
20
import sys
33
21
from optparse import OptionParser
34
22
 
35
23
 
36
 
preamble_plain = """\
37
 
####################
38
 
Bazaar Release Notes
39
 
####################
40
 
 
41
 
 
42
 
.. contents:: List of Releases
43
 
   :depth: 2
44
 
 
45
 
"""
46
 
 
47
 
preamble_sphinx = """\
48
 
####################
49
 
Bazaar Release Notes
50
 
####################
51
 
 
52
 
 
53
 
.. toctree::
54
 
   :maxdepth: 2
55
 
 
56
 
"""
57
 
 
58
 
 
59
 
def natural_sort_key(file_name):
60
 
    """Split 'aaa-N.MMbbb' into ('aaa-', N, '.' MM, 'bbb')
61
 
    
62
 
    e.g. 1.10b1 will sort as greater than 1.2::
63
 
 
64
 
        >>> natural_sort_key('bzr-1.10b1.txt') > natural_sort_key('bzr-1.2.txt')
65
 
        True
 
24
def split_into_topics(lines, out_file, out_dir):
 
25
    """Split a large NEWS file into topics, one per release.
 
26
 
 
27
    Releases are detected by matching headings that look like
 
28
    release names. Topics are created with matching names
 
29
    replacing spaces with dashes.
66
30
    """
67
 
    file_name = os.path.basename(file_name)
68
 
    parts = re.findall(r'(?:[0-9]+|[^0-9]+)', file_name)
69
 
    result = []
70
 
    for part in parts:
71
 
        if re.match('^[0-9]+$', part) is not None:
72
 
            part = int(part)
73
 
        result.append(part)
74
 
    return tuple(result)
75
 
 
76
 
 
77
 
def output_news_file_sphinx(out_file, news_file_name):
78
 
    news_file_name = os.path.basename(news_file_name)
79
 
    if not news_file_name.endswith('.txt'):
80
 
        raise AssertionError(
81
 
            'NEWS file %s does not have .txt extension.'
82
 
            % (news_file_name,))
83
 
    doc_name = news_file_name[:-4]
84
 
    link_text = doc_name.replace('-', ' ')
85
 
    out_file.write('   %s <%s>\n' % (link_text, doc_name))
86
 
 
87
 
 
88
 
def output_news_file_plain(out_file, news_file_name):
89
 
    f = open(news_file_name, 'rb')
90
 
    try:
91
 
        lines = f.readlines()
92
 
    finally:
93
 
        f.close()
94
 
    title = os.path.basename(news_file_name)[len('bzr-'):-len('.txt')]
95
 
    for line in lines:
96
 
        if line == '####################\n':
97
 
            line = '#' * len(title) + '\n'
98
 
        elif line == 'Bazaar Release Notes\n':
99
 
            line = title + '\n'
100
 
        elif line == '.. toctree::\n':
101
 
            continue
102
 
        elif line == '   :maxdepth: 1\n':
103
 
            continue
104
 
        out_file.write(line)
105
 
    out_file.write('\n\n')
 
31
    topic_file = None
 
32
    for index, line in enumerate(lines):
 
33
        maybe_new_topic = line[:4] in ('bzr ', 'bzr-',)
 
34
        if maybe_new_topic and lines[index + 1].startswith('####'):
 
35
            release = line.strip()
 
36
            if topic_file is None:
 
37
                # First topic found
 
38
                out_file.write(".. toctree::\n   :maxdepth: 1\n\n")
 
39
            else:
 
40
                # close the current topic
 
41
                topic_file.close()
 
42
            topic_file = open_topic_file(out_file, out_dir, release)
 
43
        elif topic_file:
 
44
            topic_file.write(line)
 
45
        else:
 
46
            # FIXME: the 'content' directive is used for rst2html (and
 
47
            # conflicts with the 'toctree' we insert), we should get rid of
 
48
            # that once we fully switch to sphinx -- vila 20100505
 
49
            if (line.startswith('.. contents::')
 
50
                or line.startswith('   :depth:')):
 
51
                    continue
 
52
            # Still in the header - dump content straight to output
 
53
            out_file.write(line)
 
54
    if topic_file is not None:
 
55
        # Close the last topic_file opened
 
56
        topic_file.close()
 
57
 
 
58
 
 
59
def open_topic_file(out_file, out_dir, release):
 
60
    topic_name = release.replace(' ', '-')
 
61
    out_file.write("   %s <%s>\n" % (release, topic_name,))
 
62
    topic_path = os.path.join(out_dir, "%s.txt" % (topic_name,))
 
63
    result = open(topic_path, 'w')
 
64
    result.write("%s\n" % (release,))
 
65
    return result
106
66
 
107
67
 
108
68
def main(argv):
109
69
    # Check usage
110
 
    parser = OptionParser(usage="%prog OUTPUT_FILE NEWS_FILE [NEWS_FILE ...]")
 
70
    parser = OptionParser(usage="%prog SOURCE DESTINATION")
111
71
    (options, args) = parser.parse_args(argv)
112
 
    if len(args) < 2:
 
72
    if len(args) != 2:
113
73
        parser.print_help()
114
74
        sys.exit(1)
115
75
 
116
76
    # Open the files and do the work
117
 
    out_file_name = args[0]
118
 
    news_file_names = sorted(args[1:], key=natural_sort_key, reverse=True)
119
 
 
120
 
    if os.path.basename(out_file_name) == 'index.txt':
121
 
        preamble = preamble_sphinx
122
 
        output_news_file = output_news_file_sphinx
123
 
    else:
124
 
        preamble = preamble_plain
125
 
        output_news_file = output_news_file_plain
126
 
 
127
 
    out_file = open(out_file_name, 'w')
128
 
    try:
129
 
        out_file.write(preamble)
130
 
        for news_file_name in news_file_names:
131
 
            output_news_file(out_file, news_file_name)
132
 
    finally:
133
 
        out_file.close()
 
77
    infile_name = args[0]
 
78
    outfile_name = args[1]
 
79
    outdir = os.path.dirname(outfile_name)
 
80
    infile = open(infile_name, 'r')
 
81
    try:
 
82
        lines = infile.readlines()
 
83
    finally:
 
84
        infile.close()
 
85
    outfile = open(outfile_name, 'w')
 
86
    try:
 
87
        split_into_topics(lines, outfile, outdir)
 
88
    finally:
 
89
        outfile.close()
134
90
 
135
91
 
136
92
if __name__ == '__main__':