line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Csistck::Test::Template; |
2
|
|
|
|
|
|
|
|
3
|
17
|
|
|
17
|
|
284
|
use 5.010; |
|
17
|
|
|
|
|
51
|
|
4
|
17
|
|
|
17
|
|
83
|
use strict; |
|
17
|
|
|
|
|
45
|
|
|
17
|
|
|
|
|
382
|
|
5
|
17
|
|
|
17
|
|
74
|
use warnings; |
|
17
|
|
|
|
|
24
|
|
|
17
|
|
|
|
|
484
|
|
6
|
|
|
|
|
|
|
|
7
|
17
|
|
|
17
|
|
76
|
use base 'Csistck::Test::FileBase'; |
|
17
|
|
|
|
|
19
|
|
|
17
|
|
|
|
|
1289
|
|
8
|
17
|
|
|
17
|
|
86
|
use Csistck::Oper qw/debug/; |
|
17
|
|
|
|
|
25
|
|
|
17
|
|
|
|
|
849
|
|
9
|
17
|
|
|
17
|
|
98
|
use Csistck::Util qw/hash_file hash_string/; |
|
17
|
|
|
|
|
38
|
|
|
17
|
|
|
|
|
1181
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our @EXPORT_OK = qw/template/; |
12
|
|
|
|
|
|
|
|
13
|
17
|
|
|
17
|
|
105
|
use Template; |
|
17
|
|
|
|
|
35
|
|
|
17
|
|
|
|
|
516
|
|
14
|
17
|
|
|
17
|
|
75
|
use File::Copy; |
|
17
|
|
|
|
|
29
|
|
|
17
|
|
|
|
|
978
|
|
15
|
17
|
|
|
17
|
|
97
|
use Sys::Hostname::Long qw//; |
|
17
|
|
|
|
|
24
|
|
|
17
|
|
|
|
|
315
|
|
16
|
17
|
|
|
17
|
|
66
|
use FindBin; |
|
17
|
|
|
|
|
24
|
|
|
17
|
|
|
|
|
550
|
|
17
|
17
|
|
|
17
|
|
80
|
use Text::Diff (); |
|
17
|
|
|
|
|
26
|
|
|
17
|
|
|
|
|
8575
|
|
18
|
|
|
|
|
|
|
|
19
|
2
|
|
|
2
|
1
|
1225
|
sub template { Csistck::Test::Template->new(@_); }; |
20
|
|
|
|
|
|
|
|
21
|
8
|
|
|
8
|
0
|
19
|
sub desc { sprintf("Template check for destination %s", shift->dest); } |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub file_check { |
24
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
25
|
0
|
|
|
|
|
|
my $tplout; |
26
|
|
|
|
|
|
|
|
27
|
0
|
0
|
|
|
|
|
$self->template_file(\$tplout) |
28
|
0
|
|
|
|
|
|
or die("Template file not processed: template=<${\$self->src}>"); |
29
|
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
|
my $hashsrc = hash_string($tplout); |
31
|
0
|
|
|
|
|
|
my $hashdst = hash_file($self->dest); |
32
|
|
|
|
|
|
|
|
33
|
0
|
|
0
|
|
|
|
return (defined $hashsrc and defined $hashdst and ($hashsrc eq $hashdst)); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub file_repair { |
37
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
38
|
0
|
|
|
|
|
|
debug(sprintf("Output template: template=<%s> dest=<%s>", |
39
|
|
|
|
|
|
|
$self->src, $self->dest)); |
40
|
|
|
|
|
|
|
# TODO tmp file for template |
41
|
0
|
0
|
|
|
|
|
open(my $h, '>', $self->dest) |
42
|
|
|
|
|
|
|
or die("Permission denied writing template"); |
43
|
0
|
|
|
|
|
|
$self->template_file($h); |
44
|
0
|
|
|
|
|
|
close($h); |
45
|
0
|
|
|
|
|
|
return 1; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub file_diff { |
49
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
50
|
0
|
|
|
|
|
|
my $temp_h; |
51
|
|
|
|
|
|
|
# TODO prune args in common function |
52
|
0
|
|
|
|
|
|
$self->template_file(\$temp_h); |
53
|
0
|
|
|
|
|
|
say(Text::Diff::diff($self->dest, \$temp_h)); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# Processing absoulte template name and outputs to reference |
57
|
|
|
|
|
|
|
# variable $out. Die on error or unreadable template file |
58
|
|
|
|
|
|
|
sub template_file { |
59
|
0
|
|
|
0
|
0
|
|
my ($self, $out) = @_; |
60
|
|
|
|
|
|
|
|
61
|
0
|
0
|
|
|
|
|
die("Invalid template name") |
62
|
|
|
|
|
|
|
unless($self->src =~ /^[A-Za-z0-9\-\_][A-Za-z0-9\/\-\_\.]+$/); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# Build object, finish checks |
65
|
0
|
|
|
|
|
|
my $t = Template->new(); |
66
|
0
|
|
|
|
|
|
my $file = get_absolute_template($self->src); |
67
|
0
|
|
|
|
|
|
$self->{hostname} = Sys::Hostname::Long::hostname_long(); |
68
|
0
|
0
|
|
|
|
|
die("Template not found") |
69
|
|
|
|
|
|
|
if(! -e $file); |
70
|
0
|
0
|
|
|
|
|
die("Permission denied reading template") |
71
|
|
|
|
|
|
|
if(! -r $file); |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# Create Template object, no config for now |
74
|
0
|
|
|
|
|
|
open(my $h, $file); |
75
|
0
|
0
|
|
|
|
|
$t->process($h, $self, $out) or die $t->error(); |
76
|
0
|
|
|
|
|
|
close($h); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# Get absoulte path from relative path |
80
|
|
|
|
|
|
|
# TODO error checking |
81
|
|
|
|
|
|
|
sub get_absolute_template { |
82
|
0
|
|
|
0
|
0
|
|
my $template = shift; |
83
|
0
|
|
|
|
|
|
return join "/", $FindBin::Bin, $template; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
1; |
87
|
|
|
|
|
|
|
__END__ |