Debian.pm 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 ($version, $options) = ($2, $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. my ($ok, $msg) = version_check($version);
  112. unless ($ok) {
  113. push @errors, sprintf(_g("version '%s' is invalid: %s"), $version, $msg);
  114. }
  115. } else {
  116. push @errors, _g("the header doesn't match the expected regex");
  117. }
  118. return @errors;
  119. }
  120. sub check_trailer {
  121. my ($self) = @_;
  122. my @errors;
  123. if (defined($self->{trailer}) and $self->{trailer} =~ $regex_trailer) {
  124. if ($3 ne ' ') {
  125. push @errors, _g("badly formatted trailer line");
  126. }
  127. unless (defined str2time($4)) {
  128. push @errors, sprintf(_g("couldn't parse date %s"), $4);
  129. }
  130. } else {
  131. push @errors, _g("the trailer doesn't match the expected regex");
  132. }
  133. return @errors;
  134. }
  135. =item $entry->normalize()
  136. Normalize the content. Strip whitespaces at end of lines, use a single
  137. empty line to separate each part.
  138. =cut
  139. sub normalize {
  140. my ($self) = @_;
  141. $self->SUPER::normalize();
  142. #XXX: recreate header/trailer
  143. }
  144. sub get_source {
  145. my ($self) = @_;
  146. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  147. return $1;
  148. }
  149. return undef;
  150. }
  151. sub get_version {
  152. my ($self) = @_;
  153. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  154. return Dpkg::Version->new($2);
  155. }
  156. return undef;
  157. }
  158. sub get_distributions {
  159. my ($self) = @_;
  160. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  161. my $value = $3;
  162. $value =~ s/^\s+//;
  163. my @dists = split(/\s+/, $value);
  164. return @dists if wantarray;
  165. return $dists[0];
  166. }
  167. return () if wantarray;
  168. return undef;
  169. }
  170. sub get_optional_fields {
  171. my ($self) = @_;
  172. my $f = Dpkg::Control::Changelog->new();
  173. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  174. my $options = $4;
  175. $options =~ s/^\s+//;
  176. foreach my $opt (split(/\s*,\s*/, $options)) {
  177. if ($opt =~ m/^([-0-9a-z]+)\=\s*(.*\S)$/i) {
  178. $f->{$1} = $2;
  179. }
  180. }
  181. }
  182. my @closes = find_closes(join("\n", @{$self->{changes}}));
  183. if (@closes) {
  184. $f->{Closes} = join(" ", @closes);
  185. }
  186. return $f;
  187. }
  188. sub get_urgency {
  189. my ($self) = @_;
  190. my $f = $self->get_optional_fields();
  191. if (exists $f->{Urgency}) {
  192. $f->{Urgency} =~ s/\s.*$//;
  193. return lc($f->{Urgency});
  194. }
  195. return undef;
  196. }
  197. sub get_maintainer {
  198. my ($self) = @_;
  199. if (defined($self->{trailer}) and $self->{trailer} =~ $regex_trailer) {
  200. return "$1 <$2>";
  201. }
  202. return undef;
  203. }
  204. sub get_timestamp {
  205. my ($self) = @_;
  206. if (defined($self->{trailer}) and $self->{trailer} =~ $regex_trailer) {
  207. return $4;
  208. }
  209. return undef;
  210. }
  211. =back
  212. =head1 UTILITY FUNCTIONS
  213. =head3 my @closed_bugs = find_closes($changes)
  214. Takes one string as argument and finds "Closes: #123456, #654321" statements
  215. as supported by the Debian Archive software in it. Returns all closed bug
  216. numbers in an array.
  217. =cut
  218. sub find_closes {
  219. my $changes = shift;
  220. my %closes;
  221. while ($changes &&
  222. ($changes =~ /closes:\s*(?:bug)?\#?\s?\d+(?:,\s*(?:bug)?\#?\s?\d+)*/ig)) {
  223. $closes{$_} = 1 foreach($& =~ /\#?\s?(\d+)/g);
  224. }
  225. my @closes = sort { $a <=> $b } keys %closes;
  226. return @closes;
  227. }
  228. =head1 AUTHOR
  229. Raphaël Hertzog <hertzog@debian.org>.
  230. =cut
  231. 1;