| 1 | package File::Irenamer; | ||||||||||||||||||||
| 2 | ############################################################################## | ||||||||||||||||||||
| 3 | # $Id: Irenamer.pm,v 1.3 2004/11/07 13:59:23 bheckel Exp bheckel $ | ||||||||||||||||||||
| 4 | # | ||||||||||||||||||||
| 5 | # $Log: Irenamer.pm,v $ | ||||||||||||||||||||
| 6 | # Revision 1.3 2004/11/07 13:59:23 bheckel | ||||||||||||||||||||
| 7 | # Adjust docs | ||||||||||||||||||||
| 8 | # | ||||||||||||||||||||
| 9 | # Revision 1.2 2004/11/07 02:30:36 bheckel | ||||||||||||||||||||
| 10 | # Cleaned up release for initial upload to CPAN | ||||||||||||||||||||
| 11 | # | ||||||||||||||||||||
| 12 | # Revision 1.1 2004/11/07 02:29:39 bheckel | ||||||||||||||||||||
| 13 | # Initial revision | ||||||||||||||||||||
| 14 | # | ||||||||||||||||||||
| 15 | ############################################################################## | ||||||||||||||||||||
| 16 | 1 | ▶ | use 5.008002; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 1 | ▶ | use Getopt::Std; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 1 | ▶ | use File::Find; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 1 | require Exporter; | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||
| 25 | 1 | our %EXPORT_TAGS = ( 'all' => [ qw( | |||||||||||||||||||
| 26 | ) ] ); | ||||||||||||||||||||
| 27 | 1 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||
| 28 | 1 | our @EXPORT = qw( | |||||||||||||||||||
| 29 | InteractiveRename | ||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||
| 31 | 1 | our $VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/o); | |||||||||||||||||||
| 32 | 1 | our %opts; | |||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 1 | getopts('drv', \%opts); | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | # Main | ||||||||||||||||||||
| 38 | 0 | ▶ | sub InteractiveRename { | ||||||||||||||||||
|
Subroutine
InteractiveRename: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||
| 39 | 0 | my (@options) = @_; | |||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 0 | ▶ | $opts{d} = 1 if grep { /debug/ } @options; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | 0 | ▶ | $opts{r} = 1 if grep { /recurse/ } @options; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | 0 | ▶ | $opts{v} = 1 if grep { /verbose/ } @options; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 0 | ▶ | if ( $ARGV[0] ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | # Remove user's trailing slash if necessary. | ||||||||||||||||||||
| 47 | 0 | $ARGV[0] =~ s#/$##; | |||||||||||||||||||
| 48 | } else { | ||||||||||||||||||||
| 49 | # Default to current working dir. | ||||||||||||||||||||
| 50 | 0 | $ARGV[0] = '.'; | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 0 | ▶ | print "DEBUG \$ARGV[0] is $ARGV[0]\n" if $opts{d}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 0 | RenameFiles(ParseChanges(EditFilesInEditor(ReadFiles($ARGV[0])))); | |||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 0 | exit 0; | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 1 | my @FILES; | |||||||||||||||||||
| 62 | 0 | ▶ | sub Wanted { | ||||||||||||||||||
|
Subroutine
Wanted: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 63 | 0 | ▶ | -f $_ and push @FILES, $File::Find::name; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 0 | ▶ | sub ReadFiles { | ||||||||||||||||||
|
Subroutine
ReadFiles: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||
| 68 | 0 | my $dir = shift; | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 0 | @FILES = (); | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 0 | ▶ | die "ERROR: no input directory in ReadFiles(). Exiting.\n" if ! $dir; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 0 | ▶ | if ( $opts{r} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | 0 | find \&Wanted, $dir; | |||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||
| 77 | 0 | ▶ | opendir DH, "$dir" or die "Error: $0: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | 0 | ▶ | @FILES = grep { !/^..?$/ && !-d } map "$dir/$_", readdir DH; | ||||||||||||||||||
|
Condition0%0/3
/^..?$/ || -d $_
MC/DC0%0/2
/^..?$/ || -d $_
/^..?$/
-d $_
Truth table0%0/3
A || B
A= /^..?$/B= -d $_
| |||||||||||||||||||||
| 79 | 0 | close DH; | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 0 | return @FILES; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 0 | ▶ | sub EditFilesInEditor { | ||||||||||||||||||
|
Subroutine
EditFilesInEditor: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||
| 87 | 0 | my @orignames = @_; | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 0 | my $rand = int(rand(42)) + time + $$; | |||||||||||||||||||
| 90 | 0 | my $tmpfile = "$ENV{TMP}/renamer-pl.$rand.tmp"; | |||||||||||||||||||
| 91 | 0 | ▶ | open TMPF, ">$tmpfile" || die "Can't create $tmpfile in $ENV{TMP}: $!\n"; | ||||||||||||||||||
|
Condition0%0/2
">$tmpfile" || die("Can't create $tmpfile in $ENV{'TMP'}: $!\n")
MC/DC0%0/1
">$tmpfile" || die("Can't create $tmpfile in $ENV{'TMP'}: $!\n")
">$tmpfile"
Truth table0%0/2
A
A= ">$tmpfile"
| |||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 0 | print TMPF <<"EOT"; | |||||||||||||||||||
| 94 | # | ||||||||||||||||||||
| 95 | # Changes to these filenames will be made after exiting this | ||||||||||||||||||||
| 96 | # tempfile. Delete all lines to cancel. | ||||||||||||||||||||
| 97 | # | ||||||||||||||||||||
| 98 | EOT | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 0 | for ( @orignames ) { | |||||||||||||||||||
| 101 | 0 | print TMPF "$_\n"; | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 0 | close TMPF; | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 0 | system "$ENV{EDITOR} $tmpfile"; | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | # We make changes to $tmpfile within our favorite editor...Vim, right? ;-) | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 0 | ▶ | open TMPF, "<$tmpfile" || die "Can't open $tmpfile : $!\n"; | ||||||||||||||||||
|
Condition0%0/2
"<$tmpfile" || die("Can't open $tmpfile : $!\n")
MC/DC0%0/1
"<$tmpfile" || die("Can't open $tmpfile : $!\n")
"<$tmpfile"
Truth table0%0/2
A
A= "<$tmpfile"
| |||||||||||||||||||||
| 111 | 0 | my @newnames = <TMPF>; | |||||||||||||||||||
| 112 | 0 | @newnames = grep { !/^#/ } @newnames; | |||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 0 | ▶ | if ( $opts{d} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | 0 | close TMPF; | |||||||||||||||||||
| 116 | 0 | print "DEBUG: $tmpfile\n"; | |||||||||||||||||||
| 117 | } else { | ||||||||||||||||||||
| 118 | # TODO only if user does not cancel | ||||||||||||||||||||
| 119 | 0 | ▶ | close TMPF && unlink $tmpfile; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 0 | return \@orignames, \@newnames; | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | # Parse saved editor diffs into a hash. | ||||||||||||||||||||
| 127 | 0 | ▶ | sub ParseChanges { | ||||||||||||||||||
|
Subroutine
ParseChanges: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||
| 128 | 0 | my $origarrref = shift; | |||||||||||||||||||
| 129 | 0 | my $newarrref = shift; | |||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 0 | ▶ | if ( scalar @$origarrref != scalar @$newarrref ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 132 | 0 | die "ERROR: Problem with parsing filename change file.\nDifferent " . | |||||||||||||||||||
| 133 | "number of files in old vs. new. Exiting.\n"; | ||||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 0 | my %same = map { $_, 1 } @$origarrref; | |||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 0 | my %torename; | |||||||||||||||||||
| 139 | 0 | my $i = 0; | |||||||||||||||||||
| 140 | 0 | foreach my $f ( @$newarrref ) { | |||||||||||||||||||
| 141 | 0 | chomp $f; # take away newlines used previously for easier editing | |||||||||||||||||||
| 142 | 0 | ▶ | if ( ! $same{$f} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 143 | 0 | $torename{@$origarrref[$i]} = @$newarrref[$i]; | |||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | 0 | $i++; | |||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 0 | return \%torename; | |||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | 0 | ▶ | sub RenameFiles { | ||||||||||||||||||
|
Subroutine
RenameFiles: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||||||||||
| 153 | 0 | my $hr = shift; | |||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 0 | ▶ | if ( ! keys %$hr ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 156 | 0 | ▶ | die "No changes requested. Exiting.\n" if $opts{v}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 157 | } else { | ||||||||||||||||||||
| 158 | 0 | ▶ | print "We are about to rename:\n" if $opts{v}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 159 | } | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | 0 | foreach ( sort keys %$hr ) { | |||||||||||||||||||
| 162 | 0 | ▶ | print " FROM:\t$_\n TO:\t$$hr{$_}\n\n" if $opts{v}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 163 | } | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | 0 | ▶ | if ( $opts{v} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 166 | 0 | print "Proceed? [yes/no] "; | |||||||||||||||||||
| 167 | 0 | ▶ | if ( <STDIN> !~ /yes/i ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 168 | 0 | die "cancelled\n"; | |||||||||||||||||||
| 169 | } | ||||||||||||||||||||
| 170 | } | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | # It's possible to try to rename a file into a subdir that doesn't exist, | ||||||||||||||||||||
| 173 | # etc. Can't stop all stupidity but can at least whine about it. | ||||||||||||||||||||
| 174 | 0 | my $rc = 1; | |||||||||||||||||||
| 175 | 0 | foreach ( sort keys %$hr ) { | |||||||||||||||||||
| 176 | 0 | $rc = rename $_, $$hr{$_}; | |||||||||||||||||||
| 177 | 0 | ▶ | print "Potential error during rename of $_\n" if $rc ne 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 178 | } | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 0 | ▶ | if ( $opts{v} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 181 | 0 | print "Directory contents after committed changes:\n"; | |||||||||||||||||||
| 182 | 0 | map { print " $_\n" } ReadFiles($ARGV[0]); | |||||||||||||||||||
| 183 | } | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | 0 | return 0; | |||||||||||||||||||
| 186 | } | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | 1 | 1; | |||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | __END__ | ||||||||||||||||||||