Debian.pm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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, write to the Free Software
  17. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. #
  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. 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 parse
  48. Parses either the file named in configuration item C<infile>, the content
  49. of the filehandle in configuration item C<inhandle>, or the string
  50. saved in configuration item C<instring> (the latter requires IO::String).
  51. You can set a filename to use for reporting errors with configuration
  52. item C<reportfile>.
  53. Accepts a hash ref as optional argument which can contain configuration
  54. items.
  55. Returns C<undef> in case of error (e.g. "file not found", B<not> parse
  56. errors) and the object if successful. If C<undef> was returned, you
  57. can get the reason for the failure by calling the L<get_error> method.
  58. =cut
  59. sub parse {
  60. my ($self, $config) = @_;
  61. foreach my $c (keys %$config) {
  62. $self->{config}{$c} = $config->{$c};
  63. }
  64. my ($fh, $file);
  65. if ($file = $self->{config}{infile}) {
  66. open $fh, '<', $file or do {
  67. $self->_do_fatal_error( _g("can't open file %s: %s"),
  68. $file, $! );
  69. return undef;
  70. };
  71. } elsif ($fh = $self->{config}{inhandle}) {
  72. $file = 'FileHandle';
  73. } elsif (my $string = $self->{config}{instring}) {
  74. eval { require IO::String };
  75. if ($@) {
  76. $self->_do_fatal_error( _g("can't load IO::String: %s"),
  77. $@ );
  78. return undef;
  79. }
  80. $fh = IO::String->new( $string );
  81. $file = 'String';
  82. } else {
  83. $self->_do_fatal_error(_g('no changelog file specified'));
  84. return undef;
  85. }
  86. if (defined($self->{config}{reportfile})) {
  87. $file = $self->{config}{reportfile};
  88. }
  89. $self->reset_parse_errors;
  90. $self->{data} = [];
  91. my $expect = FIRST_HEADING;
  92. my $entry = Dpkg::Changelog::Entry::Debian->new();
  93. my @blanklines = ();
  94. my $unknowncounter = 1; # to make version unique, e.g. for using as id
  95. while (<$fh>) {
  96. chomp;
  97. if ($_ =~ $regex_header) {
  98. (my $options = $4) =~ s/^\s+//;
  99. unless ($expect eq FIRST_HEADING || $expect eq NEXT_OR_EOF) {
  100. $self->_do_parse_error($file, $.,
  101. sprintf(_g("found start of entry where expected %s"),
  102. $expect), "$_");
  103. }
  104. unless ($entry->is_empty) {
  105. push @{$self->{data}}, $entry;
  106. $entry = Dpkg::Changelog::Entry::Debian->new();
  107. last if $self->_abort_early;
  108. }
  109. $entry->set_part('header', $_);
  110. foreach my $error ($entry->check_header()) {
  111. $self->_do_parse_error($file, $., $error, $_);
  112. }
  113. $expect= START_CHANGES;
  114. @blanklines = ();
  115. } elsif (m/^(;;\s*)?Local variables:/io) {
  116. last; # skip Emacs variables at end of file
  117. } elsif (m/^vim:/io) {
  118. last; # skip vim variables at end of file
  119. } elsif (m/^\$\w+:.*\$/o) {
  120. next; # skip stuff that look like a CVS keyword
  121. } elsif (m/^\# /o) {
  122. next; # skip comments, even that's not supported
  123. } elsif (m,^/\*.*\*/,o) {
  124. next; # more comments
  125. } 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
  126. || m/^(\w+\s+\w+\s+\d{1,2},?\s*\d{4})\s+(.*)\s+(<|\()(.*)(\)|>)/o
  127. || m/^(\w[-+0-9a-z.]*) \(([^\(\) \t]+)\)\;?/io
  128. || m/^([\w.+-]+)(-| )(\S+) Debian (\S+)/io
  129. || m/^Changes from version (.*) to (.*):/io
  130. || m/^Changes for [\w.+-]+-[\w.+-]+:?\s*$/io
  131. || m/^Old Changelog:\s*$/io
  132. || m/^(?:\d+:)?\w[\w.+~-]*:?\s*$/o) {
  133. # save entries on old changelog format verbatim
  134. # we assume the rest of the file will be in old format once we
  135. # hit it for the first time
  136. $self->{oldformat} = "$_\n";
  137. $self->{oldformat} .= join "", <$fh>;
  138. } elsif (m/^\S/) {
  139. $self->_do_parse_error($file, $.,
  140. _g("badly formatted heading line"), "$_");
  141. } elsif ($_ =~ $regex_trailer) {
  142. unless ($expect eq CHANGES_OR_TRAILER) {
  143. $self->_do_parse_error($file, $.,
  144. sprintf(_g("found trailer where expected %s"), $expect), "$_");
  145. }
  146. $entry->set_part("trailer", $_);
  147. $entry->extend_part("blank_after_changes", [ @blanklines ]);
  148. @blanklines = ();
  149. foreach my $error ($entry->check_header()) {
  150. $self->_do_parse_error($file, $., $error, $_);
  151. }
  152. $expect = NEXT_OR_EOF;
  153. } elsif (m/^ \-\-/) {
  154. $self->_do_parse_error($file, $.,
  155. _g( "badly formatted trailer line" ), "$_");
  156. } elsif (m/^\s{2,}(\S)/) {
  157. unless ($expect eq START_CHANGES or $expect eq CHANGES_OR_TRAILER) {
  158. $self->_do_parse_error($file, $., sprintf(_g("found change data" .
  159. " where expected %s"), $expect), "$_");
  160. if ($expect eq NEXT_OR_EOF and not $entry->is_empty) {
  161. # lets assume we have missed the actual header line
  162. push @{$self->{data}}, $entry;
  163. $entry = Dpkg::Changelog::Entry::Debian->new();
  164. $entry->set_part('header', "unknown (unknown" . ($unknowncounter++) . ") unknown; urgency=unknown");
  165. }
  166. }
  167. # Keep raw changes
  168. $entry->extend_part('changes', [ @blanklines, $_ ]);
  169. @blanklines = ();
  170. $expect = CHANGES_OR_TRAILER;
  171. } elsif (!m/\S/) {
  172. if ($expect eq START_CHANGES) {
  173. $entry->extend_part("blank_after_header", $_);
  174. next;
  175. } elsif ($expect eq NEXT_OR_EOF) {
  176. $entry->extend_part("blank_after_trailer", $_);
  177. next;
  178. } elsif ($expect ne CHANGES_OR_TRAILER) {
  179. $self->_do_parse_error($file, $.,
  180. sprintf(_g("found blank line where expected %s"), $expect));
  181. }
  182. push @blanklines, $_;
  183. } else {
  184. $self->_do_parse_error($file, $., _g("unrecognised line"), "$_");
  185. unless ($expect eq START_CHANGES or $expect eq CHANGES_OR_TRAILER) {
  186. # lets assume change data if we expected it
  187. $entry->extend_part("changes", [ @blanklines, $_]);
  188. @blanklines = ();
  189. $expect = CHANGES_OR_TRAILER;
  190. }
  191. }
  192. }
  193. unless ($expect eq NEXT_OR_EOF) {
  194. $self->_do_parse_error($file, $.,
  195. sprintf(_g("found eof where expected %s"), $expect));
  196. }
  197. unless ($entry->is_empty) {
  198. push @{$self->{data}}, $entry;
  199. }
  200. if ($self->{config}{infile}) {
  201. close $fh or do {
  202. $self->_do_fatal_error( _g("can't close file %s: %s"),
  203. $file, $!);
  204. return undef;
  205. };
  206. }
  207. return $self;
  208. }
  209. 1;
  210. __END__
  211. =head1 SEE ALSO
  212. Dpkg::Changelog
  213. Description of the Debian changelog format in the Debian policy:
  214. L<http://www.debian.org/doc/debian-policy/ch-source.html#s-dpkgchangelog>.
  215. =head1 AUTHORS
  216. Frank Lichtenheld, E<lt>frank@lichtenheld.deE<gt>
  217. Raphaël Hertzog, E<lt>hertzog@debian.orgE<gt>
  218. =cut