Changelog.pm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. # Copyright © 2005, 2007 Frank Lichtenheld <frank@lichtenheld.de>
  2. # Copyright © 2009 Raphaël Hertzog <hertzog@debian.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. =head1 NAME
  17. Dpkg::Changelog - base class to implement a changelog parser
  18. =head1 DESCRIPTION
  19. Dpkg::Changelog is a class representing a changelog file
  20. as an array of changelog entries (Dpkg::Changelog::Entry).
  21. By deriving this object and implementing its parse method, you
  22. add the ability to fill this object with changelog entries.
  23. =head2 FUNCTIONS
  24. =cut
  25. package Dpkg::Changelog;
  26. use strict;
  27. use warnings;
  28. our $VERSION = "1.00";
  29. use Dpkg;
  30. use Dpkg::Gettext;
  31. use Dpkg::ErrorHandling qw(:DEFAULT report);
  32. use Dpkg::Control;
  33. use Dpkg::Control::Changelog;
  34. use Dpkg::Control::Fields;
  35. use Dpkg::Index;
  36. use Dpkg::Version;
  37. use Dpkg::Vendor qw(run_vendor_hook);
  38. use base qw(Dpkg::Interface::Storable);
  39. use overload
  40. '@{}' => sub { return $_[0]->{data} };
  41. =over 4
  42. =item my $c = Dpkg::Changelog->new(%options)
  43. Creates a new changelog object.
  44. =cut
  45. sub new {
  46. my ($this, %opts) = @_;
  47. my $class = ref($this) || $this;
  48. my $self = {
  49. verbose => 1,
  50. parse_errors => []
  51. };
  52. bless $self, $class;
  53. $self->set_options(%opts);
  54. return $self;
  55. }
  56. =item $c->load($filename)
  57. Parse $filename as a changelog.
  58. =cut
  59. =item $c->set_options(%opts)
  60. Change the value of some options. "verbose" (defaults to 1) defines
  61. whether parse errors are displayed as warnings by default. "reportfile"
  62. is a string to use instead of the name of the file parsed, in particular
  63. in error messages. "range" defines the range of entries that we want to
  64. parse, the parser will stop as soon as it has parsed enough data to
  65. satisfy $c->get_range($opts{'range'}).
  66. =cut
  67. sub set_options {
  68. my ($self, %opts) = @_;
  69. $self->{$_} = $opts{$_} foreach keys %opts;
  70. }
  71. =item $c->reset_parse_errors()
  72. Can be used to delete all information about errors ocurred during
  73. previous L<parse> runs.
  74. =cut
  75. sub reset_parse_errors {
  76. my ($self) = @_;
  77. $self->{parse_errors} = [];
  78. }
  79. =item $c->parse_error($line_nr, $error, [$line])
  80. Record a new parse error at line $line_nr. The error message is specified
  81. with $error and a copy of the line can be recorded in $line.
  82. =cut
  83. sub parse_error {
  84. my ($self, $file, $line_nr, $error, $line) = @_;
  85. shift;
  86. push @{$self->{parse_errors}}, [ @_ ];
  87. if ($self->{verbose}) {
  88. if ($line) {
  89. warning("%20s(l$line_nr): $error\nLINE: $line", $file);
  90. } else {
  91. warning("%20s(l$line_nr): $error", $file);
  92. }
  93. }
  94. }
  95. =item $c->get_parse_errors()
  96. Returns all error messages from the last L<parse> run.
  97. If called in scalar context returns a human readable
  98. string representation. If called in list context returns
  99. an array of arrays. Each of these arrays contains
  100. =over 4
  101. =item 1.
  102. a string describing the origin of the data (a filename usually). If the
  103. reportfile configuration option was given, its value will be used instead.
  104. =item 2.
  105. the line number where the error occurred
  106. =item 3.
  107. an error description
  108. =item 4.
  109. the original line
  110. =back
  111. =cut
  112. sub get_parse_errors {
  113. my ($self) = @_;
  114. if (wantarray) {
  115. return @{$self->{parse_errors}};
  116. } else {
  117. my $res = "";
  118. foreach my $e (@{$self->{parse_errors}}) {
  119. if ($e->[3]) {
  120. $res .= report(_g('warning'),_g("%s(l%s): %s\nLINE: %s"), @$e );
  121. } else {
  122. $res .= report(_g('warning'),_g("%s(l%s): %s"), @$e );
  123. }
  124. }
  125. return $res;
  126. }
  127. }
  128. =item $c->set_unparsed_tail($tail)
  129. Add a string representing unparsed lines after the changelog entries.
  130. Use undef as $tail to remove the unparsed lines currently set.
  131. =item $c->get_unparsed_tail()
  132. Return a string representing the unparsed lines after the changelog
  133. entries. Returns undef if there's no such thing.
  134. =cut
  135. sub set_unparsed_tail {
  136. my ($self, $tail) = @_;
  137. $self->{'unparsed_tail'} = $tail;
  138. }
  139. sub get_unparsed_tail {
  140. my ($self) = @_;
  141. return $self->{'unparsed_tail'};
  142. }
  143. =item @{$c}
  144. Returns all the Dpkg::Changelog::Entry objects contained in this changelog
  145. in the order in which they have been parsed.
  146. =item $c->get_range($range)
  147. Returns an array (if called in list context) or a reference to an array of
  148. Dpkg::Changelog::Entry objects which each represent one entry of the
  149. changelog. $range is a hash reference describing the range of entries
  150. to return. See section L<"RANGE SELECTION">.
  151. =cut
  152. sub __sanity_check_range {
  153. my ($self, $r) = @_;
  154. my $data = $self->{data};
  155. if (defined($r->{offset}) and not defined($r->{count})) {
  156. warning(_g("'offset' without 'count' has no effect")) if $self->{verbose};
  157. delete $r->{offset};
  158. }
  159. if ((defined($r->{count}) || defined($r->{offset})) &&
  160. (defined($r->{from}) || defined($r->{since}) ||
  161. defined($r->{to}) || defined($r->{'until'})))
  162. {
  163. warning(_g("you can't combine 'count' or 'offset' with any other " .
  164. "range option")) if $self->{verbose};
  165. delete $r->{from};
  166. delete $r->{since};
  167. delete $r->{to};
  168. delete $r->{'until'};
  169. }
  170. if (defined($r->{from}) && defined($r->{since})) {
  171. warning(_g("you can only specify one of 'from' and 'since', using " .
  172. "'since'")) if $self->{verbose};
  173. delete $r->{from};
  174. }
  175. if (defined($r->{to}) && defined($r->{'until'})) {
  176. warning(_g("you can only specify one of 'to' and 'until', using " .
  177. "'until'")) if $self->{verbose};
  178. delete $r->{to};
  179. }
  180. # Handle non-existing versions
  181. my (%versions, @versions);
  182. foreach my $entry (@{$data}) {
  183. $versions{$entry->get_version()->as_string()} = 1;
  184. push @versions, $entry->get_version()->as_string();
  185. }
  186. if ((defined($r->{since}) and not exists $versions{$r->{since}})) {
  187. warning(_g("'%s' option specifies non-existing version"), "since");
  188. warning(_g("use newest entry that is smaller than the one specified"));
  189. foreach my $v (@versions) {
  190. if (version_compare_relation($v, REL_LT, $r->{since})) {
  191. $r->{since} = $v;
  192. last;
  193. }
  194. }
  195. if (not exists $versions{$r->{since}}) {
  196. # No version was smaller, include all
  197. warning(_g("none found, starting from the oldest entry"));
  198. delete $r->{since};
  199. $r->{from} = $versions[-1];
  200. }
  201. }
  202. if ((defined($r->{from}) and not exists $versions{$r->{from}})) {
  203. warning(_g("'%s' option specifies non-existing version"), "from");
  204. warning(_g("use oldest entry that is bigger than the one specified"));
  205. my $oldest;
  206. foreach my $v (@versions) {
  207. if (version_compare_relation($v, REL_GT, $r->{from})) {
  208. $oldest = $v;
  209. }
  210. }
  211. if (defined($oldest)) {
  212. $r->{from} = $oldest;
  213. } else {
  214. warning(_g("no such entry found, ignoring '%s' parameter"), "from");
  215. delete $r->{from}; # No version was bigger
  216. }
  217. }
  218. if (defined($r->{'until'}) and not exists $versions{$r->{'until'}}) {
  219. warning(_g("'%s' option specifies non-existing version"), "until");
  220. warning(_g("use oldest entry that is bigger than the one specified"));
  221. my $oldest;
  222. foreach my $v (@versions) {
  223. if (version_compare_relation($v, REL_GT, $r->{'until'})) {
  224. $oldest = $v;
  225. }
  226. }
  227. if (defined($oldest)) {
  228. $r->{'until'} = $oldest;
  229. } else {
  230. warning(_g("no such entry found, ignoring '%s' parameter"), "until");
  231. delete $r->{'until'}; # No version was bigger
  232. }
  233. }
  234. if (defined($r->{to}) and not exists $versions{$r->{to}}) {
  235. warning(_g("'%s' option specifies non-existing version"), "to");
  236. warning(_g("use newest entry that is smaller than the one specified"));
  237. foreach my $v (@versions) {
  238. if (version_compare_relation($v, REL_LT, $r->{to})) {
  239. $r->{to} = $v;
  240. last;
  241. }
  242. }
  243. if (not exists $versions{$r->{to}}) {
  244. # No version was smaller
  245. warning(_g("no such entry found, ignoring '%s' parameter"), "to");
  246. delete $r->{to};
  247. }
  248. }
  249. if (defined($r->{since}) and $data->[0]->get_version() eq $r->{since}) {
  250. warning(_g("'since' option specifies most recent version, ignoring"));
  251. delete $r->{since};
  252. }
  253. if (defined($r->{'until'}) and $data->[-1]->get_version() eq $r->{'until'}) {
  254. warning(_g("'until' option specifies oldest version, ignoring"));
  255. delete $r->{'until'};
  256. }
  257. }
  258. sub get_range {
  259. my ($self, $range) = @_;
  260. $range = {} unless defined $range;
  261. my $res = $self->_data_range($range);
  262. if (defined $res) {
  263. return @$res if wantarray;
  264. return $res;
  265. } else {
  266. return () if wantarray;
  267. return undef;
  268. }
  269. }
  270. sub _data_range {
  271. my ($self, $range) = @_;
  272. my $data = $self->{data} or return undef;
  273. return [ @$data ] if $range->{all};
  274. unless (grep { m/^(since|until|from|to|count|offset)$/ } keys %$range) {
  275. return [ @$data ];
  276. }
  277. $self->__sanity_check_range($range);
  278. my ($start, $end);
  279. if (defined($range->{count})) {
  280. my $offset = $range->{offset} || 0;
  281. my $count = $range->{count};
  282. # Convert count/offset in start/end
  283. if ($offset > 0) {
  284. $offset -= ($count < 0);
  285. } elsif ($offset < 0) {
  286. $offset = $#$data + ($count > 0) + $offset;
  287. } else {
  288. $offset = $#$data if $count < 0;
  289. }
  290. $start = $end = $offset;
  291. $start += $count+1 if $count < 0;
  292. $end += $count-1 if $count > 0;
  293. # Check limits
  294. $start = 0 if $start < 0;
  295. return if $start > $#$data;
  296. $end = $#$data if $end > $#$data;
  297. return if $end < 0;
  298. $end = $start if $end < $start;
  299. return [ @{$data}[$start .. $end] ];
  300. }
  301. my @result;
  302. my $include = 1;
  303. $include = 0 if defined($range->{to}) or defined($range->{'until'});
  304. foreach (@$data) {
  305. my $v = $_->get_version();
  306. $include = 1 if defined($range->{to}) and $v eq $range->{to};
  307. last if defined($range->{since}) and $v eq $range->{since};
  308. push @result, $_ if $include;
  309. $include = 1 if defined($range->{'until'}) and $v eq $range->{'until'};
  310. last if defined($range->{from}) and $v eq $range->{from};
  311. }
  312. return \@result if scalar(@result);
  313. return undef;
  314. }
  315. =item $c->abort_early()
  316. Returns true if enough data have been parsed to be able to return all
  317. entries selected by the range set at creation (or with set_options).
  318. =cut
  319. sub abort_early {
  320. my ($self) = @_;
  321. my $data = $self->{data} or return;
  322. my $r = $self->{range} or return;
  323. my $count = $r->{count} || 0;
  324. my $offset = $r->{offset} || 0;
  325. return if $r->{all};
  326. return unless grep { m/^(since|until|from|to|count|offset)$/ } keys %$r;
  327. return if $offset < 0 or $count < 0;
  328. if (defined($r->{count})) {
  329. if ($offset > 0) {
  330. $offset -= ($count < 0);
  331. }
  332. my $start = my $end = $offset;
  333. $end += $count-1 if $count > 0;
  334. return ($start < @$data and $end < @$data);
  335. }
  336. return unless defined($r->{since}) or defined($r->{from});
  337. foreach (@$data) {
  338. my $v = $_->get_version();
  339. return 1 if defined($r->{since}) and $v eq $r->{since};
  340. return 1 if defined($r->{from}) and $v eq $r->{from};
  341. }
  342. return;
  343. }
  344. =item $c->save($filename)
  345. Save the changelog in the given file.
  346. =item $c->output()
  347. =item "$c"
  348. Returns a string representation of the changelog (it's a concatenation of
  349. the string representation of the individual changelog entries).
  350. =item $c->output($fh)
  351. Output the changelog to the given filehandle.
  352. =cut
  353. sub output {
  354. my ($self, $fh) = @_;
  355. my $str = "";
  356. foreach my $entry (@{$self}) {
  357. my $text = $entry->output();
  358. print $fh $text if defined $fh;
  359. $str .= $text if defined wantarray;
  360. }
  361. my $text = $self->get_unparsed_tail();
  362. if (defined $text) {
  363. print $fh $text if defined $fh;
  364. $str .= $text if defined wantarray;
  365. }
  366. return $str;
  367. }
  368. =item my $control = $c->dpkg($range)
  369. Returns a Dpkg::Control::Changelog object representing the entries selected
  370. by the optional range specifier (see L<"RANGE SELECTION"> for details).
  371. Returns undef in no entries are matched.
  372. The following fields are contained in the object:
  373. =over 4
  374. =item Source
  375. package name (in the first entry)
  376. =item Version
  377. packages' version (from first entry)
  378. =item Distribution
  379. target distribution (from first entry)
  380. =item Urgency
  381. urgency (highest of all printed entries)
  382. =item Maintainer
  383. person that created the (first) entry
  384. =item Date
  385. date of the (first) entry
  386. =item Closes
  387. bugs closed by the entry/entries, sorted by bug number
  388. =item Changes
  389. content of the the entry/entries
  390. =back
  391. =cut
  392. our ( @URGENCIES, %URGENCIES );
  393. BEGIN {
  394. @URGENCIES = qw(low medium high critical emergency);
  395. my $i = 1;
  396. %URGENCIES = map { $_ => $i++ } @URGENCIES;
  397. }
  398. sub dpkg {
  399. my ($self, $range) = @_;
  400. my @data = $self->get_range($range) or return undef;
  401. my $entry = shift @data;
  402. my $f = Dpkg::Control::Changelog->new();
  403. $f->{Urgency} = $entry->get_urgency() || "unknown";
  404. $f->{Source} = $entry->get_source() || "unknown";
  405. $f->{Version} = $entry->get_version() || "unknown";
  406. $f->{Distribution} = join(" ", $entry->get_distributions());
  407. $f->{Maintainer} = $entry->get_maintainer() || '';
  408. $f->{Date} = $entry->get_timestamp() || '';
  409. $f->{Changes} = $entry->get_dpkg_changes();
  410. # handle optional fields
  411. my $opts = $entry->get_optional_fields();
  412. my %closes;
  413. foreach (keys %$opts) {
  414. if (/^Urgency$/i) { # Already dealt
  415. } elsif (/^Closes$/i) {
  416. $closes{$_} = 1 foreach (split(/\s+/, $opts->{Closes}));
  417. } else {
  418. field_transfer_single($opts, $f);
  419. }
  420. }
  421. foreach $entry (@data) {
  422. my $oldurg = $f->{Urgency} || '';
  423. my $oldurgn = $URGENCIES{$f->{Urgency}} || -1;
  424. my $newurg = $entry->get_urgency() || '';
  425. my $newurgn = $URGENCIES{$newurg} || -1;
  426. $f->{Urgency} = ($newurgn > $oldurgn) ? $newurg : $oldurg;
  427. $f->{Changes} .= "\n" . $entry->get_dpkg_changes();
  428. # handle optional fields
  429. $opts = $entry->get_optional_fields();
  430. foreach (keys %$opts) {
  431. if (/^Closes$/i) {
  432. $closes{$_} = 1 foreach (split(/\s+/, $opts->{Closes}));
  433. } elsif (not exists $f->{$_}) { # Don't overwrite an existing field
  434. field_transfer_single($opts, $f);
  435. }
  436. }
  437. }
  438. if (scalar keys %closes) {
  439. $f->{Closes} = join " ", sort { $a <=> $b } keys %closes;
  440. }
  441. run_vendor_hook("post-process-changelog-entry", $f);
  442. return $f;
  443. }
  444. =item my @controls = $c->rfc822($range)
  445. Returns a Dpkg::Index containing Dpkg::Control::Changelog objects where
  446. each object represents one entry in the changelog that is part of the
  447. range requested (see L<"RANGE SELECTION"> for details). For the format of
  448. such an object see the description of the L<"dpkg"> method (while ignoring
  449. the remarks about which values are taken from the first entry).
  450. =cut
  451. sub rfc822 {
  452. my ($self, $range) = @_;
  453. my @data = $self->get_range($range) or return undef;
  454. my $index = Dpkg::Index->new(type => CTRL_CHANGELOG);
  455. foreach my $entry (@data) {
  456. my $f = Dpkg::Control::Changelog->new();
  457. $f->{Urgency} = $entry->get_urgency() || "unknown";
  458. $f->{Source} = $entry->get_source() || "unknown";
  459. $f->{Version} = $entry->get_version() || "unknown";
  460. $f->{Distribution} = join(" ", $entry->get_distributions());
  461. $f->{Maintainer} = $entry->get_maintainer() || "";
  462. $f->{Date} = $entry->get_timestamp() || "";
  463. $f->{Changes} = $entry->get_dpkg_changes();
  464. # handle optional fields
  465. my $opts = $entry->get_optional_fields();
  466. foreach (keys %$opts) {
  467. field_transfer_single($opts, $f) unless exists $f->{$_};
  468. }
  469. run_vendor_hook("post-process-changelog-entry", $f);
  470. $index->add($f);
  471. }
  472. return $index;
  473. }
  474. =back
  475. =head1 RANGE SELECTION
  476. A range selection is described by a hash reference where
  477. the allowed keys and values are described below.
  478. The following options take a version number as value.
  479. =over 4
  480. =item since
  481. Causes changelog information from all versions strictly
  482. later than B<version> to be used.
  483. =item until
  484. Causes changelog information from all versions strictly
  485. earlier than B<version> to be used.
  486. =item from
  487. Similar to C<since> but also includes the information for the
  488. specified B<version> itself.
  489. =item to
  490. Similar to C<until> but also includes the information for the
  491. specified B<version> itself.
  492. =back
  493. The following options don't take version numbers as values:
  494. =over 4
  495. =item all
  496. If set to a true value, all entries of the changelog are returned,
  497. this overrides all other options.
  498. =item count
  499. Expects a signed integer as value. Returns C<value> entries from the
  500. top of the changelog if set to a positive integer, and C<abs(value)>
  501. entries from the tail if set to a negative integer.
  502. =item offset
  503. Expects a signed integer as value. Changes the starting point for
  504. C<count>, either counted from the top (positive integer) or from
  505. the tail (negative integer). C<offset> has no effect if C<count>
  506. wasn't given as well.
  507. =back
  508. Some examples for the above options. Imagine an example changelog with
  509. entries for the versions 1.2, 1.3, 2.0, 2.1, 2.2, 3.0 and 3.1.
  510. Range Included entries
  511. C<{ since =E<gt> '2.0' }> 3.1, 3.0, 2.2
  512. C<{ until =E<gt> '2.0' }> 1.3, 1.2
  513. C<{ from =E<gt> '2.0' }> 3.1, 3.0, 2.2, 2.1, 2.0
  514. C<{ to =E<gt> '2.0' }> 2.0, 1.3, 1.2
  515. C<{ count =E<gt> 2 }> 3.1, 3.0
  516. C<{ count =E<gt> -2 }> 1.3, 1.2
  517. C<{ count =E<gt> 3, offset=E<gt> 2 }> 2.2, 2.1, 2.0
  518. C<{ count =E<gt> 2, offset=E<gt> -3 }> 2.0, 1.3
  519. C<{ count =E<gt> -2, offset=E<gt> 3 }> 3.0, 2.2
  520. C<{ count =E<gt> -2, offset=E<gt> -3 }> 2.2, 2.1
  521. Any combination of one option of C<since> and C<from> and one of
  522. C<until> and C<to> returns the intersection of the two results
  523. with only one of the options specified.
  524. =head1 AUTHOR
  525. Frank Lichtenheld, E<lt>frank@lichtenheld.deE<gt>
  526. Raphaël Hertzog, E<lt>hertzog@debian.orgE<gt>
  527. =cut
  528. 1;