Debian.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #
  2. # Dpkg::Changelog::Debian
  3. #
  4. # Copyright 1996 Ian Jackson
  5. # Copyright 2005 Frank Lichtenheld <frank@lichtenheld.de>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. #
  21. =head1 NAME
  22. Dpkg::Changelog::Debian - parse Debian changelogs
  23. =head1 SYNOPSIS
  24. use Parse::DebianChangelog;
  25. my $chglog = Parse::DebianChangelog->init( { infile => 'debian/changelog',
  26. HTML => { outfile => 'changelog.html' } );
  27. $chglog->html;
  28. # the following is semantically equivalent
  29. my $chglog = Parse::DebianChangelog->init();
  30. $chglog->parse( { infile => 'debian/changelog' } );
  31. $chglog->html( { outfile => 'changelog.html' } );
  32. my $changes = $chglog->dpkg_str( { since => '1.0-1' } );
  33. print $changes;
  34. =head1 DESCRIPTION
  35. Dpkg::Changelog::Debian parses Debian changelogs as described in the Debian
  36. policy (version 3.6.2.1 at the time of this writing). See section
  37. L<"SEE ALSO"> for locations where to find this definition.
  38. The parser tries to ignore most cruft like # or /* */ style comments,
  39. CVS comments, vim variables, emacs local variables and stuff from
  40. older changelogs with other formats at the end of the file.
  41. NOTE: most of these are ignored silently currently, there is no
  42. parser error issued for them. This should become configurable in the
  43. future.
  44. =head2 METHODS
  45. =cut
  46. package Dpkg::Changelog::Debian;
  47. use strict;
  48. use warnings;
  49. use Fcntl qw( :flock );
  50. use English;
  51. use Date::Parse;
  52. use Dpkg;
  53. use Dpkg::Gettext;
  54. use Dpkg::Changelog qw( :util );
  55. use base qw(Dpkg::Changelog);
  56. =pod
  57. =head3 parse
  58. Parses either the file named in configuration item C<infile>, the content
  59. of the filehandle in configuration item C<inhandle>, or the string
  60. saved in configuration item C<instring> (the latter requires IO::String).
  61. You can set a filename to use for reporting errors with configuration
  62. item C<reportfile>.
  63. Accepts a hash ref as optional argument which can contain configuration
  64. items.
  65. Returns C<undef> in case of error (e.g. "file not found", B<not> parse
  66. errors) and the object if successful. If C<undef> was returned, you
  67. can get the reason for the failure by calling the L<get_error> method.
  68. =cut
  69. sub parse {
  70. my ($self, $config) = @_;
  71. foreach my $c (keys %$config) {
  72. $self->{config}{$c} = $config->{$c};
  73. }
  74. my ($fh, $file);
  75. if ($file = $self->{config}{infile}) {
  76. open $fh, '<', $file or do {
  77. $self->_do_fatal_error( _g("can't open file %s: %s"),
  78. $file, $! );
  79. return undef;
  80. };
  81. } elsif ($fh = $self->{config}{inhandle}) {
  82. $file = 'FileHandle';
  83. } elsif (my $string = $self->{config}{instring}) {
  84. eval { require IO::String };
  85. if ($@) {
  86. $self->_do_fatal_error( _g("can't load IO::String: %s"),
  87. $@ );
  88. return undef;
  89. }
  90. $fh = IO::String->new( $string );
  91. $file = 'String';
  92. } else {
  93. $self->_do_fatal_error(_g('no changelog file specified'));
  94. return undef;
  95. }
  96. if (defined($self->{config}{reportfile})) {
  97. $file = $self->{config}{reportfile};
  98. }
  99. $self->reset_parse_errors;
  100. $self->{data} = [];
  101. # based on /usr/lib/dpkg/parsechangelog/debian
  102. my $expect='first heading';
  103. my $entry = new Dpkg::Changelog::Entry;
  104. my $blanklines = 0;
  105. my $unknowncounter = 1; # to make version unique, e.g. for using as id
  106. while (<$fh>) {
  107. s/\s*\n$//;
  108. # printf(STDERR "%-39.39s %-39.39s\n",$expect,$_);
  109. if (m/^(\w[-+0-9a-z.]*) \(([^\(\) \t]+)\)((\s+[-0-9a-z]+)+)\;/i) {
  110. unless ($expect eq 'first heading'
  111. || $expect eq 'next heading or eof') {
  112. $entry->{ERROR} = [ $file, $NR,
  113. sprintf(_g("found start of entry where expected %s"),
  114. $expect), "$_" ];
  115. $self->_do_parse_error(@{$entry->{ERROR}});
  116. }
  117. unless ($entry->is_empty) {
  118. $entry->{'Closes'} = find_closes( $entry->{Changes} );
  119. # print STDERR, Dumper($entry);
  120. push @{$self->{data}}, $entry;
  121. $entry = new Dpkg::Changelog::Entry;
  122. last if $self->_abort_early;
  123. }
  124. {
  125. $entry->{'Source'} = "$1";
  126. $entry->{'Version'} = "$2";
  127. $entry->{'Header'} = "$_";
  128. ($entry->{'Distribution'} = "$3") =~ s/^\s+//;
  129. $entry->{'Changes'} = $entry->{'Urgency_comment'} = '';
  130. $entry->{'Urgency'} = $entry->{'Urgency_lc'} = 'unknown';
  131. }
  132. (my $rhs = $POSTMATCH) =~ s/^\s+//;
  133. my %kvdone;
  134. # print STDERR "RHS: $rhs\n";
  135. for my $kv (split(/\s*,\s*/,$rhs)) {
  136. $kv =~ m/^([-0-9a-z]+)\=\s*(.*\S)$/i ||
  137. $self->_do_parse_error($file, $NR,
  138. sprintf(_g("bad key-value after \`;': \`%s'"), $kv));
  139. my $k = ucfirst $1;
  140. my $v = $2;
  141. $kvdone{$k}++ && $self->_do_parse_error($file, $NR,
  142. sprintf(_g("repeated key-value %s"), $k));
  143. if ($k eq 'Urgency') {
  144. $v =~ m/^([-0-9a-z]+)((\s+.*)?)$/i ||
  145. $self->_do_parse_error($file, $NR,
  146. _g("badly formatted urgency value"),
  147. $v);
  148. $entry->{'Urgency'} = "$1";
  149. $entry->{'Urgency_lc'} = lc("$1");
  150. $entry->{'Urgency_comment'} = "$2";
  151. } elsif ($k =~ m/^X[BCS]+-/i) {
  152. # Extensions - XB for putting in Binary,
  153. # XC for putting in Control, XS for putting in Source
  154. $entry->{$k}= $v;
  155. } else {
  156. $self->_do_parse_error($file, $NR,
  157. sprintf(_g("unknown key-value key %s - copying to XS-%s"), $k, $k));
  158. $entry->{"XS-$k"} = $v;
  159. }
  160. }
  161. $expect= 'start of change data';
  162. $blanklines = 0;
  163. } elsif (m/^(;;\s*)?Local variables:/io) {
  164. last; # skip Emacs variables at end of file
  165. } elsif (m/^vim:/io) {
  166. last; # skip vim variables at end of file
  167. } elsif (m/^\$\w+:.*\$/o) {
  168. next; # skip stuff that look like a CVS keyword
  169. } elsif (m/^\# /o) {
  170. next; # skip comments, even that's not supported
  171. } elsif (m,^/\*.*\*/,o) {
  172. next; # more comments
  173. } 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
  174. || m/^(\w+\s+\w+\s+\d{1,2},?\s*\d{4})\s+(.*)\s+(<|\()(.*)(\)|>)/o
  175. || m/^(\w[-+0-9a-z.]*) \(([^\(\) \t]+)\)\;?/io
  176. || m/^([\w.+-]+)(-| )(\S+) Debian (\S+)/io
  177. || m/^Changes from version (.*) to (.*):/io
  178. || m/^Changes for [\w.+-]+-[\w.+-]+:?$/io
  179. || m/^Old Changelog:$/io
  180. || m/^(?:\d+:)?\w[\w.+~-]*:?$/o) {
  181. # save entries on old changelog format verbatim
  182. # we assume the rest of the file will be in old format once we
  183. # hit it for the first time
  184. $self->{oldformat} = "$_\n";
  185. $self->{oldformat} .= join "", <$fh>;
  186. } elsif (m/^\S/) {
  187. $self->_do_parse_error($file, $NR,
  188. _g("badly formatted heading line"), "$_");
  189. } elsif (m/^ \-\- (.*) <(.*)>( ?)((\w+\,\s*)?\d{1,2}\s+\w+\s+\d{4}\s+\d{1,2}:\d\d:\d\d\s+[-+]\d{4}(\s+\([^\\\(\)]\))?)$/o) {
  190. $expect eq 'more change data or trailer' ||
  191. $self->_do_parse_error($file, $NR,
  192. sprintf(_g("found trailer where expected %s"),
  193. $expect), "$_");
  194. if ($3 ne ' ') {
  195. $self->_do_parse_error($file, $NR,
  196. _g( "badly formatted trailer line" ),
  197. "$_");
  198. }
  199. $entry->{'Trailer'} = $_;
  200. $entry->{'Maintainer'} = "$1 <$2>" unless $entry->{'Maintainer'};
  201. unless($entry->{'Date'} && defined $entry->{'Timestamp'}) {
  202. $entry->{'Date'} = "$4";
  203. $entry->{'Timestamp'} = str2time($4);
  204. unless (defined $entry->{'Timestamp'}) {
  205. $self->_do_parse_error( $file, $NR,
  206. sprintf(_g("couldn't parse date %s"),
  207. "$4"));
  208. }
  209. }
  210. $expect = 'next heading or eof';
  211. } elsif (m/^ \-\-/) {
  212. $entry->{ERROR} = [ $file, $NR,
  213. _g( "badly formatted trailer line" ), "$_" ];
  214. $self->_do_parse_error(@{$entry->{ERROR}});
  215. # $expect = 'next heading or eof'
  216. # if $expect eq 'more change data or trailer';
  217. } elsif (m/^\s{2,}(\S)/) {
  218. $expect eq 'start of change data'
  219. || $expect eq 'more change data or trailer'
  220. || do {
  221. $self->_do_parse_error($file, $NR,
  222. sprintf(_g("found change data where expected %s"),
  223. $expect), "$_");
  224. if (($expect eq 'next heading or eof')
  225. && !$entry->is_empty) {
  226. # lets assume we have missed the actual header line
  227. $entry->{'Closes'} = find_closes( $entry->{Changes} );
  228. # print STDERR, Dumper($entry);
  229. push @{$self->{data}}, $entry;
  230. $entry = Dpkg::Changelog::Entry->init();
  231. $entry->{Source} =
  232. $entry->{Distribution} = $entry->{Urgency} =
  233. $entry->{Urgency_LC} = 'unknown';
  234. $entry->{Version} = 'unknown'.($unknowncounter++);
  235. $entry->{Urgency_Comment} = '';
  236. $entry->{ERROR} = [ $file, $NR,
  237. sprintf(_g("found change data where expected %s"),
  238. $expect), "$_" ];
  239. }
  240. };
  241. $entry->{'Changes'} .= (" \n" x $blanklines)." $_\n";
  242. if (!$entry->{'Items'} || ($1 eq '*')) {
  243. $entry->{'Items'} ||= [];
  244. push @{$entry->{'Items'}}, "$_\n";
  245. } else {
  246. $entry->{'Items'}[-1] .= (" \n" x $blanklines)." $_\n";
  247. }
  248. $blanklines = 0;
  249. $expect = 'more change data or trailer';
  250. } elsif (!m/\S/) {
  251. next if $expect eq 'start of change data'
  252. || $expect eq 'next heading or eof';
  253. $expect eq 'more change data or trailer'
  254. || $self->_do_parse_error($file, $NR,
  255. sprintf(_g("found blank line where expected %s"),
  256. $expect));
  257. $blanklines++;
  258. } else {
  259. $self->_do_parse_error($file, $NR, _g( "unrecognised line" ),
  260. "$_");
  261. ($expect eq 'start of change data'
  262. || $expect eq 'more change data or trailer')
  263. && do {
  264. # lets assume change data if we expected it
  265. $entry->{'Changes'} .= (" \n" x $blanklines)." $_\n";
  266. if (!$entry->{'Items'}) {
  267. $entry->{'Items'} ||= [];
  268. push @{$entry->{'Items'}}, "$_\n";
  269. } else {
  270. $entry->{'Items'}[-1] .= (" \n" x $blanklines)." $_\n";
  271. }
  272. $blanklines = 0;
  273. $expect = 'more change data or trailer';
  274. $entry->{ERROR} = [ $file, $NR, _g( "unrecognised line" ),
  275. "$_" ];
  276. };
  277. }
  278. }
  279. $expect eq 'next heading or eof'
  280. || do {
  281. $entry->{ERROR} = [ $file, $NR,
  282. sprintf(_g("found eof where expected %s"),
  283. $expect) ];
  284. $self->_do_parse_error( @{$entry->{ERROR}} );
  285. };
  286. unless ($entry->is_empty) {
  287. $entry->{'Closes'} = find_closes( $entry->{Changes} );
  288. push @{$self->{data}}, $entry;
  289. }
  290. if ($self->{config}{infile}) {
  291. close $fh or do {
  292. $self->_do_fatal_error( _g("can't close file %s: %s"),
  293. $file, $!);
  294. return undef;
  295. };
  296. }
  297. # use Data::Dumper;
  298. # print Dumper( $self );
  299. return $self;
  300. }
  301. 1;
  302. __END__
  303. =head1 SEE ALSO
  304. Dpkg::Changelog
  305. Description of the Debian changelog format in the Debian policy:
  306. L<http://www.debian.org/doc/debian-policy/ch-source.html#s-dpkgchangelog>.
  307. =head1 AUTHOR
  308. Frank Lichtenheld, E<lt>frank@lichtenheld.deE<gt>
  309. =head1 COPYRIGHT AND LICENSE
  310. Copyright (C) 2005 by Frank Lichtenheld
  311. This program is free software; you can redistribute it and/or modify
  312. it under the terms of the GNU General Public License as published by
  313. the Free Software Foundation; either version 2 of the License, or
  314. (at your option) any later version.
  315. This program is distributed in the hope that it will be useful,
  316. but WITHOUT ANY WARRANTY; without even the implied warranty of
  317. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  318. GNU General Public License for more details.
  319. You should have received a copy of the GNU General Public License
  320. along with this program; if not, write to the Free Software
  321. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  322. =cut