Changelog.pm 17 KB

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