Dpkg/Changelog/Parse.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 153 CC 14 SCAR 42.5
CC
14
Cov
0%
changelog_parse
132.0
_changelog_detect_format
20.0
BEGIN
2.0
CRAP 70.3
1 # Copyright © 2005, 2007 Frank Lichtenheld <frank@lichtenheld.de>
2 # Copyright © 2009 Raphaël Hertzog <hertzog@debian.org>
3 # Copyright © 2010, 2012-2015 Guillem Jover <guillem@debian.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17  
18 =encoding utf8
19  
20 =head1 NAME
21  
22 Dpkg::Changelog::Parse - generic changelog parser for dpkg-parsechangelog
23  
24 =head1 DESCRIPTION
25  
26 This module provides a set of functions which reproduce all the features
27 of dpkg-parsechangelog.
28  
29 =cut
30  
31 package Dpkg::Changelog::Parse 2.02;
32  
33 use v5.36;
34  
35 our @EXPORT = qw(
36 changelog_parse
37 );
38  
39 use Exporter qw(import);
40 use List::Util qw(none);
41  
42 use Dpkg ();
43 use Dpkg::Gettext;
44 use Dpkg::ErrorHandling;
45 use Dpkg::Control::Changelog;
46  
47 sub _changelog_detect_format {
48 my $filename = shift;
49 my $format = 'debian';
50  
51 # Extract the format from the changelog file if possible.
52 if ($filename ne '-') {
53 local $_;
54  
55 open my $format_fh, '<', $filename
56 or syserr(g_('cannot open file %s'), $filename);
57 if (-s $format_fh > 4096) {
58 seek $format_fh, -4096, 2
59 or syserr(g_('cannot seek into file %s'), $filename);
60 }
61 while (<$format_fh>) {
62 $format = $1 if m/\schangelog-format:\s+([0-9a-z]+)\W/;
63 }
64 close $format_fh;
65 }
66  
67 return $format;
68 }
69  
70 =head1 FUNCTIONS
71  
72 =over 4
73  
74 =item $fields = changelog_parse(%opts)
75  
76 This function will parse a changelog. In list context, it returns as many
77 L<Dpkg::Control> objects as the parser did create. In scalar context, it will
78 return only the first one. If the parser did not return any data, it will
79 return an empty list in list context or undef on scalar context. If the
80 parser failed, it will die. Any parse errors will be printed as warnings
81 on standard error, but this can be disabled by passing $opts{verbose} to 0.
82  
83 The parsing itself is done by a parser module (searched in the standard
84 perl library directories. That module is named according to the format that
85 it is able to parse, with the name capitalized. By default it is either
86 L<Dpkg::Changelog::Debian> (from the "debian" format) or the format name looked
87 up in the 40 last lines of the changelog itself (extracted with this perl
88 regular expression "\schangelog-format:\s+([0-9a-z]+)\W"). But it can be
89 overridden with $opts{changelogformat}.
90  
91 All the other keys in %opts are forwarded to the parser module constructor.
92  
93 Options:
94  
95 =over
96  
97 =item B<filename>
98  
99 Set the changelog file to parse.
100 Defaults to F<debian/changelog>.
101  
102 =item B<file>
103  
104 Deprecated alias for B<filename>.
105 It emits a deprecation warning, but if it is required for backwards
106 compatibility it can be passed alongside B<filename> and the warning
107 will not be emitted.
108  
109 =item B<label>
110  
111 Set the changelog name used in output messages.
112 Defaults to $opts{filename}.
113  
114 =item B<compression>
115  
116 Set a boolean on whether to load the file without compression support.
117 If the file is the default compression is disabled,
118 otherwise the default is to enable compression.
119  
120 =item B<changelogformat>
121  
122 Set the changelog input format to use.
123  
124 =item B<format>
125  
126 Set the output format to use.
127 Defaults to "dpkg".
128  
129 =item B<verbose>
130  
131 Set whether to print any parse errors as warnings to standard error.
132 Defaults to true.
133  
134 =back
135  
136 =cut
137  
138 sub changelog_parse {
139 my (%opts) = @_;
140  
141 $opts{verbose} //= 1;
142 if (exists $opts{file} && ! exists $opts{filename}) {
143 warnings::warnif('deprecated',
144 'Dpkg::Changelog::Parse::changelog_parse() option file is deprecated, ' .
145 'switch to use filename, or pass file alongside it');
146 }
147 $opts{filename} //= $opts{file} // 'debian/changelog';
148 $opts{label} //= $opts{filename};
149 $opts{changelogformat} //= _changelog_detect_format($opts{filename});
150 $opts{format} //= 'dpkg';
151 $opts{compression} //= $opts{filename} ne 'debian/changelog';
152  
153 my @range_opts = qw(since until from to offset count reverse all);
154 $opts{all} = 1 if exists $opts{all};
155 if (none { defined $opts{$_} } @range_opts) {
156 $opts{count} = 1;
157 }
158 my $range;
159 foreach my $opt (@range_opts) {
160 $range->{$opt} = $opts{$opt} if exists $opts{$opt};
161 }
162  
163 # Find the right changelog parser.
164 my $format = ucfirst lc $opts{changelogformat};
165 my $changes;
166 my $module = "Dpkg::Changelog::$format";
167 eval qq{
168 require $module;
169 };
170 error(g_('changelog format %s is unknown: %s'), $format, $@) if $@;
171 $changes = $module->new();
172 error(g_('changelog format %s is not a Dpkg::Changelog class'), $format)
173 unless $changes->isa('Dpkg::Changelog');
174 $changes->set_options(
175 reportfile => $opts{label},
176 verbose => $opts{verbose},
177 range => $range,
178 );
179  
180 # Load and parse the changelog.
181 $changes->load($opts{filename}, compression => $opts{compression})
182 or error(g_('cannot parse changelog %s'), $opts{filename});
183  
184 # Get the output into several Dpkg::Control objects.
185 my @res;
186 if ($opts{format} eq 'dpkg') {
187 push @res, $changes->format_range('dpkg', $range);
188 } elsif ($opts{format} eq 'rfc822') {
189 push @res, $changes->format_range('rfc822', $range);
190 } else {
191 error(g_('unknown output format %s'), $opts{format});
192 }
193  
194 if (wantarray) {
195 return @res;
196 } else {
197 return $res[0] if @res;
198 return;
199 }
200 }
201  
202 =back
203  
204 =head1 CHANGES
205  
206 =head2 Version 2.02 (dpkg 1.23.0)
207  
208 New options: 'filename in changelog_parse().
209  
210 Deprecated options: 'file' in changelog_parse().
211  
212 =head2 Version 2.01 (dpkg 1.20.6)
213  
214 New option: 'verbose' in changelog_parse().
215  
216 =head2 Version 2.00 (dpkg 1.20.0)
217  
218 Remove functions: changelog_parse_debian(), changelog_parse_plugin().
219  
220 Remove warnings: For options 'forceplugin', 'libdir'.
221  
222 =head2 Version 1.03 (dpkg 1.19.0)
223  
224 New option: 'compression' in changelog_parse().
225  
226 =head2 Version 1.02 (dpkg 1.18.8)
227  
228 Deprecated functions: changelog_parse_debian(), changelog_parse_plugin().
229  
230 Obsolete options: forceplugin, libdir.
231  
232 =head2 Version 1.01 (dpkg 1.18.2)
233  
234 New functions: changelog_parse_debian(), changelog_parse_plugin().
235  
236 =head2 Version 1.00 (dpkg 1.15.6)
237  
238 Mark the module as public.
239  
240 =cut
241  
242 1;