This file is indexed.

/usr/share/apt-listchanges/DebianFiles.py is in apt-listchanges 3.10.

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
# vim:set fileencoding=utf-8 et ts=4 sts=4 sw=4:
#
#   apt-listchanges - Show changelog entries between the installed versions
#                     of a set of packages and the versions contained in
#                     corresponding .deb files
#
#   Copyright (C) 2000-2006  Matt Zimmerman  <mdz@debian.org>
#   Copyright (C) 2006       Pierre Habouzit <madcoder@debian.org>
#   Copyright (C) 2016       Robert Luberda  <robert@debian.org>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public
#   License along with this program; if not, write to the Free
#   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
#   MA 02111-1307 USA
#

import re
import sys, os
import tempfile
import gzip
import errno
import glob
import shutil
import shlex
import signal
import subprocess

import apt_pkg
import ALCLog
from ALChacks import _
from functools import reduce

# TODO:
# indexed lookups by package at least, maybe by arbitrary field

def _numeric_urgency(u):
    urgency_map = { 'critical'  : 1,
                    'emergency' : 1,
                    'high'      : 2,
                    'medium'    : 3,
                    'low'       : 4 }

    return urgency_map.get(u.lower(), 0)


class ControlStanza:
    source_version_re = re.compile('^\S+ \((?P<version>.*)\).*')
    fields_to_read = [ 'Package', 'Source', 'Version', 'Architecture', 'Status' ]

    def __init__(self, s):
        field = None

        for line in s.split('\n'):
            if not line:
                break
            if line[0] in (' ', '\t'):
                if field:
                    setattr(self, field, getattr(self, field) + '\n' + line)
            else:
                field, value = line.split(':', 1)
                if field in self.fields_to_read:
                    setattr(self, field, value.lstrip())
                else:
                    field = None


    def source(self):
        return getattr(self, 'Source', self.Package).split(' ')[0]

    def installed(self):
        return hasattr(self, 'Status') and self.Status.split(' ')[2] == 'installed'

    def version(self):
        """
        This function returns the version of the package. One would like it to
        be the "binary" version, though we have the tough case of source
        package whose binary packages versioning scheme is different from the
        source one (see OOo, linux-source, ...).

        This code does the following, if the Source field is set with a
        specified version, then we use the binary version if and only if the
        source version is a prefix. We must do that because of binNMUs.
        """
        v = self.Version
        if hasattr(self, 'Source'):
            match = self.source_version_re.match(self.Source)
            if match:
                sv = match.group('version')
                if not v.startswith(sv):
                    return sv
        return v


class ControlParser:
    def __init__(self):
        self.stanzas = []
        self.index = {}

    def makeindex(self, field):
        self.index[field] = {}
        for stanza in self.stanzas:
            self.index[field][getattr(stanza, field)] = stanza

    def readfile(self, file):
        try:
            self.stanzas += [ControlStanza(x) for x in open(file, 'r', encoding='utf-8', errors='replace').read().split('\n\n') if x]
        except Exception as ex:
            raise RuntimeError(_("Error processing '%(what)s': %(errmsg)s") %
                                {'what': file, 'errmsg': str(ex)}) from ex

    def readdeb(self, deb):
        try:
            command = ['dpkg-deb', '-f', deb] + ControlStanza.fields_to_read
            output = subprocess.check_output(command)
            self.stanzas.append(ControlStanza(output.decode('utf-8', 'replace')))
        except Exception as ex:
            raise RuntimeError(_("Error processing '%(what)s': %(errmsg)s") %
                                {'what': file, 'errmsg': str(ex)}) from ex

    def find(self, field, value):
        if field in self.index:
            if value in self.index[field]:
                return self.index[field][value]
            else:
                return None
        else:
            for stanza in self.stanzas:
                if hasattr(stanza, field) and getattr(stanza, field) == value:
                    return stanza
        return None

class ChangelogParser:
    _changelog_header = re.compile('^\S+ \((?P<version>.*)\) .*;.*urgency=(?P<urgency>\w+).*')
    _changelog_header_ancient = re.compile('^(\S+ \(?\d.*\)|Old Changelog:|Changes|ChangeLog begins|Mon|Tue|Wed|Thu|Fri|Sat|Sun).*')
    _changelog_header_emacs = re.compile('(;;\s*)?Local\s+variables.*', re.IGNORECASE)

    def __init__(self):
        self._entry = ''
        self._entries = []

    def parse(self, fd, since_version):
        '''Parse changelog or news from the given file descriptor.
        If since_version is specified, only return entries later
        than the specified version.'''
        urgency = _numeric_urgency('low')
        is_debian_changelog = False
        ancient = False

        for line in fd.readlines():
            line = line.decode('utf-8', 'replace')

            if line.startswith(' ') or line == '\n':
                self._add_line(line)

            elif line.startswith('#'):
                continue

            else:
                match = self._changelog_header.match(line) if not ancient else None
                if match:
                    if since_version and apt_pkg.version_compare(match.group('version'),
                                                                 since_version) <= 0:
                        break
                    is_debian_changelog = True
                    urgency = min(_numeric_urgency(match.group('urgency')),
                                  urgency)
                    self._save_entry(line)

                elif self._changelog_header_ancient.match(line):
                    if not is_debian_changelog: # probably upstream changelog in GNU format
                        break
                    ancient = True
                    self._save_entry(line)

                elif self._changelog_header_emacs.match(line):
                    break

                elif is_debian_changelog:
                    self._add_line(line)

                else:
                    break

        if not is_debian_changelog:
            return None, urgency

        self._save_entry(None)

        return self._entries, urgency

    def _add_line(self, line):
        self._entry += line

    def _save_entry(self, new_entry_header):
        self._entry = self._entry.strip()
        if self._entry != '':
            self._entries += [ self._entry + '\n' ]
        self._entry = new_entry_header

