Parse.pm 5.1 KB

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