Changelog.pm 24 KB

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