Debian.pm 6.8 KB

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