Debian.pm 6.9 KB

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