Debian.pm 6.9 KB

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