This file is indexed.

/usr/lib/python3/dist-packages/changelog/changelog.py is in python3-changelog 0.3.5-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#! coding: utf-8


import re
from docutils.parsers.rst import Directive
from docutils.statemachine import StringList
from docutils import nodes
from sphinx.util.console import bold
import os
from sphinx.util.osutil import copyfile
import textwrap
import itertools
import collections
import sys

py2k = sys.version_info < (3, 0)
if py2k:
    import md5
else:
    import hashlib as md5

def _is_html(app):
    return app.builder.name in ('html', 'readthedocs')   # 'readthedocs', classy

def _comma_list(text):
    return re.split(r"\s*,\s*", text.strip())

def _parse_content(content):
    d = {}
    d['text'] = []
    idx = 0
    for line in content:
        idx += 1
        m = re.match(r' *\:(.+?)\:(?: +(.+))?', line)
        if m:
            attrname, value = m.group(1, 2)
            d[attrname] = value or ''
        elif idx == 1 and line:
            # accomodate a unique value on the edge of .. change::
            continue
        else:
            break
    d["text"] = content[idx:]
    return d


class EnvDirective(object):
    @property
    def env(self):
        return self.state.document.settings.env

    @classmethod
    def changes(cls, env):
        return env.temp_data['ChangeLogDirective_changes']

