Changelog.pm 18 KB

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