Util.pm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #
  2. # Parse::DebianChangelog::Util
  3. #
  4. # Copyright 1996 Ian Jackson
  5. # Copyright 2005 Frank Lichtenheld <frank@lichtenheld.de>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. #
  21. =head1 NAME
  22. Parse::DebianChangelog::Util - utility functions for parsing Debian changelogs
  23. =head1 DESCRIPTION
  24. This is currently only used internally by Parse::DebianChangelog.
  25. There may be still API changes until this module is finalized.
  26. =head2 Functions
  27. =cut
  28. package Parse::DebianChangelog::Util;
  29. use strict;
  30. use warnings;
  31. require Exporter;
  32. our @ISA = qw(Exporter);
  33. our %EXPORT_TAGS = ( 'all' => [ qw(
  34. find_closes
  35. data2rfc822
  36. data2rfc822_mult
  37. get_dpkg_changes
  38. ) ] );
  39. our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
  40. our @EXPORT = qw(
  41. );
  42. =pod
  43. =head3 find_closes
  44. Takes one string as argument and finds "Closes: #123456, #654321" statements
  45. as supported by the Debian Archive software in it. Returns all closed bug
  46. numbers in an array reference.
  47. =cut
  48. sub find_closes {
  49. my $changes = shift;
  50. my @closes = ();
  51. while ($changes && ($changes =~ /closes:\s*(?:bug)?\#?\s?\d+(?:,\s*(?:bug)?\#?\s?\d+)*/ig)) {
  52. push(@closes, $& =~ /\#?\s?(\d+)/g);
  53. }
  54. @closes = sort { $a <=> $b } @closes;
  55. return \@closes;
  56. }
  57. =pod
  58. =head3 data2rfc822
  59. Takes two hash references as arguments. The first should contain the
  60. data to output in RFC822 format. The second can contain a sorting order
  61. for the fields. The higher the numerical value of the hash value, the
  62. earlier the field is printed if it exists.
  63. Return the data in RFC822 format as string.
  64. =cut
  65. sub data2rfc822 {
  66. my ($data, $fieldimps) = @_;
  67. my $rfc822_str = '';
  68. # based on /usr/lib/dpkg/controllib.pl
  69. for my $f (sort { $fieldimps->{$b} <=> $fieldimps->{$a} } keys %$data) {
  70. my $v= $data->{$f} or next;
  71. $v =~ m/\S/o || next; # delete whitespace-only fields
  72. $v =~ m/\n\S/o
  73. && warn(__g("field %s has newline then non whitespace >%s<",
  74. $f, $v ));
  75. $v =~ m/\n[ \t]*\n/o && warn(__g("field %s has blank lines >%s<",
  76. $f, $v ));
  77. $v =~ m/\n$/o && warn(__g("field %s has trailing newline >%s<",
  78. $f, $v ));
  79. $v =~ s/\$\{\}/\$/go;
  80. $rfc822_str .= "$f: $v\n";
  81. }
  82. return $rfc822_str;
  83. }
  84. =pod
  85. =head3 data2rfc822_mult
  86. The first argument should be an array ref to an array of hash references.
  87. The second argument is a hash reference and has the same meaning as
  88. the second argument of L<data2rfc822>.
  89. Calls L<data2rfc822> for each element of the array given as first
  90. argument and returns the concatenated results.
  91. =cut
  92. sub data2rfc822_mult {
  93. my ($data, $fieldimps) = @_;
  94. my @rfc822 = ();
  95. foreach my $entry (@$data) {
  96. push @rfc822, data2rfc822($entry,$fieldimps);
  97. }
  98. return join "\n", @rfc822;
  99. }
  100. =pod
  101. =head3 get_dpkg_changes
  102. Takes a Parse::DebianChangelog::Entry object as first argument.
  103. Returns a string that is suitable for using it in a C<Changes> field
  104. in the output format of C<dpkg-parsechangelog>.
  105. =cut
  106. sub get_dpkg_changes {
  107. my $changes = "\n ".($_[0]->Header||'')."\n .\n".($_[0]->Changes||'');
  108. chomp $changes;
  109. $changes =~ s/^ $/ ./mgo;
  110. return $changes;
  111. }
  112. 1;
  113. __END__
  114. =head1 SEE ALSO
  115. Parse::DebianChangelog, Parse::DebianChangelog::Entry
  116. =head1 AUTHOR
  117. Frank Lichtenheld, E<lt>frank@lichtenheld.deE<gt>
  118. =head1 COPYRIGHT AND LICENSE
  119. Copyright (C) 2005 by Frank Lichtenheld
  120. This program is free software; you can redistribute it and/or modify
  121. it under the terms of the GNU General Public License as published by
  122. the Free Software Foundation; either version 2 of the License, or
  123. (at your option) any later version.
  124. This program is distributed in the hope that it will be useful,
  125. but WITHOUT ANY WARRANTY; without even the implied warranty of
  126. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  127. GNU General Public License for more details.
  128. You should have received a copy of the GNU General Public License
  129. along with this program; if not, write to the Free Software
  130. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  131. =cut