Parse.pm 8.5 KB

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