« auto/share/dist/App-RegexFileUtils/ppt/cp.pl
↑ Summary
auto/share/dist/App-RegexFileUtils/ppt/touch.pl »
| 1 | package App::RegexFileUtils; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use File::ShareDir::Dist qw( dist_share ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use File::Which qw( which ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # ABSTRACT: use regexes with file utils like rm, cp, mv, ln | ||||||||||||||||||||||||||||
| 10 | 8 | our $VERSION = '0.08'; # VERSION | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | sub main { | ||||||||||||||||||||||||||
|
Subroutine
main: called CC=35 SCAR=42.5
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | 5 | my $class = shift; | |||||||||||||||||||||||||||
| 15 | 5 | my $mode = shift; | |||||||||||||||||||||||||||
| 16 | 5 | my $appname = $mode; | |||||||||||||||||||||||||||
| 17 | 5 | $mode =~ s!^.*/!!; | |||||||||||||||||||||||||||
| 18 | 5 | my @args = @_; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 5 | my @options = (); | |||||||||||||||||||||||||||
| 21 | 5 | my $verbose = 0; | |||||||||||||||||||||||||||
| 22 | 5 | my $do_hidden = 0; | |||||||||||||||||||||||||||
| 23 | 5 | my $re; | |||||||||||||||||||||||||||
| 24 | my $sub; | ||||||||||||||||||||||||||||
| 25 | 5 | my $modifiers; | |||||||||||||||||||||||||||
| 26 | 5 | my $modifiers_match = ''; | |||||||||||||||||||||||||||
| 27 | 5 | my $tr; | |||||||||||||||||||||||||||
| 28 | 5 | my $static_dest = 0; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 5 | ▶ | while(defined($args[0]) && $args[0] =~ /^-/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $args[0] and $args[0] =~ /^-/
MC/DC50%1/2
defined $args[0] and $args[0] =~ /^-/
defined $args[0]
$args[0] =~ /^-/
Truth table66%2/3
A and B
A= defined $args[0]B= $args[0] =~ /^-/
| |||||||||||||||||||||||||||||
| 31 | 1 | my $arg = shift @args; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | if($arg =~ /^--recmd$/) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 34 | 0 | $mode = shift @args; | |||||||||||||||||||||||||||
| 35 | 0 | $mode =~ s!^.*/!!; | |||||||||||||||||||||||||||
| 36 | } elsif($arg =~ /^--reverbose$/) { | ||||||||||||||||||||||||||||
| 37 | 0 | $verbose = 1; | |||||||||||||||||||||||||||
| 38 | } elsif($arg =~ /^--reall$/) { | ||||||||||||||||||||||||||||
| 39 | 0 | $do_hidden = 1; | |||||||||||||||||||||||||||
| 40 | } else { | ||||||||||||||||||||||||||||
| 41 | 1 | push @options, $arg; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 5 | my $dest = pop @args; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 5 | ▶ | unless(defined $dest) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | print STDERR "usage: $appname [options] [source files] /pattern/[substitution/]\n"; | |||||||||||||||||||||||||||
| 49 | 0 | print STDERR " $appname [options] /pattern/ /path/to/destination\n"; | |||||||||||||||||||||||||||
| 50 | 0 | print STDERR "\n"; | |||||||||||||||||||||||||||
| 51 | 0 | print STDERR "--recmd [command] change the behavior of the tool\n"; | |||||||||||||||||||||||||||
| 52 | 0 | print STDERR "--verbose print commands before they are executed\n"; | |||||||||||||||||||||||||||
| 53 | 0 | print STDERR "--reall include hidden (so called `dot') files\n"; | |||||||||||||||||||||||||||
| 54 | 0 | print STDERR "\n"; | |||||||||||||||||||||||||||
| 55 | 0 | print STDERR "all other arguments are passed to the system tool\n"; | |||||||||||||||||||||||||||
| 56 | 0 | exit; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 5 | my $orig_mode = $mode; | |||||||||||||||||||||||||||
| 60 | 5 | $mode =~ s/^re//; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 5 | my %modes = ( | |||||||||||||||||||||||||||
| 63 | 'mv' => 'mv', | ||||||||||||||||||||||||||||
| 64 | 'move' => 'mv', | ||||||||||||||||||||||||||||
| 65 | 'rename' => 'mv', | ||||||||||||||||||||||||||||
| 66 | 'cp' => 'cp', | ||||||||||||||||||||||||||||
| 67 | 'copy' => 'cp', | ||||||||||||||||||||||||||||
| 68 | 'ln' => 'ln', | ||||||||||||||||||||||||||||
| 69 | 'link' => 'ln', | ||||||||||||||||||||||||||||
| 70 | 'symlink' => 'ln', | ||||||||||||||||||||||||||||
| 71 | 'rm' => 'rm', | ||||||||||||||||||||||||||||
| 72 | 'remove' => 'rm', | ||||||||||||||||||||||||||||
| 73 | 'unlink' => 'rm', | ||||||||||||||||||||||||||||
| 74 | 'touch' => 'touch', | ||||||||||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 5 | ▶ | unshift @options, '-s' if $mode eq 'symlink'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 5 | $mode = $modes{$mode}; | |||||||||||||||||||||||||||
| 80 | 5 | ▶ | unless(defined $mode) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | print STDERR "unknown mode $orig_mode\n"; | |||||||||||||||||||||||||||
| 82 | 0 | exit; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 5 | my $no_dest = 0; | |||||||||||||||||||||||||||
| 86 | 5 | ▶ | if($mode eq 'touch' || $mode eq 'rm') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$mode eq "touch" or $mode eq "rm"
MC/DC100%2/2
$mode eq "touch" or $mode eq "rm"
$mode eq "touch"
$mode eq "rm"
Truth table100%3/3
A or B
A= $mode eq "touch"B= $mode eq "rm"
| |||||||||||||||||||||||||||||
| 87 | 2 | $no_dest = 1; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 5 | ▶ | if($dest =~ m!^(s|)/(.*)/(.*)/([ig]*)$!) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 91 | 2 | $re = $2; | |||||||||||||||||||||||||||
| 92 | 2 | $sub = $3; | |||||||||||||||||||||||||||
| 93 | 2 | $modifiers = $4; | |||||||||||||||||||||||||||
| 94 | 2 | ▶ | $modifiers_match = 'i' if $modifiers =~ /i/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 2 | ▶ | if($no_dest) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | print STDERR "substitution `$mode' doesn't make sense\n"; | |||||||||||||||||||||||||||
| 98 | 0 | exit; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | elsif($dest =~ m!tr/(.*)/(.*)/$!) { | ||||||||||||||||||||||||||||
| 104 | 0 | $tr = $1; | |||||||||||||||||||||||||||
| 105 | 0 | $sub = $2; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if($no_dest) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | print STDERR "translation `$mode' doesn't make sense\n"; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | elsif($dest =~ m!^(m|)/(.*)/([i]*)$!) { | ||||||||||||||||||||||||||||
| 113 | 2 | $re = $2; | |||||||||||||||||||||||||||
| 114 | 2 | $modifiers = $3; | |||||||||||||||||||||||||||
| 115 | 2 | $modifiers_match = $3; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | elsif(-d $dest) { | ||||||||||||||||||||||||||||
| 119 | 1 | my $src = pop @args; | |||||||||||||||||||||||||||
| 120 | 1 | ▶ | if($src =~ m!^(m|)/(.*)/([i]*)$!) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 1 | $static_dest = 1; | |||||||||||||||||||||||||||
| 122 | 1 | $re = $2; | |||||||||||||||||||||||||||
| 123 | 1 | $modifiers = $3; | |||||||||||||||||||||||||||
| 124 | 1 | $modifiers_match = $3; | |||||||||||||||||||||||||||
| 125 | } else { | ||||||||||||||||||||||||||||
| 126 | 0 | die "source is not a regex"; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | else { | ||||||||||||||||||||||||||||
| 131 | 0 | die "destination is not a directory or a regex"; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 5 | my @files = @args; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 5 | ▶ | if(@files ==0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 5 | ▶ | opendir(DIR, '.') || die "unable to opendir `.' $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 5 | @files = readdir(DIR); | |||||||||||||||||||||||||||
| 139 | 5 | closedir DIR; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 5 | for(@files) { | |||||||||||||||||||||||||||
| 143 | 35 | ▶ | next if /^\./ && !$do_hidden; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
/^\./ and not $do_hidden
MC/DC50%1/2
/^\./ and not $do_hidden
/^\./
not $do_hidden
Truth table66%2/3
A and not(B)
A= /^\./B= not $do_hidden
| |||||||||||||||||||||||||||||
| 144 | 25 | ▶ | next unless eval "/$re/$modifiers_match" || defined $tr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
eval "/$re/$modifiers_match" or defined $tr
MC/DC50%1/2
eval "/$re/$modifiers_match" or defined $tr
eval "/$re/$modifiers_match"
defined $tr
Truth table66%2/3
A or B
A= eval "/$re/$modifiers_match"B= defined $tr
| |||||||||||||||||||||||||||||
| 145 | 14 | my $old = $_; | |||||||||||||||||||||||||||
| 146 | 14 | my $new = $old; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 14 | my @cmd = ($mode, @options, $old); | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 14 | ▶ | if(defined $tr) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 151 | 0 | eval "\$new =~ tr/$tr/$sub/"; | |||||||||||||||||||||||||||
| 152 | } elsif(defined $sub) { | ||||||||||||||||||||||||||||
| 153 | 7 | eval "\$new =~ s/$re/$sub/$modifiers"; | |||||||||||||||||||||||||||
| 154 | } elsif($static_dest) { | ||||||||||||||||||||||||||||
| 155 | 2 | $new = $dest; | |||||||||||||||||||||||||||
| 156 | } else { | ||||||||||||||||||||||||||||
| 157 | 5 | ▶ | if($no_dest) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 5 | $new = ''; | |||||||||||||||||||||||||||
| 159 | } else { | ||||||||||||||||||||||||||||
| 160 | 0 | $new = '.'; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 14 | ▶ | push @cmd, $new unless $no_dest; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 14 | ▶ | print "% @cmd\n" if $verbose; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 14 | __PACKAGE__->_fix_path(\@cmd); | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 14 | system @cmd; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 14 | ▶ | if ($? == -1) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 172 | 0 | print STDERR "failed to execute: $!\n"; | |||||||||||||||||||||||||||
| 173 | 0 | exit 2; | |||||||||||||||||||||||||||
| 174 | } elsif ($? & 127) { | ||||||||||||||||||||||||||||
| 175 | 0 | print STDERR "child died with signal ", $? & 127, "\n"; | |||||||||||||||||||||||||||
| 176 | } elsif($? >> 8) { | ||||||||||||||||||||||||||||
| 177 | 1 | print "child exited with value ", $? >> 8, "\n"; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 8 | ▶ | use constant _share_dir => do { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||
| 183 | 8 | my $path; | |||||||||||||||||||||||||||
| 184 | 8 | $path = dist_share('App-RegexFileUtils'); | |||||||||||||||||||||||||||
| 185 | 8 | ▶ | die 'can not find share directory' unless $path && -d "$path/ppt"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$path and -d "$path/ppt"
MC/DC0%0/2
$path and -d "$path/ppt"
$path
-d "$path/ppt"
Truth table33%1/3
A and B
A= $pathB= -d "$path/ppt"
| |||||||||||||||||||||||||||||
| 186 | 8 | $path; | |||||||||||||||||||||||||||
| 187 | 8 | }; | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | sub _fix_path | ||||||||||||||||||||||||||||
| 190 | 14 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_fix_path: called CC=3 SCAR=17.3
| |||||||||||||||||||||||||||||
| 191 | 14 | my($class, $cmd) = @_; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 14 | ▶ | return unless $^O eq 'MSWin32'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 0 | ▶ | return if which($cmd->[0]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 0 | $cmd->[0] = File::Spec->catfile( | |||||||||||||||||||||||||||
| 198 | App::RegexFileUtils->_share_dir, | ||||||||||||||||||||||||||||
| 199 | 'ppt', $cmd->[0] . '.pl', | ||||||||||||||||||||||||||||
| 200 | ); | ||||||||||||||||||||||||||||
| 201 | 0 | unshift @$cmd, $^X; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 8 | 1; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | __END__ | ||||||||||||||||||||||||||||