Debian.pm 6.6 KB

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