| OLD | NEW |
| (Empty) |
| 1 # Copyright © 2009 Raphaël Hertzog <hertzog@debian.org> | |
| 2 # Copyright © 2012-2013 Guillem Jover <guillem@debian.org> | |
| 3 # | |
| 4 # This program is free software; you can redistribute it and/or modify | |
| 5 # it under the terms of the GNU General Public License as published by | |
| 6 # the Free Software Foundation; either version 2 of the License, or | |
| 7 # (at your option) any later version. | |
| 8 # | |
| 9 # This program is distributed in the hope that it will be useful, | |
| 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 # GNU General Public License for more details. | |
| 13 # | |
| 14 # You should have received a copy of the GNU General Public License | |
| 15 # along with this program. If not, see <https://www.gnu.org/licenses/>. | |
| 16 | |
| 17 package Dpkg::Changelog::Entry::Debian; | |
| 18 | |
| 19 use strict; | |
| 20 use warnings; | |
| 21 | |
| 22 our $VERSION = '1.01'; | |
| 23 | |
| 24 use Exporter qw(import); | |
| 25 use Dpkg::Changelog::Entry; | |
| 26 use parent qw(Dpkg::Changelog::Entry); | |
| 27 our @EXPORT_OK = qw(match_header match_trailer find_closes | |
| 28 $regex_header $regex_trailer); | |
| 29 | |
| 30 use Date::Parse; | |
| 31 | |
| 32 use Dpkg::Gettext; | |
| 33 use Dpkg::Control::Fields; | |
| 34 use Dpkg::Control::Changelog; | |
| 35 use Dpkg::Version; | |
| 36 | |
| 37 =encoding utf8 | |
| 38 | |
| 39 =head1 NAME | |
| 40 | |
| 41 Dpkg::Changelog::Entry::Debian - represents a Debian changelog entry | |
| 42 | |
| 43 =head1 DESCRIPTION | |
| 44 | |
| 45 This object represents a Debian changelog entry. It implements the | |
| 46 generic interface Dpkg::Changelog::Entry. Only functions specific to this | |
| 47 implementation are described below. | |
| 48 | |
| 49 =cut | |
| 50 | |
| 51 my $name_chars = qr/[-+0-9a-z.]/i; | |
| 52 | |
| 53 # XXX: Backwards compatibility, stop exporting on VERSION 2.00. | |
| 54 ## no critic (Variables::ProhibitPackageVars) | |
| 55 | |
| 56 # The matched content is the source package name ($1), the version ($2), | |
| 57 # the target distributions ($3) and the options on the rest of the line ($4). | |
| 58 our $regex_header = qr/^(\w$name_chars*) \(([^\(\) \t]+)\)((?:\s+$name_chars+)+)
\;(.*?)\s*$/i; | |
| 59 | |
| 60 # The matched content is the maintainer name ($1), its email ($2), | |
| 61 # some blanks ($3) and the timestamp ($4). | |
| 62 our $regex_trailer = qr/^ \-\- (.*) <(.*)>( ?)((\w+\,\s*)?\d{1,2}\s+\w+\s+\d{4}
\s+\d{1,2}:\d\d:\d\d\s+[-+]\d{4}(\s+\([^\\\(\)]\))?)\s*$/o; | |
| 63 | |
| 64 ## use critic | |
| 65 | |
| 66 =head1 FUNCTIONS | |
| 67 | |
| 68 =over 4 | |
| 69 | |
| 70 =item my @items = $entry->get_change_items() | |
| 71 | |
| 72 Return a list of change items. Each item contains at least one line. | |
| 73 A change line starting with an asterisk denotes the start of a new item. | |
| 74 Any change line like "[ Raphaël Hertzog ]" is treated like an item of its | |
| 75 own even if it starts a set of items attributed to this person (the | |
| 76 following line necessarily starts a new item). | |
| 77 | |
| 78 =cut | |
| 79 | |
| 80 sub get_change_items { | |
| 81 my ($self) = @_; | |
| 82 my (@items, @blanks, $item); | |
| 83 foreach my $line (@{$self->get_part('changes')}) { | |
| 84 if ($line =~ /^\s*\*/) { | |
| 85 push @items, $item if defined $item; | |
| 86 $item = "$line\n"; | |
| 87 } elsif ($line =~ /^\s*\[\s[^\]]+\s\]\s*$/) { | |
| 88 push @items, $item if defined $item; | |
| 89 push @items, "$line\n"; | |
| 90 $item = undef; | |
| 91 @blanks = (); | |
| 92 } elsif ($line =~ /^\s*$/) { | |
| 93 push @blanks, "$line\n"; | |
| 94 } else { | |
| 95 if (defined $item) { | |
| 96 $item .= "@blanks$line\n"; | |
| 97 } else { | |
| 98 $item = "$line\n"; | |
| 99 } | |
| 100 @blanks = (); | |
| 101 } | |
| 102 } | |
| 103 push @items, $item if defined $item; | |
| 104 return @items; | |
| 105 } | |
| 106 | |
| 107 =item my @errors = $entry->check_header() | |
| 108 | |
| 109 =item my @errors = $entry->check_trailer() | |
| 110 | |
| 111 Return a list of errors. Each item in the list is an error message | |
| 112 describing the problem. If the empty list is returned, no errors | |
| 113 have been found. | |
| 114 | |
| 115 =cut | |
| 116 | |
| 117 sub check_header { | |
| 118 my ($self) = @_; | |
| 119 my @errors; | |
| 120 if (defined($self->{header}) and $self->{header} =~ $regex_header) { | |
| 121 my ($version, $options) = ($2, $4); | |
| 122 $options =~ s/^\s+//; | |
| 123 my %optdone; | |
| 124 foreach my $opt (split(/\s*,\s*/, $options)) { | |
| 125 unless ($opt =~ m/^([-0-9a-z]+)\=\s*(.*\S)$/i) { | |
| 126 push @errors, sprintf(_g("bad key-value after \`;': \`%s'"), $op
t); | |
| 127 next; | |
| 128 } | |
| 129 my ($k, $v) = (field_capitalize($1), $2); | |
| 130 if ($optdone{$k}) { | |
| 131 push @errors, sprintf(_g('repeated key-value %s'), $k); | |
| 132 } | |
| 133 $optdone{$k} = 1; | |
| 134 if ($k eq 'Urgency') { | |
| 135 push @errors, sprintf(_g('badly formatted urgency value: %s'), $
v) | |
| 136 unless ($v =~ m/^([-0-9a-z]+)((\s+.*)?)$/i); | |
| 137 } elsif ($k eq 'Binary-Only') { | |
| 138 push @errors, sprintf(_g('bad binary-only value: %s'), $v) | |
| 139 unless ($v eq 'yes'); | |
| 140 } elsif ($k =~ m/^X[BCS]+-/i) { | |
| 141 } else { | |
| 142 push @errors, sprintf(_g('unknown key-value %s'), $k); | |
| 143 } | |
| 144 } | |
| 145 my ($ok, $msg) = version_check($version); | |
| 146 unless ($ok) { | |
| 147 push @errors, sprintf(_g("version '%s' is invalid: %s"), $version, $
msg); | |
| 148 } | |
| 149 } else { | |
| 150 push @errors, _g("the header doesn't match the expected regex"); | |
| 151 } | |
| 152 return @errors; | |
| 153 } | |
| 154 | |
| 155 sub check_trailer { | |
| 156 my ($self) = @_; | |
| 157 my @errors; | |
| 158 if (defined($self->{trailer}) and $self->{trailer} =~ $regex_trailer) { | |
| 159 if ($3 ne ' ') { | |
| 160 push @errors, _g('badly formatted trailer line'); | |
| 161 } | |
| 162 unless (defined str2time($4)) { | |
| 163 push @errors, sprintf(_g("couldn't parse date %s"), $4); | |
| 164 } | |
| 165 } else { | |
| 166 push @errors, _g("the trailer doesn't match the expected regex"); | |
| 167 } | |
| 168 return @errors; | |
| 169 } | |
| 170 | |
| 171 =item $entry->normalize() | |
| 172 | |
| 173 Normalize the content. Strip whitespaces at end of lines, use a single | |
| 174 empty line to separate each part. | |
| 175 | |
| 176 =cut | |
| 177 | |
| 178 sub normalize { | |
| 179 my ($self) = @_; | |
| 180 $self->SUPER::normalize(); | |
| 181 #XXX: recreate header/trailer | |
| 182 } | |
| 183 | |
| 184 sub get_source { | |
| 185 my ($self) = @_; | |
| 186 if (defined($self->{header}) and $self->{header} =~ $regex_header) { | |
| 187 return $1; | |
| 188 } | |
| 189 return; | |
| 190 } | |
| 191 | |
| 192 sub get_version { | |
| 193 my ($self) = @_; | |
| 194 if (defined($self->{header}) and $self->{header} =~ $regex_header) { | |
| 195 return Dpkg::Version->new($2); | |
| 196 } | |
| 197 return; | |
| 198 } | |
| 199 | |
| 200 sub get_distributions { | |
| 201 my ($self) = @_; | |
| 202 if (defined($self->{header}) and $self->{header} =~ $regex_header) { | |
| 203 my $value = $3; | |
| 204 $value =~ s/^\s+//; | |
| 205 my @dists = split(/\s+/, $value); | |
| 206 return @dists if wantarray; | |
| 207 return $dists[0]; | |
| 208 } | |
| 209 return; | |
| 210 } | |
| 211 | |
| 212 sub get_optional_fields { | |
| 213 my ($self) = @_; | |
| 214 my $f = Dpkg::Control::Changelog->new(); | |
| 215 if (defined($self->{header}) and $self->{header} =~ $regex_header) { | |
| 216 my $options = $4; | |
| 217 $options =~ s/^\s+//; | |
| 218 foreach my $opt (split(/\s*,\s*/, $options)) { | |
| 219 if ($opt =~ m/^([-0-9a-z]+)\=\s*(.*\S)$/i) { | |
| 220 $f->{$1} = $2; | |
| 221 } | |
| 222 } | |
| 223 } | |
| 224 my @closes = find_closes(join("\n", @{$self->{changes}})); | |
| 225 if (@closes) { | |
| 226 $f->{Closes} = join(' ', @closes); | |
| 227 } | |
| 228 return $f; | |
| 229 } | |
| 230 | |
| 231 sub get_urgency { | |
| 232 my ($self) = @_; | |
| 233 my $f = $self->get_optional_fields(); | |
| 234 if (exists $f->{Urgency}) { | |
| 235 $f->{Urgency} =~ s/\s.*$//; | |
| 236 return lc($f->{Urgency}); | |
| 237 } | |
| 238 return; | |
| 239 } | |
| 240 | |
| 241 sub get_maintainer { | |
| 242 my ($self) = @_; | |
| 243 if (defined($self->{trailer}) and $self->{trailer} =~ $regex_trailer) { | |
| 244 return "$1 <$2>"; | |
| 245 } | |
| 246 return; | |
| 247 } | |
| 248 | |
| 249 sub get_timestamp { | |
| 250 my ($self) = @_; | |
| 251 if (defined($self->{trailer}) and $self->{trailer} =~ $regex_trailer) { | |
| 252 return $4; | |
| 253 } | |
| 254 return; | |
| 255 } | |
| 256 | |
| 257 =back | |
| 258 | |
| 259 =head1 UTILITY FUNCTIONS | |
| 260 | |
| 261 =over 4 | |
| 262 | |
| 263 =item my $bool = match_header($line) | |
| 264 | |
| 265 Checks if the line matches a valid changelog header line. | |
| 266 | |
| 267 =cut | |
| 268 | |
| 269 sub match_header { | |
| 270 my ($line) = @_; | |
| 271 | |
| 272 return $line =~ /$regex_header/; | |
| 273 } | |
| 274 | |
| 275 =item my $bool = match_trailer($line) | |
| 276 | |
| 277 Checks if the line matches a valid changelog trailing line. | |
| 278 | |
| 279 =cut | |
| 280 | |
| 281 sub match_trailer { | |
| 282 my ($line) = @_; | |
| 283 | |
| 284 return $line =~ /$regex_trailer/; | |
| 285 } | |
| 286 | |
| 287 =item my @closed_bugs = find_closes($changes) | |
| 288 | |
| 289 Takes one string as argument and finds "Closes: #123456, #654321" statements | |
| 290 as supported by the Debian Archive software in it. Returns all closed bug | |
| 291 numbers in an array. | |
| 292 | |
| 293 =cut | |
| 294 | |
| 295 sub find_closes { | |
| 296 my $changes = shift; | |
| 297 my %closes; | |
| 298 | |
| 299 while ($changes && | |
| 300 ($changes =~ /closes:\s*(?:bug)?\#?\s?\d+(?:,\s*(?:bug)?\#?\s?\d+)*/i
g)) { | |
| 301 $closes{$_} = 1 foreach($& =~ /\#?\s?(\d+)/g); | |
| 302 } | |
| 303 | |
| 304 my @closes = sort { $a <=> $b } keys %closes; | |
| 305 return @closes; | |
| 306 } | |
| 307 | |
| 308 =back | |
| 309 | |
| 310 =head1 CHANGES | |
| 311 | |
| 312 =head2 Version 1.01 | |
| 313 | |
| 314 New functions: match_header(), match_trailer() | |
| 315 Deprecated variables: $regex_header, $regex_trailer | |
| 316 | |
| 317 =head1 AUTHOR | |
| 318 | |
| 319 Raphaël Hertzog <hertzog@debian.org>. | |
| 320 | |
| 321 =cut | |
| 322 | |
| 323 1; | |
| OLD | NEW |