Changelog.pm 18 KB

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