~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to tools/generate_release_notes.py

  • Committer: Andrew Bennetts
  • Date: 2010-10-13 00:26:41 UTC
  • mto: This revision was merged to the branch mainline in revision 5498.
  • Revision ID: andrew.bennetts@canonical.com-20101013002641-9tlh9k89mlj1666m
Keep docs-plain working.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
from optparse import OptionParser
34
34
 
35
35
 
36
 
preamble = """\
 
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 = """\
37
48
####################
38
49
Bazaar Release Notes
39
50
####################
53
64
        >>> natural_sort_key('bzr-1.10b1.txt') > natural_sort_key('bzr-1.2.txt')
54
65
        True
55
66
    """
 
67
    file_name = os.path.basename(file_name)
56
68
    parts = re.findall(r'(?:[0-9]+|[^0-9]+)', file_name)
57
69
    result = []
58
70
    for part in parts:
62
74
    return tuple(result)
63
75
 
64
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')
 
106
 
 
107
 
65
108
def main(argv):
66
109
    # Check usage
67
110
    parser = OptionParser(usage="%prog OUTPUT_FILE NEWS_FILE [NEWS_FILE ...]")
72
115
 
73
116
    # Open the files and do the work
74
117
    out_file_name = args[0]
75
 
    news_file_names = map(os.path.basename, args[1:])
76
 
    news_file_names = sorted(news_file_names, key=natural_sort_key,
77
 
        reverse=True)
 
118
    news_file_names = sorted(args[1:], key=natural_sort_key, reverse=True)
 
119
 
 
120
    if 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
78
126
 
79
127
    out_file = open(out_file_name, 'w')
80
128
    try:
81
129
        out_file.write(preamble)
82
130
        for news_file_name in news_file_names:
83
 
            if not news_file_name.endswith('.txt'):
84
 
                raise AssertionError(
85
 
                    'NEWS file %s does not have .txt extension.'
86
 
                    % (news_file_name,))
87
 
            doc_name = news_file_name[:-4]
88
 
            link_text = doc_name.replace('-', ' ')
89
 
            out_file.write('   %s <%s>\n' % (link_text, doc_name))
 
131
            output_news_file(out_file, news_file_name)
90
132
    finally:
91
133
        out_file.close()
92
134