blib/lib/Youri/Package/RPM/Generator.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 87 CC 6 SCAR 21.7
CC
6
Cov
0%
get_binaries
20.0
get_source
12.0
new
2.0
CRAP 8.8
1 # $Id: Generator.pm 2386 2013-01-03 20:41:58Z guillomovitch $
2 package Youri::Package::RPM::Generator;
3  
4 =head1 NAME
5  
6 Youri::Package::RPM::Generator - Template-based rpm generator
7  
8 =head1 DESCRIPTION
9  
10 This is a rpm package generator, intended to quickly generate real rpm packages
11 for testing purposes.
12  
13 =cut
14  
15 use strict;
16 use warnings;
17 use version; our $VERSION = qv('0.1.3');
18  
19 use Carp;
20 use English qw/-no_match_vars/;
21 use File::Temp qw/tempdir/;
22 use Text::Template;
23  
24 my %defaults = (
25 name => 'test',
26 version => 1,
27 release => 1,
28 arch => 'noarch',
29 summary => 'test package',
30 license => 'free',
31 group => 'testing'
32 );
33  
34 my $template = Text::Template->new(TYPE => 'STRING', SOURCE => <<'EOF');
35 Name: [% $name %]
36 Version: [% $version %]
37 Release: [% $release %]
38 Summary: [% $summary %]
39 License: [% $license %]
40 Group: [% $group %]
41 [% $url ? "Url: $url" : '' %]
42 [% $buildarch ? "BuildArch: $buildarch" : '' %]
43 BuildRoot: %{_tmppath}/%{name}-%{version}
44  
45 %description
46 [% $description %]
47  
48 %prep
49 rm -rf %{buildroot}
50 %setup -T -c
51  
52 %build
53  
54 %install
55 install -d -m 755 %{buildroot}
56  
57 %clean
58 rm -rf %{buildroot}
59  
60 %files
61 %defattr(-,root,root)
62  
63 %changelog
64 [% $changelog %]
65 EOF
66  
67 =head1 CLASS METHODS
68  
69 =head2 new(%options)
70  
71 Creates and returns a new Youri::Package::Generator object.
72  
73 Available parameters:
74  
75 =over
76  
77 =item tags $tags
78  
79 Hashref of tags for created packages.
80  
81 =back
82  
83 Default tags values:
84  
85 =over
86  
87 =item name test
88  
89 =item version 1
90  
91 =item release 1
92  
93 =item arch noarch
94  
95 =item summary test package
96  
97 =item license free
98  
99 =item group testing
100  
101 =back
102  
103 =cut
104  
105 sub new {
106 my ($class, %options) = @_;
107  
108 foreach my $tag (qw/name version release summary group license/) {
109 $options{tags}->{$tag} ||= $defaults{$tag};
110 }
111  
112 my $topdir = tempdir(CLEANUP => 0);
113 mkdir "$topdir/$_" foreach qw/SPECS BUILD RPMS SRPMS SOURCES tmp/;
114  
115 my $spec = "$topdir/SPECS/$options{tags}->{name}.spec";
116 open my $fh, '>', $spec or die "Can't open $spec: $!";
117  
118 $template->fill_in(
119 DELIMITERS => [ '[% ', ' %]' ],
120 HASH => $options{tags},
121 OUTPUT => $fh
122 );
123 close $fh;
124  
125 my $self = bless {
126 _topdir => $topdir,
127 _tags => $options{tags},
128 }, $class;
129  
130 return $self;
131 }
132  
133 =head1 INSTANCE METHODS
134  
135 =head2 get_source
136  
137 Generate the source package, and return the corresponding file.
138  
139 =cut
140  
141 sub get_source {
142 my ($self) = @_;
143 croak "Not a class method" unless ref $self;
144  
145 my $command = sprintf(
146 'rpmbuild -bs --define "_topdir %s" %s/SPECS/%s.spec >/dev/null 2>&1',
147 $self->{_topdir},
148 $self->{_topdir},
149 $self->{_tags}->{name}
150 );
151 my $status = system $command;
152 die "Can't execute $command: $ERRNO" if $status;
153  
154 my $dir = $self->{_topdir} . '/SRPMS';
155 return <$dir/*.rpm>;
156 }
157  
158 =head2 get_binaries
159  
160 Generate the binary packages, and return the corresponding files.
161  
162 =cut
163  
164 sub get_binaries {
165 my ($self) = @_;
166 croak "Not a class method" unless ref $self;
167  
168 my $dir = $self->{_topdir} . '/RPMS/';
169 if ($self->{_tags}->{buildarch}) {
170 $dir .= $self->{_tags}->{buildarch};
171 } else {
172 my $target_cpu = `rpm --eval %_target_cpu`;
173 chomp $target_cpu;
174 $dir .= $target_cpu;
175 }
176  
177 mkdir $dir;
178  
179 my $command = sprintf(
180 'rpmbuild -bb --define "_topdir %s" %s/SPECS/%s.spec >/dev/null 2>&1',
181 $self->{_topdir},
182 $self->{_topdir},
183 $self->{_tags}->{name}
184 );
185 my $status = system $command;
186 die "Can't execute $command: $ERRNO" if $status;
187  
188 return <$dir/*.rpm>;
189 }
190  
191 1;