| 1 | package YAML::LoadBundle; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Load a directory of YAML files as a bundle | ||||||||||||||||||||||||||||
| 3 | 5 | $YAML::LoadBundle::VERSION = '0.003'; | |||||||||||||||||||||||||||
| 4 | 5 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Cwd qw( abs_path ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Digest::SHA1 qw( sha1_hex sha1 ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use File::Find qw( find ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use Hash::Merge::Simple (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use Scalar::Util qw( reftype refaddr ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use Storable qw( freeze ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use YAML::XS qw(Load); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 5 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 18 | load_yaml | ||||||||||||||||||||||||||||
| 19 | load_yaml_bundle | ||||||||||||||||||||||||||||
| 20 | add_yaml_observer | ||||||||||||||||||||||||||||
| 21 | remove_yaml_observer | ||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | 5 | our %EXPORT_TAGS = ( all => [@EXPORT_OK] ); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 5 | our $CacheDir; | |||||||||||||||||||||||||||
| 26 | 5 | ▶ | $CacheDir = $ENV{YAML_LOADBUNDLE_CACHEDIR} unless defined $CacheDir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 5 | my @load_yaml_observers; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub add_yaml_observer { | ||||||||||||||||||||||||||||
| 31 | 1 | ▶ | my $observer = shift; | ||||||||||||||||||||||||||
|
Subroutine
add_yaml_observer: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | die "Observer must be a code ref." unless ref($observer) eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 1 | push @load_yaml_observers, $observer; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | sub _notify_yaml_observers { | ||||||||||||||||||||||||||||
| 37 | 5 | ▶ | my $file = shift; | ||||||||||||||||||||||||||
|
Subroutine
_notify_yaml_observers: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 38 | 5 | for my $observer (@load_yaml_observers) { | |||||||||||||||||||||||||||
| 39 | 1 | $observer->($file); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | sub remove_yaml_observer { | ||||||||||||||||||||||||||||
| 44 | 1 | ▶ | my $observer = shift; | ||||||||||||||||||||||||||
|
Subroutine
remove_yaml_observer: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 1 | ▶ | die "Observer must be a code ref." unless ref($observer) eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 1 | my $obref = refaddr $observer; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | @load_yaml_observers = grep { | ||||||||||||||||||||||||||||
| 49 | 1 | refaddr($_) != $obref | |||||||||||||||||||||||||||
| 50 | } @load_yaml_observers; | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | sub load_yaml { | ||||||||||||||||||||||||||||
| 54 | 27 | ▶ | my ($arg, $dont_cache) = @_; | ||||||||||||||||||||||||||
|
Subroutine
load_yaml: called CC=11 SCAR=24.2
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 27 | my @yaml; | |||||||||||||||||||||||||||
| 56 | my $cache_mtime; | ||||||||||||||||||||||||||||
| 57 | 27 | my %params; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 27 | ▶ | if (ref $arg) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 60 | 2 | @yaml = <$arg>; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | elsif ($arg =~ /\n/) { | ||||||||||||||||||||||||||||
| 63 | 20 | my $digest = sha1($arg); | |||||||||||||||||||||||||||
| 64 | 20 | $cache_mtime = 1; | |||||||||||||||||||||||||||
| 65 | 20 | my $perl = _yaml_cache_peek($digest, $cache_mtime); | |||||||||||||||||||||||||||
| 66 | 20 | ▶ | return $perl if defined $perl; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 16 | open my $fh, '<', \$arg; | |||||||||||||||||||||||||||
| 68 | 16 | @yaml = <$fh>; | |||||||||||||||||||||||||||
| 69 | 16 | $arg = $digest; | |||||||||||||||||||||||||||
| 70 | 16 | $params{no_disk_cache} = 1; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | ▶ | elsif (-f $arg and -s _) { | |||||||||||||||||||||||||||
|
Condition33%1/3
-f $arg and -s _
MC/DC0%0/2
-f $arg and -s _
-f $arg
-s _
Truth table33%1/3
A and B
A= -f $argB= -s _
| |||||||||||||||||||||||||||||
| 73 | # $arg is a file path. | ||||||||||||||||||||||||||||
| 74 | 5 | _notify_yaml_observers($arg); | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 5 | my $mtime = (stat _)[9]; | |||||||||||||||||||||||||||
| 77 | 5 | my $perl = _yaml_cache_peek($arg, $mtime); | |||||||||||||||||||||||||||
| 78 | 5 | ▶ | return $perl if defined $perl; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 4 | ▶ | open my $fh, $arg | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | or croak "Can't open YAML file $arg: $!"; | ||||||||||||||||||||||||||||
| 82 | 4 | @yaml = <$fh>; | |||||||||||||||||||||||||||
| 83 | 4 | $cache_mtime = $mtime; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | else { | ||||||||||||||||||||||||||||
| 86 | 0 | croak "Can't load empty/missing YAML file: $arg."; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 22 | my $perl; | |||||||||||||||||||||||||||
| 90 | 22 | eval { $perl = Load(join '', @yaml) }; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 22 | ▶ | die "$@\nYAML File: $arg\n" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # Can't cache/flatten empty YAML | ||||||||||||||||||||||||||||
| 95 | 21 | ▶ | return unless $perl; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # TODO: this is a temporary fix. previous functionaly skipped caching if a | ||||||||||||||||||||||||||||
| 98 | # second arg was passed into load_yaml. a recent refactor introduced a bug | ||||||||||||||||||||||||||||
| 99 | # that caused the code to never cache. as a temporary workaround we will | ||||||||||||||||||||||||||||
| 100 | # just set $cache_mtime to 0 if there's a second arg to this method. this | ||||||||||||||||||||||||||||
| 101 | # will tell _unravel_and_cache to skip the caching step. | ||||||||||||||||||||||||||||
| 102 | 21 | ▶ | $cache_mtime = 0 if $dont_cache; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 21 | $perl = _unravel_and_cache($arg, $perl, $cache_mtime, %params); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 21 | return $perl; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | my $shallow_merge = sub { | ||||||||||||||||||||||||||||
| 109 | 16 | ▶ | my ($left, $right) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 110 | 16 | ▶ | if (reftype($left) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 1 | $left = { map %$_, @$left }; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | return ( | ||||||||||||||||||||||||||||
| 114 | 16 | ▶ | (map { %$_ } (reftype($left) eq 'ARRAY' ? @$left : $left)), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | %$right | ||||||||||||||||||||||||||||
| 116 | ); | ||||||||||||||||||||||||||||
| 117 | 5 | }; | |||||||||||||||||||||||||||
| 118 | my $deep_merge = sub { | ||||||||||||||||||||||||||||
| 119 | 17 | ▶ | my ($left, $right) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 120 | 17 | ▶ | return %{ Hash::Merge::Simple->merge( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | (reftype($left) eq 'ARRAY' ? @$left : $left), | ||||||||||||||||||||||||||||
| 122 | $right, | ||||||||||||||||||||||||||||
| 123 | ) }; | ||||||||||||||||||||||||||||
| 124 | 5 | }; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # in order of priority: | ||||||||||||||||||||||||||||
| 127 | 5 | my @SPECIAL = qw( | |||||||||||||||||||||||||||
| 128 | -merge | ||||||||||||||||||||||||||||
| 129 | export | ||||||||||||||||||||||||||||
| 130 | -export | ||||||||||||||||||||||||||||
| 131 | import | ||||||||||||||||||||||||||||
| 132 | -import | ||||||||||||||||||||||||||||
| 133 | ); | ||||||||||||||||||||||||||||
| 134 | 5 | my %SPECIAL = ( | |||||||||||||||||||||||||||
| 135 | -import => $shallow_merge, | ||||||||||||||||||||||||||||
| 136 | import => $shallow_merge, | ||||||||||||||||||||||||||||
| 137 | -export => $shallow_merge, | ||||||||||||||||||||||||||||
| 138 | export => $shallow_merge, | ||||||||||||||||||||||||||||
| 139 | -merge => $deep_merge, | ||||||||||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | # Note: This used to add a (heavy) dependency on Data::Visitor | ||||||||||||||||||||||||||||
| 143 | # to do these simple transformations. I *think* this is exactly | ||||||||||||||||||||||||||||
| 144 | # equivalent to what it used to do. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | sub _unravel { | ||||||||||||||||||||||||||||
| 147 | 12477 | ▶ | my $data = shift; | ||||||||||||||||||||||||||
|
Subroutine
_unravel: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 12477 | ▶ | if (ref $data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 12445 | ▶ | if (reftype $data eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 151 | 12424 | return _unravel_hash($data); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | elsif (reftype $data eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 154 | 21 | for my $elt (@$data) { | |||||||||||||||||||||||||||
| 155 | 48 | $elt = _unravel($elt); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | 21 | return $data; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 32 | return $data; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # Note: this modifies the argument in place. But sometimes it returns | ||||||||||||||||||||||||||||
| 165 | # a different reference, in order to replace itself in the enclosing | ||||||||||||||||||||||||||||
| 166 | # data structure. (If it encounters a "-flatten" entry.) | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | sub _unravel_hash { | ||||||||||||||||||||||||||||
| 169 | 12424 | ▶ | my $data = shift; | ||||||||||||||||||||||||||
|
Subroutine
_unravel_hash: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 12424 | ▶ | while (my @keys = grep { exists $data->{$_} } @SPECIAL) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 15 | for my $key (@keys) { | |||||||||||||||||||||||||||
| 173 | 17 | my $handler = $SPECIAL{$key}; | |||||||||||||||||||||||||||
| 174 | 17 | my $val = delete $data->{$key}; | |||||||||||||||||||||||||||
| 175 | 17 | ▶ | next unless $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 17 | %$data = $handler->(_unravel($val), $data); | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 12424 | ▶ | if (keys %$data == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 33 | ▶ | if (my $arrs = $data->{-flatten}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 182 | 3 | _unravel($arrs); | |||||||||||||||||||||||||||
| 183 | 3 | return [ map @$_, @$arrs ]; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | elsif (my $hrefs = $data->{-flattenhash}) { | ||||||||||||||||||||||||||||
| 186 | 1 | _unravel($hrefs); | |||||||||||||||||||||||||||
| 187 | 1 | return { map %$_, @$hrefs }; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 12420 | for my $elt (values %$data) { | |||||||||||||||||||||||||||
| 192 | 123603 | ▶ | $elt = _unravel($elt) if ref($elt); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 12420 | return $data; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | { | ||||||||||||||||||||||||||||
| 201 | 5 | my %YAML_cache; | |||||||||||||||||||||||||||
| 202 | sub _unravel_and_cache { | ||||||||||||||||||||||||||||
| 203 | 23 | ▶ | my ($path, $perl, $cache_mtime, %params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_unravel_and_cache: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 23 | _unravel($perl); | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | # TODO: need a better way to explicitly not cache here | ||||||||||||||||||||||||||||
| 208 | 23 | ▶ | if ($cache_mtime) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | 20 | my $frozen = Storable::freeze($perl); | |||||||||||||||||||||||||||
| 210 | 20 | $YAML_cache{$path} = [ $cache_mtime, $frozen ]; | |||||||||||||||||||||||||||
| 211 | 20 | ▶ | if ($CacheDir and not $params{no_disk_cache}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$YAML::LoadBundle::CacheDir and not $params{"no_disk_cache"}
MC/DC100%2/2
$YAML::LoadBundle::CacheDir and not $params{"no_disk_cache"}
$YAML::LoadBundle::CacheDir
not $params{"no_disk_cache"}
Truth table100%3/3
A and not(B)
A= $YAML::LoadBundle::CacheDirB= not $params{"no_disk_cache"}
| |||||||||||||||||||||||||||||
| 212 | 2 | my $cache_file = join "/", $CacheDir, sha1_hex($path); | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 2 | eval { mkdir $CacheDir }; | |||||||||||||||||||||||||||
| 215 | 2 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | 0 | warn "Can't write yaml cache: $@"; | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | else { | ||||||||||||||||||||||||||||
| 219 | 2 | ▶ | open my $fh, '>', $cache_file or die "Cannot open $cache_file for writing $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 2 | print $fh $frozen; | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 23 | return $perl; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | sub _yaml_cache_peek { | ||||||||||||||||||||||||||||
| 229 | 27 | ▶ | my ($path, $mtime) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_yaml_cache_peek: called CC=7 SCAR=26.7
| |||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 27 | my $cache = $YAML_cache{$path}; | |||||||||||||||||||||||||||
| 232 | 27 | ▶ | if ($cache) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 233 | 5 | my ($oldtime, $oldyaml) = @$cache; | |||||||||||||||||||||||||||
| 234 | 5 | ▶ | return Storable::thaw($oldyaml) if $oldtime == $mtime; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | elsif ($CacheDir) { | ||||||||||||||||||||||||||||
| 237 | 7 | my $cache_file = join "/", $CacheDir, sha1_hex($path); | |||||||||||||||||||||||||||
| 238 | 7 | ▶ | if (-f $cache_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | 0 | my $cache_time = (stat $cache_file)[9]; | |||||||||||||||||||||||||||
| 240 | 0 | ▶ | if ($cache_time >= $mtime) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | 0 | open my $fh, "<$cache_file"; | |||||||||||||||||||||||||||
| 242 | 0 | my $file_contents = do { local $/; <$fh> }; | |||||||||||||||||||||||||||
| 243 | 0 | my $thawed = Storable::thaw($file_contents); | |||||||||||||||||||||||||||
| 244 | 0 | $YAML_cache{$path} = [ $mtime, $file_contents ]; | |||||||||||||||||||||||||||
| 245 | 0 | ▶ | return $thawed if $cache_time >= $mtime; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | else { | ||||||||||||||||||||||||||||
| 248 | 0 | unlink $cache_file; | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 22 | return; | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | { | ||||||||||||||||||||||||||||
| 258 | 5 | my %default_options = ( | |||||||||||||||||||||||||||
| 259 | follow_symlinks_when => 'bundled', | ||||||||||||||||||||||||||||
| 260 | follow_symlinks_fail => 'error', | ||||||||||||||||||||||||||||
| 261 | conf_suffixes => [ 'conf', 'yml' ], | ||||||||||||||||||||||||||||
| 262 | max_depth => 20, | ||||||||||||||||||||||||||||
| 263 | ); | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | my %symlink_skipper = ( | ||||||||||||||||||||||||||||
| 266 | 0 | ▶ | error => sub { croak "Symlink $_[1] was skipped.\nYAML Bundle: $_[0]\n" }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 267 | 0 | ▶ | warn => sub { carp "Symlink $_[1] was skipped.\nYAML Bundle: $_[0]\n" }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 268 | ▶ | ignore => sub { }, | |||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 269 | 5 | ); | |||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | sub _merge_bundle { | ||||||||||||||||||||||||||||
| 272 | 16 | ▶ | my ($current, $nested) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_merge_bundle: called CC=4 SCAR=16.4
| |||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 16 | ▶ | if (ref($nested) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 275 | 0 | ▶ | $current = [] unless defined $current; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | 0 | return +{ $deep_merge->($current, $nested) }; | |||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | else { | ||||||||||||||||||||||||||||
| 279 | 16 | ▶ | $current = {} unless defined $current; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | 16 | return +{ $deep_merge->($current, $nested) }; | |||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | sub load_yaml_bundle { | ||||||||||||||||||||||||||||
| 285 | 18 | ▶ | my ($path, $given_options) = @_; | ||||||||||||||||||||||||||
|
Subroutine
load_yaml_bundle: called CC=29 SCAR=36.7
Pod
documented
| |||||||||||||||||||||||||||||
| 286 | 18 | my $cache_mtime; | |||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | # Setup the default configuration | ||||||||||||||||||||||||||||
| 289 | my %options = ( | ||||||||||||||||||||||||||||
| 290 | 18 | ▶ | %{ $given_options || {} }, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 291 | %default_options, | ||||||||||||||||||||||||||||
| 292 | ); | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | # Add _vars to the options to allow recursive calls to share state. | ||||||||||||||||||||||||||||
| 295 | 4 | $options{_match_suffix} = join "|", map { quotemeta } @{ $options{conf_suffixes} } | |||||||||||||||||||||||||||
| 296 | 18 | ▶ | unless defined $options{_match_suffix}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 297 | 18 | $options{max_depth}--; | |||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | # Calculate the absolute base path to start from | ||||||||||||||||||||||||||||
| 300 | 18 | ▶ | unless (defined $options{_original_path}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 301 | 2 | $options{_original_path} = abs_path($path); | |||||||||||||||||||||||||||
| 302 | 2 | $options{_original_path_length} = length $options{_original_path}; | |||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | # This is the top call, so check the cache | ||||||||||||||||||||||||||||
| 305 | 2 | my $this_mtime; | |||||||||||||||||||||||||||
| 306 | 2 | $cache_mtime = 0; | |||||||||||||||||||||||||||
| 307 | find({ | ||||||||||||||||||||||||||||
| 308 | follow_fast => 1, | ||||||||||||||||||||||||||||
| 309 | wanted => sub { | ||||||||||||||||||||||||||||
| 310 | 22 | ▶ | if (/^.*\.(?:$options{_match_suffix})\z/s) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 311 | 10 | $this_mtime = (lstat _)[9]; | |||||||||||||||||||||||||||
| 312 | 10 | ▶ | $cache_mtime = $this_mtime if $this_mtime > $cache_mtime; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | }, | ||||||||||||||||||||||||||||
| 315 | }, | ||||||||||||||||||||||||||||
| 316 | $options{_original_path}, | ||||||||||||||||||||||||||||
| 317 | 4 | grep { -f $_ } | |||||||||||||||||||||||||||
| 318 | 4 | map { "$options{_original_path}.$_" } | |||||||||||||||||||||||||||
| 319 | 2 | @{ $options{conf_suffixes} } | |||||||||||||||||||||||||||
| 320 | ); | ||||||||||||||||||||||||||||
| 321 | 2 | my $perl = _yaml_cache_peek($path, $cache_mtime); | |||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 2 | ▶ | return $perl if defined $perl; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 18 | my $symlink_skipper = $symlink_skipper{ $options{follow_symlinks_fail} }; | |||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | # Stop, we've gone too far. | ||||||||||||||||||||||||||||
| 329 | 18 | ▶ | if ($options{max_depth} < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | 0 | carp "Reached maximum path search depth while at $path.\nYAML Bundle: $options{_original_path}\n"; | |||||||||||||||||||||||||||
| 331 | 0 | return; | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 18 | my $perl; | |||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | # Do we have a top level .conf/.yml/.whatever in the bundle? | ||||||||||||||||||||||||||||
| 337 | 18 | for my $suffix (@{ $options{conf_suffixes} }) { | |||||||||||||||||||||||||||
| 338 | 36 | my $file = $path . '.' . $suffix; | |||||||||||||||||||||||||||
| 339 | 36 | ▶ | if (-f $file and -s _) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-f $file and -s _
MC/DC50%1/2
-f $file and -s _
-f $file
-s _
Truth table66%2/3
A and B
A= -f $fileB= -s _
| |||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | # If $perl is already defined, we have a case where multiple | ||||||||||||||||||||||||||||
| 342 | # configuration files are present, which is not a defined case. | ||||||||||||||||||||||||||||
| 343 | 10 | ▶ | carp "Multiple configuration files match $path. This will lead to unexpected results.\nYAML Bundle: $options{_original_path}\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 344 | if defined $perl; | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | # We don't use load_yml because we don't want the intermediate | ||||||||||||||||||||||||||||
| 347 | # pieces cached and it does a lot of work we'd repeat anyway. | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | 10 | ▶ | open my $fh, $file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 350 | or croak "Can't open YAML file $file: $!"; | ||||||||||||||||||||||||||||
| 351 | 10 | my $yaml = do { local $/; <$fh> }; | |||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 10 | $perl = eval { Load($yaml) }; | |||||||||||||||||||||||||||
| 354 | 10 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 355 | 0 | croak "Eror in file $file: $@\nYAML Bundle: $options{_original_path}\n"; | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | # if no file found, we have to start somewhere | ||||||||||||||||||||||||||||
| 361 | 18 | ▶ | $perl = {} unless defined $perl; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | # If this is a directory, let's suck in all the nested configs | ||||||||||||||||||||||||||||
| 364 | 18 | ▶ | if (-d $path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 365 | 12 | ▶ | opendir my $dir_fh, $path or croak "Cannot opendir $path: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | # Saves us from duplicating work while recursing... | ||||||||||||||||||||||||||||
| 368 | 12 | my %closed_list; | |||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | 12 | ▶ | ENTRY: while (my $entry = readdir $dir_fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | # Ignore all dot files | ||||||||||||||||||||||||||||
| 373 | 44 | ▶ | next if $entry =~ m{^[.]}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 20 | my $nested_path = abs_path("$path/$entry"); | |||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 20 | ▶ | if (not defined $nested_path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 378 | 0 | croak "Broken symlink or other problem while locating $path/$entry.\nYAML Bundle: $options{_original_path}\n"; | |||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | # If bundled, make sure this abs path is in the root bas path | ||||||||||||||||||||||||||||
| 382 | 20 | ▶ | if ($options{follow_symlinks_when} eq 'bundled') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 383 | 20 | ▶ | unless (substr($nested_path, 0, $options{_original_path_length}) eq $options{_original_path}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | 0 | $symlink_skipper->($options{_original_path}, "$path/$entry"); | |||||||||||||||||||||||||||
| 385 | 0 | next ENTRY; | |||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | # If never, skip any symlink | ||||||||||||||||||||||||||||
| 390 | elsif ($options{follow_symlinks_when} eq 'never') { | ||||||||||||||||||||||||||||
| 391 | 0 | ▶ | if (-l "$path/$entry") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 392 | 0 | $symlink_skipper->($options{_original_path}, "$path/$entry"); | |||||||||||||||||||||||||||
| 393 | 0 | next ENTRY; | |||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | # Is this a directory? If so, load that as a bundle. | ||||||||||||||||||||||||||||
| 398 | 20 | ▶ | if (-d $nested_path) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 399 | 10 | ▶ | next ENTRY if $closed_list{$nested_path}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | # We don't follow symlinks to directories. This is a naive way | ||||||||||||||||||||||||||||
| 402 | # to prevent infinite recursion. | ||||||||||||||||||||||||||||
| 403 | 6 | ▶ | if (-l "$path/$entry") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 404 | 0 | croak "Symlink to directory $path/$entry is not permitted.\nYAML Bundle: $options{_original_path}\n"; | |||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | # Load the nested bundle and merge. | ||||||||||||||||||||||||||||
| 408 | 6 | $closed_list{$nested_path}++; | |||||||||||||||||||||||||||
| 409 | $perl->{ $entry } = _merge_bundle( | ||||||||||||||||||||||||||||
| 410 | 6 | $perl->{ $entry }, | |||||||||||||||||||||||||||
| 411 | load_yaml_bundle($nested_path, \%options) | ||||||||||||||||||||||||||||
| 412 | ); | ||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | # Is this a file with the right suffix? | ||||||||||||||||||||||||||||
| 416 | ▶ | elsif (-f $nested_path and $entry =~ s/[.](?:$options{_match_suffix})$//) { | |||||||||||||||||||||||||||
|
Condition33%1/3
-f $nested_path and $entry =~ s/[.](?:$options{"_match_suffix"})$//
MC/DC0%0/2
-f $nested_path and $entry =~ s/[.](?:$options{"_match_suffix"})$//
-f $nested_path
$entry =~ s/[.](?:$options{"_match_suffix"})$//
Truth table33%1/3
A and B
A= -f $nested_pathB= $entry =~ s/[.](?:$options{"_match_suffix"})$//
| |||||||||||||||||||||||||||||
| 417 | 10 | my $nested_path_minus_suffix = $nested_path; | |||||||||||||||||||||||||||
| 418 | 10 | $nested_path_minus_suffix =~ s/[.](?:$options{_match_suffix})$//; | |||||||||||||||||||||||||||
| 419 | 10 | ▶ | next ENTRY if $closed_list{$nested_path_minus_suffix}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | # Load the nested bundle and merge. | ||||||||||||||||||||||||||||
| 422 | 10 | $closed_list{$nested_path_minus_suffix}++; | |||||||||||||||||||||||||||
| 423 | $perl->{ $entry } = _merge_bundle( | ||||||||||||||||||||||||||||
| 424 | 10 | $perl->{ $entry }, | |||||||||||||||||||||||||||
| 425 | load_yaml_bundle($nested_path_minus_suffix, \%options) | ||||||||||||||||||||||||||||
| 426 | ); | ||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | # What the hey? Carp about this... | ||||||||||||||||||||||||||||
| 430 | else { | ||||||||||||||||||||||||||||
| 431 | 0 | carp "Ignoring unexpected path $nested_path of unknown type.\nYAML Bundle: $options{_original_path}\n"; | |||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | # Only unravel our format layer and cache the top | ||||||||||||||||||||||||||||
| 437 | # $cache_mtime is only set in the call _original_path is set | ||||||||||||||||||||||||||||
| 438 | 18 | ▶ | if ($cache_mtime) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 439 | 2 | $perl = _unravel_and_cache($options{_original_path}, $perl, $cache_mtime); | |||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | 18 | return $perl; | |||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | 5 | 1; | |||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | __END__ | ||||||||||||||||||||||||||||