Debian.pm 6.9 KB

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