class ChangeLogDirective(EnvDirective, Directive):
    has_content = True

    default_section = 'misc'

    def _organize_by_section(self, changes):
        compound_sections = [(s, s.split(" ")) for s in
                                self.sections if " " in s]

        bysection = collections.defaultdict(list)
        all_sections = set()
        for rec in changes:
            if self.version not in rec['versions']:
                continue
            inner_tag = rec['tags'].intersection(self.inner_tag_sort)
            if inner_tag:
                inner_tag = inner_tag.pop()
            else:
                inner_tag = ""

            for compound, comp_words in compound_sections:
                if rec['tags'].issuperset(comp_words):
                    bysection[(compound, inner_tag)].append(rec)
                    all_sections.add(compound)
                    break
            else:
                intersect = rec['tags'].intersection(self.sections)
                if intersect:
                    for sec in rec['sorted_tags']:
                        if sec in intersect:
                            bysection[(sec, inner_tag)].append(rec)
                            all_sections.add(sec)
                            break
                else:
                    bysection[(self.default_section, inner_tag)].append(rec)
        return bysection, all_sections

    def _setup_run(self):
        self.sections = self.env.config.changelog_sections
        self.inner_tag_sort = self.env.config.changelog_inner_tag_sort + [""]
        if 'ChangeLogDirective_changes' not in self.env.temp_data:
            self.env.temp_data['ChangeLogDirective_changes'] = []
        self._parsed_content = _parse_content(self.content)

        self.version = version = self._parsed_content.get('version', '')
        self.env.temp_data['ChangeLogDirective_version'] = version

        p = nodes.paragraph('', '',)
        self.state.nested_parse(self.content[1:], 0, p)

    def run(self):
        self._setup_run()

        if 'ChangeLogDirective_includes' in self.env.temp_data:
            return []

        changes = self.changes(self.env)
        output = []

        id_prefix = "change-%s" % (self.version, )
        topsection = self._run_top(id_prefix)
        output.append(topsection)

        bysection, all_sections = self._organize_by_section(changes)

        counter = itertools.count()

        sections_to_render = [s for s in self.sections if s in all_sections]
        if not sections_to_render:
            for cat in self.inner_tag_sort:
                append_sec = self._append_node()

                for rec in bysection[(self.default_section, cat)]:
                    rec["id"] = "%s-%s" % (id_prefix, next(counter))

                    self._render_rec(rec, None, cat, append_sec)

                if append_sec.children:
                    topsection.append(append_sec)
        else:
            for section in sections_to_render + [self.default_section]:
                sec = nodes.section('',
                        nodes.title(section, section),
                        ids=["%s-%s" % (id_prefix, section.replace(" ", "-"))]
                )

                append_sec = self._append_node()
                sec.append(append_sec)

                for cat in self.inner_tag_sort:
                    for rec in bysection[(section, cat)]:
                        rec["id"] = "%s-%s" % (id_prefix, next(counter))
                        self._render_rec(rec, section, cat, append_sec)

                if append_sec.children:
                    topsection.append(sec)

        return output

    def _append_node(self):
        return nodes.bullet_list()

    def _run_top(self, id_prefix):
        version = self._parsed_content.get('version', '')
        topsection = nodes.section('',
                nodes.title(version, version),
                ids=[id_prefix]
            )

        if self._parsed_content.get("released"):
            topsection.append(nodes.Text("Released: %s" %
                        self._parsed_content['released']))
        else:
            topsection.append(nodes.Text("no release date"))

        intro_para = nodes.paragraph('', '')
        len_ = -1
        for len_, text in enumerate(self._parsed_content['text']):
            if ".. change::" in text:
                break

        # if encountered any text elements that didn't start with
        # ".. change::", those become the intro
        if len_ > 0:
            self.state.nested_parse(self._parsed_content['text'][0:len_], 0,
                            intro_para)
            topsection.append(intro_para)

        return topsection


    def _render_rec(self, rec, section, cat, append_sec):
        para = rec['node'].deepcopy()

        text = _text_rawsource_from_node(para)

        to_hash = "%s %s" % (self.version, text[0:100])
        targetid = "change-%s" % (
                        md5.md5(to_hash.encode('ascii', 'ignore')
                            ).hexdigest())
        targetnode = nodes.target('', '', ids=[targetid])
        para.insert(0, targetnode)
        permalink = nodes.reference('', '',
                        nodes.Text(u"¶", u"¶"),
                        refid=targetid,
                        classes=['changeset-link', 'headerlink'],
                    )
        para.append(permalink)

        if len(rec['versions']) > 1:

            backported_changes = rec['sorted_versions'][rec['sorted_versions'].index(self.version) + 1:]
            if backported_changes:
                backported = nodes.paragraph('')
                backported.append(nodes.Text("This change is also ", ""))
                backported.append(nodes.strong("", "backported"))
                backported.append(nodes.Text(" to: %s" % ", ".join(backported_changes), ""))
                para.append(backported)

        insert_ticket = nodes.paragraph('')
        para.append(insert_ticket)

        i = 0
        for collection, render, prefix in (
                (rec['tickets'], self.env.config.changelog_render_ticket, "#%s"),
                (rec['pullreq'], self.env.config.changelog_render_pullreq,
                                            "pull request %s"),
                (rec['changeset'], self.env.config.changelog_render_changeset, "r%s"),
            ):
            for refname in collection:
                if i > 0:
                    insert_ticket.append(nodes.Text(", ", ", "))
                else:
                    insert_ticket.append(nodes.Text("References: """))
                i += 1
                if render is not None:
                    if isinstance(render, dict):
                        if ":" in refname:
                            typ, refval = refname.split(":")
                        else:
                            typ = "default"
                            refval = refname
                        refuri = render[typ] % refval
                    else:
                        refuri = render % refname
                    node = nodes.reference('', '',
                            nodes.Text(prefix % refname, prefix % refname),
                            refuri=refuri
                        )
                else:
                    node = nodes.Text(prefix % refname, prefix % refname)
                insert_ticket.append(node)

        if rec['tags']:
            tag_node = nodes.strong('',
                        " ".join("[%s]" % t for t
                            in
                                [t1 for t1 in [section, cat]
                                    if t1 in rec['tags']] +

                                list(rec['tags'].difference([section, cat]))
                        ) + " "
                    )
            para.children[0].insert(0, tag_node)

        append_sec.append(
            nodes.list_item('',
                nodes.target('', '', ids=[rec['id']]),
                para
            )
        )

