Changelog.pm 18 KB

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