Debian.pm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 Date::Parse;
  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+\([^\\\(\)]\))?)\s*$/o;
  52. ## use critic
  53. =head1 METHODS
  54. =over 4
  55. =item my @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 my @errors = $entry->check_header()
  89. =item my @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. unless (defined str2time($4)) {
  139. push @errors, sprintf(g_("couldn't parse date %s"), $4);
  140. }
  141. } else {
  142. push @errors, g_("the trailer doesn't match the expected regex");
  143. }
  144. return @errors;
  145. }
  146. =item $entry->normalize()
  147. Normalize the content. Strip whitespaces at end of lines, use a single
  148. empty line to separate each part.
  149. =cut
  150. sub normalize {
  151. my $self = shift;
  152. $self->SUPER::normalize();
  153. #XXX: recreate header/trailer
  154. }
  155. sub get_source {
  156. my $self = shift;
  157. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  158. return $1;
  159. }
  160. return;
  161. }
  162. sub get_version {
  163. my $self = shift;
  164. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  165. return Dpkg::Version->new($2);
  166. }
  167. return;
  168. }
  169. sub get_distributions {
  170. my $self = shift;
  171. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  172. my $value = $3;
  173. $value =~ s/^\s+//;
  174. my @dists = split(/\s+/, $value);
  175. return @dists if wantarray;
  176. return $dists[0];
  177. }
  178. return;
  179. }
  180. sub get_optional_fields {
  181. my $self = shift;
  182. my $f = Dpkg::Control::Changelog->new();
  183. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  184. my $options = $4;
  185. $options =~ s/^\s+//;
  186. foreach my $opt (split(/\s*,\s*/, $options)) {
  187. if ($opt =~ m/^([-0-9a-z]+)\=\s*(.*\S)$/i) {
  188. $f->{$1} = $2;
  189. }
  190. }
  191. }
  192. my @closes = find_closes(join("\n", @{$self->{changes}}));
  193. if (@closes) {
  194. $f->{Closes} = join(' ', @closes);
  195. }
  196. return $f;
  197. }
  198. sub get_urgency {
  199. my $self = shift;
  200. my $f = $self->get_optional_fields();
  201. if (exists $f->{Urgency}) {
  202. $f->{Urgency} =~ s/\s.*$//;
  203. return lc($f->{Urgency});
  204. }
  205. return;
  206. }
  207. sub get_maintainer {
  208. my $self = shift;
  209. if (defined($self->{trailer}) and $self->{trailer} =~ $regex_trailer) {
  210. return "$1 <$2>";
  211. }
  212. return;
  213. }
  214. sub get_timestamp {
  215. my $self = shift;
  216. if (defined($self->{trailer}) and $self->{trailer} =~ $regex_trailer) {
  217. return $4;
  218. }
  219. return;
  220. }
  221. =back
  222. =head1 UTILITY FUNCTIONS
  223. =over 4
  224. =item my $bool = match_header($line)
  225. Checks if the line matches a valid changelog header line.
  226. =cut
  227. sub match_header {
  228. my $line = shift;
  229. return $line =~ /$regex_header/;
  230. }
  231. =item my $bool = match_trailer($line)
  232. Checks if the line matches a valid changelog trailing line.
  233. =cut
  234. sub match_trailer {
  235. my $line = shift;
  236. return $line =~ /$regex_trailer/;
  237. }
  238. =item my @closed_bugs = find_closes($changes)
  239. Takes one string as argument and finds "Closes: #123456, #654321" statements
  240. as supported by the Debian Archive software in it. Returns all closed bug
  241. numbers in an array.
  242. =cut
  243. sub find_closes {
  244. my $changes = shift;
  245. my %closes;
  246. while ($changes &&
  247. ($changes =~ /closes:\s*(?:bug)?\#?\s?\d+(?:,\s*(?:bug)?\#?\s?\d+)*/pig)) {
  248. $closes{$_} = 1 foreach (${^MATCH} =~ /\#?\s?(\d+)/g);
  249. }
  250. my @closes = sort { $a <=> $b } keys %closes;
  251. return @closes;
  252. }
  253. =back
  254. =head1 CHANGES
  255. =head2 Version 1.01 (dpkg 1.17.2)
  256. New functions: match_header(), match_trailer()
  257. Deprecated variables: $regex_header, $regex_trailer
  258. =head2 Version 1.00 (dpkg 1.15.6)
  259. Mark the module as public.
  260. =head1 AUTHOR
  261. Raphaël Hertzog <hertzog@debian.org>.
  262. =cut
  263. 1;