Debian.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. my $name_chars = qr/[-+0-9a-z.]/i;
  110. if (m/^(\w$name_chars*) \(([^\(\) \t]+)\)((\s+$name_chars+)+)\;/i) {
  111. unless ($expect eq 'first heading'
  112. || $expect eq 'next heading or eof') {
  113. $entry->{ERROR} = [ $file, $NR,
  114. sprintf(_g("found start of entry where expected %s"),
  115. $expect), "$_" ];
  116. $self->_do_parse_error(@{$entry->{ERROR}});
  117. }
  118. unless ($entry->is_empty) {
  119. $entry->{'Closes'} = find_closes( $entry->{Changes} );
  120. # print STDERR, Dumper($entry);
  121. push @{$self->{data}}, $entry;
  122. $entry = new Dpkg::Changelog::Entry;
  123. last if $self->_abort_early;
  124. }
  125. {
  126. $entry->{'Source'} = "$1";
  127. $entry->{'Version'} = "$2";
  128. $entry->{'Header'} = "$_";
  129. ($entry->{'Distribution'} = "$3") =~ s/^\s+//;
  130. $entry->{'Changes'} = $entry->{'Urgency_comment'} = '';
  131. $entry->{'Urgency'} = $entry->{'Urgency_lc'} = 'unknown';
  132. }
  133. (my $rhs = $POSTMATCH) =~ s/^\s+//;
  134. my %kvdone;
  135. # print STDERR "RHS: $rhs\n";
  136. for my $kv (split(/\s*,\s*/,$rhs)) {
  137. $kv =~ m/^([-0-9a-z]+)\=\s*(.*\S)$/i ||
  138. $self->_do_parse_error($file, $NR,
  139. sprintf(_g("bad key-value after \`;': \`%s'"), $kv));
  140. my $k = ucfirst $1;
  141. my $v = $2;
  142. $kvdone{$k}++ && $self->_do_parse_error($file, $NR,
  143. sprintf(_g("repeated key-value %s"), $k));
  144. if ($k eq 'Urgency') {
  145. $v =~ m/^([-0-9a-z]+)((\s+.*)?)$/i ||
  146. $self->_do_parse_error($file, $NR,
  147. _g("badly formatted urgency value"),
  148. $v);
  149. $entry->{'Urgency'} = "$1";
  150. $entry->{'Urgency_lc'} = lc("$1");
  151. $entry->{'Urgency_comment'} = "$2";
  152. } elsif ($k =~ m/^X[BCS]+-/i) {
  153. # Extensions - XB for putting in Binary,
  154. # XC for putting in Control, XS for putting in Source
  155. $entry->{$k}= $v;
  156. } else {
  157. $self->_do_parse_error($file, $NR,
  158. sprintf(_g("unknown key-value key %s - copying to XS-%s"), $k, $k));
  159. $entry->{"XS-$k"} = $v;
  160. }
  161. }
  162. $expect= 'start of change data';
  163. $blanklines = 0;
  164. } elsif (m/^(;;\s*)?Local variables:/io) {
  165. last; # skip Emacs variables at end of file
  166. } elsif (m/^vim:/io) {
  167. last; # skip vim variables at end of file
  168. } elsif (m/^\$\w+:.*\$/o) {
  169. next; # skip stuff that look like a CVS keyword
  170. } elsif (m/^\# /o) {
  171. next; # skip comments, even that's not supported
  172. } elsif (m,^/\*.*\*/,o) {
  173. next; # more comments
  174. } 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
  175. || m/^(\w+\s+\w+\s+\d{1,2},?\s*\d{4})\s+(.*)\s+(<|\()(.*)(\)|>)/o
  176. || m/^(\w[-+0-9a-z.]*) \(([^\(\) \t]+)\)\;?/io
  177. || m/^([\w.+-]+)(-| )(\S+) Debian (\S+)/io
  178. || m/^Changes from version (.*) to (.*):/io
  179. || m/^Changes for [\w.+-]+-[\w.+-]+:?$/io
  180. || m/^Old Changelog:$/io
  181. || m/^(?:\d+:)?\w[\w.+~-]*:?$/o) {
  182. # save entries on old changelog format verbatim
  183. # we assume the rest of the file will be in old format once we
  184. # hit it for the first time
  185. $self->{oldformat} = "$_\n";
  186. $self->{oldformat} .= join "", <$fh>;
  187. } elsif (m/^\S/) {
  188. $self->_do_parse_error($file, $NR,
  189. _g("badly formatted heading line"), "$_");
  190. } 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) {
  191. $expect eq 'more change data or trailer' ||
  192. $self->_do_parse_error($file, $NR,
  193. sprintf(_g("found trailer where expected %s"),
  194. $expect), "$_");
  195. if ($3 ne ' ') {
  196. $self->_do_parse_error($file, $NR,
  197. _g( "badly formatted trailer line" ),
  198. "$_");
  199. }
  200. $entry->{'Trailer'} = $_;
  201. $entry->{'Maintainer'} = "$1 <$2>" unless $entry->{'Maintainer'};
  202. unless($entry->{'Date'} && defined $entry->{'Timestamp'}) {
  203. $entry->{'Date'} = "$4";
  204. $entry->{'Timestamp'} = str2time($4);
  205. unless (defined $entry->{'Timestamp'}) {
  206. $self->_do_parse_error( $file, $NR,
  207. sprintf(_g("couldn't parse date %s"),
  208. "$4"));
  209. }
  210. }
  211. $expect = 'next heading or eof';
  212. } elsif (m/^ \-\-/) {
  213. $entry->{ERROR} = [ $file, $NR,
  214. _g( "badly formatted trailer line" ), "$_" ];
  215. $self->_do_parse_error(@{$entry->{ERROR}});
  216. # $expect = 'next heading or eof'
  217. # if $expect eq 'more change data or trailer';
  218. } elsif (m/^\s{2,}(\S)/) {
  219. $expect eq 'start of change data'
  220. || $expect eq 'more change data or trailer'
  221. || do {
  222. $self->_do_parse_error($file, $NR,
  223. sprintf(_g("found change data where expected %s"),
  224. $expect), "$_");
  225. if (($expect eq 'next heading or eof')
  226. && !$entry->is_empty) {
  227. # lets assume we have missed the actual header line
  228. $entry->{'Closes'} = find_closes( $entry->{Changes} );
  229. # print STDERR, Dumper($entry);
  230. push @{$self->{data}}, $entry;
  231. $entry = new Dpkg::Changelog::Entry;
  232. $entry->{Source} =
  233. $entry->{Distribution} = $entry->{Urgency} =
  234. $entry->{Urgency_LC} = 'unknown';
  235. $entry->{Version} = 'unknown'.($unknowncounter++);
  236. $entry->{Urgency_Comment} = '';
  237. $entry->{ERROR} = [ $file, $NR,
  238. sprintf(_g("found change data where expected %s"),
  239. $expect), "$_" ];
  240. }
  241. };
  242. $entry->{'Changes'} .= (" \n" x $blanklines)." $_\n";
  243. if (!$entry->{'Items'} || ($1 eq '*')) {
  244. $entry->{'Items'} ||= [];
  245. push @{$entry->{'Items'}}, "$_\n";
  246. } else {
  247. $entry->{'Items'}[-1] .= (" \n" x $blanklines)." $_\n";
  248. }
  249. $blanklines = 0;
  250. $expect = 'more change data or trailer';
  251. } elsif (!m/\S/) {
  252. next if $expect eq 'start of change data'
  253. || $expect eq 'next heading or eof';
  254. $expect eq 'more change data or trailer'
  255. || $self->_do_parse_error($file, $NR,
  256. sprintf(_g("found blank line where expected %s"),
  257. $expect));
  258. $blanklines++;
  259. } else {
  260. $self->_do_parse_error($file, $NR, _g( "unrecognised line" ),
  261. "$_");
  262. ($expect eq 'start of change data'
  263. || $expect eq 'more change data or trailer')
  264. && do {
  265. # lets assume change data if we expected it
  266. $entry->{'Changes'} .= (" \n" x $blanklines)." $_\n";
  267. if (!$entry->{'Items'}) {
  268. $entry->{'Items'} ||= [];
  269. push @{$entry->{'Items'}}, "$_\n";
  270. } else {
  271. $entry->{'Items'}[-1] .= (" \n" x $blanklines)." $_\n";
  272. }
  273. $blanklines = 0;
  274. $expect = 'more change data or trailer';
  275. $entry->{ERROR} = [ $file, $NR, _g( "unrecognised line" ),
  276. "$_" ];
  277. };
  278. }
  279. }
  280. $expect eq 'next heading or eof'
  281. || do {
  282. $entry->{ERROR} = [ $file, $NR,
  283. sprintf(_g("found eof where expected %s"),
  284. $expect) ];
  285. $self->_do_parse_error( @{$entry->{ERROR}} );
  286. };
  287. unless ($entry->is_empty) {
  288. $entry->{'Closes'} = find_closes( $entry->{Changes} );
  289. push @{$self->{data}}, $entry;
  290. }
  291. if ($self->{config}{infile}) {
  292. close $fh or do {
  293. $self->_do_fatal_error( _g("can't close file %s: %s"),
  294. $file, $!);
  295. return undef;
  296. };
  297. }
  298. # use Data::Dumper;
  299. # print Dumper( $self );
  300. return $self;
  301. }
  302. 1;
  303. __END__
  304. =head1 SEE ALSO
  305. Dpkg::Changelog
  306. Description of the Debian changelog format in the Debian policy:
  307. L<http://www.debian.org/doc/debian-policy/ch-source.html#s-dpkgchangelog>.
  308. =head1 AUTHOR
  309. Frank Lichtenheld, E<lt>frank@lichtenheld.deE<gt>
  310. =head1 COPYRIGHT AND LICENSE
  311. Copyright (C) 2005 by Frank Lichtenheld
  312. This program is free software; you can redistribute it and/or modify
  313. it under the terms of the GNU General Public License as published by
  314. the Free Software Foundation; either version 2 of the License, or
  315. (at your option) any later version.
  316. This program is distributed in the hope that it will be useful,
  317. but WITHOUT ANY WARRANTY; without even the implied warranty of
  318. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  319. GNU General Public License for more details.
  320. You should have received a copy of the GNU General Public License
  321. along with this program; if not, write to the Free Software
  322. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  323. =cut