| 1 | package Config::Maker::Metaconfig; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 9 | ▶ | use File::Temp qw(tempfile); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 9 | ▶ | use Config::Maker; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 9 | ▶ | use Config::Maker::Type; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 9 | ▶ | use Config::Maker::Config; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 9 | ▶ | use Config::Maker::Driver; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 117 | ▶ | sub type { | ||||||||||||||||||||||||||
|
Subroutine
type: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 18 | 117 | Config::Maker::Type->new(@_); | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # Top-level element "search-path" | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 9 | my $search = type( | |||||||||||||||||||||||||||
| 24 | name => 'search-path', | ||||||||||||||||||||||||||||
| 25 | format => [simple => [zero_list => 'string']], | ||||||||||||||||||||||||||||
| 26 | contexts => [opt => '/'], | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # Top-level element "output-dir" | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 9 | my $output = type( | |||||||||||||||||||||||||||
| 32 | name => 'output-dir', | ||||||||||||||||||||||||||||
| 33 | format => [simple => ['string']], | ||||||||||||||||||||||||||||
| 34 | contexts => [opt => '/'], | ||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # Top-level element "cache-dir" | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 9 | my $cached = type( | |||||||||||||||||||||||||||
| 40 | name => 'cache-dir', | ||||||||||||||||||||||||||||
| 41 | format => [simple => ['string']], | ||||||||||||||||||||||||||||
| 42 | contexts => [opt => '/'], | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # Top-level element "config" | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 9 | my $config = type( | |||||||||||||||||||||||||||
| 48 | name => 'config', | ||||||||||||||||||||||||||||
| 49 | format => ['named_group' => ['string']], | ||||||||||||||||||||||||||||
| 50 | contexts => [any => '/'], | ||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # The template element | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 9 | my $template = type( | |||||||||||||||||||||||||||
| 56 | name => 'template', | ||||||||||||||||||||||||||||
| 57 | format => ['anon_group'], | ||||||||||||||||||||||||||||
| 58 | contexts => [any => $config], | ||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 9 | my $src = type( | |||||||||||||||||||||||||||
| 62 | name => 'src', | ||||||||||||||||||||||||||||
| 63 | format => [simple => ['string']], | ||||||||||||||||||||||||||||
| 64 | contexts => [one => $template], | ||||||||||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 9 | my $out = type( | |||||||||||||||||||||||||||
| 68 | name => 'out', | ||||||||||||||||||||||||||||
| 69 | format => [simple => ['string']], | ||||||||||||||||||||||||||||
| 70 | contexts => [opt => $template], | ||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 9 | my $command = type( | |||||||||||||||||||||||||||
| 74 | name => 'command', | ||||||||||||||||||||||||||||
| 75 | format => [simple => ['string']], | ||||||||||||||||||||||||||||
| 76 | contexts => [opt => $template], | ||||||||||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||||||||||
| 78 | 9 | $template->addchecks(mand => 'out|command'); | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 9 | my $cache = type( | |||||||||||||||||||||||||||
| 81 | name => 'cache', | ||||||||||||||||||||||||||||
| 82 | format => [simple => ['string']], | ||||||||||||||||||||||||||||
| 83 | contexts => [opt => $template], | ||||||||||||||||||||||||||||
| 84 | ); | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 9 | my $enc = type( | |||||||||||||||||||||||||||
| 87 | name => 'enc', | ||||||||||||||||||||||||||||
| 88 | format => [simple => ['string']], | ||||||||||||||||||||||||||||
| 89 | contexts => [opt => $template], | ||||||||||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Metatypes... | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 27 | ▶ | sub metatype { | ||||||||||||||||||||||||||
|
Subroutine
metatype: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 95 | 27 | my ($name) = @_; | |||||||||||||||||||||||||||
| 96 | 27 | type( | |||||||||||||||||||||||||||
| 97 | name => $name, | ||||||||||||||||||||||||||||
| 98 | format => [simple => ['string']], | ||||||||||||||||||||||||||||
| 99 | contexts => [opt => '//'], | ||||||||||||||||||||||||||||
| 100 | ); | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 9 | metatype('meta'); | |||||||||||||||||||||||||||
| 104 | 9 | metatype('template'); | |||||||||||||||||||||||||||
| 105 | 9 | metatype('output'); | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # And now the real code... | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 89 | ▶ | sub _qual($$) { | ||||||||||||||||||||||||||
|
Subroutine
_qual: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 110 | 89 | my ($file, $dir) = @_; | |||||||||||||||||||||||||||
| 111 | 89 | ▶ | return unless $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 86 | ▶ | if(File::Spec->file_name_is_absolute($file)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | return $file; | |||||||||||||||||||||||||||
| 114 | } else { | ||||||||||||||||||||||||||||
| 115 | 86 | return File::Spec->rel2abs($file, $dir); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 45 | ▶ | sub _get_cfg { | ||||||||||||||||||||||||||
|
Subroutine
_get_cfg: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 120 | 45 | Config::Maker::Config->new(@_); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 9 | our @unlink; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 45 | ▶ | sub do { | ||||||||||||||||||||||||||
|
Subroutine
do: called CC=24 SCAR=35.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 126 | 45 | my ($class, $metaname, $noinst, $force) = @_; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 45 | my $meta = Config::Maker::Config->new($metaname)->{root}; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 45 | local @Config::Maker::path = @{$meta->getval('search-path', ['/etc/'])}; | |||||||||||||||||||||||||||
| 131 | 45 | { local $"=', '; DBG "Search path: @Config::Maker::path"; } | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 45 | my $outdir = $meta->getval('output-dir', '/etc/'); | |||||||||||||||||||||||||||
| 134 | 45 | DBG "Output-dir: $outdir"; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 45 | my $cachedir = $meta->getval('cache-dir', '/var/cache/configit/'); | |||||||||||||||||||||||||||
| 137 | 45 | DBG "Cache-dir: $cachedir"; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # For each config file and each template... | ||||||||||||||||||||||||||||
| 140 | 45 | for my $cfg ($meta->get('config')) { | |||||||||||||||||||||||||||
| 141 | 45 | LOG "Processing config $cfg"; | |||||||||||||||||||||||||||
| 142 | 45 | my $conf = _get_cfg($cfg->{-value}); | |||||||||||||||||||||||||||
| 143 | 45 | for my $tmpl ($cfg->get('template')) { | |||||||||||||||||||||||||||
| 144 | 45 | my ($fh, $name, $cache, $output); | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # Find output name... | ||||||||||||||||||||||||||||
| 147 | 45 | $output = $tmpl->get('out'); | |||||||||||||||||||||||||||
| 148 | 45 | ▶ | if($output) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 42 | $output = _qual($output, $outdir); | |||||||||||||||||||||||||||
| 150 | 42 | ($fh, $name) = tempfile( | |||||||||||||||||||||||||||
| 151 | basename($output, qr/\..*/) . ".cmXXXXXX", | ||||||||||||||||||||||||||||
| 152 | DIR => dirname($output)); | ||||||||||||||||||||||||||||
| 153 | } else { | ||||||||||||||||||||||||||||
| 154 | 3 | $output = ''; | |||||||||||||||||||||||||||
| 155 | 3 | ($fh, $name) = tempfile( | |||||||||||||||||||||||||||
| 156 | basename($tmpl->get1('src'), qr/\..*/) . ".cmXXXXXXXX", | ||||||||||||||||||||||||||||
| 157 | DIR => File::Spec->tmpdir); | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | 45 | DBG "Using $name as temporary for $tmpl output"; | |||||||||||||||||||||||||||
| 160 | 45 | push @unlink, $name; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # Find cache name... | ||||||||||||||||||||||||||||
| 163 | 45 | $cache = $tmpl->get('cache'); | |||||||||||||||||||||||||||
| 164 | 45 | ▶ | if($cache) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 8 | $cache = _qual($cache, $cachedir); | |||||||||||||||||||||||||||
| 166 | 8 | $fh = Config::Maker::Tee->new($fh, $cache); | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | # Set up the magical elements for the config... | ||||||||||||||||||||||||||||
| 170 | 45 | $conf->set_meta(meta => $meta); | |||||||||||||||||||||||||||
| 171 | 45 | $conf->set_meta(template => $tmpl); | |||||||||||||||||||||||||||
| 172 | 45 | $conf->set_meta(output => $output); | |||||||||||||||||||||||||||
| 173 | # Process the thing... | ||||||||||||||||||||||||||||
| 174 | 45 | Config::Maker::Driver->process( | |||||||||||||||||||||||||||
| 175 | $tmpl->get1('src'), | ||||||||||||||||||||||||||||
| 176 | $conf, $fh, $tmpl->get('enc'), | ||||||||||||||||||||||||||||
| 177 | ); | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 43 | $tmpl->{-data} = [$fh, $name, $cache]; | |||||||||||||||||||||||||||
| 180 | 43 | close $fh; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | # Now, for each template install the temporary file... | ||||||||||||||||||||||||||||
| 185 | 43 | ▶ | if($noinst) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 0 | for my $tmpl ($meta->get('config/template')) { | |||||||||||||||||||||||||||
| 187 | 0 | my ($fh, $name, $cache) = @{$tmpl->{-data}}; | |||||||||||||||||||||||||||
| 188 | 0 | ▶ | if($cache && $fh->cmpcache) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$cache and $fh->cmpcache
MC/DC0%0/2
$cache and $fh->cmpcache
$cache
$fh->cmpcache
Truth table0%0/3
A and B
A= $cacheB= $fh->cmpcache
| |||||||||||||||||||||||||||||
| 189 | 0 | LOG "Output of ".$tmpl->get('src')." unchanged"; | |||||||||||||||||||||||||||
| 190 | 0 | ▶ | next unless $force; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | 0 | @unlink = grep { $_ ne $name } @unlink; | |||||||||||||||||||||||||||
| 193 | 0 | my $dest; | |||||||||||||||||||||||||||
| 194 | 0 | ▶ | if($dest = _qual($tmpl->get('out'), $outdir)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 0 | print STDOUT "Install: $name $dest\n"; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | 0 | ▶ | if($dest = $tmpl->get('command')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 0 | print STDOUT "Invoke: $dest < $name\n"; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | } else { | ||||||||||||||||||||||||||||
| 202 | 43 | for my $tmpl ($meta->get('config/template')) { | |||||||||||||||||||||||||||
| 203 | 43 | my ($fh, $name, $cache) = @{$tmpl->{-data}}; | |||||||||||||||||||||||||||
| 204 | 43 | ▶ | if($cache && $fh->cmpcache) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$cache and $fh->cmpcache
MC/DC100%2/2
$cache and $fh->cmpcache
$cache
$fh->cmpcache
Truth table100%3/3
A and B
A= $cacheB= $fh->cmpcache
| |||||||||||||||||||||||||||||
| 205 | 5 | LOG "Output of ".$tmpl->get('src')." unchanged"; | |||||||||||||||||||||||||||
| 206 | 5 | ▶ | next unless $force; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | 39 | my $dest; | |||||||||||||||||||||||||||
| 209 | 39 | ▶ | if($dest = _qual($tmpl->get('out'), $outdir)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 36 | LOG "Installing $dest"; | |||||||||||||||||||||||||||
| 211 | 36 | ▶ | rename $name, $dest | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | or croak "Failed to install $dest: $!"; | ||||||||||||||||||||||||||||
| 213 | 36 | @unlink = grep { $_ ne $name } @unlink; | |||||||||||||||||||||||||||
| 214 | 36 | $name = $dest; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | 39 | ▶ | if($dest = $tmpl->get('command')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | 5 | LOG "Invoking $dest"; | |||||||||||||||||||||||||||
| 218 | 5 | my $pid = fork; | |||||||||||||||||||||||||||
| 219 | 5 | ▶ | croak "Failed to fork: $!" unless defined $pid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 5 | ▶ | unless($pid) { # The child... | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | 2 | open STDIN, '<', $name; | |||||||||||||||||||||||||||
| 222 | 2 | exec $dest; | |||||||||||||||||||||||||||
| 223 | 0 | die "Failed to exec $dest: $!"; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | # The parent... | ||||||||||||||||||||||||||||
| 226 | 3 | ▶ | waitpid($pid, 0) != -1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | or croak "Wait failed: $!"; | ||||||||||||||||||||||||||||
| 228 | 3 | ▶ | croak "Command failed: $?" if "$?"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | 37 | ▶ | if($cache) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | 4 | $fh->savecache; | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | # should be done...(!) | ||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 7 | ▶ | END { | ||||||||||||||||||||||||||
|
Subroutine
END: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||
| 239 | 7 | foreach(@unlink) { | |||||||||||||||||||||||||||
| 240 | 7 | ▶ | unlink $_ or warn "Unlinking `$_' failed: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 9 | 1; | |||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | __END__ | ||||||||||||||||||||||||||||