Debian Codegraph @

bin/news-to-specfile-changelog

raw symbols SCIP
1 #! /usr/bin/python
2 # -*- coding: utf-8 -*-
3 #
4 # bin/news-to-specfile-changelog
5 # Part of ComixCursors, a desktop cursor theme.
6 #
7 # Copyright © 2010–2013 Ben Finney <[email protected].au>
8 #
9 # This work is free software: you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This work is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this work. If not, see <http://www.gnu.org/licenses/>.
21
22 """ Render the NEWS file to a specfile changelog field value.
23
24 The project NEWS file is a reStructuredText document, with each
25 section describing a version of the project. The document is
26 intended to be readable as-is by end users.
27
28 This program transforms the document to a compact list of changes,
29 suitable for the “changelog” field of an RPM specfile.
30
31 Requires:
32
33 * Docutils <http://docutils.sourceforge.net/>
34
35 """
36
37 import datetime
38 import textwrap
39
40 from docutils.core import publish_cmdline, default_description
41 import docutils.nodes
42 import docutils.writers
43
44
45 class SpecChangelogWriter(docutils.writers.Writer):
46 """ Docutils writer to produce a changelog field for RPM spec files.
47 """
48
49 supported = ('spec_changelog')
50 """ Formats this writer supports. """
51
52 def __init__(self):
53 docutils.writers.Writer.__init__(self)
54 self.translator_class = SpecChangelogTranslator
55
56 def translate(self):
57 visitor = self.translator_class(self.document)
58 self.document.walkabout(visitor)
59 self.output = visitor.astext()
60
61
62 class NewsEntry(object):
63 """ An individual entry from the NEWS document. """
64
65 def __init__(
66 self, released=None, version=None, maintainer=None, body=None):
67 self.released = released
68 self.version = version
69 self.maintainer = maintainer
70 self.body = body
71
72 def as_specfile_changelog_entry(self):
73 """ Format the news entry as an RPM specfile changelog entry. """
74 # Reference: <URL:http://fedoraproject.org/wiki/PackagingGuidelines>
75
76 released_timestamp_text = self.released.strftime("%a %b %d %Y")
77 header = " ".join([
78 "*", released_timestamp_text, self.maintainer, "-", self.version])
79 text = "\n".join([header, self.body])
80 return text
81
82
83
84 def get_name_for_field_body(node):
85 """ Return the text of the field name of a field body node. """
86 field_node = node.parent
87 field_name_node_index = field_node.first_child_matching_class(
88 docutils.nodes.field_name)
89 field_name_node = field_node.children[field_name_node_index]
90 field_name = unicode(field_name_node.children[0])
91 return field_name
92
93
94 class InvalidFormatError(ValueError):
95 """ Raised when the document is not a valid NEWS document. """
96
97
98 def news_timestamp_to_datetime(text):
99 """ Return a datetime value from the news entry timestamp. """
100 if text == "FUTURE":
101 timestamp = datetime.datetime.max
102 else:
103 timestamp = datetime.datetime.strptime(text, "%Y-%m-%d")
104 return timestamp
105
106
107 class SpecChangelogTranslator(docutils.nodes.SparseNodeVisitor):
108 """ Translator from document nodes to a changelog for RPM spec files. """
109
110 wrap_width = 78
111
112 field_convert_funcs = {
113 'released': news_timestamp_to_datetime,
114 'maintainer': str,
115 }
116
117 def __init__(self, document):
118 docutils.nodes.NodeVisitor.__init__(self, document)
119 self.settings = document.settings
120 self.current_field_name = None
121 self.body = u""
122 self.indent_width = 0
123 self.initial_indent = u""
124 self.subsequent_indent = u""
125 self.current_entry = None
126
127 def astext(self):
128 """ Return the translated document as text. """
129 return self.body
130
131 def append_to_current_entry(self, text):
132 if self.current_entry is not None:
133 if self.current_entry.body is not None:
134 self.current_entry.body += text
135
136 def visit_Text(self, node):
137 raw_text = node.astext()
138 text = textwrap.fill(
139 raw_text,
140 width=self.wrap_width,
141 initial_indent=self.initial_indent,
142 subsequent_indent=self.subsequent_indent)
143 self.append_to_current_entry(text)
144
145 def depart_Text(self, node):
146 pass
147
148 def visit_comment(self, node):
149 raise docutils.nodes.SkipNode
150
151 def depart_comment(self, node):
152 pass
153
154 def visit_field_body(self, node):
155 convert_func = self.field_convert_funcs[self.current_field_name]
156 attr_name = self.current_field_name
157 attr_value = convert_func(node.astext())
158 setattr(self.current_entry, attr_name, attr_value)
159 raise docutils.nodes.SkipNode
160
161 def depart_field_body(self, node):
162 pass
163
164 def visit_field_list(self, node):
165 section_node = node.parent
166 if not isinstance(section_node, docutils.nodes.section):
167 raise InvalidFormatError(
168 "Unexpected field list within " + repr(section_node))
169
170 def depart_field_list(self, node):
171 self.current_field_name = None
172 if self.current_entry is not None:
173 self.current_entry.body = u""
174
175 def visit_field_name(self, node):
176 field_name = node.astext()
177 if field_name.lower() not in ("released", "maintainer"):
178 raise InvalidFormatError(
179 "Unexpected field name %(field_name)s" % vars())
180 self.current_field_name = field_name.lower()
181 raise docutils.nodes.SkipNode
182
183 def depart_field_name(self, node):
184 pass
185
186 def adjust_indent_width(self, delta):
187 self.indent_width += delta
188 self.subsequent_indent = u" " * self.indent_width
189 self.initial_indent = self.subsequent_indent
190
191 def visit_list_item(self, node):
192 self.adjust_indent_width(+2)
193 self.initial_indent = self.subsequent_indent[:-2]
194 self.append_to_current_entry(self.initial_indent + "- ")
195
196 def depart_list_item(self, node):
197 self.adjust_indent_width(-2)
198 self.append_to_current_entry("\n")
199
200 def visit_section(self, node):
201 self.current_entry = NewsEntry()
202
203 def depart_section(self, node):
204 self.body += self.current_entry.as_specfile_changelog_entry() + "\n"
205 self.current_entry = None
206
207 def visit_title(self, node):
208 title_text = node.astext()
209 words = title_text.split(" ")
210 self.current_entry.version = words[-1]
211
212 def depart_title(self, node):
213 pass
214
215
216 description = (
217 u"Render the NEWS file to a specfile changelog field value."
218 + u" " + default_description)
219 publish_cmdline(writer=SpecChangelogWriter(), description=description)