lib/RPM/Packager.pm

filter: Statementstmt 89.0% 98 / 110 Branchbran 75.0% 9 / 12 Conditioncond 70.5% 12 / 17 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 90.0% 18 / 20 Podpod 10.0% 1 / 10 totaltotal 79.7% 142 / 178 CC 17 SCAR 9.5
CC
17
Cov
86%
create_rpm
6.0
populate_opts
4.1
should_gpgsign
3.0
CRAP 2.6
↑ Summary blib/script/rpm_packager.pl »
1 package RPM::Packager;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Expect;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use RPM::Packager::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 RPM::Packager - Manifest-based approach for building RPMs
15  
16 =head1 VERSION
17  
18 Version 0.3.5
19  
20 =cut
21  
22 1 our $VERSION = 'v0.3.5';
23  
24 =head1 SYNOPSIS
25  
26 Building RPMs should be easy.
27  
28 This is a manifest approach to easily create custom RPMs. Once this module is installed, building RPMs should be as
29 simple as writing a YAML file that looks like the following:
30  
31 ---
32 name: testpackage
33 version: grep Changelog # version string or some command to retrieve it
34 os: el6 # optional, don't specify anything if package is os-independent
35 dependencies:
36 - perl-YAML > 0.5
37 - perl-JSON
38 files:
39 bin: /usr/local/bin # directory-based mapping. RPM will install CWD/bin/* to /usr/local/bin.
40 user: apache # specify the owner of files. default: root
41 group: apache # specify the group owner of files. default: root
42 sign: # optionally, gpg signing of RPM
43 gpg_name: ED16CAB # provide the GPG key ID
44 passphrase_cmd: cat secret_file # command to retrieve the secret
45 after_install: path/to/script # shellscript to run after the package is installed (%post)
46 architecture: noarch # specify the architecture for this package (default: x86_64)
47  
48 Then run:
49  
50 rpm_packager.pl <path_to_manifest.yml>
51  
52 Note : You need to have fpm available in PATH. For GPG signing, you need to have proper keys imported.
53  
54 Note2: The 'release' field of RPM will be determined by the BUILD_NUMBER env variable plus 'os' field, like '150.el7'.
55 If BUILD_NUMBER is unavailable, 1 will be used. If os is unspecified, nothing will be appended.
56  
57 You may also interact with the library directly as long as you pass in the manifest information in a hash:
58  
59 use RPM::Packager;
60  
61 my %args = (
62 name => 'testpackage',
63 version => 'grep Changelog',
64 files => { bin => '/usr/local/bin' },
65 dependencies => [
66 'perl-YAML > 0.5',
67 'perl-JSON'
68 ],
69 os => 'el6',
70 user => 'apache',
71 group => 'apache',
72 sign => {
73 'gpg_name' => 'ED16CAB',
74 'passphrase_cmd' => 'cat secret_file'
75 },
76 after_install => 'foo/bar/baz.sh',
77 architecture => 'noarch'
78 );
79  
80 my $obj = RPM::Packager->new(%args);
81 $obj->create_rpm(); # RPM produced in CWD
82  
83 =head1 SUBROUTINES/METHODS
84  
85 =head2 new(%args)
86  
87 Constructor. Pass in a hash containing manifest info.
88  
89 =cut
90  
91 15 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
92 15 my ( $class, %args ) = @_;
93 15 chomp( my $fpm = `which fpm 2>/dev/null` );
94 15 chomp( my $cp = `which cp` );
95  
96 15 my $self = {
97 fpm => $fpm,
98 cp => $cp,
99 cwd => getcwd(),
100 tempdir => File::Temp->newdir(),
101 %args
102 };
103 15 return bless $self, $class;
104 }
105  
106 3 ▶sub find_version {
Subroutine find_version: called CC=2 SCAR=6.9
Pod undocumented
107 3 my $self = shift;
108 3 my $value = $self->{version};
109 3 ▶ ( RPM::Packager::Utils::is_command($value) ) ? RPM::Packager::Utils::eval_command($value) : $value;
Branch: 1 branch
BranchTrueFalseTotal
RPM::Packager::Utils::is_command($value) ? : 1 2 3
110 }
111  
112 3 ▶sub generate_dependency_opts {
Subroutine generate_dependency_opts: called CC=3 SCAR=11.0
Pod undocumented
113 3 my $self = shift;
114 3 ▶ my $dependencies = $self->{dependencies} || [];
Condition100%2/2
$self->{'dependencies'} || []
l!l
12
MC/DC100%1/1
$self->{'dependencies'} || []
$self->{'dependencies'}
Truth table100%2/2
A
A= $self->{'dependencies'}
Aresult
11
00
115 3 my @chunks;
116 3 for my $dependency ( @{$dependencies} ) {
117 2 push @chunks, "-d '$dependency'";
118 }
119 3 return join( " ", @chunks );
120 }
121  
122 3 ▶sub generate_user_group {
Subroutine generate_user_group: called CC=3 SCAR=11.0
Pod undocumented
123 3 my $self = shift;
124 3 ▶ my $user = $self->{user} || 'root';
Condition100%2/2
$self->{'user'} || 'root'
l!l
12
MC/DC100%1/1
$self->{'user'} || 'root'
$self->{'user'}
Truth table100%2/2
A
A= $self->{'user'}
Aresult
11
00
125 3 ▶ my $group = $self->{group} || 'root';
Condition100%2/2
$self->{'group'} || 'root'
l!l
12
MC/DC100%1/1
$self->{'group'} || 'root'
$self->{'group'}
Truth table100%2/2
A
A= $self->{'group'}
Aresult
11
00
126 3 return ( $user, $group );
127 }
128  
129 1 ▶sub copy_to_tempdir {
Subroutine copy_to_tempdir: called CC=2 SCAR=6.9
Pod undocumented
130 1 my $self = shift;
131  
132 1 my $cwd = $self->{cwd};
133 1 my %hash = %{ $self->{files} };
134 1 my $tempdir = $self->{tempdir};
135  
136 1 for my $key ( keys %hash ) {
137 1 my $dst = $hash{$key};
138 1 my $target_dir = "$tempdir$dst";
139 1 make_path($target_dir);
140 1 system("$self->{cp} -r $cwd/$key/* $target_dir");
141 }
142 1 return 1;
143 }
144  
145 3 ▶sub add_gpg_opts {
Subroutine add_gpg_opts: called CC=3 SCAR=11.0
Pod undocumented
146 3 my $self = shift;
147  
148 3 ▶ return unless ( $self->should_gpgsign() );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->should_gpgsign 1 2 3
149  
150 2 my $gpg_name = $self->{sign}->{gpg_name};
151 2 my $passphrase_cmd = $self->{sign}->{passphrase_cmd};
152 2 ▶ my $opts = $self->{opts} || [];
Condition50%1/2
$self->{'opts'} || []
l!l
02
MC/DC0%0/1
$self->{'opts'} || []
$self->{'opts'}
Truth table50%1/2
A
A= $self->{'opts'}
Aresult
11
00
153 2 push @{$opts}, '--rpm-sign', '--rpm-rpmbuild-define', "'_gpg_name $gpg_name'";
154 2 $self->{opts} = $opts;
155 2 $self->{gpg_passphrase} = RPM::Packager::Utils::eval_command($passphrase_cmd);
156 }
157  
158 2 ▶sub add_after_install {
Subroutine add_after_install: called CC=3 SCAR=11.0
Pod undocumented
159 2 my $self = shift;
160 2 ▶ return unless ( $self->{after_install} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"after_install"} 1 1 2
161  
162 1 ▶ my $opts = $self->{opts} || [];
Condition50%1/2
$self->{'opts'} || []
l!l
01
MC/DC0%0/1
$self->{'opts'} || []
$self->{'opts'}
Truth table50%1/2
A
A= $self->{'opts'}
Aresult
11
00
163 1 push @{$opts}, '--after-install', $self->{after_install};
164 1 $self->{opts} = $opts;
165 }
166  
167 1 ▶sub populate_opts {
Subroutine populate_opts: called CC=4 SCAR=14.0
Pod undocumented
168 1 my $self = shift;
169 1 my $version = $self->find_version();
170 1 ▶ my $release = $ENV{BUILD_NUMBER} || 1;
Condition50%1/2
$ENV{'BUILD_NUMBER'} || 1
l!l
01
MC/DC0%0/1
$ENV{'BUILD_NUMBER'} || 1
$ENV{'BUILD_NUMBER'}
Truth table50%1/2
A
A= $ENV{'BUILD_NUMBER'}
Aresult
11
00
171 1 my $os = $self->{os};
172 1 ▶ my $iteration = ($os) ? "$release.$os" : $release;
Branch: 1 branch
BranchTrueFalseTotal
$os ? : 1 0 1
173 1 my $dependency_opts = $self->generate_dependency_opts();
174 1 ▶ my $architecture = $self->{architecture} || 'x86_64';
Condition50%1/2
$self->{'architecture'} || 'x86_64'
l!l
01
MC/DC0%0/1
$self->{'architecture'} || 'x86_64'
$self->{'architecture'}
Truth table50%1/2
A
A= $self->{'architecture'}
Aresult
11
00
175 1 my ( $user, $group ) = $self->generate_user_group();
176  
177 my @opts = (
178 $self->{fpm}, '-v', $version, '--rpm-user', $user, '--rpm-group',
179 $group, '--iteration', $iteration, '-n', $self->{name}, $dependency_opts,
180 '-s', 'dir', '-t', 'rpm', '-a', $architecture,
181 '-C', $self->{tempdir}
182 1 );
183  
184 1 $self->{opts} = [@opts];
185 1 $self->add_gpg_opts();
186 1 $self->add_after_install();
187 1 push @{ $self->{opts} }, '.'; # relative to the temporary directory
188 }
189  
190 1 ▶sub handle_interactive_prompt {
Subroutine handle_interactive_prompt: called CC=1 SCAR=0.2
Pod undocumented
191 1 my $self = shift;
192 1 my $opts = $self->{opts};
193 1 my $cmd = join( ' ', @{$opts} );
194 1 my $pass = $self->{gpg_passphrase};
195  
196 1 my $exp = Expect->new();
197 1 $exp->spawn($cmd);
198 $exp->expect(
199 undef,
200 [
201 0 ▶ qr/Enter pass phrase:/i => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
202 0 my $exp = shift;
203 0 $exp->send("$pass\n");
204 0 exp_continue;
205 }
206 1 ]
207 );
208 1 return 1;
209 }
210  
211 5 ▶sub should_gpgsign {
Subroutine should_gpgsign: called CC=3 SCAR=11.0
Pod undocumented
212 5 my $self = shift;
213 5 my $gpg_name = $self->{sign}->{gpg_name};
214 5 my $passphrase_cmd = $self->{sign}->{passphrase_cmd};
215 5 ▶ ( $gpg_name && $passphrase_cmd ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$gpg_name && $passphrase_cmd ? : 3 2 5
Condition66%2/3
$gpg_name and $passphrase_cmd
!ll&&!rl&&r
203
MC/DC50%1/2
$gpg_name and $passphrase_cmd
$gpg_name $passphrase_cmd
Truth table66%2/3
A and B
A= $gpg_nameB= $passphrase_cmd
ABresult
0X0
100
111
216 }
217  
218 =head2 create_rpm
219  
220 Creates RPM based on the information in the object
221  
222 =cut
223  
224 0 ▶sub create_rpm {
Subroutine create_rpm: not called CC=2 SCAR=17.9
225 0 my $self = shift;
226  
227 0 $self->copy_to_tempdir();
228 0 $self->populate_opts();
229  
230 0 ▶ if ( $self->should_gpgsign() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->should_gpgsign) { } 0 0 0
231 0 $self->handle_interactive_prompt();
232 }
233 else {
234 0 my $cmd = join( ' ', @{ $self->{opts} } );
235 0 system($cmd);
236 }
237 0 return 1;
238 }
239  
240 =head1 AUTHOR
241  
242 Satoshi Yagi, C<< <satoshi.yagi at yahoo.com> >>
243  
244 =head1 BUGS
245  
246 Please report any bugs or feature requests to C<bug-rpm-packager at rt.cpan.org>, or through
247 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=RPM-Packager>. I will be notified, and then you'll
248 automatically be notified of progress on your bug as I make changes.
249  
250  
251  
252  
253 =head1 SUPPORT
254  
255 You can find documentation for this module with the perldoc command.
256  
257 perldoc RPM::Packager
258  
259  
260 You can also look for information at:
261  
262 =over 4
263  
264 =item * RT: CPAN's request tracker (report bugs here)
265  
266 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=RPM-Packager>
267  
268 =item * AnnoCPAN: Annotated CPAN documentation
269  
270 L<http://annocpan.org/dist/RPM-Packager>
271  
272 =item * CPAN Ratings
273  
274 L<http://cpanratings.perl.org/d/RPM-Packager>
275  
276 =item * Search CPAN
277  
278 L<http://search.cpan.org/dist/RPM-Packager/>
279  
280 =back
281  
282  
283 =head1 ACKNOWLEDGEMENTS
284  
285  
286 =head1 LICENSE AND COPYRIGHT
287  
288 Copyright 2016 Satoshi Yagi.
289  
290 This program is free software: you can redistribute it and/or modify
291 it under the terms of the GNU General Public License as published by
292 the Free Software Foundation, either version 3 of the License, or
293 (at your option) any later version.
294  
295 This program is distributed in the hope that it will be useful,
296 but WITHOUT ANY WARRANTY; without even the implied warranty of
297 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
298 GNU General Public License for more details.
299  
300 You should have received a copy of the GNU General Public License
301 along with this program. If not, see L<http://www.gnu.org/licenses/>.
302  
303  
304 =cut
305  
306 1 1; # End of RPM::Packager
↑ Summary blib/script/rpm_packager.pl »