Debian.pm 11 KB

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