| 1 | package Zoidberg::Utils::FileSystem; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | 63 | our $VERSION = '0.981'; | |||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||
| 5 | 63 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | #use File::Spec; | ||||||||||||||||||||||||||||||||||
| 7 | 63 | ▶ | use Env qw/@PATH/; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 63 | ▶ | use File::Spec; # TODO make more use of this lib | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 63 | ▶ | use Encode; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | use Exporter::Tidy | ||||||||||||||||||||||||||||||||||
| 11 | 63 | ▶ | default => [qw/path list_path list_dir unique_file regex_glob/]; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | 63 | our $DEVNULL = File::Spec->devnull(); | |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||
| 15 | 78 | ▶ | sub path { | ||||||||||||||||||||||||||||||||
|
Subroutine
path: called CC=7 SCAR=21.4
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 16 | # return absolute path | ||||||||||||||||||||||||||||||||||
| 17 | # argument: string optional: reference | ||||||||||||||||||||||||||||||||||
| 18 | 78 | ▶ | my $string = shift || return $ENV{PWD}; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || (return $ENV{'PWD'})
MC/DC0%0/1
shift() || (return $ENV{'PWD'})
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||
| 19 | 78 | ▶ | my $refer = $_[0] ? path(shift @_) : $ENV{PWD}; # possibly recurs | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 20 | 78 | $refer =~ s/\/$//; | |||||||||||||||||||||||||||||||||
| 21 | 78 | $string =~ s{/+}{/}; # ever tried using $::main::main::main::something ? | |||||||||||||||||||||||||||||||||
| 22 | 78 | ▶ | unless ($string =~ m{^/}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 23 | 62 | ▶ | if ( $string =~ /^~([^\/]*)/ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | if ($1) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 25 | 0 | my @info = getpwnam($1); | |||||||||||||||||||||||||||||||||
| 26 | # @info = ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell). | ||||||||||||||||||||||||||||||||||
| 27 | 0 | $string =~ s{^~$1/?}{$info[7]/}; | |||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||
| 29 | 0 | else { $string =~ s{^~/?}{$ENV{HOME}/}; } | |||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||
| 31 | elsif ( $string =~ s{^\.(\.+)(/|$)}{}) { | ||||||||||||||||||||||||||||||||||
| 32 | 45 | my $l = length($1); | |||||||||||||||||||||||||||||||||
| 33 | 45 | $refer =~ s{(/[^/]*){0,$l}$}{}; | |||||||||||||||||||||||||||||||||
| 34 | 45 | $string = $refer.'/'.$string; | |||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||
| 36 | else { | ||||||||||||||||||||||||||||||||||
| 37 | 17 | $string =~ s{^\.(/|$)}{}; | |||||||||||||||||||||||||||||||||
| 38 | 17 | $string = $refer.'/'.$string; | |||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||
| 41 | 78 | $string =~ s/\\//g; | |||||||||||||||||||||||||||||||||
| 42 | 78 | return $string; | |||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||
| 45 | 17 | ▶ | sub list_dir { | ||||||||||||||||||||||||||||||||
|
Subroutine
list_dir: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 46 | 17 | ▶ | my $dir = @_ ? shift : $ENV{PWD}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 47 | 17 | ▶ | $dir =~ s#/$## unless $dir eq '/'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 48 | 17 | $dir = path($dir); | |||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||
| 50 | 17 | ▶ | opendir DIR, $dir or die "could not open dir: $dir"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 51 | 17 | my @items = grep {$_ !~ /^\.{1,2}$/} readdir DIR ; | |||||||||||||||||||||||||||||||||
| 52 | 17 | closedir DIR; | |||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||
| 54 | 17 | @items = map Encode::decode_utf8($_, 1), @items; | |||||||||||||||||||||||||||||||||
| 55 | 17 | return @items; | |||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub list_path { return map list_dir($_), grep {-d $_} @PATH } | ||||||||||||||||||||||||||||||||
|
Subroutine
list_path: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub unique_file { | ||||||||||||||||||||||||||||||||
|
Subroutine
unique_file: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | my $string = pop || "untitledXXXX"; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
pop() || 'untitledXXXX'
MC/DC0%0/1
pop() || 'untitledXXXX'
pop()
Truth table0%0/2
A
A= pop()
| |||||||||||||||||||||||||||||||||||
| 62 | 0 | my ($file, $number) = ($string, 0); | |||||||||||||||||||||||||||||||||
| 63 | 0 | $file =~ s/XXXX/$number/; | |||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | while ( -e $file ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ($number > 256) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 66 | 0 | $file = undef; | |||||||||||||||||||||||||||||||||
| 67 | 0 | last; | |||||||||||||||||||||||||||||||||
| 68 | } # infinite loop protection | ||||||||||||||||||||||||||||||||||
| 69 | else { | ||||||||||||||||||||||||||||||||||
| 70 | 0 | $file = $string; | |||||||||||||||||||||||||||||||||
| 71 | 0 | $file =~ s/XXXX/$number/; | |||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||
| 73 | 0 | $number++ | |||||||||||||||||||||||||||||||||
| 74 | }; | ||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | die qq/could not find any non-existent file for string "$string"/ | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 76 | unless defined $file; | ||||||||||||||||||||||||||||||||||
| 77 | 0 | return $file; | |||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||
| 80 | # [! => [^ | ||||||||||||||||||||||||||||||||||
| 81 | # * => .* | ||||||||||||||||||||||||||||||||||
| 82 | # ? => .? | ||||||||||||||||||||||||||||||||||
| 83 | # leave [] {} () | ||||||||||||||||||||||||||||||||||
| 84 | # quote other like $ @ % etc. | ||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||
| 86 | #sub glob { | ||||||||||||||||||||||||||||||||||
| 87 | # | ||||||||||||||||||||||||||||||||||
| 88 | #} | ||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | sub regex_glob { | ||||||||||||||||||||||||||||||||
|
Subroutine
regex_glob: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 91 | 0 | my ($glob, $opt) = @_; | |||||||||||||||||||||||||||||||||
| 92 | 0 | my @regex = $Zoidberg::CURRENT->{stringparser}->split(qr#/#, $glob); | |||||||||||||||||||||||||||||||||
| 93 | 0 | return _regex_glob_recurs(\@regex, '.', $opt); | |||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | sub _regex_glob_recurs { | ||||||||||||||||||||||||||||||||
|
Subroutine
_regex_glob_recurs: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||
| 97 | 0 | my ($regexps, $dir, $opt) = @_; | |||||||||||||||||||||||||||||||||
| 98 | 0 | my $regexp = shift @$regexps; | |||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | $regexp = "(?$opt:".$regexp.')' if $opt; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 100 | #debug "globbing for dir '$dir', regexp '$regexp', opt '$opt'\n"; | ||||||||||||||||||||||||||||||||||
| 101 | 0 | opendir DIR, $dir; | |||||||||||||||||||||||||||||||||
| 102 | my @matches = @$regexps | ||||||||||||||||||||||||||||||||||
| 103 | 0 | ? ( map { _regex_glob_recurs([@$regexps], $dir.'/'.$_, $opt) } | |||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | grep { -d $_ and $_ !~ /^\.{1,2}$/ and m/$regexp/ } readdir DIR ) | ||||||||||||||||||||||||||||||||
|
Condition0%0/6
-d $_ && !($_ =~ /^\.{1,2}$/) && /$regexp/
-d $_ and not $_ =~ /^\.{1,2}$/
MC/DC0%0/3
-d $_ && !($_ =~ /^\.{1,2}$/) && /$regexp/
-d $_
not $_ =~ /^\.{1,2}$/
/$regexp/
Truth table0%0/4
A and not(B) && C
A= -d $_B= not $_ =~ /^\.{1,2}$/C= /$regexp/
| |||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | : ( map "$dir/$_", grep { $_ !~ /^\.{1,2}$/ and m/$regexp/ } readdir DIR ) ; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
!($_ =~ /^\.{1,2}$/) && /$regexp/
MC/DC0%0/2
!($_ =~ /^\.{1,2}$/) && /$regexp/
!($_ =~ /^\.{1,2}$/)
/$regexp/
Truth table0%0/3
not(A) && B
A= !($_ =~ /^\.{1,2}$/)B= /$regexp/
| |||||||||||||||||||||||||||||||||||
| 106 | 0 | closedir DIR; | |||||||||||||||||||||||||||||||||
| 107 | 0 | return @matches; | |||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||
| 110 | 63 | 1; | |||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||
| 112 | __END__ | ||||||||||||||||||||||||||||||||||