debian.pl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/perl
  2. #
  3. # parsechangelog/debian
  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 <http://www.gnu.org/licenses/>.
  17. use strict;
  18. use warnings;
  19. use Getopt::Long qw(:config gnu_getopt auto_help);
  20. use POSIX;
  21. use Dpkg;
  22. use Dpkg::Gettext;
  23. use Dpkg::ErrorHandling;
  24. use Dpkg::Changelog::Debian;
  25. textdomain("dpkg-dev");
  26. $progname = "parsechangelog/$progname";
  27. sub version {
  28. printf _g("Debian %s version %s.\n"), $progname, $version;
  29. printf _g("
  30. Copyright (C) 1996 Ian Jackson.
  31. Copyright (C) 2005,2007 Frank Lichtenheld.");
  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>...] [<changelogfile>]
  40. Options:
  41. --help, -h print usage information
  42. --version, -V print version information
  43. --label, -l <file> name of the changelog file to
  44. use in error messages
  45. --file <file> changelog file to parse, defaults
  46. to '-' (standard input)
  47. --format <outputformat> see man page for list of available
  48. output formats, defaults to 'dpkg'
  49. for compatibility with dpkg-dev
  50. --since, -s, -v <version> include all changes later than version
  51. --until, -u <version> include all changes earlier than version
  52. --from, -f <version> include all changes equal or later
  53. than version
  54. --to, -t <version> include all changes up to or equal
  55. than version
  56. --count, -c, -n <number> include <number> entries from the top
  57. (or the tail if <number> is lower than 0)
  58. --offset, -o <number> change the starting point for --count,
  59. counted from the top (or the tail if
  60. <number> is lower than 0)
  61. --all include all changes
  62. "), $progname;
  63. }
  64. my ( $since, $until, $from, $to, $all, $count, $offset, $file, $label );
  65. my $default_file = '-';
  66. my $format = 'dpkg';
  67. my %allowed_formats = (
  68. dpkg => 1,
  69. rfc822 => 1,
  70. );
  71. sub set_format {
  72. my ($opt, $val) = @_;
  73. unless ($allowed_formats{$val}) {
  74. usageerr(_g('output format %s not supported'), $val );
  75. }
  76. $format = $val;
  77. }
  78. GetOptions( "file=s" => \$file,
  79. "label|l=s" => \$label,
  80. "since|v=s" => \$since,
  81. "until|u=s" => \$until,
  82. "from|f=s" => \$from,
  83. "to|t=s" => \$to,
  84. "count|c|n=i" => \$count,
  85. "offset|o=i" => \$offset,
  86. "help|h" => sub{usage();exit(0)},
  87. "version|V" => sub{version();exit(0)},
  88. "format=s" => \&set_format,
  89. "all|a" => \$all,
  90. )
  91. or do { usage(); exit(2) };
  92. usageerr('too many arguments') if @ARGV > 1;
  93. if (@ARGV) {
  94. if ($file && ($file ne $ARGV[0])) {
  95. usageerr(_g('more than one file specified (%s and %s)'),
  96. $file, $ARGV[0] );
  97. }
  98. $file = $ARGV[0];
  99. }
  100. $file ||= $default_file;
  101. $label ||= $file;
  102. unless (defined($since) or defined($until) or defined($from) or
  103. defined($to) or defined($offset) or defined($count) or
  104. defined($all))
  105. {
  106. $count = 1;
  107. }
  108. my %all = $all ? ( all => $all ) : ();
  109. my $range = {
  110. since => $since, until => $until, from => $from, to => $to,
  111. count => $count, offset => $offset,
  112. %all
  113. };
  114. my $changes = Dpkg::Changelog::Debian->new(reportfile => $label, range => $range);
  115. if ($file eq '-') {
  116. $changes->parse(\*STDIN, _g("<standard input>"))
  117. or error(_g('fatal error occured while parsing input'));
  118. } else {
  119. $changes->load($file)
  120. or error(_g('fatal error occured while parsing %s'), $file);
  121. }
  122. eval qq{
  123. my \$output = \$changes->$format(\$range);
  124. print \$output if defined \$output;
  125. };
  126. if ($@) {
  127. error("%s", $@);
  128. }