debian.pl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/perl
  2. #
  3. # parsechangelog/debian
  4. #
  5. # Copyright © 1996 Ian Jackson
  6. # Copyright © 2005,2007 Frank Lichtenheld
  7. # Copyright © 2006-2014 Guillem Jover <guillem@debian.org>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. use strict;
  22. use warnings;
  23. use Getopt::Long qw(:config posix_default bundling no_ignorecase);
  24. use Dpkg ();
  25. use Dpkg::Gettext;
  26. use Dpkg::ErrorHandling;
  27. use Dpkg::Changelog::Debian;
  28. textdomain('dpkg-dev');
  29. $Dpkg::PROGNAME = "parsechangelog/$Dpkg::PROGNAME";
  30. sub version {
  31. printf g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  32. printf g_('
  33. This is free software; see the GNU General Public License version 2 or
  34. later for copying conditions. There is NO warranty.
  35. ');
  36. }
  37. sub usage {
  38. printf g_(
  39. 'Usage: %s [<option>...] [<changelog-file>]')
  40. . "\n\n" . g_(
  41. "Options:
  42. --file <file> changelog <file> to parse (defaults to '-').
  43. -l, --label <file> changelog <file> name to use in error messages.
  44. --format <output-format>
  45. set the output format (defaults to 'dpkg').
  46. --all include all changes.
  47. -s, --since <version> include all changes later than <version>.
  48. -v <version> ditto.
  49. -u, --until <version> include all changes earlier than <version>.
  50. -f, --from <version> include all changes equal or later than <version>.
  51. -t, --to <version> include all changes up to or equal than <version>.
  52. -c, --count <number> include <number> entries from the top (or tail if
  53. <number> is lower than 0).
  54. -n <number> ditto.
  55. -o, --offset <number> change starting point for --count, counted from
  56. the top (or tail if <number> is lower than 0).
  57. -?, --help print usage information.
  58. -V, --version print version information.
  59. "), $Dpkg::PROGNAME;
  60. }
  61. my ( $since, $until, $from, $to, $all, $count, $offset, $file, $label );
  62. my $default_file = '-';
  63. my $format = 'dpkg';
  64. my %allowed_formats = (
  65. dpkg => 1,
  66. rfc822 => 1,
  67. );
  68. sub set_format {
  69. my ($opt, $val) = @_;
  70. unless ($allowed_formats{$val}) {
  71. usageerr(g_('output format %s not supported'), $val );
  72. }
  73. $format = $val;
  74. }
  75. my @options_spec = (
  76. 'file=s' => \$file,
  77. 'label|l=s' => \$label,
  78. 'since|v=s' => \$since,
  79. 'until|u=s' => \$until,
  80. 'from|f=s' => \$from,
  81. 'to|t=s' => \$to,
  82. 'count|c|n=i' => \$count,
  83. 'offset|o=i' => \$offset,
  84. 'help|?' => sub{ usage(); exit(0) },
  85. 'version|V' => sub{version();exit(0)},
  86. 'format=s' => \&set_format,
  87. 'all|a' => \$all,
  88. );
  89. {
  90. local $SIG{__WARN__} = sub { usageerr($_[0]) };
  91. GetOptions(@options_spec);
  92. }
  93. usageerr('too many arguments') if @ARGV > 1;
  94. if (@ARGV) {
  95. if ($file && ($file ne $ARGV[0])) {
  96. usageerr(g_('more than one file specified (%s and %s)'),
  97. $file, $ARGV[0] );
  98. }
  99. $file = $ARGV[0];
  100. }
  101. $file //= $default_file;
  102. $label //= $file;
  103. unless (defined($since) or defined($until) or defined($from) or
  104. defined($to) or defined($offset) or defined($count) or
  105. defined($all))
  106. {
  107. $count = 1;
  108. }
  109. my %all = $all ? ( all => $all ) : ();
  110. my $range = {
  111. since => $since, until => $until, from => $from, to => $to,
  112. count => $count, offset => $offset,
  113. %all
  114. };
  115. my $changes = Dpkg::Changelog::Debian->new(reportfile => $label, range => $range);
  116. $changes->load($file)
  117. or error(g_('fatal error occurred while parsing %s'), $file);
  118. eval qq{
  119. my \$output = \$changes->$format(\$range);
  120. print \$output if defined \$output;
  121. };
  122. if ($@) {
  123. error('%s', $@);
  124. }