/usr/share/perl5/Dpkg/Changelog/Parse.pm is in libdpkg-perl 1.18.4ubuntu1.
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 | # Copyright © 2005, 2007 Frank Lichtenheld <frank@lichtenheld.de>
# Copyright © 2009 Raphaël Hertzog <hertzog@debian.org>
# Copyright © 2010, 2012-2015 Guillem Jover <guillem@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, see <https://www.gnu.org/licenses/>.
=encoding utf8
=head1 NAME
Dpkg::Changelog::Parse - generic changelog parser for dpkg-parsechangelog
=head1 DESCRIPTION
This module provides a set of functions which reproduce all the features
of dpkg-parsechangelog.
=cut
package Dpkg::Changelog::Parse;
use strict;
use warnings;
our $VERSION = '1.01';
our @EXPORT = qw(
changelog_parse_debian
changelog_parse_plugin
changelog_parse
);
use Exporter qw(import);
use Dpkg ();
use Dpkg::Util qw(none);
use Dpkg::Gettext;
use Dpkg::ErrorHandling;
use Dpkg::Changelog::Debian;
use Dpkg::Control::Changelog;
=head1 FUNCTIONS
=over 4
=item $fields = changelog_parse_debian(%opt)
This function will parse a changelog. In list context, it returns as many
Dpkg::Control objects as the parser did create. In scalar context, it will
return only the first one. If the parser did not return any data, it will
return an empty list in list context or undef on scalar context. If the
parser failed, it will die.
The changelog file that is parsed is F<debian/changelog> by default but it
can be overridden with $opt{file}. The default output format is "dpkg" but
it can be overridden with $opt{format}.
The parsing itself is done by Dpkg::Changelog::Debian.
=cut
sub changelog_parse_debian {
my (%options) = @_;
# Setup and sanity checks.
$options{file} //= 'debian/changelog';
$options{label} //= $options{file};
$options{format} //= 'dpkg';
$options{all} = 1 if exists $options{all};
if (none { defined $options{$_} } qw(since until from to offset count all)) {
$options{count} = 1;
}
my $range;
foreach my $opt (qw(since until from to offset count all)) {
$range->{$opt} = $options{$opt} if exists $options{$opt};
}
my $changes = Dpkg::Changelog::Debian->new(reportfile => $options{label},
range => $range);
$changes->load($options{file})
or error(g_('fatal error occurred while parsing %s'), $options{file});
# Get the output into several Dpkg::Control objects.
my @res;
if ($options{format} eq 'dpkg') {
push @res, $changes->dpkg($range);
} elsif ($options{format} eq 'rfc822') {
push @res, $changes->rfc822($range)->get();
} else {
error(g_('unknown output format %s'), $options{format});
}
if (wantarray) {
return @res;
} else {
return $res[0] if @res;
return;
}
}
sub _changelog_detect_format {
my $file = shift;
my $format = 'debian';
# Extract the format from the changelog file if possible
if ($file ne '-') {
local $_;
open my $format_fh, '-|', 'tail', '-n', '40', $file
or syserr(g_('cannot create pipe for %s'), 'tail');
while (<$format_fh>) {
$format = $1 if m/\schangelog-format:\s+([0-9a-z]+)\W/;
}
close $format_fh or subprocerr(g_('tail of %s'), $file);
}
return $format;
}
=item $fields = changelog_parse_plugin(%opt)
This function will parse a changelog. In list context, it returns as many
Dpkg::Control objects as the parser did output. In scalar context, it will
return only the first one. If the parser did not return any data, it will
return an empty list in list context or undef on scalar context. If the
parser failed, it will die.
The parsing itself is done by an external program (searched in the
following list of directories: $opt{libdir},
F</usr/local/lib/dpkg/parsechangelog>, F</usr/lib/dpkg/parsechangelog>).
That program is named according to the format that it is able to parse. By
default it is either "debian" or the format name looked up in the 40 last
lines of the changelog itself (extracted with this perl regular expression
"\schangelog-format:\s+([0-9a-z]+)\W"). But it can be overridden
with $opt{changelogformat}. The program expects the content of the
changelog file on its standard input.
The changelog file that is parsed is F<debian/changelog> by default but it
can be overridden with $opt{file}.
All the other keys in %opt are forwarded as parameter to the external
parser. If the key starts with "-", it is passed as is. If not, it is passed
as "--<key>". If the value of the corresponding hash entry is defined, then
it is passed as the parameter that follows.
=cut
sub changelog_parse_plugin {
my (%options) = @_;
# Setup and sanity checks.
$options{file} //= 'debian/changelog';
my @parserpath = ('/usr/local/lib/dpkg/parsechangelog',
"$Dpkg::LIBDIR/parsechangelog",
'/usr/lib/dpkg/parsechangelog');
my $format;
# Extract and remove options that do not concern the changelog parser
# itself (and that we shouldn't forward)
delete $options{forceplugin};
if (exists $options{libdir}) {
unshift @parserpath, $options{libdir};
delete $options{libdir};
}
if (exists $options{changelogformat}) {
$format = $options{changelogformat};
delete $options{changelogformat};
} else {
$format = _changelog_detect_format($options{file});
}
# Find the right changelog parser
my $parser;
foreach my $dir (@parserpath) {
my $candidate = "$dir/$format";
next if not -e $candidate;
if (-x _) {
$parser = $candidate;
last;
} else {
warning(g_('format parser %s not executable'), $candidate);
}
}
error(g_('changelog format %s is unknown'), $format) if not defined $parser;
# Create the arguments for the changelog parser
my @exec = ($parser, "-l$options{file}");
foreach my $option (keys %options) {
if ($option =~ m/^-/) {
# Options passed untouched
push @exec, $option;
} else {
# Non-options are mapped to long options
push @exec, "--$option";
}
push @exec, $options{$option} if defined $options{$option};
}
# Fork and call the parser
my $pid = open(my $parser_fh, '-|');
syserr(g_('cannot fork for %s'), $parser) unless defined $pid;
if (not $pid) {
exec @exec or syserr(g_('cannot execute format parser: %s'), $parser);
}
# Get the output into several Dpkg::Control objects
my (@res, $fields);
while (1) {
$fields = Dpkg::Control::Changelog->new();
last unless $fields->parse($parser_fh, g_('output of changelog parser'));
push @res, $fields;
}
close($parser_fh) or subprocerr(g_('changelog parser %s'), $parser);
if (wantarray) {
return @res;
} else {
return $res[0] if (@res);
return;
}
}
=item $fields = changelog_parse(%opt)
This function will parse a changelog. In list context, it returns as many
Dpkg::Control objects as the parser did create. In scalar context, it will
return only the first one. If the parser did not return any data, it will
return an empty list in list context or undef on scalar context. If the
parser failed, it will die.
If $opt{forceplugin} is false and $opt{changelogformat} is "debian", then
changelog_parse_debian() is called to perform the parsing. Otherwise
changelog_parse_plugin() is used.
The changelog file that is parsed is F<debian/changelog> by default but it
can be overridden with $opt{file}.
=cut
sub changelog_parse {
my (%options) = @_;
$options{forceplugin} //= 0;
$options{file} //= 'debian/changelog';
$options{changelogformat} //= _changelog_detect_format($options{file});
if (not $options{forceplugin} and
$options{changelogformat} eq 'debian') {
return changelog_parse_debian(%options);
} else {
return changelog_parse_plugin(%options);
}
}
=back
=head1 CHANGES
=head2 Version 1.01 (dpkg 1.18.2)
New functions: changelog_parse_debian(), changelog_parse_plugin().
=head2 Version 1.00 (dpkg 1.15.6)
Mark the module as public.
=cut
1;
|