Debian.pm 11 KB

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