Parse.pm 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. # Copyright © 2005, 2007 Frank Lichtenheld <frank@lichtenheld.de>
  2. # Copyright © 2009 Raphaël Hertzog <hertzog@debian.org>
  3. # Copyright © 2010, 2012-2015 Guillem Jover <guillem@debian.org>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. =encoding utf8
  18. =head1 NAME
  19. Dpkg::Changelog::Parse - generic changelog parser for dpkg-parsechangelog
  20. =head1 DESCRIPTION
  21. This module provides a set of functions which reproduce all the features
  22. of dpkg-parsechangelog.
  23. =cut
  24. package Dpkg::Changelog::Parse;
  25. use strict;
  26. use warnings;
  27. our $VERSION = '1.02';
  28. our @EXPORT = qw(
  29. changelog_parse_debian
  30. changelog_parse_plugin
  31. changelog_parse
  32. );
  33. use Exporter qw(import);
  34. use Dpkg ();
  35. use Dpkg::Util qw(none);
  36. use Dpkg::Gettext;
  37. use Dpkg::ErrorHandling;
  38. use Dpkg::Control::Changelog;
  39. sub _changelog_detect_format {
  40. my $file = shift;
  41. my $format = 'debian';
  42. # Extract the format from the changelog file if possible
  43. if ($file ne '-') {
  44. local $_;
  45. open my $format_fh, '-|', 'tail', '-n', '40', $file
  46. or syserr(g_('cannot create pipe for %s'), 'tail');
  47. while (<$format_fh>) {
  48. $format = $1 if m/\schangelog-format:\s+([0-9a-z]+)\W/;
  49. }
  50. close $format_fh or subprocerr(g_('tail of %s'), $file);
  51. }
  52. return $format;
  53. }
  54. =head1 FUNCTIONS
  55. =over 4
  56. =item $fields = changelog_parse_debian(%opt)
  57. This function is deprecated, use changelog_parse() instead, with the changelog
  58. format set to "debian".
  59. =cut
  60. sub changelog_parse_debian {
  61. my (%options) = @_;
  62. warnings::warnif('deprecated',
  63. 'deprecated function changelog_parse_debian, use changelog_parse instead');
  64. # Force the plugin to be debian.
  65. $options{changelogformat} = 'debian';
  66. return _changelog_parse(%options);
  67. }
  68. =item $fields = changelog_parse_plugin(%opt)
  69. This function is deprecated, use changelog_parse() instead.
  70. =cut
  71. sub changelog_parse_plugin {
  72. my (%options) = @_;
  73. warnings::warnif('deprecated',
  74. 'deprecated function changelog_parse_plugin, use changelog_parse instead');
  75. return _changelog_parse(%options);
  76. }
  77. =item $fields = changelog_parse(%opt)
  78. This function will parse a changelog. In list context, it returns as many
  79. Dpkg::Control objects as the parser did create. In scalar context, it will
  80. return only the first one. If the parser did not return any data, it will
  81. return an empty list in list context or undef on scalar context. If the
  82. parser failed, it will die.
  83. The changelog file that is parsed is F<debian/changelog> by default but it
  84. can be overridden with $opt{file}. The default output format is "dpkg" but
  85. it can be overridden with $opt{format}.
  86. The parsing itself is done by a parser module (searched in the standard
  87. perl library directories. That module is named according to the format that
  88. it is able to parse, with the name capitalized. By default it is either
  89. Dpkg::Changelog::Debian (from the "debian" format) or the format name looked
  90. up in the 40 last lines of the changelog itself (extracted with this perl
  91. regular expression "\schangelog-format:\s+([0-9a-z]+)\W"). But it can be
  92. overridden with $opt{changelogformat}.
  93. All the other keys in %opt are forwarded to the parser module constructor.
  94. =cut
  95. sub _changelog_parse {
  96. my (%options) = @_;
  97. # Setup and sanity checks.
  98. if (exists $options{libdir}) {
  99. warnings::warnif('deprecated',
  100. 'obsolete libdir option, changelog parsers are now perl modules');
  101. }
  102. $options{file} //= 'debian/changelog';
  103. $options{label} //= $options{file};
  104. $options{changelogformat} //= _changelog_detect_format($options{file});
  105. $options{format} //= 'dpkg';
  106. my @range_opts = qw(since until from to offset count all);
  107. $options{all} = 1 if exists $options{all};
  108. if (none { defined $options{$_} } @range_opts) {
  109. $options{count} = 1;
  110. }
  111. my $range;
  112. foreach my $opt (@range_opts) {
  113. $range->{$opt} = $options{$opt} if exists $options{$opt};
  114. }
  115. # Find the right changelog parser.
  116. my $format = ucfirst lc $options{changelogformat};
  117. my $changes;
  118. eval qq{
  119. require Dpkg::Changelog::$format;
  120. \$changes = Dpkg::Changelog::$format->new();
  121. };
  122. error(g_('changelog format %s is unknown: %s'), $format, $@) if $@;
  123. $changes->set_options(reportfile => $options{label}, range => $range);
  124. # Load and parse the changelog.
  125. $changes->load($options{file})
  126. or error(g_('fatal error occurred while parsing %s'), $options{file});
  127. # Get the output into several Dpkg::Control objects.
  128. my @res;
  129. if ($options{format} eq 'dpkg') {
  130. push @res, $changes->format_range('dpkg', $range);
  131. } elsif ($options{format} eq 'rfc822') {
  132. push @res, $changes->format_range('rfc822', $range);
  133. } else {
  134. error(g_('unknown output format %s'), $options{format});
  135. }
  136. if (wantarray) {
  137. return @res;
  138. } else {
  139. return $res[0] if @res;
  140. return;
  141. }
  142. }
  143. sub changelog_parse {
  144. my (%options) = @_;
  145. if (exists $options{forceplugin}) {
  146. warnings::warnif('deprecated', 'obsolete forceplugin option');
  147. }
  148. return _changelog_parse(%options);
  149. }
  150. =back
  151. =head1 CHANGES
  152. =head2 Version 1.02 (dpkg 1.18.8)
  153. Deprecated functions: changelog_parse_debian(), changelog_parse_plugin().
  154. Obsolete options: $forceplugin, $libdir.
  155. =head2 Version 1.01 (dpkg 1.18.2)
  156. New functions: changelog_parse_debian(), changelog_parse_plugin().
  157. =head2 Version 1.00 (dpkg 1.15.6)
  158. Mark the module as public.
  159. =cut
  160. 1;