lib/Rex/Apache/Inject/Properties.pm

filter: Statementstmt 21.5% 36 / 167 Branchbran 0.0% 0 / 78 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 52.3% 11 / 21 Podpod 0.0% 0 / 4 totaltotal 17.4% 47 / 270 CC 46 SCAR 44.3
CC
46
Cov
15%
_find_and_parse_templates
210.0
inject
132.0
_get_template_params
56.0
CRAP 84.3
« lib/Rex/Apache/Build/Base.pm ↑ Summary lib/Rex/Apache/Inject/Template.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4 # vim: set ts=2 sw=2 tw=0:
5 # vim: set expandtab:
6  
7 package Rex::Apache::Inject::Properties;
8  
9 =begin
10  
11 =head2 SYNOPSIS
12  
13 This is a (R)?ex module to ease the deployments of PHP, Perl or other languages.
14  
15 =cut
16  
17 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use Rex::Commands::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Rex::Commands::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Rex::Commands::Upload;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Rex::Commands;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Rex::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use File::Basename qw(dirname basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Cwd qw(getcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 #require Exporter;
29 #use base qw(Exporter);
30  
31 1 ▶use vars qw(@EXPORT $real_name_from_template $template_file $template_pattern);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 @EXPORT = qw(inject
33 generate_real_name
34 template_file template_search_for);
35  
36 1 my $work_dir = getcwd;
37  
38 ############ deploy functions ################
39  
40 0 ▶sub inject {
Subroutine inject: not called CC=11 SCAR=48.8
Pod undocumented
41 0 my ( $to, @options ) = @_;
42  
43 0 my $option = {@options};
44  
45 0 my $tmp_to = $to;
46 0 ▶ if ( $tmp_to !~ m/^\// ) { $tmp_to = "../$tmp_to"; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($tmp_to =~ m[^/]) 0 0 0
47  
48 0 my $cmd1 = sprintf( _get_extract_command($to), "$tmp_to" );
49 0 my $cmd2 = sprintf( _get_pack_command($to), "$tmp_to", "." );
50  
51 0 my $template_params = _get_template_params($template_file);
52  
53 0 mkdir("tmp");
54 0 chdir("tmp");
55 0 run $cmd1;
56  
57 0 ▶ if ( exists $option->{"extract"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"extract"}) 0 0 0
58 0 for my $file_pattern ( @{ $option->{"extract"} } ) {
59  
60 0 my $find = "find ../ -name '$file_pattern'";
61 0 ▶ if ( $^O =~ m/^MSWin/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /^MSWin/i) 0 0 0
62 0 $find = "find2 ../ -name \"$file_pattern\"";
63 }
64  
65 0 for my $found_file (`$find`) {
66 0 chomp $found_file;
67  
68 0 mkdir "tmp-b";
69 0 chdir "tmp-b";
70  
71 0 my $extract_cmd =
72 sprintf( _get_extract_command($found_file), "../$found_file" );
73 0 my $compress_cmd =
74 sprintf( _get_pack_command($found_file), "../$found_file", "." );
75  
76 0 run $extract_cmd;
77  
78 0 _find_and_parse_templates();
79  
80 0 ▶ if ( exists $option->{"pre_pack_hook"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"pre_pack_hook"}) 0 0 0
81 0 &{ $option->{"pre_pack_hook"} }($found_file);
82 }
83  
84 0 run $compress_cmd;
85  
86 0 ▶ if ( exists $option->{"post_pack_hook"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"post_pack_hook"}) 0 0 0
87 0 &{ $option->{"post_pack_hook"} }($found_file);
88 }
89  
90 0 chdir "..";
91 0 rmdir "tmp-b";
92 }
93  
94 }
95 }
96  
97 0 _find_and_parse_templates();
98  
99 0 ▶ if ( exists $option->{"pre_pack_hook"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"pre_pack_hook"}) 0 0 0
100 0 &{ $option->{"pre_pack_hook"} };
101 }
102  
103 0 run $cmd2;
104 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
105 0 chdir("..");
106 0 system("rm -rf tmp");
107 0 die("Can't re-pack archive. Please check permissions. Command was: $cmd2");
108 }
109  
110 0 ▶ if ( exists $option->{"post_pack_hook"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"post_pack_hook"}) 0 0 0
111 0 &{ $option->{"post_pack_hook"} };
112 }
113  
114 0 chdir("..");
115 0 system("rm -rf tmp");
116 }
117  
118 0 ▶sub _find_and_parse_templates {
Subroutine _find_and_parse_templates: not called CC=14 SCAR=53.5
119  
120 0 my $template_params = _get_template_params($template_file);
121  
122 0 my $find = "find . -name '$template_pattern'";
123 0 ▶ if ( $^O =~ m/^MSWin/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /^MSWin/i) 0 0 0
124 0 $find = "find2 . -name \"$template_pattern\"";
125 }
126  
127 0 for my $file (`$find`) {
128 0 $/ = "\n";
129 0 chomp $file;
130 0 Rex::Logger::debug("Opening file $file");
131  
132 0 ▶ open( my $fh, "<$file" ) or die($!);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<$file" or die $! 0 0 0
133 0 my %con;
134 0 ▶ while ( my $line = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
135 0 chomp $line;
136 0 $line =~ s/\r//gs;
137 0 ▶ next if ( $line =~ /^#/ );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 0 0 0
138 0 ▶ next if ( $line =~ /^$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^$/ 0 0 0
139  
140 0 my ( $key, $val ) = $line =~ m/^(.*?)\s?[=:]\s?(.*)$/;
141 0 Rex::Logger::debug("key: -$key- => val: -$val-");
142  
143 0 ▶ unless ($key) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key) 0 0 0
144 0 Rex::Logger::info("Parser Error in $file:");
145 0 Rex::Logger::info("($.)===$line===");
146 0 next;
147 }
148 0 ▶ $val = "" unless $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $val 0 0 0
149 0 $con{$key} = $val;
150 }
151 0 close($fh);
152  
153 0 for my $k ( keys %$template_params ) {
154 0 Rex::Logger::debug( "(t): -$k- => -" . $template_params->{$k} . "-" );
155 0 ▶ if ( exists $con{$k} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $con{$k}) 0 0 0
156 Rex::Logger::info(
157 0 "setting $k to " . $template_params->{$k} . " ($file)" );
158 0 $con{$k} = $template_params->{$k};
159 }
160  
161 # else {
162 # Rex::Logger::info("$k doesn't exists in $file");
163 # }
164 }
165  
166 0 ▶ my $new_file_name =
Branch: 1 branch
BranchTrueFalseTotal
$Rex::Apache::Inject::Properties::real_name_from_template ? : 0 0 0
167 $real_name_from_template ? &$real_name_from_template($file) : $file;
168 0 Rex::Logger::debug("Writing file $new_file_name");
169 0 ▶ open( $fh, ">", $new_file_name ) or die($!);
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $new_file_name or die $! 0 0 0
170 0 for my $key ( keys %con ) {
171 0 print $fh "$key=" . $con{$key} . "\n";
172 }
173 0 close($fh);
174 }
175  
176 }
177  
178 ############ configuration functions #############
179  
180 0 ▶sub generate_real_name {
Subroutine generate_real_name: not called CC=1 SCAR=6.9
Pod undocumented
181 0 $real_name_from_template = shift;
182 }
183  
184 0 ▶sub template_file {
Subroutine template_file: not called CC=1 SCAR=6.9
Pod undocumented
185 0 $template_file = shift;
186 }
187  
188 0 ▶sub template_search_for {
Subroutine template_search_for: not called CC=1 SCAR=6.9
Pod undocumented
189 0 $template_pattern = shift;
190 }
191  
192 ############ helper functions #############
193  
194 0 ▶sub _get_extract_command {
Subroutine _get_extract_command: not called CC=6 SCAR=37.4
195 0 my ($file) = @_;
196  
197 0 ▶ if ( $file =~ m/\.tar\.gz$/ ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($file =~ /\.tar\.gz$/) { } 0 0 0
elsif ($file =~ /\.zip$/) { } 0 0 0
elsif ($file =~ /\.tar\.bz2$/) { } 0 0 0
elsif ($file =~ /\.war$/) { } 0 0 0
elsif ($file =~ /\.jar$/) { } 0 0 0
198 0 return "tar xzf %s";
199 }
200 elsif ( $file =~ m/\.zip$/ ) {
201 0 return "unzip %s";
202 }
203 elsif ( $file =~ m/\.tar\.bz2$/ ) {
204 0 return "tar xjf %s";
205 }
206 elsif ( $file =~ m/\.war$/ ) {
207 0 return "unzip %s";
208 }
209 elsif ( $file =~ m/\.jar$/ ) {
210 0 return "unzip %s";
211 }
212  
213 0 die("Unknown Archive Format.");
214 }
215  
216 0 ▶sub _get_pack_command {
Subroutine _get_pack_command: not called CC=6 SCAR=37.4
217 0 my ($file) = @_;
218  
219 0 ▶ if ( $file =~ m/\.tar\.gz$/ ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($file =~ /\.tar\.gz$/) { } 0 0 0
elsif ($file =~ /\.zip$/) { } 0 0 0
elsif ($file =~ /\.tar\.bz2$/) { } 0 0 0
elsif ($file =~ /\.war$/) { } 0 0 0
elsif ($file =~ /\.jar$/) { } 0 0 0
220 0 return "tar czf %s %s";
221 }
222 elsif ( $file =~ m/\.zip$/ ) {
223 0 return "zip -r %s %s";
224 }
225 elsif ( $file =~ m/\.tar\.bz2$/ ) {
226 0 return "tar cjf %s %s";
227 }
228 elsif ( $file =~ m/\.war$/ ) {
229 0 return "zip -r %s %s";
230 }
231 elsif ( $file =~ m/\.jar$/ ) {
232 0 return "zip -r %s %s";
233 }
234  
235 0 die("Unknown Archive Format.");
236 }
237  
238 # read the template file and return a hashref.
239 0 ▶sub _get_template_params {
Subroutine _get_template_params: not called CC=7 SCAR=40.3
240 0 my %inject;
241  
242 my $t_file;
243  
244 0 ▶ if ( $template_file =~ m/^\// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Rex::Apache::Inject::Properties::template_file =~ m[^/]) { } 0 0 0
245 0 $t_file = $template_file;
246 }
247 else {
248 0 $t_file = "$work_dir/$template_file";
249 }
250  
251 0 ▶ if ( -f "$t_file." . Rex::Config->get_environment ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f "$t_file." . "Rex::Config"->get_environment) 0 0 0
252 0 $t_file = "$t_file." . Rex::Config->get_environment;
253 }
254  
255 0 ▶ open( my $fh, "<", $t_file ) or die($!);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $t_file or die $! 0 0 0
256 0 ▶ while ( my $line = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
257 0 chomp $line;
258 0 $line =~ s/\r//gs;
259 0 ▶ next if ( $line =~ /^#/ );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 0 0 0
260 0 ▶ next if ( $line =~ /^$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^$/ 0 0 0
261 0 $line =~ s/\r//gs;
262  
263 0 my ( $key, $val ) = $line =~ m/^(.*?)\s?[=:\t]\s?(.*)$/;
264 0 $inject{$key} = $val;
265 }
266 0 close($fh);
267  
268 0 return \%inject;
269 }
270  
271 0 ▶sub _get_ext {
Subroutine _get_ext: not called CC=6 SCAR=37.4
272 0 my ($file) = @_;
273  
274 0 ▶ if ( $file =~ m/\.tar\.gz$/ ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($file =~ /\.tar\.gz$/) { } 0 0 0
elsif ($file =~ /\.zip$/) { } 0 0 0
elsif ($file =~ /\.tar\.bz2$/) { } 0 0 0
elsif ($file =~ /\.war$/) { } 0 0 0
elsif ($file =~ /\.jar$/) { } 0 0 0
275 0 return ".tar.gz";
276 }
277 elsif ( $file =~ m/\.zip$/ ) {
278 0 return ".zip";
279 }
280 elsif ( $file =~ m/\.tar\.bz2$/ ) {
281 0 return ".tar.bz2";
282 }
283 elsif ( $file =~ m/\.war$/ ) {
284 0 return ".war";
285 }
286 elsif ( $file =~ m/\.jar$/ ) {
287 0 return ".jar";
288 }
289  
290 0 die("Unknown Archive Format.");
291  
292 }
293  
294 ####### import function #######
295  
296 0 ▶sub import {
Subroutine import: not called CC=2 SCAR=10.1
297  
298 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
299 0 for my $func (@EXPORT) {
300 0 Rex::Logger::debug("Registering main::$func");
301 0 *{"$_[1]::$func"} = \&$func;
302 }
303  
304 }
305  
306 1 1;
« lib/Rex/Apache/Build/Base.pm ↑ Summary lib/Rex/Apache/Inject/Template.pm »