Debian.pm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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).
  51. 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;
  52. ## use critic
  53. =head1 METHODS
  54. =over 4
  55. =item @items = $entry->get_change_items()
  56. Return a list of change items. Each item contains at least one line.
  57. A change line starting with an asterisk denotes the start of a new item.
  58. Any change line like "[ Raphaël Hertzog ]" is treated like an item of its
  59. own even if it starts a set of items attributed to this person (the
  60. following line necessarily starts a new item).
  61. =cut
  62. sub get_change_items {
  63. my $self = shift;
  64. my (@items, @blanks, $item);
  65. foreach my $line (@{$self->get_part('changes')}) {
  66. if ($line =~ /^\s*\*/) {
  67. push @items, $item if defined $item;
  68. $item = "$line\n";
  69. } elsif ($line =~ /^\s*\[\s[^\]]+\s\]\s*$/) {
  70. push @items, $item if defined $item;
  71. push @items, "$line\n";
  72. $item = undef;
  73. @blanks = ();
  74. } elsif ($line =~ /^\s*$/) {
  75. push @blanks, "$line\n";
  76. } else {
  77. if (defined $item) {
  78. $item .= "@blanks$line\n";
  79. } else {
  80. $item = "$line\n";
  81. }
  82. @blanks = ();
  83. }
  84. }
  85. push @items, $item if defined $item;
  86. return @items;
  87. }
  88. =item @errors = $entry->check_header()
  89. =item @errors = $entry->check_trailer()
  90. Return a list of errors. Each item in the list is an error message
  91. describing the problem. If the empty list is returned, no errors
  92. have been found.
  93. =cut
  94. sub check_header {
  95. my $self = shift;
  96. my @errors;
  97. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  98. my ($version, $options) = ($2, $4);
  99. $options =~ s/^\s+//;
  100. my %optdone;
  101. foreach my $opt (split(/\s*,\s*/, $options)) {
  102. unless ($opt =~ m/^([-0-9a-z]+)\=\s*(.*\S)$/i) {
  103. push @errors, sprintf(g_("bad key-value after ';': '%s'"), $opt);
  104. next;
  105. }
  106. my ($k, $v) = (field_capitalize($1), $2);
  107. if ($optdone{$k}) {
  108. push @errors, sprintf(g_('repeated key-value %s'), $k);
  109. }
  110. $optdone{$k} = 1;
  111. if ($k eq 'Urgency') {
  112. push @errors, sprintf(g_('badly formatted urgency value: %s'), $v)
  113. unless ($v =~ m/^([-0-9a-z]+)((\s+.*)?)$/i);
  114. } elsif ($k eq 'Binary-Only') {
  115. push @errors, sprintf(g_('bad binary-only value: %s'), $v)
  116. unless ($v eq 'yes');
  117. } elsif ($k =~ m/^X[BCS]+-/i) {
  118. } else {
  119. push @errors, sprintf(g_('unknown key-value %s'), $k);
  120. }
  121. }
  122. my ($ok, $msg) = version_check($version);
  123. unless ($ok) {
  124. push @errors, sprintf(g_("version '%s' is invalid: %s"), $version, $msg);
  125. }
  126. } else {
  127. push @errors, g_("the header doesn't match the expected regex");
  128. }
  129. return @errors;
  130. }
  131. sub check_trailer {
  132. my $self = shift;
  133. my @errors;
  134. if (defined($self->{trailer}) and $self->{trailer} =~ $regex_trailer) {
  135. if ($3 ne ' ') {
  136. push @errors, g_('badly formatted trailer line');
  137. }
  138. my $fmt = '';
  139. $fmt .= '%a, ' if defined $5;
  140. $fmt .= '%d %b %Y %T %z';
  141. local $ENV{LC_ALL} = 'C';
  142. unless (defined Time::Piece->strptime($4, $fmt)) {
  143. push @errors, sprintf(g_("couldn't parse date %s"), $4);
  144. }
  145. } else {
  146. push @errors, g_("the trailer doesn't match the expected regex");
  147. }
  148. return @errors;
  149. }
  150. =item $entry->normalize()
  151. Normalize the content. Strip whitespaces at end of lines, use a single
  152. empty line to separate each part.
  153. =cut
  154. sub normalize {
  155. my $self = shift;
  156. $self->SUPER::normalize();
  157. #XXX: recreate header/trailer
  158. }
  159. sub get_source {
  160. my $self = shift;
  161. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  162. return $1;
  163. }
  164. return;
  165. }
  166. sub get_version {
  167. my $self = shift;
  168. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  169. return Dpkg::Version->new($2);
  170. }
  171. return;
  172. }
  173. sub get_distributions {
  174. my $self = shift;
  175. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  176. my @dists = split ' ', $3;
  177. return @dists if wantarray;
  178. return $dists[0];
  179. }
  180. return;
  181. }
  182. sub get_optional_fields {
  183. my $self = shift;
  184. my $f = Dpkg::Control::Changelog->new();
  185. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  186. my $options = $4;
  187. $options =~ s/^\s+//;
  188. foreach my $opt (split(/\s*,\s*/, $options)) {
  189. if ($opt =~ m/^([-0-9a-z]+)\=\s*(.*\S)$/i) {
  190. $f->{$1} = $2;
  191. }
  192. }
  193. }
  194. my @closes = find_closes(join("\n", @{$self->{changes}}));
  195. if (@closes) {
  196. $f->{Closes} = join(' ', @closes);
  197. }
  198. return $f;
  199. }
  200. sub get_urgency {
  201. my $self = shift;
  202. my $f = $self->get_optional_fields();
  203. if (exists $f->{Urgency}) {
  204. $f->{Urgency} =~ s/\s.*$//;
  205. return lc($f->{Urgency});
  206. }
  207. return;
  208. }
  209. sub get_maintainer {
  210. my $self = shift;
  211. if (defined($self->{trailer}) and $self->{trailer} =~ $regex_trailer) {
  212. return "$1 <$2>";
  213. }
  214. return;
  215. }
  216. sub get_timestamp {
  217. my $self = shift;
  218. if (defined($self->{trailer}) and $self->{trailer} =~ $regex_trailer) {
  219. return $4;
  220. }
  221. return;
  222. }
  223. =back
  224. =head1 UTILITY FUNCTIONS
  225. =over 4
  226. =item $bool = match_header($line)
  227. Checks if the line matches a valid changelog header line.
  228. =cut
  229. sub match_header {
  230. my $line = shift;
  231. return $line =~ /$regex_header/;
  232. }
  233. =item $bool = match_trailer($line)
  234. Checks if the line matches a valid changelog trailing line.
  235. =cut
  236. sub match_trailer {
  237. my $line = shift;
  238. return $line =~ /$regex_trailer/;
  239. }
  240. =item @closed_bugs = find_closes($changes)
  241. Takes one string as argument and finds "Closes: #123456, #654321" statements
  242. as supported by the Debian Archive software in it. Returns all closed bug
  243. numbers in an array.
  244. =cut
  245. sub find_closes {
  246. my $changes = shift;
  247. my %closes;
  248. while ($changes &&
  249. ($changes =~ /closes:\s*(?:bug)?\#?\s?\d+(?:,\s*(?:bug)?\#?\s?\d+)*/pig)) {
  250. $closes{$_} = 1 foreach (${^MATCH} =~ /\#?\s?(\d+)/g);
  251. }
  252. my @closes = sort { $a <=> $b } keys %closes;
  253. return @closes;
  254. }
  255. =back
  256. =head1 CHANGES
  257. =head2 Version 1.01 (dpkg 1.17.2)
  258. New functions: match_header(), match_trailer()
  259. Deprecated variables: $regex_header, $regex_trailer
  260. =head2 Version 1.00 (dpkg 1.15.6)
  261. Mark the module as public.
  262. =head1 AUTHOR
  263. Raphaël Hertzog <hertzog@debian.org>.
  264. =cut
  265. 1;