lib/Rex/Apache/Inject/Template.pm

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