Debian.pm 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. package Dpkg::Changelog::Entry::Debian;
  17. use strict;
  18. use warnings;
  19. our $VERSION = '1.01';
  20. our @EXPORT_OK = qw(
  21. $regex_header
  22. $regex_trailer
  23. match_header
  24. match_trailer
  25. find_closes
  26. );
  27. use Exporter qw(import);
  28. use Time::Piece;
  29. use Dpkg::Gettext;
  30. use Dpkg::Control::Fields;
  31. use Dpkg::Control::Changelog;
  32. use Dpkg::Changelog::Entry;
  33. use Dpkg::Version;
  34. use parent qw(Dpkg::Changelog::Entry);
  35. =encoding utf8
  36. =head1 NAME
  37. Dpkg::Changelog::Entry::Debian - represents a Debian changelog entry
  38. =head1 DESCRIPTION
  39. This object represents a Debian changelog entry. It implements the
  40. generic interface Dpkg::Changelog::Entry. Only functions specific to this
  41. implementation are described below.
  42. =cut
  43. my $name_chars = qr/[-+0-9a-z.]/i;
  44. # XXX: Backwards compatibility, stop exporting on VERSION 2.00.
  45. ## no critic (Variables::ProhibitPackageVars)
  46. # The matched content is the source package name ($1), the version ($2),
  47. # the target distributions ($3) and the options on the rest of the line ($4).
  48. our $regex_header = qr/^(\w$name_chars*) \(([^\(\) \t]+)\)((?:\s+$name_chars+)+)\;(.*?)\s*$/i;
  49. # The matched content is the maintainer name ($1), its email ($2),
  50. # some blanks ($3) and the timestamp ($4), which is decomposed into
  51. # day of week ($6), date-time ($7) and this into month name ($8).
  52. 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*$/o;
  53. my %week_day = map { $_ => 1 } qw(Mon Tue Wed Thu Fri Sat Sun);
  54. my %month_abbrev = map { $_ => 1 } qw(
  55. Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
  56. );
  57. my %month_name = map { $_ => } qw(
  58. January February March April May June July
  59. August September October November December
  60. );
  61. ## use critic
  62. =head1 METHODS
  63. =over 4
  64. =item @items = $entry->get_change_items()
  65. Return a list of change items. Each item contains at least one line.
  66. A change line starting with an asterisk denotes the start of a new item.
  67. Any change line like "[ Raphaël Hertzog ]" is treated like an item of its
  68. own even if it starts a set of items attributed to this person (the
  69. following line necessarily starts a new item).
  70. =cut
  71. sub get_change_items {
  72. my $self = shift;
  73. my (@items, @blanks, $item);
  74. foreach my $line (@{$self->get_part('changes')}) {
  75. if ($line =~ /^\s*\*/) {
  76. push @items, $item if defined $item;
  77. $item = "$line\n";
  78. } elsif ($line =~ /^\s*\[\s[^\]]+\s\]\s*$/) {
  79. push @items, $item if defined $item;
  80. push @items, "$line\n";
  81. $item = undef;
  82. @blanks = ();
  83. } elsif ($line =~ /^\s*$/) {
  84. push @blanks, "$line\n";
  85. } else {
  86. if (defined $item) {
  87. $item .= "@blanks$line\n";
  88. } else {
  89. $item = "$line\n";
  90. }
  91. @blanks = ();
  92. }
  93. }
  94. push @items, $item if defined $item;
  95. return @items;
  96. }
  97. =item @errors = $entry->check_header()
  98. =item @errors = $entry->check_trailer()
  99. Return a list of errors. Each item in the list is an error message
  100. describing the problem. If the empty list is returned, no errors
  101. have been found.
  102. =cut
  103. sub check_header {
  104. my $self = shift;
  105. my @errors;
  106. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  107. my ($version, $options) = ($2, $4);
  108. $options =~ s/^\s+//;
  109. my %optdone;
  110. foreach my $opt (split(/\s*,\s*/, $options)) {
  111. unless ($opt =~ m/^([-0-9a-z]+)\=\s*(.*\S)$/i) {
  112. push @errors, sprintf(g_("bad key-value after ';': '%s'"), $opt);
  113. next;
  114. }
  115. my ($k, $v) = (field_capitalize($1), $2);
  116. if ($optdone{$k}) {
  117. push @errors, sprintf(g_('repeated key-value %s'), $k);
  118. }
  119. $optdone{$k} = 1;
  120. if ($k eq 'Urgency') {
  121. push @errors, sprintf(g_('badly formatted urgency value: %s'), $v)
  122. unless ($v =~ m/^([-0-9a-z]+)((\s+.*)?)$/i);
  123. } elsif ($k eq 'Binary-Only') {
  124. push @errors, sprintf(g_('bad binary-only value: %s'), $v)
  125. unless ($v eq 'yes');
  126. } elsif ($k =~ m/^X[BCS]+-/i) {
  127. } else {
  128. push @errors, sprintf(g_('unknown key-value %s'), $k);
  129. }
  130. }
  131. my ($ok, $msg) = version_check($version);
  132. unless ($ok) {
  133. push @errors, sprintf(g_("version '%s' is invalid: %s"), $version, $msg);
  134. }
  135. } else {
  136. push @errors, g_("the header doesn't match the expected regex");
  137. }
  138. return @errors;
  139. }
  140. sub check_trailer {
  141. my $self = shift;
  142. my @errors;
  143. if (defined($self->{trailer}) and $self->{trailer} =~ $regex_trailer) {
  144. if ($3 ne ' ') {
  145. push @errors, g_('badly formatted trailer line');
  146. }
  147. # Validate the week day. Date::Parse used to ignore it, but Time::Piece
  148. # is much more strict and it does not gracefully handle bogus values.
  149. if (defined $5 and not exists $week_day{$6}) {
  150. push @errors, sprintf(g_('ignoring invalid week day \'%s\''), $6);
  151. }
  152. # Ignore the week day ('%a, '), as we have validated it above.
  153. local $ENV{LC_ALL} = 'C';
  154. eval {
  155. Time::Piece->strptime($7, '%d %b %Y %T %z');
  156. } or do {
  157. # Validate the month. Date::Parse used to accept both abbreviated
  158. # and full months, but Time::Piece strptime() implementation only
  159. # matches the abbreviated one with %b, which is what we want anyway.
  160. if (exists $month_name{$8}) {
  161. push @errors, sprintf(g_('uses full instead of abbreviated month name \'%s\''),
  162. $8, $month_name{$8});
  163. } elsif (not exists $month_abbrev{$8}) {
  164. push @errors, sprintf(g_('invalid abbreviated month name \'%s\''), $8);
  165. }
  166. push @errors, sprintf(g_("cannot parse non-comformant date '%s'"), $7);
  167. };
  168. } else {
  169. push @errors, g_("the trailer doesn't match the expected regex");
  170. }
  171. return @errors;
  172. }
  173. =item $entry->normalize()
  174. Normalize the content. Strip whitespaces at end of lines, use a single
  175. empty line to separate each part.
  176. =cut
  177. sub normalize {
  178. my $self = shift;
  179. $self->SUPER::normalize();
  180. #XXX: recreate header/trailer
  181. }
  182. sub get_source {
  183. my $self = shift;
  184. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  185. return $1;
  186. }
  187. return;
  188. }
  189. sub get_version {
  190. my $self = shift;
  191. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  192. return Dpkg::Version->new($2);
  193. }
  194. return;
  195. }
  196. sub get_distributions {
  197. my $self = shift;
  198. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  199. my @dists = split ' ', $3;
  200. return @dists if wantarray;
  201. return $dists[0];
  202. }
  203. return;
  204. }
  205. sub get_optional_fields {
  206. my $self = shift;
  207. my $f = Dpkg::Control::Changelog->new();
  208. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  209. my $options = $4;
  210. $options =~ s/^\s+//;
  211. foreach my $opt (split(/\s*,\s*/, $options)) {
  212. if ($opt =~ m/^([-0-9a-z]+)\=\s*(.*\S)$/i) {
  213. $f->{$1} = $2;
  214. }
  215. }
  216. }
  217. my @closes = find_closes(join("\n", @{$self->{changes}}));
  218. if (@closes) {
  219. $f->{Closes} = join(' ', @closes);
  220. }
  221. return $f;
  222. }
  223. sub get_urgency {
  224. my $self = shift;
  225. my $f = $self->get_optional_fields();
  226. if (exists $f->{Urgency}) {
  227. $f->{Urgency} =~ s/\s.*$//;
  228. return lc($f->{Urgency});
  229. }
  230. return;
  231. }
  232. sub get_maintainer {
  233. my $self = shift;
  234. if (defined($self->{trailer}) and $self->{trailer} =~ $regex_trailer) {
  235. return "$1 <$2>";
  236. }
  237. return;
  238. }
  239. sub get_timestamp {
  240. my $self = shift;
  241. if (defined($self->{trailer}) and $self->{trailer} =~ $regex_trailer) {
  242. return $4;
  243. }
  244. return;
  245. }
  246. =back
  247. =head1 UTILITY FUNCTIONS
  248. =over 4
  249. =item $bool = match_header($line)
  250. Checks if the line matches a valid changelog header line.
  251. =cut
  252. sub match_header {
  253. my $line = shift;
  254. return $line =~ /$regex_header/;
  255. }
  256. =item $bool = match_trailer($line)
  257. Checks if the line matches a valid changelog trailing line.
  258. =cut
  259. sub match_trailer {
  260. my $line = shift;
  261. return $line =~ /$regex_trailer/;
  262. }
  263. =item @closed_bugs = find_closes($changes)
  264. Takes one string as argument and finds "Closes: #123456, #654321" statements
  265. as supported by the Debian Archive software in it. Returns all closed bug
  266. numbers in an array.
  267. =cut
  268. sub find_closes {
  269. my $changes = shift;
  270. my %closes;
  271. while ($changes &&
  272. ($changes =~ /closes:\s*(?:bug)?\#?\s?\d+(?:,\s*(?:bug)?\#?\s?\d+)*/pig)) {
  273. $closes{$_} = 1 foreach (${^MATCH} =~ /\#?\s?(\d+)/g);
  274. }
  275. my @closes = sort { $a <=> $b } keys %closes;
  276. return @closes;
  277. }
  278. =back
  279. =head1 CHANGES
  280. =head2 Version 1.01 (dpkg 1.17.2)
  281. New functions: match_header(), match_trailer()
  282. Deprecated variables: $regex_header, $regex_trailer
  283. =head2 Version 1.00 (dpkg 1.15.6)
  284. Mark the module as public.
  285. =head1 AUTHOR
  286. Raphaël Hertzog <hertzog@debian.org>.
  287. =cut
  288. 1;