Debian.pm 12 KB

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