| 1 | # ABSTRACT: A module to handle files and folders | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | package Mir::FileHandler; | ||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use 5.018; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | no warnings 'experimental::smartmatch'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use DirHandle; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use File::Find; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use File::Basename qw( dirname ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Log::Log4perl; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Digest::MD5 'md5_base64'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Cache::FileCache; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | Mir::FileHandler - An Mir module to handle files and folders... | ||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | Version 0.05 | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 4 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||||||||||||||||||||
| 25 | 4 | our @rec_files = (); | |||||||||||||||||||||||||||||||||||||||||||||
| 26 | 4 | our $files_as_hash = {}; | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | 4 | our @types = (); | |||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | use Mir::FileHandler; | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # the oo way... | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | # get a new FileHandler obj for the passed root directory | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | my $o = Mir::FileHandler->new( | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | path => $path, # root folder to start with | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # get plain files list inside the root directory | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | my $list = $o->plainfiles(); # or pass a path in input | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | # get plain files list from folder and sub-folders | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | my $list = $o->plainfiles_recursive( $path, $suffix, \&found ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | # Traverses a directory tree and exec code for each file | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | $o->dir_walk( | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | path => $path, | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | code => $code, | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # Traverses a directory tree and exec code for each file | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # Stops after max success code execution | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # the sub pointed by $code has to return 1 in | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | # case of success | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # if cached_dir is set, at each iteration starts from | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | # what stored in cache, if something has been stored, | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | # otherwise starts from path | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | $o->clear_cache(); # to clear current dir stored in cache | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | $o->dir_walk_max( | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | code => $code, # not mandatory, code to exec for each file found | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | max => $max # not mandatory, max files successfully processed | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | =head1 EXPORT | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||
| 66 | =over | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | =item plainfiles | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | =item plainfiles_recursive | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | #============================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 4 | has 'path' => ( | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | required => 1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 4 | has 'cache_key' => ( | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | has 'cache' => ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | isa => 'Cache::FileCache', | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | ▶ | default => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | my $o = Cache::FileCache->new(); | |||||||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | return $o; | |||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||
| 101 | has 'log' => ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | isa => 'Log::Log4perl::Logger', | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | default => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | return Log::Log4perl->get_logger( __PACKAGE__ ); | |||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 3 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 112 | 3 | $self->cache_key( md5_base64( $self->path ) ); | |||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||
| 115 | #============================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||
| 117 | =head2 plainfiles | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||
| 119 | =head3 INPUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||
| 121 | $path: path to look for (not mandatory, eventually takes the | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | one passed at construction time) | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||
| 124 | =head3 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | An ArrayRef | ||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | =head3 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | Returns the list of folder plain files. | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | #============================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | ▶ | sub plainfiles { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
plainfiles: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 1 | ▶ | return undef unless ( -e $self->path ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 1 | return [ _pf( $self->path ) ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | #============================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||
| 144 | =head2 _pf | ||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||
| 146 | =head3 INPUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||
| 148 | $path: the path to look for docs | ||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | =head3 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | A sorted list of docs. | ||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||
| 154 | =head3 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||
| 156 | Private function. Returns the sorted list of regular | ||||||||||||||||||||||||||||||||||||||||||||||
| 157 | files in current folder. | ||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||
| 159 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||
| 161 | #============================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 1 | ▶ | sub _pf { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pf: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 1 | ▶ | my $dir = shift or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 1 | ▶ | my $dh = DirHandle->new($dir) or die "can't opendir $dir: $!"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 165 | return sort | ||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 1 | grep { -f } | |||||||||||||||||||||||||||||||||||||||||||||
| 167 | 1 | map { "$dir/$_" } | |||||||||||||||||||||||||||||||||||||||||||||
| 168 | 1 | grep { !/^\./ } | |||||||||||||||||||||||||||||||||||||||||||||
| 169 | $dh->read(); | ||||||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||
| 172 | #============================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||
| 174 | =head2 _get_path_from_cache | ||||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||
| 176 | =head3 INPUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||
| 178 | None | ||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||
| 180 | =head3 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||||
| 182 | The cache path or undef | ||||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||
| 184 | =head3 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||
| 186 | Tries to retrieve a current path cached for the root one or | ||||||||||||||||||||||||||||||||||||||||||||||
| 187 | returns undef | ||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||
| 189 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||
| 191 | #============================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 3 | ▶ | sub _get_path_from_cache { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_path_from_cache: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 194 | 3 | return $self->cache->get( $self->cache_key ); | |||||||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 3 | ▶ | sub _set_path_in_cache { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_path_in_cache: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 3 | my ( $self, $path ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 199 | 3 | ▶ | return $self->clear_cache() unless ( $path ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | return $self->cache->set( $self->cache_key, $path ); | |||||||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 6 | ▶ | sub clear_cache { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clear_cache: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 205 | 6 | $self->cache->remove( $self->cache_key ); | |||||||||||||||||||||||||||||||||||||||||||||
| 206 | 6 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||
| 209 | #============================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||
| 211 | =head2 dir_walk - traverses recursively a folder | ||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||
| 213 | =head3 INPUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||
| 215 | An hash with keys: | ||||||||||||||||||||||||||||||||||||||||||||||
| 216 | code: a coderef to apply to each file | ||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||
| 218 | =head3 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||
| 220 | -1 if root file or folder are not existent, else | ||||||||||||||||||||||||||||||||||||||||||||||
| 221 | the number of valid files. | ||||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||||
| 223 | =head3 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||||||
| 225 | Traverse a directory, triggering a sub for each file found. | ||||||||||||||||||||||||||||||||||||||||||||||
| 226 | The sub should return 1 if file is good, 0 otherwise. | ||||||||||||||||||||||||||||||||||||||||||||||
| 227 | It is meant to recursively process a folder in one shot. | ||||||||||||||||||||||||||||||||||||||||||||||
| 228 | This is the minimal logic to traverse a tree, for added, | ||||||||||||||||||||||||||||||||||||||||||||||
| 229 | features look at dir_walk_max. | ||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||
| 231 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||
| 233 | #============================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 1 | ▶ | sub dir_walk { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dir_walk: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 1 | my ( $self, %params ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 236 | 1 | my $code = $params{code}; | |||||||||||||||||||||||||||||||||||||||||||||
| 237 | 1 | ▶ | return undef unless $code; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 1 | return $self->_walk( $self->path, $code, 0); | |||||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||
| 241 | #============================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||||||
| 243 | =head2 _walk | ||||||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||||||
| 245 | =head3 INPUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||
| 247 | $top: subtree root | ||||||||||||||||||||||||||||||||||||||||||||||
| 248 | $code: code to execute against each valid file | ||||||||||||||||||||||||||||||||||||||||||||||
| 249 | $count: number of valid files till now... | ||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||
| 251 | =head3 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||||||
| 253 | The number of valid files. | ||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||
| 255 | =head3 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||
| 257 | Traverse the subtree. | ||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||
| 259 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||||||
| 261 | #============================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 2 | ▶ | sub _walk { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_walk: called CC=7 SCAR=19.7
| |||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 2 | my ( $self, $top, $code, $count ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 264 | 2 | my $DIR; | |||||||||||||||||||||||||||||||||||||||||||||
| 265 | 2 | ▶ | unless (opendir $DIR, $top) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | $self->log->error( "Couldn’t open directory $top: $!" ); | |||||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 2 | my $file; | |||||||||||||||||||||||||||||||||||||||||||||
| 270 | 2 | ▶ | while ($file = readdir $DIR) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 6 | my $item = "$top/$file"; | |||||||||||||||||||||||||||||||||||||||||||||
| 272 | 6 | ▶ | if ( -f $item ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 1 | $count += $code->($item); | |||||||||||||||||||||||||||||||||||||||||||||
| 274 | } elsif (-d $item ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 5 | ▶ | next if $file eq '.' || $file eq '..'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$file eq "." or $file eq ".."
MC/DC100%2/2
$file eq "." or $file eq ".."
$file eq "."
$file eq ".."
Truth table100%3/3
A or B
A= $file eq "."B= $file eq ".."
| |||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 1 | $count = $self->_walk( $item, $code, $count ); | |||||||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 2 | closedir $DIR; | |||||||||||||||||||||||||||||||||||||||||||||
| 280 | 2 | return $count; | |||||||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||||||
| 283 | #============================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||
| 285 | =head2 dir_walk_max - traverses recursively a folder, stops after | ||||||||||||||||||||||||||||||||||||||||||||||
| 286 | max valid files found or all tree has been | ||||||||||||||||||||||||||||||||||||||||||||||
| 287 | traversed | ||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||
| 289 | =head3 INPUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||||||
| 291 | An hash with keys: | ||||||||||||||||||||||||||||||||||||||||||||||
| 292 | code: a coderef to apply to | ||||||||||||||||||||||||||||||||||||||||||||||
| 293 | max: max number of items to evaluate | ||||||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||||||
| 295 | =head3 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||||||
| 297 | -1 if root file or folder are not existent, else | ||||||||||||||||||||||||||||||||||||||||||||||
| 298 | the number of valid files. | ||||||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||||||
| 300 | =head3 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||||||
| 302 | Traverse a directory, triggering a sub for each file found. | ||||||||||||||||||||||||||||||||||||||||||||||
| 303 | The sub should return 1 if file is good, 0 otherwise. | ||||||||||||||||||||||||||||||||||||||||||||||
| 304 | The method stops when all files are consumed or max number | ||||||||||||||||||||||||||||||||||||||||||||||
| 305 | of good files is reached. | ||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||||||
| 308 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||
| 310 | #============================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 3 | ▶ | sub dir_walk_max { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dir_walk_max: called CC=3 SCAR=11.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 3 | my ( $self, %params ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 313 | 3 | my $code = $params{code}; | |||||||||||||||||||||||||||||||||||||||||||||
| 314 | 3 | my $max = $params{max}; | |||||||||||||||||||||||||||||||||||||||||||||
| 315 | 3 | ▶ | return undef unless ( $code && $max ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$code and $max
MC/DC0%0/2
$code and $max
$code
$max
Truth table33%1/3
A and B
A= $codeB= $max
| |||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 3 | my $rel_path = $self->_get_path_from_cache(); | |||||||||||||||||||||||||||||||||||||||||||||
| 317 | 3 | return $self->_walk_max( $self->path, 0, $max, $code, $rel_path ); | |||||||||||||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||||||
| 321 | #============================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||||||
| 323 | =head2 _walk_max | ||||||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||||||
| 325 | =head3 INPUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||||||
| 327 | root : the root folder to start with | ||||||||||||||||||||||||||||||||||||||||||||||
| 328 | count : number of valid items processed | ||||||||||||||||||||||||||||||||||||||||||||||
| 329 | rel_path : the relative path from root | ||||||||||||||||||||||||||||||||||||||||||||||
| 330 | max : max number of valid items | ||||||||||||||||||||||||||||||||||||||||||||||
| 331 | code : code to run against each item | ||||||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||||||
| 333 | =head3 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||||||
| 335 | The number of valid items processed | ||||||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||||
| 337 | =head3 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||||||
| 339 | Inner loop to analize each item of the tree. | ||||||||||||||||||||||||||||||||||||||||||||||
| 340 | it calls recursively itself for each sub-tree. | ||||||||||||||||||||||||||||||||||||||||||||||
| 341 | Ends when 1 of these conditions is met: | ||||||||||||||||||||||||||||||||||||||||||||||
| 342 | - no more files found | ||||||||||||||||||||||||||||||||||||||||||||||
| 343 | - max number of valid items reached | ||||||||||||||||||||||||||||||||||||||||||||||
| 344 | this number is set to be 10 times the max number of valid items. | ||||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||||||
| 347 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||||||
| 349 | #============================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 3 | ▶ | sub _walk_max { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_walk_max: called CC=13 SCAR=27.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 3 | my ( $self, $root, $count, $max, $code, $rel_path ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 3 | ▶ | $self->log->info(sprintf("[%06d]:Scanning %s", $count, $rel_path)) if( $rel_path ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 3 | ▶ | my $path = ( $rel_path ) ? "$root/$rel_path" : $root; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 3 | $self->_set_path_in_cache( $rel_path ); | |||||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 3 | my $DIR; | |||||||||||||||||||||||||||||||||||||||||||||
| 358 | 3 | ▶ | unless (opendir $DIR, $path) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 0 | $self->log->error( "Couldn’t open directory $path $!" ); | |||||||||||||||||||||||||||||||||||||||||||||
| 360 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 3 | my $file; | |||||||||||||||||||||||||||||||||||||||||||||
| 364 | 3 | ▶ | while ( defined($file = readdir $DIR) && ( $count < $max ) ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined($file = readdir $DIR) and $count < $max
MC/DC100%2/2
defined($file = readdir $DIR) and $count < $max
defined($file = readdir $DIR)
$count < $max
Truth table100%3/3
A and B
A= defined($file = readdir $DIR)B= $count < $max
| |||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 15 | my $item = "$path/$file"; | |||||||||||||||||||||||||||||||||||||||||||||
| 366 | 15 | ▶ | if ( -f $item ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 9 | $count += $code->($item); | |||||||||||||||||||||||||||||||||||||||||||||
| 368 | } elsif (-d $item ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 6 | ▶ | next if $file eq '.' || $file eq '..'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$file eq "." or $file eq ".."
MC/DC0%0/2
$file eq "." or $file eq ".."
$file eq "."
$file eq ".."
Truth table66%2/3
A or B
A= $file eq "."B= $file eq ".."
| |||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | ▶ | $count = $self->_walk_max( $root, $count, $max, $code, ( $rel_path ) ? $rel_path.'/'.$file : $file ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 3 | closedir $DIR; | |||||||||||||||||||||||||||||||||||||||||||||
| 374 | 3 | undef $DIR; | |||||||||||||||||||||||||||||||||||||||||||||
| 375 | 3 | ▶ | unless ( $file ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | ▶ | ( $rel_path ) ? $self->_set_path_in_cache( join('/', splice(@{[split(m|/|, $rel_path)]},0,-1))) : | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 377 | $self->clear_cache(); | ||||||||||||||||||||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 3 | undef $file; | |||||||||||||||||||||||||||||||||||||||||||||
| 380 | 3 | return $count; | |||||||||||||||||||||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||||||
| 383 | #============================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||
| 385 | =head2 plainfiles_recursive | ||||||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||||||
| 387 | =head3 INPUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||||||
| 389 | $path: a path to start from. | ||||||||||||||||||||||||||||||||||||||||||||||
| 390 | $avoid: arrayref con lista risorse da evitare | ||||||||||||||||||||||||||||||||||||||||||||||
| 391 | $suffix: arrayref con lista suffissi da processare | ||||||||||||||||||||||||||||||||||||||||||||||
| 392 | $found: ref sub callback per ogni risorsa | ||||||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||||||
| 394 | =head3 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||||||
| 396 | An arrayref. | ||||||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||||||
| 398 | =head3 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||||||
| 400 | Returns recursively the list of all files from passed folder. | ||||||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||||||
| 402 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||||||
| 404 | #============================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 1 | ▶ | sub plainfiles_recursive { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
plainfiles_recursive: called CC=6 SCAR=20.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 1 | my ( $self, $path, $found ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 1 | ▶ | my $dir = ( $self && ref $self ) ? $path || $self->{path} : $path; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$self and ref $self
$path or $self->{"path"}
MC/DC0%0/2
$self and ref $self
$self
ref $self
MC/DC0%0/2
$path or $self->{"path"}
$path
$self->{"path"}
Truth table33%1/3
A and B
A= $selfB= ref $self
Truth table33%1/3
A or B
A= $pathB= $self->{"path"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 409 | 1 | ▶ | return undef unless $dir; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 1 | ▶ | $found = \&_found unless $found; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 1 | @rec_files = (); | |||||||||||||||||||||||||||||||||||||||||||||
| 414 | 1 | find ( $found, ( $dir ) ); | |||||||||||||||||||||||||||||||||||||||||||||
| 415 | 1 | return \@rec_files; | |||||||||||||||||||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 3 | ▶ | sub _found { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_found: called CC=5 SCAR=18.6
| |||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 3 | ▶ | return if ( -d ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 1 | ▶ | if ( -f && !/^\./ && !/\~$/ ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
-f $_ and not /^\./u and not /\~$/u
-f $_ and not /^\./u
MC/DC0%0/3
-f $_ and not /^\./u and not /\~$/u
-f $_
not /^\./u
not /\~$/u
Truth table25%1/4
A and not(B) and not(C)
A= -f $_B= not /^\./uC= not /\~$/u
| |||||||||||||||||||||||||||||||||||||||||||||||
| 421 | 1 | push @rec_files, $File::Find::name; | |||||||||||||||||||||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 0 | ▶ | sub _collect_as_hash { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_collect_as_hash: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||||||
| 426 | 0 | ▶ | return if ( -d && /\.svn/ ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-d $_ and /\.svn/u
MC/DC0%0/2
-d $_ and /\.svn/u
-d $_
/\.svn/u
Truth table0%0/3
A and B
A= -d $_B= /\.svn/u
| |||||||||||||||||||||||||||||||||||||||||||||||
| 427 | 0 | ▶ | if ( -f && !/^\./ && !/\~$/ ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
-f $_ and not /^\./u and not /\~$/u
-f $_ and not /^\./u
MC/DC0%0/3
-f $_ and not /^\./u and not /\~$/u
-f $_
not /^\./u
not /\~$/u
Truth table0%0/4
A and not(B) and not(C)
A= -f $_B= not /^\./uC= not /\~$/u
| |||||||||||||||||||||||||||||||||||||||||||||||
| 428 | 0 | ▶ | if ( m|(\w+)\.(\w+)$| ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 0 | push @{ $files_as_hash->{ $2 } }, $File::Find::name; | |||||||||||||||||||||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 0 | ▶ | sub _collect_of_type { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_collect_of_type: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | ▶ | return if ( -d && /\.\w+/ ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-d $_ and /\.\w+/u
MC/DC0%0/2
-d $_ and /\.\w+/u
-d $_
/\.\w+/u
Truth table0%0/3
A and B
A= -d $_B= /\.\w+/u
| |||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 0 | ▶ | if ( -f && !/^\./ && !/\~$/ ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
-f $_ and not /^\./u and not /\~$/u
-f $_ and not /^\./u
MC/DC0%0/3
-f $_ and not /^\./u and not /\~$/u
-f $_
not /^\./u
not /\~$/u
Truth table0%0/4
A and not(B) and not(C)
A= -f $_B= not /^\./uC= not /\~$/u
| |||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 0 | ▶ | if ( m|(\w+)\.(\w+)$| ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 438 | 0 | ▶ | if ( $2 ~~ @types ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 0 | push @rec_files, $File::Find::name; | |||||||||||||||||||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||||||
| 445 | #============================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||||||
| 447 | =head2 plainfiles_recursive_as_hash | ||||||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||||||
| 449 | =head3 INPUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||||||
| 451 | $path : root path (not mandatory if already passed at | ||||||||||||||||||||||||||||||||||||||||||||||
| 452 | construction time) | ||||||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||||||
| 454 | =head3 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||||||
| 456 | An HashRef. | ||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||
| 458 | =head3 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||||||
| 460 | Recursively collects files in an hash indexed by file suffix. | ||||||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||||||
| 462 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||||||
| 464 | #============================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 465 | 0 | ▶ | sub plainfiles_recursive_as_hash { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
plainfiles_recursive_as_hash: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 0 | my ( $self, $path ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||||||
| 468 | 0 | ▶ | my $dir = ( $self && ref $self ) ? $path || $self->{path} : $path; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$self and ref $self
$path or $self->{"path"}
MC/DC0%0/2
$self and ref $self
$self
ref $self
MC/DC0%0/2
$path or $self->{"path"}
$path
$self->{"path"}
Truth table0%0/3
A and B
A= $selfB= ref $self
Truth table0%0/3
A or B
A= $pathB= $self->{"path"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 469 | 0 | ▶ | return undef unless $dir; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||||||
| 471 | 0 | $files_as_hash = {}; | |||||||||||||||||||||||||||||||||||||||||||||
| 472 | 0 | $self->plainfiles_recursive( $dir, \&_collect_as_hash ); | |||||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||||||
| 474 | # foreach my $file ( @$rec_files ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 475 | # if ( $file =~ m|(\w+)\.(\w+)$| ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 476 | # push @{ $files_as_hash->{ $2 } }, $file; | ||||||||||||||||||||||||||||||||||||||||||||||
| 477 | # } | ||||||||||||||||||||||||||||||||||||||||||||||
| 478 | # } | ||||||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||||||
| 480 | 0 | return $files_as_hash; | |||||||||||||||||||||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||||||
| 483 | #============================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||||||
| 485 | =head2 plainfiles_recursive_of_type | ||||||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||||||
| 487 | =head3 INPUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||||||
| 489 | @types: list of valid file suffixes | ||||||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||||||
| 491 | =head3 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||||||
| 493 | An ArrayRef or undef in case of errors. | ||||||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||||||
| 495 | =head3 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||||||
| 497 | Recursively collects all files with valid suffixes | ||||||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||||||
| 499 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||||||
| 501 | #============================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 502 | 0 | ▶ | sub plainfiles_recursive_of_type { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
plainfiles_recursive_of_type: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 503 | 0 | my ( $self, @ptypes ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||||||
| 505 | 0 | ▶ | return undef unless $self->{path}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||||||
| 507 | 0 | @types = @ptypes; | |||||||||||||||||||||||||||||||||||||||||||||
| 508 | 0 | @rec_files = (); | |||||||||||||||||||||||||||||||||||||||||||||
| 509 | 0 | $self->plainfiles_recursive( $self->{path}, \&_collect_of_type ); | |||||||||||||||||||||||||||||||||||||||||||||
| 510 | 0 | return \@rec_files; | |||||||||||||||||||||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||||||
| 513 | #============================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||||||
| 515 | =head2 process_dir | ||||||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||||||
| 517 | =head3 INPUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||||||
| 519 | $dir : dir to start with | ||||||||||||||||||||||||||||||||||||||||||||||
| 520 | $suffix : arrayref of valid file suffixes | ||||||||||||||||||||||||||||||||||||||||||||||
| 521 | $depth : depth in processing subdirs | ||||||||||||||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||||||||||||||
| 523 | =head3 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||||||||||||||
| 525 | An arrayref | ||||||||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||||||
| 527 | =head3 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||||||||||||
| 529 | Workflow: | ||||||||||||||||||||||||||||||||||||||||||||||
| 530 | get a single dir as input and the level of recursions | ||||||||||||||||||||||||||||||||||||||||||||||
| 531 | get the list of valid dir files and process them | ||||||||||||||||||||||||||||||||||||||||||||||
| 532 | get the list of dir direct subdirs | ||||||||||||||||||||||||||||||||||||||||||||||
| 533 | depth-- | ||||||||||||||||||||||||||||||||||||||||||||||
| 534 | if depth > 0 | ||||||||||||||||||||||||||||||||||||||||||||||
| 535 | call process_dir foreach subdir | ||||||||||||||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||||||||||||||
| 537 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||||||
| 539 | #============================================================= | ||||||||||||||||||||||||||||||||||||||||||||||
| 540 | 0 | ▶ | sub process_dir ($$) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process_dir: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||||||||||
| 542 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||||||
| 544 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||||||
| 546 | Marco Masetti, C<< <marco.masetti at softeco.it> >> | ||||||||||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||||||||||
| 548 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||||||
| 550 | Please report any bugs or feature requests to C<bug-ishare-filehandler at rt.cpan.org>, or through | ||||||||||||||||||||||||||||||||||||||||||||||
| 551 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mir-FileHandler>. I will be notified, and then you'll | ||||||||||||||||||||||||||||||||||||||||||||||
| 552 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||||||||||||||||
| 554 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||||||
| 556 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||||||||||||||
| 558 | perldoc Mir::FileHandler | ||||||||||||||||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||||||||||||||||
| 560 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||||||||||||||||
| 562 | Copyright 2013 Marco Masetti. | ||||||||||||||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||||||||||||||
| 564 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||||||
| 565 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||||||||||||||||||||
| 566 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||||||||||||||||
| 568 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||||||||||||||
| 570 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||||||||||||||
| 572 | 4 | ▶ | no Moose; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 573 | 4 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||||||