line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# Nicolas Leclercq |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# vim: set ts=2 sw=2 tw=0: |
5
|
|
|
|
|
|
|
# vim: set expandtab: |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
package Rex::Template::TT; |
8
|
|
|
|
|
|
|
$Rex::Template::TT::VERSION = '1.0.0'; |
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
34917
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
55
|
|
11
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
59
|
|
12
|
|
|
|
|
|
|
|
13
|
2
|
|
|
2
|
|
9
|
use Rex -base; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
16
|
|
14
|
2
|
|
|
2
|
|
358424
|
use Rex::Helper::Path; |
|
2
|
|
|
|
|
16
|
|
|
2
|
|
|
|
|
170
|
|
15
|
2
|
|
|
2
|
|
22
|
use Template; |
|
2
|
|
|
|
|
40414
|
|
|
2
|
|
|
|
|
32
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
require Exporter; |
18
|
2
|
|
|
2
|
|
81
|
use base qw(Exporter); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
138
|
|
19
|
2
|
|
|
2
|
|
12
|
use vars qw (@EXPORT); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1117
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
@EXPORT = qw(template_toolkit); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub validate_vars { |
25
|
8
|
|
|
8
|
0
|
101
|
my ( $vars ) = @_; |
26
|
|
|
|
|
|
|
|
27
|
8
|
|
|
|
|
172
|
foreach my $key (keys %$vars) { |
28
|
184
|
50
|
|
|
|
598
|
if ($key =~ /^[\._]/) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
29
|
0
|
|
|
|
|
0
|
Rex::Logger::info( "variable name '$key' considered private by Template Toolkit", |
30
|
|
|
|
|
|
|
"warn" ); |
31
|
|
|
|
|
|
|
} elsif ($key =~ /([^a-zA-Z0-9_])/) { |
32
|
0
|
|
|
|
|
0
|
Rex::Logger::info( "variable name '$key' contains '$1' unsupported by Template Toolkit", |
33
|
|
|
|
|
|
|
"warn" ); |
34
|
|
|
|
|
|
|
} elsif ($key =~ /^(GET|CALL|SET|DEFAULT|INSERT|INCLUDE|PROCESS|WRAPPER|IF|UNLESS|ELSE|ELSIF|FOR|FOREACH|WHILE|SWITCH|CASE|USE|PLUGIN|FILTER|MACRO|PERL|RAWPERL|BLOCK|META|TRY|THROW|CATCH|FINAL|NEXT|LAST|BREAK|RETURN|STOP|CLEAR|TO|STEP|AND|OR|NOT|MOD|DIV|END)$/) { |
35
|
0
|
|
|
|
|
0
|
Rex::Logger::info( "variable name '$key' clashes with reserved Template Toolkit word", |
36
|
|
|
|
|
|
|
"warn" ); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub template_toolkit { |
42
|
4
|
|
|
4
|
0
|
1149017
|
my ( $template_path, $vars ) = @_; |
43
|
|
|
|
|
|
|
|
44
|
4
|
|
|
|
|
46
|
validate_vars( $vars ); |
45
|
|
|
|
|
|
|
|
46
|
4
|
100
|
66
|
|
|
65
|
if ( not ( ref $template_path and ref $template_path eq 'SCALAR') ) { |
47
|
|
|
|
|
|
|
# resolv template path |
48
|
2
|
|
|
|
|
28
|
$template_path = Rex::Helper::Path::resolv_path($template_path); |
49
|
2
|
|
|
|
|
46
|
$template_path = Rex::Helper::Path::get_file_path( $template_path, caller() ); |
50
|
2
|
|
|
|
|
625
|
Rex::Logger::debug("Processing template file : $template_path"); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# process template |
54
|
4
|
|
|
|
|
36
|
my $output = ''; |
55
|
|
|
|
|
|
|
my $template = Template->new( { ABSOLUTE => 1 } ) |
56
|
4
|
50
|
|
|
|
139
|
or do { |
57
|
0
|
|
|
|
|
0
|
Rex::Logger::info( $Template::ERROR, 'error' ); |
58
|
0
|
|
|
|
|
0
|
die $Template::ERROR; |
59
|
|
|
|
|
|
|
}; |
60
|
|
|
|
|
|
|
$template->process( $template_path, $vars, \$output ) |
61
|
4
|
50
|
|
|
|
25122
|
or do { |
62
|
0
|
|
|
|
|
0
|
Rex::Logger::info( $template->error(), 'error' ); |
63
|
0
|
|
|
|
|
0
|
die $template->error(); |
64
|
|
|
|
|
|
|
}; |
65
|
|
|
|
|
|
|
|
66
|
4
|
|
|
|
|
28239
|
return $output; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub import { |
70
|
|
|
|
|
|
|
|
71
|
2
|
|
|
2
|
|
29
|
my ( $class, $tag ) = @_; |
72
|
|
|
|
|
|
|
|
73
|
2
|
100
|
66
|
|
|
23
|
if ( $tag && $tag eq ":register" ) { |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# register Template::Toolkit for default template processing |
76
|
|
|
|
|
|
|
set template_function => sub { |
77
|
4
|
|
|
4
|
|
1561809
|
my ( $content, $vars ) = @_; |
78
|
|
|
|
|
|
|
|
79
|
4
|
|
|
|
|
58
|
validate_vars( $vars ); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
my $template = Template->new() |
82
|
4
|
50
|
|
|
|
161
|
or do { |
83
|
0
|
|
|
|
|
0
|
Rex::Logger::info( $Template::ERROR, 'error' ); |
84
|
0
|
|
|
|
|
0
|
die $Template::ERROR; |
85
|
|
|
|
|
|
|
}; |
86
|
4
|
|
|
|
|
26234
|
my $output; |
87
|
|
|
|
|
|
|
$template->process( \$content, $vars, \$output ) |
88
|
4
|
50
|
|
|
|
33
|
or do { |
89
|
0
|
|
|
|
|
0
|
Rex::Logger::info( $template->error(), 'error' ); |
90
|
0
|
|
|
|
|
0
|
die $template->error(); |
91
|
|
|
|
|
|
|
}; |
92
|
|
|
|
|
|
|
|
93
|
4
|
|
|
|
|
34696
|
return $output; |
94
|
1
|
|
|
|
|
21
|
}; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
2
|
|
|
|
|
583
|
__PACKAGE__->export_to_level(1); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
1; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=pod |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 NAME |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Rex::Template::TT - A module to process templates with template toolkit. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 VERSION |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
version 1.0.0 |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 SYNOPSIS |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
use Rex::Template::TT; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
task "blah", sub { |
117
|
|
|
|
|
|
|
file "/tmp/blah", |
118
|
|
|
|
|
|
|
content => template("path/to/blah.template", |
119
|
|
|
|
|
|
|
{ persons => ['bob', 'alice'] }), |
120
|
|
|
|
|
|
|
owner => "root", |
121
|
|
|
|
|
|
|
group => "root", |
122
|
|
|
|
|
|
|
mode => 644 |
123
|
|
|
|
|
|
|
}; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# to use as a default template engine |
126
|
|
|
|
|
|
|
# this will make the template() function use TemplateTookit to render |
127
|
|
|
|
|
|
|
# all the templates. This will also register all the known template variables |
128
|
|
|
|
|
|
|
# like hostname, eth0_ip and so on. |
129
|
|
|
|
|
|
|
use Rex::Template::TT ':register'; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 DESCRIPTION |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
A Rex extension module to process templates with template toolkit. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 AUTHORS |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
This module in its current publication is written by Nicolas Leclerq, based |
139
|
|
|
|
|
|
|
on prior work by Jan Gehring. The original version by Nicolas was published |
140
|
|
|
|
|
|
|
through the now-defunct I. Since Nicolas's version ended up |
141
|
|
|
|
|
|
|
being more feature-rich than Jan's, Jan gave permission to overwrite his |
142
|
|
|
|
|
|
|
version with Nicolas's. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=over |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=item * Nicolas Leclercq |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=item * Jan Gehring |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=back |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 CONTRIBUTORS |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=over |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=item * Erik Huelsmann |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=back |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head1 SEE ALSO |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=over |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=item * L |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=item * L |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item * L |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=back |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 LICENSE |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
The Apache License, Version 2.0, January 2004 |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=cut |