Debian.pm 8.0 KB

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