Debian.pm 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. # Copyright © 2009 Raphaël Hertzog <hertzog@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package Dpkg::Changelog::Entry::Debian;
  16. use strict;
  17. use warnings;
  18. our $VERSION = "1.00";
  19. use Exporter;
  20. use Dpkg::Changelog::Entry;
  21. use base qw(Exporter Dpkg::Changelog::Entry);
  22. our @EXPORT_OK = qw($regex_header $regex_trailer find_closes);
  23. use Date::Parse;
  24. use Dpkg::Gettext;
  25. use Dpkg::Control::Changelog;
  26. use Dpkg::Version;
  27. =encoding utf8
  28. =head1 NAME
  29. Dpkg::Changelog::Entry::Debian - represents a Debian changelog entry
  30. =head1 DESCRIPTION
  31. This object represents a Debian changelog entry. It implements the
  32. generic interface Dpkg::Changelog::Entry. Only functions specific to this
  33. implementation are described below.
  34. =head1 VARIABLES
  35. $regex_header, $regex_trailer are two regular expressions that can be used
  36. to match a line and know whether it's a valid header/trailer line.
  37. The matched content for $regex_header is the source package name ($1), the
  38. version ($2), the target distributions ($3) and the options on the rest
  39. of the line ($4). For $regex_trailer, it's the maintainer name ($1), its
  40. email ($2), some blanks ($3) and the timestamp ($4).
  41. =cut
  42. my $name_chars = qr/[-+0-9a-z.]/i;
  43. our $regex_header = qr/^(\w$name_chars*) \(([^\(\) \t]+)\)((?:\s+$name_chars+)+)\;(.*?)\s*$/i;
  44. 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;
  45. =head1 FUNCTIONS
  46. =over 4
  47. =item my @items = $entry->get_change_items()
  48. Return a list of change items. Each item contains at least one line.
  49. A change line starting with an asterisk denotes the start of a new item.
  50. Any change line like "[ Raphaël Hertzog ]" is treated like an item of its
  51. own even if it starts a set of items attributed to this person (the
  52. following line necessarily starts a new item).
  53. =cut
  54. sub get_change_items {
  55. my ($self) = @_;
  56. my (@items, @blanks, $item);
  57. foreach my $line (@{$self->get_part("changes")}) {
  58. if ($line =~ /^\s*\*/) {
  59. push @items, $item if defined $item;
  60. $item = "$line\n";
  61. } elsif ($line =~ /^\s*\[\s[^\]]+\s\]\s*$/) {
  62. push @items, $item if defined $item;
  63. push @items, "$line\n";
  64. $item = undef;
  65. @blanks = ();
  66. } elsif ($line =~ /^\s*$/) {
  67. push @blanks, "$line\n";
  68. } else {
  69. if (defined $item) {
  70. $item .= "@blanks$line\n";
  71. } else {
  72. $item = "$line\n";
  73. }
  74. @blanks = ();
  75. }
  76. }
  77. push @items, $item if defined $item;
  78. return @items;
  79. }
  80. =item my @errors = $entry->check_header()
  81. =item my @errors = $entry->check_trailer()
  82. Return a list of errors. Each item in the list is an error message
  83. describing the problem. If the empty list is returned, no errors
  84. have been found.
  85. =cut
  86. sub check_header {
  87. my ($self) = @_;
  88. my @errors;
  89. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  90. my $options = $4;
  91. $options =~ s/^\s+//;
  92. my %optdone;
  93. foreach my $opt (split(/\s*,\s*/, $options)) {
  94. unless ($opt =~ m/^([-0-9a-z]+)\=\s*(.*\S)$/i) {
  95. push @errors, sprintf(_g("bad key-value after \`;': \`%s'"), $opt);
  96. next;
  97. }
  98. my ($k, $v) = (ucfirst($1), $2);
  99. if ($optdone{$k}) {
  100. push @errors, sprintf(_g("repeated key-value %s"), $k);
  101. }
  102. $optdone{$k} = 1;
  103. if ($k eq 'Urgency') {
  104. push @errors, sprintf(_g("badly formatted urgency value: %s"), $v)
  105. unless ($v =~ m/^([-0-9a-z]+)((\s+.*)?)$/i);
  106. } elsif ($k =~ m/^X[BCS]+-/i) {
  107. } else {
  108. push @errors, sprintf(_g("unknown key-value %s"), $k);
  109. }
  110. }
  111. } else {
  112. push @errors, _g("the header doesn't match the expected regex");
  113. }
  114. return @errors;
  115. }
  116. sub check_trailer {
  117. my ($self) = @_;
  118. my @errors;
  119. if (defined($self->{trailer}) and $self->{trailer} =~ $regex_trailer) {
  120. if ($3 ne ' ') {
  121. push @errors, _g("badly formatted trailer line");
  122. }
  123. unless (defined str2time($4)) {
  124. push @errors, sprintf(_g("couldn't parse date %s"), $4);
  125. }
  126. } else {
  127. push @errors, _g("the trailer doesn't match the expected regex");
  128. }
  129. return @errors;
  130. }
  131. =item $entry->normalize()
  132. Normalize the content. Strip whitespaces at end of lines, use a single
  133. empty line to separate each part.
  134. =cut
  135. sub normalize {
  136. my ($self) = @_;
  137. $self->SUPER::normalize();
  138. #XXX: recreate header/trailer
  139. }
  140. sub get_source {
  141. my ($self) = @_;
  142. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  143. return $1;
  144. }
  145. return undef;
  146. }
  147. sub get_version {
  148. my ($self) = @_;
  149. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  150. return Dpkg::Version->new($2);
  151. }
  152. return undef;
  153. }
  154. sub get_distributions {
  155. my ($self) = @_;
  156. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  157. my $value = $3;
  158. $value =~ s/^\s+//;
  159. my @dists = split(/\s+/, $value);
  160. return @dists if wantarray;
  161. return $dists[0];
  162. }
  163. return () if wantarray;
  164. return undef;
  165. }
  166. sub get_optional_fields {
  167. my ($self) = @_;
  168. my $f = Dpkg::Control::Changelog->new();
  169. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  170. my $options = $4;
  171. $options =~ s/^\s+//;
  172. foreach my $opt (split(/\s*,\s*/, $options)) {
  173. if ($opt =~ m/^([-0-9a-z]+)\=\s*(.*\S)$/i) {
  174. $f->{$1} = $2;
  175. }
  176. }
  177. }
  178. my @closes = find_closes(join("\n", @{$self->{changes}}));
  179. if (@closes) {
  180. $f->{Closes} = join(" ", @closes);
  181. }
  182. return $f;
  183. }
  184. sub get_urgency {
  185. my ($self) = @_;
  186. my $f = $self->get_optional_fields();
  187. if (exists $f->{Urgency}) {
  188. $f->{Urgency} =~ s/\s.*$//;
  189. return lc($f->{Urgency});
  190. }
  191. return undef;
  192. }
  193. sub get_maintainer {
  194. my ($self) = @_;
  195. if (defined($self->{trailer}) and $self->{trailer} =~ $regex_trailer) {
  196. return "$1 <$2>";
  197. }
  198. return undef;
  199. }
  200. sub get_timestamp {
  201. my ($self) = @_;
  202. if (defined($self->{trailer}) and $self->{trailer} =~ $regex_trailer) {
  203. return $4;
  204. }
  205. return undef;
  206. }
  207. =back
  208. =head1 UTILITY FUNCTIONS
  209. =head3 my @closed_bugs = find_closes($changes)
  210. Takes one string as argument and finds "Closes: #123456, #654321" statements
  211. as supported by the Debian Archive software in it. Returns all closed bug
  212. numbers in an array.
  213. =cut
  214. sub find_closes {
  215. my $changes = shift;
  216. my %closes;
  217. while ($changes &&
  218. ($changes =~ /closes:\s*(?:bug)?\#?\s?\d+(?:,\s*(?:bug)?\#?\s?\d+)*/ig)) {
  219. $closes{$_} = 1 foreach($& =~ /\#?\s?(\d+)/g);
  220. }
  221. my @closes = sort { $a <=> $b } keys %closes;
  222. return @closes;
  223. }
  224. =head1 AUTHOR
  225. Raphaël Hertzog <hertzog@debian.org>.
  226. =cut
  227. 1;