Changelog.pm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. #
  2. # Dpkg::Changelog
  3. #
  4. # Copyright © 2005, 2007 Frank Lichtenheld <frank@lichtenheld.de>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. #
  20. =head1 NAME
  21. Dpkg::Changelog
  22. =head1 DESCRIPTION
  23. to be written
  24. =head2 Functions
  25. =cut
  26. package Dpkg::Changelog;
  27. use strict;
  28. use warnings;
  29. use English;
  30. use Dpkg;
  31. use Dpkg::Gettext;
  32. use Dpkg::ErrorHandling qw(warning report syserr subprocerr);
  33. use Dpkg::Cdata;
  34. use Dpkg::Fields;
  35. use base qw(Exporter);
  36. our %EXPORT_TAGS = ( 'util' => [ qw(
  37. find_closes
  38. data2rfc822
  39. data2rfc822_mult
  40. get_dpkg_changes
  41. parse_changelog
  42. ) ] );
  43. our @EXPORT_OK = @{$EXPORT_TAGS{util}};
  44. =pod
  45. =head3 init
  46. Creates a new object instance. Takes a reference to a hash as
  47. optional argument, which is interpreted as configuration options.
  48. There are currently no supported general configuration options, but
  49. see the other methods for more specific configuration options which
  50. can also specified to C<init>.
  51. If C<infile>, C<inhandle>, or C<instring> are specified, C<parse()>
  52. is called from C<init>. If a fatal error is encountered during parsing
  53. (e.g. the file can't be opened), C<init> will not return a
  54. valid object but C<undef>!
  55. =cut
  56. sub init {
  57. my $classname = shift;
  58. my $config = shift || {};
  59. my $self = {};
  60. bless( $self, $classname );
  61. $config->{verbose} = 1 if $config->{debug};
  62. $self->{config} = $config;
  63. $self->reset_parse_errors;
  64. if ($self->{config}{infile}
  65. || $self->{config}{inhandle}
  66. || $self->{config}{instring}) {
  67. defined($self->parse) or return undef;
  68. }
  69. return $self;
  70. }
  71. =pod
  72. =head3 reset_parse_errors
  73. Can be used to delete all information about errors ocurred during
  74. previous L<parse> runs. Note that C<parse()> also calls this method.
  75. =cut
  76. sub reset_parse_errors {
  77. my ($self) = @_;
  78. $self->{errors}{parser} = [];
  79. }
  80. sub _do_parse_error {
  81. my ($self, $file, $line_nr, $error, $line) = @_;
  82. shift;
  83. push @{$self->{errors}{parser}}, [ @_ ];
  84. unless ($self->{config}{quiet}) {
  85. if ($line) {
  86. warning("%20s(l$NR): $error\nLINE: $line", $file);
  87. } else {
  88. warning("%20s(l$NR): $error", $file);
  89. }
  90. }
  91. }
  92. =pod
  93. =head3 get_parse_errors
  94. Returns all error messages from the last L<parse> run.
  95. If called in scalar context returns a human readable
  96. string representation. If called in list context returns
  97. an array of arrays. Each of these arrays contains
  98. =over 4
  99. =item 1.
  100. the filename of the parsed file or C<FileHandle> or C<String>
  101. if the input came from a file handle or a string. If the
  102. reportfile configuration option was given, its value will be
  103. 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. NOTE: This format isn't stable yet and may change in later versions
  112. of this module.
  113. =cut
  114. sub get_parse_errors {
  115. my ($self) = @_;
  116. if (wantarray) {
  117. return @{$self->{errors}{parser}};
  118. } else {
  119. my $res = "";
  120. foreach my $e (@{$self->{errors}{parser}}) {
  121. if ($e->[3]) {
  122. $res .= report(_g('warning'),_g("%s(l%s): %s\nLINE: %s"), @$e );
  123. } else {
  124. $res .= report(_g('warning'),_g("%s(l%s): %s"), @$e );
  125. }
  126. }
  127. return $res;
  128. }
  129. }
  130. sub _do_fatal_error {
  131. my ($self, $msg, @msg) = @_;
  132. $self->{errors}{fatal} = report(_g('fatal error'), $msg, @msg);
  133. warning($msg, @msg) unless $self->{config}{quiet};
  134. }
  135. =pod
  136. =head3 get_error
  137. Get the last non-parser error (e.g. the file to parse couldn't be opened).
  138. =cut
  139. sub get_error {
  140. my ($self) = @_;
  141. return $self->{errors}{fatal};
  142. }
  143. =pod
  144. =head3 data
  145. C<data> returns an array (if called in list context) or a reference
  146. to an array of Dpkg::Changelog::Entry objects which each
  147. represent one entry of the changelog.
  148. This method supports the common output options described in
  149. section L<"COMMON OUTPUT OPTIONS">.
  150. =cut
  151. sub data {
  152. my ($self, $config) = @_;
  153. my $data = $self->{data};
  154. if ($config) {
  155. $self->{config}{DATA} = $config if $config;
  156. $data = $self->_data_range( $config ) or return undef;
  157. }
  158. return @$data if wantarray;
  159. return $data;
  160. }
  161. sub __sanity_check_range {
  162. my ( $data, $from, $to, $since, $until, $start, $end ) = @_;
  163. if (($$start || $$end) && ($$from || $$since || $$to || $$until)) {
  164. warning(_g( "you can't combine 'count' or 'offset' with any other range option" ));
  165. $$from = $$since = $$to = $$until = '';
  166. }
  167. if ($$from && $$since) {
  168. warning(_g( "you can only specify one of 'from' and 'since', using 'since'" ));
  169. $$from = '';
  170. }
  171. if ($$to && $$until) {
  172. warning(_g( "you can only specify one of 'to' and 'until', using 'until'" ));
  173. $$to = '';
  174. }
  175. if ($$since && ($data->[0]{Version} eq $$since)) {
  176. warning(_g( "'since' option specifies most recent version, ignoring" ));
  177. $$since = '';
  178. }
  179. if ($$until && ($data->[$#{$data}]{Version} eq $$until)) {
  180. warning(_g( "'until' option specifies oldest version, ignoring" ));
  181. $$until = '';
  182. }
  183. $$start = 0 if $$start < 0;
  184. return if $$start > $#$data;
  185. $$end = $#$data if $$end > $#$data;
  186. return if $$end < 0;
  187. $$end = $$start if $$end < $$start;
  188. #TODO: compare versions
  189. return 1;
  190. }
  191. sub _data_range {
  192. my ($self, $config) = @_;
  193. my $data = $self->data or return undef;
  194. return [ @$data ] if $config->{all};
  195. my $since = $config->{since} || '';
  196. my $until = $config->{until} || '';
  197. my $from = $config->{from} || '';
  198. my $to = $config->{to} || '';
  199. my $count = $config->{count} || 0;
  200. my $offset = $config->{offset} || 0;
  201. return if $offset and not $count;
  202. if ($offset > 0) {
  203. $offset -= ($count < 0);
  204. } elsif ($offset < 0) {
  205. $offset = $#$data + ($count > 0) + $offset;
  206. } else {
  207. $offset = $#$data if $count < 0;
  208. }
  209. my $start = my $end = $offset;
  210. $start += $count+1 if $count < 0;
  211. $end += $count-1 if $count > 0;
  212. return unless __sanity_check_range( $data, \$from, \$to,
  213. \$since, \$until,
  214. \$start, \$end );
  215. unless ($from or $to or $since or $until or $start or $end) {
  216. return [ @$data ] if $config->{default_all} and not $count;
  217. return [ $data->[0] ];
  218. }
  219. return [ @{$data}[$start .. $end] ] if $start or $end;
  220. my @result;
  221. my $include = 1;
  222. $include = 0 if $to or $until;
  223. foreach (@$data) {
  224. my $v = $_->{Version};
  225. $include = 1 if $v eq $to;
  226. last if $v eq $since;
  227. push @result, $_ if $include;
  228. $include = 1 if $v eq $until;
  229. last if $v eq $from;
  230. }
  231. return \@result;
  232. }
  233. sub _abort_early {
  234. my ($self) = @_;
  235. my $data = $self->data or return;
  236. my $config = $self->{config} or return;
  237. # use Data::Dumper;
  238. # warn "Abort early? (\$# = $#$data)\n".Dumper($config);
  239. return if $config->{all};
  240. my $since = $config->{since} || '';
  241. my $until = $config->{until} || '';
  242. my $from = $config->{from} || '';
  243. my $to = $config->{to} || '';
  244. my $count = $config->{count} || 0;
  245. my $offset = $config->{offset} || 0;
  246. return if $offset and not $count;
  247. return if $offset < 0 or $count < 0;
  248. if ($offset > 0) {
  249. $offset -= ($count < 0);
  250. }
  251. my $start = my $end = $offset;
  252. $end += $count-1 if $count > 0;
  253. unless ($from or $to or $since or $until or $start or $end) {
  254. return if not $count;
  255. return 1 if @$data;
  256. }
  257. return 1 if ($start or $end)
  258. and $start < @$data and $end < @$data;
  259. return unless $since or $from;
  260. foreach (@$data) {
  261. my $v = $_->{Version};
  262. return 1 if $v eq $since;
  263. return 1 if $v eq $from;
  264. }
  265. return;
  266. }
  267. =pod
  268. =head3 dpkg
  269. (and B<dpkg_str>)
  270. C<dpkg> returns a hash (in list context) or a hash reference
  271. (in scalar context) where the keys are field names and the values are
  272. field values. The following fields are given:
  273. =over 4
  274. =item Source
  275. package name (in the first entry)
  276. =item Version
  277. packages' version (from first entry)
  278. =item Distribution
  279. target distribution (from first entry)
  280. =item Urgency
  281. urgency (highest of all printed entries)
  282. =item Maintainer
  283. person that created the (first) entry
  284. =item Date
  285. date of the (first) entry
  286. =item Closes
  287. bugs closed by the entry/entries, sorted by bug number
  288. =item Changes
  289. content of the the entry/entries
  290. =back
  291. C<dpkg_str> returns a stringified version of this hash. The fields are
  292. ordered like in the list above.
  293. Both methods support the common output options described in
  294. section L<"COMMON OUTPUT OPTIONS">.
  295. =head3 dpkg_str
  296. See L<dpkg>.
  297. =cut
  298. our ( @CHANGELOG_FIELDS, %CHANGELOG_FIELDS );
  299. our ( @URGENCIES, %URGENCIES );
  300. BEGIN {
  301. @CHANGELOG_FIELDS = qw(Source Version Distribution
  302. Urgency Maintainer Date Closes Changes
  303. Timestamp Header Items Trailer
  304. Urgency_comment Urgency_lc);
  305. tie %CHANGELOG_FIELDS, 'Dpkg::Fields::Object';
  306. %CHANGELOG_FIELDS = map { $_ => 1 } @CHANGELOG_FIELDS;
  307. @URGENCIES = qw(low medium high critical emergency);
  308. my $i = 1;
  309. %URGENCIES = map { $_ => $i++ } @URGENCIES;
  310. }
  311. sub dpkg {
  312. my ($self, $config) = @_;
  313. $self->{config}{DPKG} = $config if $config;
  314. $config = $self->{config}{DPKG} || {};
  315. my $data = $self->_data_range( $config ) or return undef;
  316. my $f = new Dpkg::Changelog::Entry;
  317. foreach my $field (qw( Urgency Source Version
  318. Distribution Maintainer Date )) {
  319. $f->{$field} = $data->[0]{$field};
  320. }
  321. # handle unknown fields
  322. foreach my $field (keys %{$data->[0]}) {
  323. next if $CHANGELOG_FIELDS{$field};
  324. $f->{$field} = $data->[0]{$field};
  325. }
  326. $f->{Changes} = get_dpkg_changes( $data->[0] );
  327. $f->{Closes} = [ @{$data->[0]{Closes}} ];
  328. my $first = 1; my $urg_comment = '';
  329. foreach my $entry (@$data) {
  330. $first = 0, next if $first;
  331. my $oldurg = $f->{Urgency} || '';
  332. my $oldurgn = $URGENCIES{$f->{Urgency}} || -1;
  333. my $newurg = $entry->{Urgency_lc} || '';
  334. my $newurgn = $URGENCIES{$entry->{Urgency_lc}} || -1;
  335. $f->{Urgency} = ($newurgn > $oldurgn) ? $newurg : $oldurg;
  336. $urg_comment .= $entry->{Urgency_comment};
  337. $f->{Changes} .= "\n .".get_dpkg_changes( $entry );
  338. push @{$f->{Closes}}, @{$entry->{Closes}};
  339. # handle unknown fields
  340. foreach my $field (keys %$entry) {
  341. next if $CHANGELOG_FIELDS{$field};
  342. next if exists $f->{$field};
  343. $f->{$field} = $entry->{$field};
  344. }
  345. }
  346. $f->{Closes} = join " ", sort { $a <=> $b } @{$f->{Closes}};
  347. $f->{Urgency} .= $urg_comment;
  348. return %$f if wantarray;
  349. return $f;
  350. }
  351. sub dpkg_str {
  352. return data2rfc822(scalar dpkg(@_));
  353. }
  354. =pod
  355. =head3 rfc822
  356. (and B<rfc822_str>)
  357. C<rfc822> returns an array of hashes (in list context) or a reference
  358. to this array (in scalar context) where each hash represents one entry
  359. in the changelog. For the format of such a hash see the description
  360. of the L<"dpkg"> method (while ignoring the remarks about which
  361. values are taken from the first entry).
  362. C<rfc822_str> returns a stringified version of this array.
  363. Both methods support the common output options described in
  364. section L<"COMMON OUTPUT OPTIONS">.
  365. =head3 rfc822_str
  366. See L<rfc822>.
  367. =cut
  368. sub rfc822 {
  369. my ($self, $config) = @_;
  370. $self->{config}{RFC822} = $config if $config;
  371. $config = $self->{config}{RFC822} || {};
  372. my $data = $self->_data_range( $config ) or return undef;
  373. my @out_data;
  374. foreach my $entry (@$data) {
  375. my $f = new Dpkg::Changelog::Entry;
  376. foreach my $field (qw( Urgency Source Version
  377. Distribution Maintainer Date )) {
  378. $f->{$field} = $entry->{$field};
  379. }
  380. $f->{Urgency} .= $entry->{Urgency_Comment};
  381. $f->{Changes} = get_dpkg_changes( $entry );
  382. $f->{Closes} = join " ", sort { $a <=> $b } @{$entry->{Closes}};
  383. # handle unknown fields
  384. foreach my $field (keys %$entry) {
  385. next if $CHANGELOG_FIELDS{$field};
  386. $f->{$field} = $entry->{$field};
  387. }
  388. push @out_data, $f;
  389. }
  390. return @out_data if wantarray;
  391. return \@out_data;
  392. }
  393. sub rfc822_str {
  394. return data2rfc822(scalar rfc822(@_));
  395. }
  396. =pod
  397. =head1 COMMON OUTPUT OPTIONS
  398. The following options are supported by all output methods,
  399. all take a version number as value:
  400. =over 4
  401. =item since
  402. Causes changelog information from all versions strictly
  403. later than B<version> to be used.
  404. =item until
  405. Causes changelog information from all versions strictly
  406. earlier than B<version> to be used.
  407. =item from
  408. Similar to C<since> but also includes the information for the
  409. specified B<version> itself.
  410. =item to
  411. Similar to C<until> but also includes the information for the
  412. specified B<version> itself.
  413. =back
  414. The following options also supported by all output methods but
  415. don't take version numbers as values:
  416. =over 4
  417. =item all
  418. If set to a true value, all entries of the changelog are returned,
  419. this overrides all other options.
  420. =item count
  421. Expects a signed integer as value. Returns C<value> entries from the
  422. top of the changelog if set to a positive integer, and C<abs(value)>
  423. entries from the tail if set to a negative integer.
  424. =item offset
  425. Expects a signed integer as value. Changes the starting point for
  426. C<count>, either counted from the top (positive integer) or from
  427. the tail (negative integer). C<offset> has no effect if C<count>
  428. wasn't given as well.
  429. =back
  430. Some examples for the above options. Imagine an example changelog with
  431. entries for the versions 1.2, 1.3, 2.0, 2.1, 2.2, 3.0 and 3.1.
  432. Call Included entries
  433. C<E<lt>formatE<gt>({ since =E<gt> '2.0' })> 3.1, 3.0, 2.2
  434. C<E<lt>formatE<gt>({ until =E<gt> '2.0' })> 1.3, 1.2
  435. C<E<lt>formatE<gt>({ from =E<gt> '2.0' })> 3.1, 3.0, 2.2, 2.1, 2.0
  436. C<E<lt>formatE<gt>({ to =E<gt> '2.0' })> 2.0, 1.3, 1.2
  437. C<E<lt>formatE<gt>({ count =E<gt> 2 }>> 3.1, 3.0
  438. C<E<lt>formatE<gt>({ count =E<gt> -2 }>> 1.3, 1.2
  439. C<E<lt>formatE<gt>({ count =E<gt> 3,
  440. offset=E<gt> 2 }>> 2.2, 2.1, 2.0
  441. C<E<lt>formatE<gt>({ count =E<gt> 2,
  442. offset=E<gt> -3 }>> 2.0, 1.3
  443. C<E<lt>formatE<gt>({ count =E<gt> -2,
  444. offset=E<gt> 3 }>> 3.0, 2.2
  445. C<E<lt>formatE<gt>({ count =E<gt> -2,
  446. offset=E<gt> -3 }>> 2.2, 2.1
  447. Any combination of one option of C<since> and C<from> and one of
  448. C<until> and C<to> returns the intersection of the two results
  449. with only one of the options specified.
  450. =head1 UTILITY FUNCTIONS
  451. =head3 find_closes
  452. Takes one string as argument and finds "Closes: #123456, #654321" statements
  453. as supported by the Debian Archive software in it. Returns all closed bug
  454. numbers in an array reference.
  455. =cut
  456. sub find_closes {
  457. my $changes = shift;
  458. my @closes = ();
  459. while ($changes &&
  460. ($changes =~ /closes:\s*(?:bug)?\#?\s?\d+(?:,\s*(?:bug)?\#?\s?\d+)*/ig)) {
  461. push(@closes, $& =~ /\#?\s?(\d+)/g);
  462. }
  463. @closes = sort { $a <=> $b } @closes;
  464. return \@closes;
  465. }
  466. =pod
  467. =head3 data2rfc822
  468. Takes a single argument, either a Dpkg::Changelog::Entry object
  469. or a reference to an array of such objects.
  470. Returns the data in RFC822 format as string.
  471. =cut
  472. sub data2rfc822 {
  473. my ($data) = @_;
  474. if (ref($data) eq "ARRAY") {
  475. my @rfc822 = ();
  476. foreach my $entry (@$data) {
  477. push @rfc822, data2rfc822($entry);
  478. }
  479. return join "\n", @rfc822;
  480. } else {
  481. my $rfc822_str = $data->output;
  482. return $rfc822_str;
  483. }
  484. }
  485. =pod
  486. =head3 get_dpkg_changes
  487. Takes a Dpkg::Changelog::Entry object as first argument.
  488. Returns a string that is suitable for using it in a C<Changes> field
  489. in the output format of C<dpkg-parsechangelog>.
  490. =cut
  491. sub get_dpkg_changes {
  492. my $changes = "\n ".($_[0]->{Header}||'')."\n .\n".($_[0]->{Changes}||'');
  493. chomp $changes;
  494. $changes =~ s/^ $/ ./mgo;
  495. return $changes;
  496. }
  497. =pod
  498. =head3 parse_changelog($file, $format, $since)
  499. Calls "dpkg-parsechangelog -l$file -F$format -v$since" and returns a
  500. Dpkg::Fields::Object with the values output by the program.
  501. =cut
  502. sub parse_changelog {
  503. my ($changelogfile, $changelogformat, $since) = @_;
  504. my @exec = ('dpkg-parsechangelog');
  505. push(@exec, "-l$changelogfile");
  506. push(@exec, "-F$changelogformat") if defined($changelogformat);
  507. push(@exec, "-v$since") if defined($since);
  508. open(PARSECH, "-|", @exec) || syserr(_g("fork for parse changelog"));
  509. my $fields = parsecdata(\*PARSECH, _g("parsed version of changelog"));
  510. close(PARSECH) || subprocerr(_g("parse changelog"));
  511. return $fields;
  512. }
  513. =head1 NAME
  514. Dpkg::Changelog::Entry - represents one entry in a Debian changelog
  515. =head1 SYNOPSIS
  516. =head1 DESCRIPTION
  517. =cut
  518. package Dpkg::Changelog::Entry;
  519. sub new {
  520. my ($classname) = @_;
  521. tie my %entry, 'Dpkg::Fields::Object';
  522. tied(%entry)->set_field_importance(@CHANGELOG_FIELDS);
  523. my $entry = \%entry;
  524. bless $entry, $classname;
  525. }
  526. sub is_empty {
  527. my ($self) = @_;
  528. return !($self->{Changes}
  529. || $self->{Source}
  530. || $self->{Version}
  531. || $self->{Maintainer}
  532. || $self->{Date});
  533. }
  534. sub output {
  535. my $self = shift;
  536. return tied(%$self)->output(@_);
  537. }
  538. 1;
  539. __END__
  540. =head1 AUTHOR
  541. Frank Lichtenheld, E<lt>frank@lichtenheld.deE<gt>
  542. =head1 COPYRIGHT AND LICENSE
  543. Copyright E<copy> 2005, 2007 by Frank Lichtenheld
  544. This program is free software; you can redistribute it and/or modify
  545. it under the terms of the GNU General Public License as published by
  546. the Free Software Foundation; either version 2 of the License, or
  547. (at your option) any later version.
  548. This program is distributed in the hope that it will be useful,
  549. but WITHOUT ANY WARRANTY; without even the implied warranty of
  550. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  551. GNU General Public License for more details.
  552. You should have received a copy of the GNU General Public License
  553. along with this program; if not, write to the Free Software
  554. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  555. =cut