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