Parse.pm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. use Dpkg ();
  29. use Dpkg::Gettext;
  30. use Dpkg::ErrorHandling;
  31. use Dpkg::Control::Changelog;
  32. use Exporter qw(import);
  33. our @EXPORT = qw(changelog_parse);
  34. =over 4
  35. =item my $fields = changelog_parse(%opt)
  36. This function will parse a changelog. In list context, it return as many
  37. Dpkg::Control object as the parser did output. In scalar context, it will
  38. return only the first one. If the parser didn't return any data, it will
  39. return an empty in list context or undef on scalar context. If the parser
  40. failed, it will die.
  41. The parsing itself is done by an external program (searched in the
  42. following list of directories: $opt{libdir},
  43. F</usr/local/lib/dpkg/parsechangelog>, F</usr/lib/dpkg/parsechangelog>) That
  44. program is named according to the format that it's able to parse. By
  45. default it's either "debian" or the format name lookep up in the 40 last
  46. lines of the changelog itself (extracted with this perl regular expression
  47. "\schangelog-format:\s+([0-9a-z]+)\W"). But it can be overridden
  48. with $opt{changelogformat}. The program expects the content of the
  49. changelog file on its standard input.
  50. The changelog file that is parsed is F<debian/changelog> by default but it
  51. can be overridden with $opt{file}.
  52. All the other keys in %opt are forwarded as parameter to the external
  53. parser. If the key starts with "-", it's passed as is. If not, it's passed
  54. as "--<key>". If the value of the corresponding hash entry is defined, then
  55. it's passed as the parameter that follows.
  56. =cut
  57. sub changelog_parse {
  58. my (%options) = @_;
  59. my @parserpath = ('/usr/local/lib/dpkg/parsechangelog',
  60. "$Dpkg::LIBDIR/parsechangelog",
  61. '/usr/lib/dpkg/parsechangelog');
  62. my $format = 'debian';
  63. my $force = 0;
  64. # Extract and remove options that do not concern the changelog parser
  65. # itself (and that we shouldn't forward)
  66. if (exists $options{libdir}) {
  67. unshift @parserpath, $options{libdir};
  68. delete $options{libdir};
  69. }
  70. if (exists $options{changelogformat}) {
  71. $format = $options{changelogformat};
  72. delete $options{changelogformat};
  73. $force = 1;
  74. }
  75. # Set a default filename
  76. if (not exists $options{file}) {
  77. $options{file} = 'debian/changelog';
  78. }
  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 (keys %options) {
  106. if (m/^-/) {
  107. # Options passed untouched
  108. push @exec, $_;
  109. } else {
  110. # Non-options are mapped to long options
  111. push @exec, "--$_";
  112. }
  113. push @exec, $options{$_} if defined($options{$_});
  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 exec 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. =cut
  138. 1;