class ChangeLogImportDirective(EnvDirective, Directive):
    has_content = True

    def _setup_run(self):
        if 'ChangeLogDirective_changes' not in self.env.temp_data:
            self.env.temp_data['ChangeLogDirective_changes'] = []

    def run(self):
        self._setup_run()

        # tell ChangeLogDirective we're here, also prevent
        # nested .. include calls
        if 'ChangeLogDirective_includes' not in self.env.temp_data:
            self.env.temp_data['ChangeLogDirective_includes'] = True
            p = nodes.paragraph('', '',)
            self.state.nested_parse(self.content, 0, p)
            del self.env.temp_data['ChangeLogDirective_includes']

        return []

class ChangeDirective(EnvDirective, Directive):
    has_content = True

    def run(self):
        content = _parse_content(self.content)
        p = nodes.paragraph('', '',)
        sorted_tags = _comma_list(content.get('tags', ''))
        declared_version = self.env.temp_data['ChangeLogDirective_version']
        versions = set(_comma_list(content.get("versions", ""))).difference(['']).\
                            union([declared_version])

        # if we don't refer to any other versions and we're in an include,
        # skip
        if len(versions) == 1 and 'ChangeLogDirective_includes' in self.env.temp_data:
            return []

        def int_ver(ver):
            out = []
            for dig in ver.split("."):
                try:
                    out.append(int(dig))
                except ValueError:
                    out.append(0)
            return tuple(out)

        rec = {
            'tags': set(sorted_tags).difference(['']),
            'tickets': set(_comma_list(content.get('tickets', ''))).difference(['']),
            'pullreq': set(_comma_list(content.get('pullreq', ''))).difference(['']),
            'changeset': set(_comma_list(content.get('changeset', ''))).difference(['']),
            'node': p,
            'type': "change",
            "title": content.get("title", None),
            'sorted_tags': sorted_tags,
            "versions": versions,
            "sorted_versions": list(reversed(sorted(versions, key=int_ver)))
        }

        self.state.nested_parse(content['text'], 0, p)
        ChangeLogDirective.changes(self.env).append(rec)

        return []

def _text_rawsource_from_node(node):
    src = []
    stack = [node]
    while stack:
        n = stack.pop(0)
        if isinstance(n, nodes.Text):
            src.append(n.rawsource)
        stack.extend(n.children)
    return "".join(src)

def _rst2sphinx(text):
    return StringList(
        [line.strip() for line in textwrap.dedent(text).split("\n")]
    )


def make_ticket_link(name, rawtext, text, lineno, inliner,
                      options={}, content=[]):
    env = inliner.document.settings.env
    render_ticket = env.config.changelog_render_ticket or "%s"
    prefix = "#%s"
    if render_ticket:
        ref = render_ticket % text
        node = nodes.reference(rawtext, prefix % text, refuri=ref, **options)
    else:
        node = nodes.Text(prefix % text, prefix % text)
    return [node], []

def add_stylesheet(app):
    app.add_stylesheet('changelog.css')

def copy_stylesheet(app, exception):
    app.info(bold('The name of the builder is: %s' % app.builder.name), nonl=True)

    if not _is_html(app) or exception:
        return
    app.info(bold('Copying sphinx_paramlinks stylesheet... '), nonl=True)

    source = os.path.abspath(os.path.dirname(__file__))

    # the '_static' directory name is hardcoded in
    # sphinx.builders.html.StandaloneHTMLBuilder.copy_static_files.
    # would be nice if Sphinx could improve the API here so that we just
    # give it the path to a .css file and it does the right thing.
    dest = os.path.join(app.builder.outdir, '_static', 'changelog.css')
    copyfile(os.path.join(source, "changelog.css"), dest)
    app.info('done')

def setup(app):
    app.add_directive('changelog', ChangeLogDirective)
    app.add_directive('change', ChangeDirective)
    app.add_directive('changelog_imports', ChangeLogImportDirective)
    app.add_config_value("changelog_sections", [], 'env')
    app.add_config_value("changelog_inner_tag_sort", [], 'env')
    app.add_config_value("changelog_render_ticket", None, 'env')
    app.add_config_value("changelog_render_pullreq", None, 'env')
    app.add_config_value("changelog_render_changeset", None, 'env')
    app.connect('builder-inited', add_stylesheet)
    app.connect('build-finished', copy_stylesheet)
    app.add_role('ticket', make_ticket_link)