blib/lib/Zoidberg/Utils/FileSystem.pm

filter: Statementstmt 56.3% 40 / 71 Branchbran 34.6% 9 / 26 Conditioncond 7.6% 1 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 63.6% 7 / 11 Podpod 60.0% 3 / 5 totaltotal 45.1% 60 / 133 CC 19 SCAR 28.2
CC
19
Cov
45%
_regex_glob_recurs
42.0
unique_file
30.0
path
8.5
CRAP 16.8
« blib/lib/Zoidberg/PluginHash.pm ↑ Summary blib/lib/Zoidberg/Shell.pm »
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'})
l!l
780
MC/DC0%0/1
shift() || (return $ENV{'PWD'})
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
19 78 ▶ my $refer = $_[0] ? path(shift @_) : $ENV{PWD}; # possibly recurs
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 78 78
20 78 $refer =~ s/\/$//;
21 78 $string =~ s{/+}{/}; # ever tried using $::main::main::main::something ?
22 78 ▶ unless ($string =~ m{^/}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($string =~ m[^/]) 62 16 78
23 62 ▶ if ( $string =~ /^~([^\/]*)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($string =~ m[^~([^/]*)]) { } 0 62 62
elsif ($string =~ s[^\.(\.+)(/|$)][]) { } 45 17 62
24 0 ▶ if ($1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1) { } 0 0 0
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
BranchTrueFalseTotal
@_ ? : 17 0 17
47 17 ▶ $dir =~ s#/$## unless $dir eq '/';
Branch: 1 branch
BranchTrueFalseTotal
unless $dir eq "/" 17 0 17
48 17 $dir = path($dir);
49  
50 17 ▶ opendir DIR, $dir or die "could not open dir: $dir";
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $dir or die "could not open dir: $dir" 17 0 17
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'
l!l
00
MC/DC0%0/1
pop() || 'untitledXXXX'
pop()
Truth table0%0/2
A
A= pop()
Aresult
11
00
62 0 my ($file, $number) = ($string, 0);
63 0 $file =~ s/XXXX/$number/;
64 0 ▶ while ( -e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 0 0 0
65 0 ▶ if ($number > 256) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number > 256) { } 0 0 0
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
BranchTrueFalseTotal
unless defined $file 0 0 0
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
BranchTrueFalseTotal
if $opt 0 0 0
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/
!ll&&!rl&&r
000
-d $_ and not $_ =~ /^\.{1,2}$/
!ll&&!rl&&r
000
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/
ABCresult
0XX0
10X0
1100
1111
105 0 ▶ : ( map "$dir/$_", grep { $_ !~ /^\.{1,2}$/ and m/$regexp/ } readdir DIR ) ;
Branch: 1 branch
BranchTrueFalseTotal
@$regexps ? : 0 0 0
Condition0%0/3
!($_ =~ /^\.{1,2}$/) && /$regexp/
!ll&&!rl&&r
000
MC/DC0%0/2
!($_ =~ /^\.{1,2}$/) && /$regexp/
!($_ =~ /^\.{1,2}$/) /$regexp/
Truth table0%0/3
not(A) && B
A= !($_ =~ /^\.{1,2}$/)B= /$regexp/
ABresult
0X0
100
111
106 0 closedir DIR;
107 0 return @matches;
108 }
109  
110 63 1;
111  
112 __END__
« blib/lib/Zoidberg/PluginHash.pm ↑ Summary blib/lib/Zoidberg/Shell.pm »