Changelog.pm 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. #
  2. # Dpkg::Changelog
  3. #
  4. # Copyright © 2005, 2007 Frank Lichtenheld <frank@lichtenheld.de>
  5. # Copyright © 2009 Raphaël Hertzog <hertzog@debian.org>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. #
  21. =head1 NAME
  22. Dpkg::Changelog
  23. =head1 DESCRIPTION
  24. FIXME: to be written
  25. =head2 Functions
  26. =cut
  27. package Dpkg::Changelog;
  28. use strict;
  29. use warnings;
  30. use English;
  31. use Dpkg;
  32. use Dpkg::Gettext;
  33. use Dpkg::ErrorHandling qw(:DEFAULT report);
  34. use Dpkg::Control;
  35. use Dpkg::Version qw(compare_versions);
  36. use base qw(Exporter);
  37. our %EXPORT_TAGS = ( 'util' => [ qw(
  38. find_closes
  39. data2rfc822
  40. data2rfc822_mult
  41. get_dpkg_changes
  42. parse_changelog
  43. ) ] );
  44. our @EXPORT_OK = @{$EXPORT_TAGS{util}};
  45. =pod
  46. =head3 init
  47. Creates a new object instance. Takes a reference to a hash as
  48. optional argument, which is interpreted as configuration options.
  49. There are currently no supported general configuration options, but
  50. see the other methods for more specific configuration options which
  51. can also specified to C<init>.
  52. If C<infile>, C<inhandle>, or C<instring> are specified, C<parse()>
  53. is called from C<init>. If a fatal error is encountered during parsing
  54. (e.g. the file can't be opened), C<init> will not return a
  55. valid object but C<undef>!
  56. =cut
  57. sub init {
  58. my $classname = shift;
  59. my $config = shift || {};
  60. my $self = {};
  61. bless( $self, $classname );
  62. $config->{verbose} = 1 if $config->{debug};
  63. $self->{config} = $config;
  64. $self->reset_parse_errors;
  65. if ($self->{config}{infile}
  66. || $self->{config}{inhandle}
  67. || $self->{config}{instring}) {
  68. defined($self->parse) or return undef;
  69. }
  70. return $self;
  71. }
  72. =pod
  73. =head3 reset_parse_errors
  74. Can be used to delete all information about errors ocurred during
  75. previous L<parse> runs. Note that C<parse()> also calls this method.
  76. =cut
  77. sub reset_parse_errors {
  78. my ($self) = @_;
  79. $self->{errors}{parser} = [];
  80. }
  81. sub _do_parse_error {
  82. my ($self, $file, $line_nr, $error, $line) = @_;
  83. shift;
  84. push @{$self->{errors}{parser}}, [ @_ ];
  85. unless ($self->{config}{quiet}) {
  86. if ($line) {
  87. warning("%20s(l$NR): $error\nLINE: $line", $file);
  88. } else {
  89. warning("%20s(l$NR): $error", $file);
  90. }
  91. }
  92. }
  93. =pod
  94. =head3 get_parse_errors
  95. Returns all error messages from the last L<parse> run.
  96. If called in scalar context returns a human readable
  97. string representation. If called in list context returns
  98. an array of arrays. Each of these arrays contains
  99. =over 4
  100. =item 1.
  101. the filename of the parsed file or C<FileHandle> or C<String>
  102. if the input came from a file handle or a string. If the
  103. reportfile configuration option was given, its value will be
  104. 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. NOTE: This format isn't stable yet and may change in later versions
  113. of this module.
  114. =cut
  115. sub get_parse_errors {
  116. my ($self) = @_;
  117. if (wantarray) {
  118. return @{$self->{errors}{parser}};
  119. } else {
  120. my $res = "";
  121. foreach my $e (@{$self->{errors}{parser}}) {
  122. if ($e->[3]) {
  123. $res .= report(_g('warning'),_g("%s(l%s): %s\nLINE: %s"), @$e );
  124. } else {
  125. $res .= report(_g('warning'),_g("%s(l%s): %s"), @$e );
  126. }
  127. }
  128. return $res;
  129. }
  130. }
  131. sub _do_fatal_error {
  132. my ($self, $msg, @msg) = @_;
  133. $self->{errors}{fatal} = report(_g('fatal error'), $msg, @msg);
  134. warning($msg, @msg) unless $self->{config}{quiet};
  135. }
  136. =pod
  137. =head3 get_error
  138. Get the last non-parser error (e.g. the file to parse couldn't be opened).
  139. =cut
  140. sub get_error {
  141. my ($self) = @_;
  142. return $self->{errors}{fatal};
  143. }
  144. =pod
  145. =head3 data
  146. C<data> returns an array (if called in list context) or a reference
  147. to an array of Dpkg::Changelog::Entry objects which each
  148. represent one entry of the changelog.
  149. This method supports the common output options described in
  150. section L<"COMMON OUTPUT OPTIONS">.
  151. =cut
  152. sub data {
  153. my ($self, $config) = @_;
  154. my $data = $self->{data};
  155. if ($config) {
  156. $self->{config}{DATA} = $config if $config;
  157. $data = $self->_data_range( $config ) or return undef;
  158. }
  159. return @$data if wantarray;
  160. return $data;
  161. }
  162. sub __sanity_check_range {
  163. my ( $data, $from, $to, $since, $until, $start, $end ) = @_;
  164. if (($$start || $$end) &&
  165. (length($$from) || length($$since) || length($$to) || length($$until)))
  166. {
  167. warning(_g( "you can't combine 'count' or 'offset' with any other range option" ));
  168. $$from = $$since = $$to = $$until = '';
  169. }
  170. if (length($$from) && length($$since)) {
  171. warning(_g( "you can only specify one of 'from' and 'since', using 'since'" ));
  172. $$from = '';
  173. }
  174. if (length($$to) && length($$until)) {
  175. warning(_g( "you can only specify one of 'to' and 'until', using 'until'" ));
  176. $$to = '';
  177. }
  178. $$start = 0 if $$start < 0;
  179. return if $$start > $#$data;
  180. $$end = $#$data if $$end > $#$data;
  181. return if $$end < 0;
  182. $$end = $$start if $$end < $$start;
  183. # Handle non-existing versions
  184. my (%versions, @versions);
  185. foreach my $entry (@{$data}) {
  186. $versions{$entry->{Version}} = 1;
  187. push @versions, $entry->{Version};
  188. }
  189. if ((length($$since) and not exists $versions{$$since})) {
  190. warning(_g("'%s' option specifies non-existing version"), "since");
  191. warning(_g("use newest entry that is smaller than the one specified"));
  192. foreach my $v (@versions) {
  193. if (compare_versions($v, "<<", $$since)) {
  194. $$since = $v;
  195. last;
  196. }
  197. }
  198. if (not exists $versions{$$since}) {
  199. # No version was smaller, include all
  200. warning(_g("none found, starting from the oldest entry"));
  201. $$since = '';
  202. $$from = $versions[-1];
  203. }
  204. }
  205. if ((length($$from) and not exists $versions{$$from})) {
  206. warning(_g("'%s' option specifies non-existing version"), "from");
  207. warning(_g("use oldest entry that is bigger than the one specified"));
  208. my $oldest;
  209. foreach my $v (@versions) {
  210. if (compare_versions($v, ">>", $$from)) {
  211. $oldest = $v;
  212. }
  213. }
  214. if (defined($oldest)) {
  215. $$from = $oldest;
  216. } else {
  217. warning(_g("no such entry found, ignoring '%s' parameter"), "from");
  218. $$from = ''; # No version was bigger
  219. }
  220. }
  221. if ((length($$until) and not exists $versions{$$until})) {
  222. warning(_g("'%s' option specifies non-existing version"), "until");
  223. warning(_g("use oldest entry that is bigger than the one specified"));
  224. my $oldest;
  225. foreach my $v (@versions) {
  226. if (compare_versions($v, ">>", $$until)) {
  227. $oldest = $v;
  228. }
  229. }
  230. if (defined($oldest)) {
  231. $$until = $oldest;
  232. } else {
  233. warning(_g("no such entry found, ignoring '%s' parameter"), "until");
  234. $$until = ''; # No version was bigger
  235. }
  236. }
  237. if ((length($$to) and not exists $versions{$$to})) {
  238. warning(_g("'%s' option specifies non-existing version"), "to");
  239. warning(_g("use newest entry that is smaller than the one specified"));
  240. foreach my $v (@versions) {
  241. if (compare_versions($v, "<<", $$to)) {
  242. $$to = $v;
  243. last;
  244. }
  245. }
  246. if (not exists $versions{$$to}) {
  247. # No version was smaller
  248. warning(_g("no such entry found, ignoring '%s' parameter"), "to");
  249. $$to = '';
  250. }
  251. }
  252. if (length($$since) && ($data->[0]{Version} eq $$since)) {
  253. warning(_g( "'since' option specifies most recent version, ignoring" ));
  254. $$since = '';
  255. }
  256. if (length($$until) && ($data->[$#{$data}]{Version} eq $$until)) {
  257. warning(_g( "'until' option specifies oldest version, ignoring" ));
  258. $$until = '';
  259. }
  260. return 1;
  261. }
  262. sub _data_range {
  263. my ($self, $config) = @_;
  264. my $data = $self->data or return undef;
  265. return [ @$data ] if $config->{all};
  266. my ($since, $until, $from, $to, $count, $offset) = ('', '', '', '', 0, 0);
  267. $since = $config->{since} if defined($config->{since});
  268. $until = $config->{until} if defined($config->{until});
  269. $from = $config->{from} if defined($config->{from});
  270. $to = $config->{to} if defined($config->{to});
  271. $count = $config->{count} if defined($config->{count});
  272. $offset = $config->{offset} if defined($config->{offset});
  273. return if $offset and not $count;
  274. if ($offset > 0) {
  275. $offset -= ($count < 0);
  276. } elsif ($offset < 0) {
  277. $offset = $#$data + ($count > 0) + $offset;
  278. } else {
  279. $offset = $#$data if $count < 0;
  280. }
  281. my $start = my $end = $offset;
  282. $start += $count+1 if $count < 0;
  283. $end += $count-1 if $count > 0;
  284. return unless __sanity_check_range( $data, \$from, \$to,
  285. \$since, \$until,
  286. \$start, \$end );
  287. unless (length($from) or length($to) or length($since) or length($until)
  288. or $start or $end)
  289. {
  290. return [ @$data ] if $config->{default_all} and not $count;
  291. return [ $data->[0] ];
  292. }
  293. return [ @{$data}[$start .. $end] ] if $start or $end;
  294. my @result;
  295. my $include = 1;
  296. $include = 0 if length($to) or length($until);
  297. foreach (@$data) {
  298. my $v = $_->{Version};
  299. $include = 1 if $v eq $to;
  300. last if $v eq $since;
  301. push @result, $_ if $include;
  302. $include = 1 if $v eq $until;
  303. last if $v eq $from;
  304. }
  305. return \@result if scalar(@result);
  306. return undef;
  307. }
  308. sub _abort_early {
  309. my ($self) = @_;
  310. my $data = $self->data or return;
  311. my $config = $self->{config} or return;
  312. # use Data::Dumper;
  313. # warn "Abort early? (\$# = $#$data)\n".Dumper($config);
  314. return if $config->{all};
  315. my ($since, $until, $from, $to, $count, $offset) = ('', '', '', '', 0, 0);
  316. $since = $config->{since} if defined($config->{since});
  317. $until = $config->{until} if defined($config->{until});
  318. $from = $config->{from} if defined($config->{from});
  319. $to = $config->{to} if defined($config->{to});
  320. $count = $config->{count} if defined($config->{count});
  321. $offset = $config->{offset} if defined($config->{offset});
  322. return if $offset and not $count;
  323. return if $offset < 0 or $count < 0;
  324. if ($offset > 0) {
  325. $offset -= ($count < 0);
  326. }
  327. my $start = my $end = $offset;
  328. $end += $count-1 if $count > 0;
  329. unless (length($from) or length($to) or length($since) or length($until)
  330. or $start or $end)
  331. {
  332. return if not $count;
  333. return 1 if @$data;
  334. }
  335. return 1 if ($start or $end)
  336. and $start < @$data and $end < @$data;
  337. return unless length($since) or length($from);
  338. foreach (@$data) {
  339. my $v = $_->{Version};
  340. return 1 if $v eq $since;
  341. return 1 if $v eq $from;
  342. }
  343. return;
  344. }
  345. =pod
  346. =head3 dpkg
  347. (and B<dpkg_str>)
  348. C<dpkg> returns a hash (in list context) or a hash reference
  349. (in scalar context) where the keys are field names and the values are
  350. field values. The following fields are given:
  351. =over 4
  352. =item Source
  353. package name (in the first entry)
  354. =item Version
  355. packages' version (from first entry)
  356. =item Distribution
  357. target distribution (from first entry)
  358. =item Urgency
  359. urgency (highest of all printed entries)
  360. =item Maintainer
  361. person that created the (first) entry
  362. =item Date
  363. date of the (first) entry
  364. =item Closes
  365. bugs closed by the entry/entries, sorted by bug number
  366. =item Changes
  367. content of the the entry/entries
  368. =back
  369. C<dpkg_str> returns a stringified version of this hash. The fields are
  370. ordered like in the list above.
  371. Both methods support the common output options described in
  372. section L<"COMMON OUTPUT OPTIONS">.
  373. =head3 dpkg_str
  374. See L<dpkg>.
  375. =cut
  376. our ( @CHANGELOG_FIELDS, $CHANGELOG_FIELDS );
  377. our ( @URGENCIES, %URGENCIES );
  378. BEGIN {
  379. @CHANGELOG_FIELDS = qw(Source Version Distribution
  380. Urgency Maintainer Date Closes Changes
  381. Timestamp Header Items Trailer
  382. Urgency_comment Urgency_lc);
  383. $CHANGELOG_FIELDS = Dpkg::Control->new(type => CTRL_CHANGELOG);
  384. %$CHANGELOG_FIELDS = map { $_ => 1 } @CHANGELOG_FIELDS;
  385. @URGENCIES = qw(low medium high critical emergency);
  386. my $i = 1;
  387. %URGENCIES = map { $_ => $i++ } @URGENCIES;
  388. }
  389. sub dpkg {
  390. my ($self, $config) = @_;
  391. $self->{config}{DPKG} = $config if $config;
  392. $config = $self->{config}{DPKG} || {};
  393. my $data = $self->_data_range( $config ) or return undef;
  394. my $f = new Dpkg::Changelog::Entry;
  395. foreach my $field (qw( Urgency Source Version
  396. Distribution Maintainer Date )) {
  397. $f->{$field} = $data->[0]{$field};
  398. }
  399. # handle unknown fields
  400. foreach my $field (keys %{$data->[0]}) {
  401. next if $CHANGELOG_FIELDS->{$field};
  402. $f->{$field} = $data->[0]{$field};
  403. }
  404. $f->{Changes} = get_dpkg_changes( $data->[0] );
  405. $f->{Closes} = [ @{$data->[0]{Closes}} ];
  406. my $first = 1; my $urg_comment = '';
  407. foreach my $entry (@$data) {
  408. $first = 0, next if $first;
  409. my $oldurg = $f->{Urgency} || '';
  410. my $oldurgn = $URGENCIES{$f->{Urgency}} || -1;
  411. my $newurg = $entry->{Urgency_lc} || '';
  412. my $newurgn = $URGENCIES{$entry->{Urgency_lc}} || -1;
  413. $f->{Urgency} = ($newurgn > $oldurgn) ? $newurg : $oldurg;
  414. $urg_comment .= $entry->{Urgency_comment};
  415. $f->{Changes} .= "\n .".get_dpkg_changes( $entry );
  416. push @{$f->{Closes}}, @{$entry->{Closes}};
  417. # handle unknown fields
  418. foreach my $field (keys %$entry) {
  419. next if $CHANGELOG_FIELDS->{$field};
  420. next if exists $f->{$field};
  421. $f->{$field} = $entry->{$field};
  422. }
  423. }
  424. $f->{Closes} = join " ", sort { $a <=> $b } @{$f->{Closes}};
  425. $f->{Urgency} .= $urg_comment;
  426. return %$f if wantarray;
  427. return $f;
  428. }
  429. sub dpkg_str {
  430. return data2rfc822(scalar dpkg(@_));
  431. }
  432. =pod
  433. =head3 rfc822
  434. (and B<rfc822_str>)
  435. C<rfc822> returns an array of hashes (in list context) or a reference
  436. to this array (in scalar context) where each hash represents one entry
  437. in the changelog. For the format of such a hash see the description
  438. of the L<"dpkg"> method (while ignoring the remarks about which
  439. values are taken from the first entry).
  440. C<rfc822_str> returns a stringified version of this array.
  441. Both methods support the common output options described in
  442. section L<"COMMON OUTPUT OPTIONS">.
  443. =head3 rfc822_str
  444. See L<rfc822>.
  445. =cut
  446. sub rfc822 {
  447. my ($self, $config) = @_;
  448. $self->{config}{RFC822} = $config if $config;
  449. $config = $self->{config}{RFC822} || {};
  450. my $data = $self->_data_range( $config ) or return undef;
  451. my @out_data;
  452. foreach my $entry (@$data) {
  453. my $f = new Dpkg::Changelog::Entry;
  454. foreach my $field (qw( Urgency Source Version
  455. Distribution Maintainer Date )) {
  456. $f->{$field} = $entry->{$field};
  457. }
  458. $f->{Urgency} .= $entry->{Urgency_Comment};
  459. $f->{Changes} = get_dpkg_changes( $entry );
  460. $f->{Closes} = join " ", sort { $a <=> $b } @{$entry->{Closes}};
  461. # handle unknown fields
  462. foreach my $field (keys %$entry) {
  463. next if $CHANGELOG_FIELDS->{$field};
  464. $f->{$field} = $entry->{$field};
  465. }
  466. push @out_data, $f;
  467. }
  468. return @out_data if wantarray;
  469. return \@out_data;
  470. }
  471. sub rfc822_str {
  472. return data2rfc822(scalar rfc822(@_));
  473. }
  474. =pod
  475. =head1 COMMON OUTPUT OPTIONS
  476. The following options are supported by all output methods,
  477. all take a version number as value:
  478. =over 4
  479. =item since
  480. Causes changelog information from all versions strictly
  481. later than B<version> to be used.
  482. =item until
  483. Causes changelog information from all versions strictly
  484. earlier than B<version> to be used.
  485. =item from
  486. Similar to C<since> but also includes the information for the
  487. specified B<version> itself.
  488. =item to
  489. Similar to C<until> but also includes the information for the
  490. specified B<version> itself.
  491. =back
  492. The following options are also supported by all output methods but
  493. 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. Call Included entries
  511. C<E<lt>formatE<gt>({ since =E<gt> '2.0' })> 3.1, 3.0, 2.2
  512. C<E<lt>formatE<gt>({ until =E<gt> '2.0' })> 1.3, 1.2
  513. C<E<lt>formatE<gt>({ from =E<gt> '2.0' })> 3.1, 3.0, 2.2, 2.1, 2.0
  514. C<E<lt>formatE<gt>({ to =E<gt> '2.0' })> 2.0, 1.3, 1.2
  515. C<E<lt>formatE<gt>({ count =E<gt> 2 }>> 3.1, 3.0
  516. C<E<lt>formatE<gt>({ count =E<gt> -2 }>> 1.3, 1.2
  517. C<E<lt>formatE<gt>({ count =E<gt> 3,
  518. offset=E<gt> 2 }>> 2.2, 2.1, 2.0
  519. C<E<lt>formatE<gt>({ count =E<gt> 2,
  520. offset=E<gt> -3 }>> 2.0, 1.3
  521. C<E<lt>formatE<gt>({ count =E<gt> -2,
  522. offset=E<gt> 3 }>> 3.0, 2.2
  523. C<E<lt>formatE<gt>({ count =E<gt> -2,
  524. 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 UTILITY FUNCTIONS
  529. =head3 find_closes
  530. Takes one string as argument and finds "Closes: #123456, #654321" statements
  531. as supported by the Debian Archive software in it. Returns all closed bug
  532. numbers in an array reference.
  533. =cut
  534. sub find_closes {
  535. my $changes = shift;
  536. my @closes = ();
  537. while ($changes &&
  538. ($changes =~ /closes:\s*(?:bug)?\#?\s?\d+(?:,\s*(?:bug)?\#?\s?\d+)*/ig)) {
  539. push(@closes, $& =~ /\#?\s?(\d+)/g);
  540. }
  541. @closes = sort { $a <=> $b } @closes;
  542. return \@closes;
  543. }
  544. =pod
  545. =head3 data2rfc822
  546. Takes a single argument, either a Dpkg::Changelog::Entry object
  547. or a reference to an array of such objects.
  548. Returns the data in RFC822 format as string.
  549. =cut
  550. sub data2rfc822 {
  551. my ($data) = @_;
  552. if (ref($data) eq "ARRAY") {
  553. my @rfc822 = ();
  554. foreach my $entry (@$data) {
  555. push @rfc822, data2rfc822($entry);
  556. }
  557. return join "\n", @rfc822;
  558. } elsif (ref($data)) {
  559. my $rfc822_str = $data->output;
  560. return $rfc822_str;
  561. } else {
  562. return;
  563. }
  564. }
  565. =pod
  566. =head3 get_dpkg_changes
  567. Takes a Dpkg::Changelog::Entry object as first argument.
  568. Returns a string that is suitable for using it in a C<Changes> field
  569. in the output format of C<dpkg-parsechangelog>.
  570. =cut
  571. sub get_dpkg_changes {
  572. my $changes = "\n ".($_[0]->{Header}||'')."\n .\n".($_[0]->{Changes}||'');
  573. chomp $changes;
  574. $changes =~ s/^ $/ ./mgo;
  575. return $changes;
  576. }
  577. =pod
  578. =head3 my $fields = parse_changelog(%opt)
  579. This function will parse a changelog. In list context, it return as many
  580. Dpkg::Control object as the parser did output. In scalar context, it will
  581. return only the first one. If the parser didn't return any data, it will
  582. return an empty in list context or undef on scalar context. If the parser
  583. failed, it will die.
  584. The parsing itself is done by an external program (searched in the
  585. following list of directories: $opt{libdir},
  586. /usr/local/lib/dpkg/parsechangelog, /usr/lib/dpkg/parsechangelog) That
  587. program is named according to the format that it's able to parse. By
  588. default it's either "debian" or the format name lookep up in the 40 last
  589. lines of the changelog itself (extracted with this perl regular expression
  590. "\schangelog-format:\s+([0-9a-z]+)\W"). But it can be overriden
  591. with $opt{changelogformat}. The program expects the content of the
  592. changelog file on its standard input.
  593. The changelog file that is parsed is debian/changelog by default but it
  594. can be overriden with $opt{file}.
  595. All the other keys in %opt are forwarded as parameter to the external
  596. parser. If the key starts with "-", it's passed as is. If not, it's passed
  597. as "--<key>". If the value of the corresponding hash entry is defined, then
  598. it's passed as the parameter that follows.
  599. =cut
  600. sub parse_changelog {
  601. my (%options) = @_;
  602. my @parserpath = ("/usr/local/lib/dpkg/parsechangelog",
  603. "$dpkglibdir/parsechangelog",
  604. "/usr/lib/dpkg/parsechangelog");
  605. my $format = "debian";
  606. my $changelogfile = "debian/changelog";
  607. my $force = 0;
  608. # Extract and remove options that do not concern the changelog parser
  609. # itself (and that we shouldn't forward)
  610. if (exists $options{"libdir"}) {
  611. unshift @parserpath, $options{"libdir"};
  612. delete $options{"libdir"};
  613. }
  614. if (exists $options{"file"}) {
  615. $changelogfile = $options{"file"};
  616. delete $options{"file"};
  617. }
  618. if (exists $options{"changelogformat"}) {
  619. $format = $options{"changelogformat"};
  620. delete $options{"changelogformat"};
  621. $force = 1;
  622. }
  623. # XXX: For compatibility with old parsers, don't use --since but -v
  624. # This can be removed later (in lenny+1 for example)
  625. if (exists $options{"since"}) {
  626. my $since = $options{"since"};
  627. $options{"-v$since"} = undef;
  628. delete $options{"since"};
  629. }
  630. # Extract the format from the changelog file if possible
  631. unless($force or ($changelogfile eq "-")) {
  632. open(P, "-|", "tail", "-n", "40", $changelogfile);
  633. while(<P>) {
  634. $format = $1 if m/\schangelog-format:\s+([0-9a-z]+)\W/;
  635. }
  636. close(P) or subprocerr(_g("tail of %s"), $changelogfile);
  637. }
  638. # Find the right changelog parser
  639. my $parser;
  640. foreach my $dir (@parserpath) {
  641. my $candidate = "$dir/$format";
  642. next if not -e $candidate;
  643. if (-x _) {
  644. $parser = $candidate;
  645. last;
  646. } else {
  647. warning(_g("format parser %s not executable"), $candidate);
  648. }
  649. }
  650. error(_g("changelog format %s is unknown"), $format) if not defined $parser;
  651. # Create the arguments for the changelog parser
  652. my @exec = ($parser, "-l$changelogfile");
  653. foreach (keys %options) {
  654. if (m/^-/) {
  655. # Options passed untouched
  656. push @exec, $_;
  657. } else {
  658. # Non-options are mapped to long options
  659. push @exec, "--$_";
  660. }
  661. push @exec, $options{$_} if defined($options{$_});
  662. }
  663. # Fork and call the parser
  664. my $pid = open(P, "-|");
  665. syserr(_g("fork for %s"), $parser) unless defined $pid;
  666. if (not $pid) {
  667. if ($changelogfile ne "-") {
  668. open(STDIN, "<", $changelogfile) or
  669. syserr(_g("cannot open %s"), $changelogfile);
  670. }
  671. exec(@exec) || syserr(_g("cannot exec format parser: %s"), $parser);
  672. }
  673. # Get the output into several Dpkg::Control objects
  674. my (@res, $fields);
  675. while (1) {
  676. $fields = Dpkg::Control->new(type => CTRL_CHANGELOG);
  677. last unless $fields->parse_fh(\*P, _g("output of changelog parser"));
  678. push @res, $fields;
  679. }
  680. close(P) or subprocerr(_g("changelog parser %s"), $parser);
  681. if (wantarray) {
  682. return @res;
  683. } else {
  684. return $res[0] if (@res);
  685. return undef;
  686. }
  687. }
  688. =head1 NAME
  689. Dpkg::Changelog::Entry - represents one entry in a Debian changelog
  690. =head1 SYNOPSIS
  691. FIXME: to be written
  692. =head1 DESCRIPTION
  693. =cut
  694. package Dpkg::Changelog::Entry;
  695. use Dpkg::Control;
  696. use base qw(Dpkg::Control);
  697. sub new {
  698. my ($classname) = @_;
  699. my $entry = Dpkg::Control->new(type => CTRL_CHANGELOG);
  700. $entry->set_output_order(@CHANGELOG_FIELDS);
  701. bless $entry, $classname;
  702. }
  703. sub is_empty {
  704. my ($self) = @_;
  705. return !($self->{Changes}
  706. || $self->{Source}
  707. || $self->{Version}
  708. || $self->{Maintainer}
  709. || $self->{Date});
  710. }
  711. 1;
  712. __END__
  713. =head1 AUTHOR
  714. Frank Lichtenheld, E<lt>frank@lichtenheld.deE<gt>
  715. =head1 COPYRIGHT AND LICENSE
  716. Copyright E<copy> 2005, 2007 by Frank Lichtenheld
  717. Copyright E<copy> 2009 by Raphael Hertzog
  718. This program is free software; you can redistribute it and/or modify
  719. it under the terms of the GNU General Public License as published by
  720. the Free Software Foundation; either version 2 of the License, or
  721. (at your option) any later version.
  722. This program is distributed in the hope that it will be useful,
  723. but WITHOUT ANY WARRANTY; without even the implied warranty of
  724. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  725. GNU General Public License for more details.
  726. You should have received a copy of the GNU General Public License
  727. along with this program; if not, write to the Free Software
  728. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  729. =cut