File Coverage

blib/lib/RPM/Specfile.pm
Criterion Covered Total %
statement 246 374 65.7
branch 65 114 57.0
condition 24 44 54.5
subroutine 46 59 77.9
pod 54 55 98.1
total 435 646 67.3


line stmt bran cond sub pod time code
1             package RPM::Specfile;
2              
3 1     1   14884 use POSIX;
  1         24964  
  1         10  
4              
5 1     1   3979 use strict;
  1         3  
  1         65  
6              
7 1     1   7 use vars qw/$VERSION/;
  1         8  
  1         1635  
8              
9             $VERSION = '1.51';
10              
11             sub new {
12 1     1 1 122 my $class = shift;
13              
14 1         4 my $self = bless { }, $class;
15              
16 1         4 return $self;
17             }
18              
19             my @simple_accessors =
20             qw(
21             build buildarch buildrequires buildroot check clean description distribution
22             epoch file_param group install license macros name packager post postun
23             pre preun prep release requires summary url vendor version
24             );
25              
26             foreach my $field (@simple_accessors) {
27             my $sub = q {
28             sub RPM::Specfile::[[field]] {
29             my $self = shift;
30             if (@_) {
31             my $value = shift;
32             $self->{__[[field]]__} = $value;
33             }
34             return $self->{__[[field]]__};
35             }
36             };
37              
38             $sub =~ s/\[\[field\]\]/$field/g;
39 3 100   3 1 33 eval $sub;
  3 50   1 1 8  
  1 50   1 1 1  
  1 100   4 1 3  
  3 50   1 0 20  
  1 50   1 1 3  
  1 100   3 1 12  
  0 50   1 1 0  
  0 50   1 1 0  
  1 100   3 1 6  
  1 100   3 1 2  
  1 100   3 1 5  
  0 100   3 1 0  
  0 100   3 1 0  
  1 100   3 1 6  
  4 50   1 1 5  
  4 100   3 1 13  
  1 100   3 1 2  
  1 100   3 1 3  
  4 100   5 1 42  
  1 100   3 1 2  
  1 100   3 1 4  
  0 50   1 1 0  
  0 50   1 1 0  
  1 50   1 1 5  
  1 50   1 1 2  
  1 100   3 1 5  
  0         0  
  0         0  
  1         5  
  3         31  
  3         15  
  1         2  
  1         3  
  3         11  
  1         2  
  1         4  
  0         0  
  0         0  
  1         4  
  1         2  
  1         5  
  0         0  
  0         0  
  1         12  
  3         28  
  3         8  
  1         2  
  1         3  
  3         14  
  3         32  
  3         10  
  1         2  
  1         3  
  3         10  
  3         36  
  3         10  
  1         2  
  1         3  
  3         8  
  3         33  
  3         14  
  1         4  
  1         4  
  3         9  
  3         150  
  3         9  
  1         3  
  1         7  
  3         36  
  3         35  
  3         9  
  1         2  
  1         3  
  3         12  
  1         2  
  1         4  
  0         0  
  0         0  
  1         5  
  3         33  
  3         9  
  1         2  
  1         3  
  3         8  
  3         30  
  3         20  
  1         2  
  1         3  
  3         10  
  3         31  
  3         8  
  1         3  
  1         11  
  3         9  
  5         36  
  5         12  
  2         3  
  2         5  
  5         15  
  3         31  
  3         9  
  1         2  
  1         2  
  3         9  
  3         31  
  3         8  
  1         2  
  1         3  
  3         10  
  1         3  
  1         4  
  0         0  
  0         0  
  1         12  
  1         2  
  1         4  
  0         0  
  0         0  
  1         6  
  1         2  
  1         9  
  0         0  
  0         0  
  1         5  
  1         2  
  1         5  
  0         0  
  0         0  
  1         4  
  3         63  
  3         9  
  1         3  
  1         3  
  3         10  
40              
41             if ($@) {
42             die $@;
43             }
44             }
45              
46             my @array_accessors = qw/source patch changelog provide require file buildrequire prefix/;
47              
48             foreach my $field (@array_accessors) {
49             my $sub = q {
50             sub RPM::Specfile::[[field]] {
51             my $self = shift;
52             $self->{__[[field]]__} ||= [ ];
53              
54             if (@_) {
55             my $index = shift;
56             if (@_) {
57             my $value = shift;
58             $self->{__[[field]]__}->[$index] = $value;
59             }
60             return $self->{__[[field]]__}->[$index];
61             }
62             else {
63             return @{$self->{__[[field]]__}};
64             }
65             }
66              
67             sub RPM::Specfile::push_[[field]] {
68             my $self = shift;
69             my $entry = shift;
70              
71             $self->{__[[field]]__} ||= [ ];
72             push @{$self->{__[[field]]__}}, $entry;
73             }
74              
75             sub RPM::Specfile::clear_[[field]] {
76             my $self = shift;
77             my $entry = shift;
78              
79             $self->{__[[field]]__} = [ ];
80             }
81              
82             };
83              
84             $sub =~ s/\[\[field\]\]/$field/g;
85 1 0 50 1 1 2 eval $sub;
  1 50 50 1 1 4  
  1 0 50 0 1 4  
  0 50 50 0 1 0  
  0 0 50 0 1 0  
  0 50 0 0 1 0  
  0 0 100 0 1 0  
  0 50 50 0 1 0  
  1 0 0 0 1 9  
  1 50 100 0 1 6  
  1 0 50 1 1 2  
  1 0 0 1 1 5  
  1 0 100 1 1 3  
  0 50 100 0 1 0  
  0 0 50 2 1 0  
  0 50 50 1 1 0  
  0     0 1 0  
  0     2 1 0  
  1     1 1 2  
  1     0 1 7  
  0     2 1 0  
  0     2 1 0  
  0     1 1 0  
  0     1 1 0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  1         2  
  1         21  
  1         4  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  1         2  
  1         5  
  1         3  
  1         4  
  1         4  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  1         2  
  1         6  
  1         2  
  1         7  
  1         3  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  1         2  
  1         6  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  2         51  
  2         3  
  2         15  
  2         2  
  2         6  
  1         26  
  1         2  
  1         7  
  1         2  
  1         3  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  2         47  
  2         3  
  2         10  
  2         2  
  2         7  
  1         25  
  1         3  
  1         7  
  1         2  
  1         3  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  2         51  
  2         3  
  2         9  
  2         2  
  2         26  
  2         53  
  2         4  
  2         11  
  2         3  
  2         16  
  1         3  
  1         5  
  1         10  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  1         2  
  1         7  
  1         2  
  1         11  
  1         4  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  1         3  
  1         6  
86              
87             if ($@) {
88             die $@;
89             }
90             }
91              
92              
93             sub add_changelog_entry {
94 0     0 1 0 my $self = shift;
95 0         0 my $who = shift;
96 0         0 my $entry = shift;
97 0         0 my $version = shift;
98              
99 0         0 POSIX::setlocale( &POSIX::LC_ALL, "C" );
100              
101 0         0 my $output;
102 0         0 $output .= strftime("* %a %b %d %Y $who", localtime time);
103 0 0       0 $output .= " - $version" if $version;
104 0         0 $output .= "\n- $entry\n";
105              
106 0         0 $self->push_changelog($output);
107             }
108              
109             sub generate_specfile {
110 1     1 1 7 my $self = shift;
111              
112 1         2 my $output;
113              
114 1         10 my %defaults =
115             ( buildroot => "%{_tmppath}/%{name}-%{version}-%{release}-root" );
116 1   33     37 $self->$_($self->$_() || $defaults{$_}) foreach keys %defaults;
117              
118 1         9 my %proper_names = ( url => 'URL',
119             buildroot => 'BuildRoot',
120             buildrequires => 'BuildRequires',
121             buildarch => 'BuildArch',
122             );
123              
124             #
125             # Add any macro definitions to the begining.
126 1 50       62 $output .= $self->macros() . "\n" if defined $self->macros();
127              
128 1         4 foreach my $tag (qw/summary name version release epoch packager vendor distribution license group url buildroot buildarch/) {
129 13   66     53 my $proper = $proper_names{$tag} || ucfirst $tag;
130              
131 13 100       319 next unless defined $self->$tag();
132 6         145 $output .= "$proper: " . $self->$tag() . "\n";
133             }
134              
135             my $req_format = sub {
136 4     4   7 my $req = shift;
137 4         5 my $ver = shift;
138              
139 4 100       9 if (ref $req) {
140 2         6 ($req, $ver) = @$req;
141             }
142              
143 4 100 66     19 if (defined $ver and $ver != 0) {
144 2         21 return "$req >= $ver";
145             }
146             else {
147 2         9 return "$req";
148             }
149 1         20 };
150              
151 1         2 foreach my $tag (qw/requires buildrequires/) {
152 2   66     10 my $proper = $proper_names{$tag} || ucfirst $tag;
153              
154 2 50       55 next unless defined $self->$tag();
155              
156 0         0 $output .= "$proper: " . $req_format->($self->$tag) . "\n";
157             }
158              
159 1         29 my @reqs = $self->buildrequire;
160 1         5 for my $i (0 .. $#reqs) {
161 2         6 $output .= "BuildRequires: " . $req_format->($reqs[$i]) . "\n";
162             }
163              
164 1         31 @reqs = $self->require;
165 1         40 for my $i (0 .. $#reqs) {
166 2         8 $output .= "Requires: " . $req_format->($reqs[$i]) . "\n";
167             }
168              
169 1         32 my @sources = $self->source;
170 1         4 for my $i (0 .. $#sources) {
171 2         8 $output .= "Source$i: $sources[$i]\n";
172             }
173              
174 1         30 my @patches = $self->patch;
175 1         3 for my $i (0 .. $#patches) {
176 2         8 $output .= "Patch$i: $patches[$i]\n";
177             }
178              
179             #
180             # Add any prefixes:
181 1         29 my @prefixes = $self->prefix;
182 1         4 for my $i (0 .. $#prefixes) {
183 1         5 $output .= "Prefix: $prefixes[$i]\n";
184             }
185 1         3 $output .= "\n";
186              
187             #
188             # Add patch entries to the %prep section if they exist:
189 1         27 my $prep = $self->prep();
190 1         3 for my $i (0 .. $#patches) {
191 2 100       7 $prep .= "\n" if($i == 0); # Just in case they did not add a newline
192 2         6 $prep .= "%patch${i} -p1\n";
193             }
194 1 50       29 $self->prep($prep) if(defined($prep));
195              
196 1 50       27 if ($self->check) {
197 0         0 my $build = $self->build;
198              
199 0         0 my ($cond, $body) = (undef, $self->check);
200 0 0       0 if (ref $body) {
201 0         0 $cond = $body->[0];
202 0         0 $body = $body->[1];
203             }
204              
205 0         0 $build .= "\n%check";
206 0 0       0 if ($cond) {
207 0         0 $build .= " $cond";
208             }
209 0         0 $build .= "\n$body\n";
210              
211 0         0 $self->build($build);
212             }
213              
214 1         3 foreach my $sect (qw/description prep build install clean pre post preun postun/) {
215 9 100       228 next if(!defined($self->$sect()));
216 8         14 $output .= "%$sect\n";
217 8         198 my $content = $self->$sect();
218             # remove leading and trailing whitespace and spurious linefeeds
219 8         31 $content =~ s/^\s*\n*//s;
220 8         121 $content =~ s/[\s\n]*$/\n\n/s;
221 8         27 $output .= $content;
222             }
223              
224 1 50       27 if ($self->file_param) {
225 1         33 $output .= "%files " . $self->file_param . "\n";
226             }
227             else {
228 0         0 $output .= "%files\n";
229             }
230 1         29 $output .= "$_\n" foreach $self->file;
231              
232 1         3 $output .= "\n%changelog\n";
233 1         28 $output .= "$_\n" foreach $self->changelog;
234              
235 1         245 return $output;
236             }
237              
238             sub write_specfile {
239 0     0 1   my $self = shift;
240 0           my $dest = shift;
241              
242 0 0         open FH, ">$dest"
243             or die "Can't open $dest: $!";
244              
245 0           print FH $self->generate_specfile;
246              
247 0           close FH;
248             }
249              
250             1;
251              
252             __END__