class Package:
    def __init__(self, path):
        self.path = path

        parser = ControlParser()
        parser.readdeb(self.path)
        pkgdata = parser.stanzas[0]

        self.binary  = pkgdata.Package
        self.source  = pkgdata.source()
        self.Version = pkgdata.version()
        self.arch = pkgdata.Architecture

    def extract_changes(self, which, since_version=None, reverse=None):
        '''Extract changelog entries, news or both from the package.
        If since_version is specified, only return entries later than the specified version.
        returns a sequence of Changes objects.'''

        news_filenames = self._changelog_variations('NEWS.Debian')
        changelog_filenames = self._changelog_variations('changelog.Debian')
        changelog_filenames_binnmu = self._changelog_variations('changelog.Debian.' + self.arch)
        changelog_filenames_native = self._changelog_variations('changelog')

        filenames = []
        if which == 'both' or which == 'news':
            filenames.extend(news_filenames)
        if which == 'both' or which == 'changelogs':
            filenames.extend(changelog_filenames)
            filenames.extend(changelog_filenames_binnmu)
            filenames.extend(changelog_filenames_native)

        tempdir = self._extract_contents(filenames)

        find_first = lambda acc, fname: acc or self._read_changelog(os.path.join(tempdir, fname), since_version, reverse)

        news       = reduce(find_first, news_filenames, None)
        changelog  = reduce(find_first, changelog_filenames + changelog_filenames_native, None)
        binnmu     = reduce(find_first, changelog_filenames_binnmu, None)

        shutil.rmtree(tempdir, 1)

        return (news, changelog, binnmu)

    def _extract_contents(self, filenames):
        tempdir = tempfile.mkdtemp(prefix='apt-listchanges')

        extract_command = 'dpkg-deb --fsys-tarfile %s | tar xf - --wildcards -C %s %s 2>/dev/null' % (
            shlex.quote(self.path), shlex.quote(tempdir),
            ' '.join([shlex.quote(x) for x in filenames])
        )

        # tar exits unsuccessfully if _any_ of the files we wanted
        # were not available, so we can't do much with its status
        status = os.system(extract_command)

        if os.WIFSIGNALED(status) and os.WTERMSIG(status) == signal.SIGINT:
            shutil.rmtree(tempdir, 1)
            raise KeyboardInterrupt

        return tempdir

    def _open_changelog_file(self, filename):
        filenames = glob.glob(filename)

        for filename in filenames:
            try:
                if os.path.isdir(filename):
                    ALCLog.error(_("Ignoring `%s' (seems to be a directory!)") % filename)
                elif filename.endswith('.gz'):
                    return gzip.GzipFile(filename)
                else:
                    return open(filename, 'rb')
                break
            except IOError as e:
                if e.errno == errno.ENOENT:
                    pass
                else:
                    raise
        return None

    def _read_changelog(self, filename, since_version, reverse=False):
        fd = self._open_changelog_file(filename)
        if not fd:
            return None

        entries, urgency = ChangelogParser().parse(fd, since_version)

        if not urgency:
            return None # not a Debian changelog file

        if not entries:
            # Valid Debian changelog, but all entries filtered out due
            # to --since-version. Return empty string to prevent the caller
            # from searching for another Debian changelog file.
            return Changes(self.source, "", urgency)

        if reverse:
            entries.reverse()

        changes = "\n".join(entries) + "\n"

        return Changes(self.source, changes, urgency)

    def _changelog_variations(self, filename):
        formats = ['./usr/share/doc/*/%s.gz',
                   './usr/share/doc/*/%s']
        return [x % filename for x in formats]

class Changes:
    def __init__(self, package, changes, urgency):
        self.package = package
        self.changes = changes
        self.urgency = urgency

    def merge_binnmu(self, other, reverse = False):
        assert self.package == other.package
        self.urgency = min(self.urgency, other.urgency)
        # Assumption: binnmu has greater version than regular changelog
        if reverse:
            self.changes = self.changes + other.changes
        else:
            self.changes = other.changes + self.changes

__all__ = [ 'ControlParser', 'Package' ]