Parse.pm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 single function changelog_parse() which reproduces
  22. all the features of dpkg-parsechangelog.
  23. =head2 FUNCTIONS
  24. =cut
  25. package Dpkg::Changelog::Parse;
  26. use strict;
  27. use warnings;
  28. our $VERSION = '1.00';
  29. our @EXPORT = qw(
  30. changelog_parse
  31. );
  32. use Exporter qw(import);
  33. use Dpkg ();
  34. use Dpkg::Gettext;
  35. use Dpkg::ErrorHandling;
  36. use Dpkg::Control::Changelog;
  37. =over 4
  38. =item my $fields = changelog_parse(%opt)
  39. This function will parse a changelog. In list context, it return as many
  40. Dpkg::Control object as the parser did output. In scalar context, it will
  41. return only the first one. If the parser didn't return any data, it will
  42. return an empty in list context or undef on scalar context. If the parser
  43. failed, it will die.
  44. The parsing itself is done by an external program (searched in the
  45. following list of directories: $opt{libdir},
  46. F</usr/local/lib/dpkg/parsechangelog>, F</usr/lib/dpkg/parsechangelog>) That
  47. program is named according to the format that it's able to parse. By
  48. default it's either "debian" or the format name lookep up in the 40 last
  49. lines of the changelog itself (extracted with this perl regular expression
  50. "\schangelog-format:\s+([0-9a-z]+)\W"). But it can be overridden
  51. with $opt{changelogformat}. The program expects the content of the
  52. changelog file on its standard input.
  53. The changelog file that is parsed is F<debian/changelog> by default but it
  54. can be overridden with $opt{file}.
  55. All the other keys in %opt are forwarded as parameter to the external
  56. parser. If the key starts with "-", it's passed as is. If not, it's passed
  57. as "--<key>". If the value of the corresponding hash entry is defined, then
  58. it's passed as the parameter that follows.
  59. =cut
  60. sub changelog_parse {
  61. my (%options) = @_;
  62. my @parserpath = ('/usr/local/lib/dpkg/parsechangelog',
  63. "$Dpkg::LIBDIR/parsechangelog",
  64. '/usr/lib/dpkg/parsechangelog');
  65. my $format = 'debian';
  66. my $force = 0;
  67. # Extract and remove options that do not concern the changelog parser
  68. # itself (and that we shouldn't forward)
  69. if (exists $options{libdir}) {
  70. unshift @parserpath, $options{libdir};
  71. delete $options{libdir};
  72. }
  73. if (exists $options{changelogformat}) {
  74. $format = $options{changelogformat};
  75. delete $options{changelogformat};
  76. $force = 1;
  77. }
  78. # Set a default filename
  79. $options{file} //= 'debian/changelog';
  80. my $changelogfile = $options{file};
  81. # Extract the format from the changelog file if possible
  82. unless ($force or ($changelogfile eq '-')) {
  83. local $_;
  84. open(my $format_fh, '-|', 'tail', '-n', '40', $changelogfile)
  85. or syserr(g_('cannot create pipe for %s'), 'tail');
  86. while (<$format_fh>) {
  87. $format = $1 if m/\schangelog-format:\s+([0-9a-z]+)\W/;
  88. }
  89. close($format_fh) or subprocerr(g_('tail of %s'), $changelogfile);
  90. }
  91. # Find the right changelog parser
  92. my $parser;
  93. foreach my $dir (@parserpath) {
  94. my $candidate = "$dir/$format";
  95. next if not -e $candidate;
  96. if (-x _) {
  97. $parser = $candidate;
  98. last;
  99. } else {
  100. warning(g_('format parser %s not executable'), $candidate);
  101. }
  102. }
  103. error(g_('changelog format %s is unknown'), $format) if not defined $parser;
  104. # Create the arguments for the changelog parser
  105. my @exec = ($parser, "-l$changelogfile");
  106. foreach my $option (keys %options) {
  107. if ($option =~ m/^-/) {
  108. # Options passed untouched
  109. push @exec, $option;
  110. } else {
  111. # Non-options are mapped to long options
  112. push @exec, "--$option";
  113. }
  114. push @exec, $options{$option} if defined $options{$option};
  115. }
  116. # Fork and call the parser
  117. my $pid = open(my $parser_fh, '-|');
  118. syserr(g_('cannot fork for %s'), $parser) unless defined $pid;
  119. if (not $pid) {
  120. exec @exec or syserr(g_('cannot execute format parser: %s'), $parser);
  121. }
  122. # Get the output into several Dpkg::Control objects
  123. my (@res, $fields);
  124. while (1) {
  125. $fields = Dpkg::Control::Changelog->new();
  126. last unless $fields->parse($parser_fh, g_('output of changelog parser'));
  127. push @res, $fields;
  128. }
  129. close($parser_fh) or subprocerr(g_('changelog parser %s'), $parser);
  130. if (wantarray) {
  131. return @res;
  132. } else {
  133. return $res[0] if (@res);
  134. return;
  135. }
  136. }
  137. =back
  138. =head1 CHANGES
  139. =head2 Version 1.00
  140. Mark the module as public.
  141. =cut
  142. 1;