Changelog.pm 25 KB

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