Debian.pm 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # Copyright © 1996 Ian Jackson
  2. # Copyright © 2005 Frank Lichtenheld <frank@lichtenheld.de>
  3. # Copyright © 2009 Raphaël Hertzog <hertzog@debian.org>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. =head1 NAME
  18. Dpkg::Changelog::Debian - parse Debian changelogs
  19. =head1 DESCRIPTION
  20. Dpkg::Changelog::Debian parses Debian changelogs as described in the Debian
  21. policy (version 3.6.2.1 at the time of this writing). See section
  22. L<"SEE ALSO"> for locations where to find this definition.
  23. The parser tries to ignore most cruft like # or /* */ style comments,
  24. CVS comments, vim variables, emacs local variables and stuff from
  25. older changelogs with other formats at the end of the file.
  26. NOTE: most of these are ignored silently currently, there is no
  27. parser error issued for them. This should become configurable in the
  28. future.
  29. =head2 METHODS
  30. =cut
  31. package Dpkg::Changelog::Debian;
  32. use strict;
  33. use warnings;
  34. use Dpkg::Gettext;
  35. use Dpkg::Changelog qw(:util);
  36. use base qw(Dpkg::Changelog);
  37. use Dpkg::Changelog::Entry::Debian qw($regex_header $regex_trailer);
  38. use constant {
  39. FIRST_HEADING => _g('first heading'),
  40. NEXT_OR_EOF => _g('next heading or eof'),
  41. START_CHANGES => _g('start of change data'),
  42. CHANGES_OR_TRAILER => _g('more change data or trailer'),
  43. };
  44. =pod
  45. =head3 $c->parse($fh, $description)
  46. Read the filehandle and parse a Debian changelog in it. Returns the number
  47. of changelog entries that have been parsed with success.
  48. =cut
  49. sub parse {
  50. my ($self, $fh, $file) = @_;
  51. $file = $self->{reportfile} if exists $self->{reportfile};
  52. $self->reset_parse_errors;
  53. $self->{data} = [];
  54. $self->set_unparsed_tail(undef);
  55. my $expect = FIRST_HEADING;
  56. my $entry = Dpkg::Changelog::Entry::Debian->new();
  57. my @blanklines = ();
  58. my $unknowncounter = 1; # to make version unique, e.g. for using as id
  59. while (<$fh>) {
  60. chomp;
  61. if ($_ =~ $regex_header) {
  62. (my $options = $4) =~ s/^\s+//;
  63. unless ($expect eq FIRST_HEADING || $expect eq NEXT_OR_EOF) {
  64. $self->parse_error($file, $.,
  65. sprintf(_g("found start of entry where expected %s"),
  66. $expect), "$_");
  67. }
  68. unless ($entry->is_empty) {
  69. push @{$self->{data}}, $entry;
  70. $entry = Dpkg::Changelog::Entry::Debian->new();
  71. last if $self->abort_early();
  72. }
  73. $entry->set_part('header', $_);
  74. foreach my $error ($entry->check_header()) {
  75. $self->parse_error($file, $., $error, $_);
  76. }
  77. $expect= START_CHANGES;
  78. @blanklines = ();
  79. } elsif (m/^(;;\s*)?Local variables:/io) {
  80. last; # skip Emacs variables at end of file
  81. } elsif (m/^vim:/io) {
  82. last; # skip vim variables at end of file
  83. } elsif (m/^\$\w+:.*\$/o) {
  84. next; # skip stuff that look like a CVS keyword
  85. } elsif (m/^\# /o) {
  86. next; # skip comments, even that's not supported
  87. } elsif (m,^/\*.*\*/,o) {
  88. next; # more comments
  89. } elsif (m/^(\w+\s+\w+\s+\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}\s+[\w\s]*\d{4})\s+(.*)\s+(<|\()(.*)(\)|>)/o
  90. || m/^(\w+\s+\w+\s+\d{1,2},?\s*\d{4})\s+(.*)\s+(<|\()(.*)(\)|>)/o
  91. || m/^(\w[-+0-9a-z.]*) \(([^\(\) \t]+)\)\;?/io
  92. || m/^([\w.+-]+)(-| )(\S+) Debian (\S+)/io
  93. || m/^Changes from version (.*) to (.*):/io
  94. || m/^Changes for [\w.+-]+-[\w.+-]+:?\s*$/io
  95. || m/^Old Changelog:\s*$/io
  96. || m/^(?:\d+:)?\w[\w.+~-]*:?\s*$/o) {
  97. # save entries on old changelog format verbatim
  98. # we assume the rest of the file will be in old format once we
  99. # hit it for the first time
  100. $self->set_unparsed_tail("$_\n" . join("", <$fh>));
  101. } elsif (m/^\S/) {
  102. $self->parse_error($file, $., _g("badly formatted heading line"), "$_");
  103. } elsif ($_ =~ $regex_trailer) {
  104. unless ($expect eq CHANGES_OR_TRAILER) {
  105. $self->parse_error($file, $.,
  106. sprintf(_g("found trailer where expected %s"), $expect), "$_");
  107. }
  108. $entry->set_part("trailer", $_);
  109. $entry->extend_part("blank_after_changes", [ @blanklines ]);
  110. @blanklines = ();
  111. foreach my $error ($entry->check_trailer()) {
  112. $self->parse_error($file, $., $error, $_);
  113. }
  114. $expect = NEXT_OR_EOF;
  115. } elsif (m/^ \-\-/) {
  116. $self->parse_error($file, $., _g("badly formatted trailer line"), "$_");
  117. } elsif (m/^\s{2,}(\S)/) {
  118. unless ($expect eq START_CHANGES or $expect eq CHANGES_OR_TRAILER) {
  119. $self->parse_error($file, $., sprintf(_g("found change data" .
  120. " where expected %s"), $expect), "$_");
  121. if ($expect eq NEXT_OR_EOF and not $entry->is_empty) {
  122. # lets assume we have missed the actual header line
  123. push @{$self->{data}}, $entry;
  124. $entry = Dpkg::Changelog::Entry::Debian->new();
  125. $entry->set_part('header', "unknown (unknown" . ($unknowncounter++) . ") unknown; urgency=unknown");
  126. }
  127. }
  128. # Keep raw changes
  129. $entry->extend_part('changes', [ @blanklines, $_ ]);
  130. @blanklines = ();
  131. $expect = CHANGES_OR_TRAILER;
  132. } elsif (!m/\S/) {
  133. if ($expect eq START_CHANGES) {
  134. $entry->extend_part("blank_after_header", $_);
  135. next;
  136. } elsif ($expect eq NEXT_OR_EOF) {
  137. $entry->extend_part("blank_after_trailer", $_);
  138. next;
  139. } elsif ($expect ne CHANGES_OR_TRAILER) {
  140. $self->parse_error($file, $.,
  141. sprintf(_g("found blank line where expected %s"), $expect));
  142. }
  143. push @blanklines, $_;
  144. } else {
  145. $self->parse_error($file, $., _g("unrecognised line"), "$_");
  146. unless ($expect eq START_CHANGES or $expect eq CHANGES_OR_TRAILER) {
  147. # lets assume change data if we expected it
  148. $entry->extend_part("changes", [ @blanklines, $_]);
  149. @blanklines = ();
  150. $expect = CHANGES_OR_TRAILER;
  151. }
  152. }
  153. }
  154. unless ($expect eq NEXT_OR_EOF) {
  155. $self->parse_error($file, $., sprintf(_g("found eof where expected %s"),
  156. $expect));
  157. }
  158. unless ($entry->is_empty) {
  159. push @{$self->{data}}, $entry;
  160. }
  161. return scalar @{$self->{data}};
  162. }
  163. 1;
  164. __END__
  165. =head1 SEE ALSO
  166. Dpkg::Changelog
  167. Description of the Debian changelog format in the Debian policy:
  168. L<http://www.debian.org/doc/debian-policy/ch-source.html#s-dpkgchangelog>.
  169. =head1 AUTHORS
  170. Frank Lichtenheld, E<lt>frank@lichtenheld.deE<gt>
  171. Raphaël Hertzog, E<lt>hertzog@debian.orgE<gt>
  172. =cut