Parse.pm 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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.01';
  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::Changelog::Debian;
  39. use Dpkg::Control::Changelog;
  40. =head1 FUNCTIONS
  41. =over 4
  42. =item $fields = changelog_parse_debian(%opt)
  43. This function will parse a changelog. In list context, it returns as many
  44. Dpkg::Control objects as the parser did create. In scalar context, it will
  45. return only the first one. If the parser did not return any data, it will
  46. return an empty list in list context or undef on scalar context. If the
  47. parser failed, it will die.
  48. The changelog file that is parsed is F<debian/changelog> by default but it
  49. can be overridden with $opt{file}. The default output format is "dpkg" but
  50. it can be overridden with $opt{format}.
  51. The parsing itself is done by Dpkg::Changelog::Debian.
  52. =cut
  53. sub changelog_parse_debian {
  54. my (%options) = @_;
  55. # Setup and sanity checks.
  56. $options{file} //= 'debian/changelog';
  57. $options{label} //= $options{file};
  58. $options{format} //= 'dpkg';
  59. $options{all} = 1 if exists $options{all};
  60. if (none { defined $options{$_} } qw(since until from to offset count all)) {
  61. $options{count} = 1;
  62. }
  63. my $range;
  64. foreach my $opt (qw(since until from to offset count all)) {
  65. $range->{$opt} = $options{$opt} if exists $options{$opt};
  66. }
  67. my $changes = Dpkg::Changelog::Debian->new(reportfile => $options{label},
  68. range => $range);
  69. $changes->load($options{file})
  70. or error(g_('fatal error occurred while parsing %s'), $options{file});
  71. # Get the output into several Dpkg::Control objects.
  72. my @res;
  73. if ($options{format} eq 'dpkg') {
  74. push @res, $changes->dpkg($range);
  75. } elsif ($options{format} eq 'rfc822') {
  76. push @res, $changes->rfc822($range)->get();
  77. } else {
  78. error(g_('unknown output format %s'), $options{format});
  79. }
  80. if (wantarray) {
  81. return @res;
  82. } else {
  83. return $res[0] if @res;
  84. return;
  85. }
  86. }
  87. sub _changelog_detect_format {
  88. my $file = shift;
  89. my $format = 'debian';
  90. # Extract the format from the changelog file if possible
  91. if ($file ne '-') {
  92. local $_;
  93. open my $format_fh, '-|', 'tail', '-n', '40', $file
  94. or syserr(g_('cannot create pipe for %s'), 'tail');
  95. while (<$format_fh>) {
  96. $format = $1 if m/\schangelog-format:\s+([0-9a-z]+)\W/;
  97. }
  98. close $format_fh or subprocerr(g_('tail of %s'), $file);
  99. }
  100. return $format;
  101. }
  102. =item $fields = changelog_parse_plugin(%opt)
  103. This function will parse a changelog. In list context, it returns as many
  104. Dpkg::Control objects as the parser did output. In scalar context, it will
  105. return only the first one. If the parser did not return any data, it will
  106. return an empty list in list context or undef on scalar context. If the
  107. parser failed, it will die.
  108. The parsing itself is done by an external program (searched in the
  109. following list of directories: $opt{libdir},
  110. F</usr/local/lib/dpkg/parsechangelog>, F</usr/lib/dpkg/parsechangelog>).
  111. That program is named according to the format that it is able to parse. By
  112. default it is either "debian" or the format name looked up in the 40 last
  113. lines of the changelog itself (extracted with this perl regular expression
  114. "\schangelog-format:\s+([0-9a-z]+)\W"). But it can be overridden
  115. with $opt{changelogformat}. The program expects the content of the
  116. changelog file on its standard input.
  117. The changelog file that is parsed is F<debian/changelog> by default but it
  118. can be overridden with $opt{file}.
  119. All the other keys in %opt are forwarded as parameter to the external
  120. parser. If the key starts with "-", it is passed as is. If not, it is passed
  121. as "--<key>". If the value of the corresponding hash entry is defined, then
  122. it is passed as the parameter that follows.
  123. =cut
  124. sub changelog_parse_plugin {
  125. my (%options) = @_;
  126. # Setup and sanity checks.
  127. $options{file} //= 'debian/changelog';
  128. my @parserpath = ('/usr/local/lib/dpkg/parsechangelog',
  129. "$Dpkg::LIBDIR/parsechangelog",
  130. '/usr/lib/dpkg/parsechangelog');
  131. my $format;
  132. # Extract and remove options that do not concern the changelog parser
  133. # itself (and that we shouldn't forward)
  134. delete $options{forceplugin};
  135. if (exists $options{libdir}) {
  136. unshift @parserpath, $options{libdir};
  137. delete $options{libdir};
  138. }
  139. if (exists $options{changelogformat}) {
  140. $format = $options{changelogformat};
  141. delete $options{changelogformat};
  142. } else {
  143. $format = _changelog_detect_format($options{file});
  144. }
  145. # Find the right changelog parser
  146. my $parser;
  147. foreach my $dir (@parserpath) {
  148. my $candidate = "$dir/$format";
  149. next if not -e $candidate;
  150. if (-x _) {
  151. $parser = $candidate;
  152. last;
  153. } else {
  154. warning(g_('format parser %s not executable'), $candidate);
  155. }
  156. }
  157. error(g_('changelog format %s is unknown'), $format) if not defined $parser;
  158. # Create the arguments for the changelog parser
  159. my @exec = ($parser, "-l$options{file}");
  160. foreach my $option (keys %options) {
  161. if ($option =~ m/^-/) {
  162. # Options passed untouched
  163. push @exec, $option;
  164. } else {
  165. # Non-options are mapped to long options
  166. push @exec, "--$option";
  167. }
  168. push @exec, $options{$option} if defined $options{$option};
  169. }
  170. # Fork and call the parser
  171. my $pid = open(my $parser_fh, '-|');
  172. syserr(g_('cannot fork for %s'), $parser) unless defined $pid;
  173. if (not $pid) {
  174. exec @exec or syserr(g_('cannot execute format parser: %s'), $parser);
  175. }
  176. # Get the output into several Dpkg::Control objects
  177. my (@res, $fields);
  178. while (1) {
  179. $fields = Dpkg::Control::Changelog->new();
  180. last unless $fields->parse($parser_fh, g_('output of changelog parser'));
  181. push @res, $fields;
  182. }
  183. close($parser_fh) or subprocerr(g_('changelog parser %s'), $parser);
  184. if (wantarray) {
  185. return @res;
  186. } else {
  187. return $res[0] if (@res);
  188. return;
  189. }
  190. }
  191. =item $fields = changelog_parse(%opt)
  192. This function will parse a changelog. In list context, it returns as many
  193. Dpkg::Control objects as the parser did create. In scalar context, it will
  194. return only the first one. If the parser did not return any data, it will
  195. return an empty list in list context or undef on scalar context. If the
  196. parser failed, it will die.
  197. If $opt{forceplugin} is false and $opt{changelogformat} is "debian", then
  198. changelog_parse_debian() is called to perform the parsing. Otherwise
  199. changelog_parse_plugin() is used.
  200. The changelog file that is parsed is F<debian/changelog> by default but it
  201. can be overridden with $opt{file}.
  202. =cut
  203. sub changelog_parse {
  204. my (%options) = @_;
  205. $options{forceplugin} //= 0;
  206. $options{file} //= 'debian/changelog';
  207. $options{changelogformat} //= _changelog_detect_format($options{file});
  208. if (not $options{forceplugin} and
  209. $options{changelogformat} eq 'debian') {
  210. return changelog_parse_debian(%options);
  211. } else {
  212. return changelog_parse_plugin(%options);
  213. }
  214. }
  215. =back
  216. =head1 CHANGES
  217. =head2 Version 1.01 (dpkg 1.18.2)
  218. New functions: changelog_parse_debian(), changelog_parse_plugin().
  219. =head2 Version 1.00 (dpkg 1.15.6)
  220. Mark the module as public.
  221. =cut
  222. 1;