ShellKit/TextFile.pm untested

filter: Statementstmt 0.0% 0 / 85 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 178 CC 27 SCAR 53.9
CC
27
Cov
0%
replace_text
380.0
show_file
56.0
find_text
12.0
CRAP 218.9
1 package Term::ShellKit::TextFile;
2  
3 require Term::ShellKit;
4  
5 ######################################################################
6  
7 sub file_slurp {
8 my $file = shift;
9 # warn "Reading file: $file\n";
10 local *FILE;
11 open FILE, "$file" or die "Can't open $file: $!";
12 local $/ = undef;
13 return <FILE>;
14 }
15  
16 sub file_splurt {
17 my $file = shift;
18 # warn "Writing file: $file \n";
19 local *FILE;
20 open FILE, ">$file" or die "Can't write to $file: $!";
21 print FILE shift();
22 }
23  
24 ######################################################################
25  
26 sub show_file {
27 my $file = shift;
28 my %opts = @_;
29
30 if ( exists $opts{number} ) {
31 $opts{number} = ( $opts{number} and $opts{number} !~ /^n/ ) ? 1 : 0
32 }
33
34 if ( $opts{line} ) {
35 $opts{line} =~ s/\D//g;
36 $opts{window} ||= 2;
37 } else {
38 warn "No windows without lines" if $opts{window};
39 }
40
41 my $text = file_slurp( $file );
42 my @lines = split /\n/, $text;
43  
44 if ( $opts{number} ) {
45 my $i = 1;
46 foreach ( @lines ) { $_ = ($i ++) . ": " . $_ }
47 }
48 if ( $opts{line} ) {
49 @lines = @lines[ $opts{line} - $opts{window} .. $opts{line} + $opts{window} ];
50 }
51 @lines;
52 }
53  
54 ######################################################################
55  
56 sub file_grep {
57 my $str = shift;
58 $shell->shell_auto_command( qw( execute find ), @_, qw( -type f -exec egrep ), $str, '{}', '/dev/null', "\\;" );
59 }
60  
61 ######################################################################
62  
63 use Cwd;
64 use File::Find;
65  
66 sub find_text {
67 my $term = shift(@ARGV);
68 $term = "\Q$term" if $q;
69
70 my $dirs = join(' ', @ARGV)
71 || join(' ', map "/opt/bdev/$_", qw( lib htdocs Manager includes bin conf ))
72 || '.';
73
74 my @skips = qw( CVS .dir5_0.wmd .#* *.jpg *.jpeg *.gif *.bmp );
75
76 my $command = "find $dirs \\( " . join(' -o ', map "-name '$_'", @skips ) . " \\) -prune -o -type f -exec egrep '$term' {} /dev/null \\;";
77
78 print "$0: $command\n\n" if ( $v );
79
80 exec( $command );
81 }
82  
83 sub replace_text {
84 my $term = shift(@_);
85
86 $term = "(\Q$term\E)" unless ( $p );
87
88 $term = "(?i)$term" if ( $i );
89 $term = "(?e)$term" if ( $e );
90
91 my $rplc = shift(@_);
92
93 my $cwd = cwd();
94 my @targets = scalar(@_) ? map ( "$cwd/$_", @_ ) : $cwd;
95
96 print "$0: {$term} => {$rplc} in ". join(', ', @targets) . "\n";
97
98 my $tempfile = File::Name->new( '/tmp/tmp_replace_text_buffer' );
99
100 finddepth( sub {
101 my $filename = $File::Find::name;
102 if ( $filename =~ m{/CVS/} ) {
103 next;
104 }
105 if ( $filename =~ m{/\.} ) {
106 next;
107 }
108 if ( -d $filename ) {
109 next;
110 }
111 unless ( -R $filename ) {
112 warn "Skipping $filename, not readable...\n" if ( $v );
113 next;
114 }
115 warn "Scanning $filename...\n" if ( $v );
116
117 my $contents = file_slurp($filename);
118 next unless $contents =~ /$term/;
119
120 unless ( $f ) {
121 while ( 1 ) {
122 print "$0: Change $filename? (Yes/no/view/all/quit) ";
123 my $in = <STDIN>;
124 if ( ! $in or $in =~ /^y/i ) {
125 last;
126 } elsif ( $in =~ /^n/i ) {
127 return;
128 } elsif ( $in =~ /^a/i ) {
129 ++ $f;
130 last;
131 } elsif ( $in =~ /^q/i ) {
132 exit;
133 } elsif ( $in =~ /^v/i ) {
134 my $temp = $contents;
135 $temp =~ s{$term}{$rplc}g;
136 $tempfile->set_text_contents($temp);
137 system("diff -C1 $filename $$tempfile | more");
138 }
139 }
140 }
141
142 $contents =~ s{$term}{$rplc}g;
143 warn "Changing $filename...\n" if ( $v );
144 splurt_file($filename, $contents);
145
146 1;
147 }, @targets );
148 }
149  
150 1;
151  
152 ######################################################################
153  
154 =head1 COMMANDS
155  
156 The following commands are available.
157  
158  
159 =head2 show_file (alias at)
160  
161 Show the contents of a file.
162  
163 =over 4
164  
165 =item *
166  
167 show_file I<filename>: Display file contents.
168  
169 =item *
170  
171 show_file I<filename> line I<lineno> (window I<lines>): show a window of I<lines>, or by default 3, around the specific I<lineno>.
172  
173 =item *
174  
175 show_file I<filename> number yes: show line numbers
176  
177 =back
178  
179  
180 =head2 file_grep
181  
182 Recursive grep through directories.
183  
184 =over 4
185  
186 =item *
187  
188 file_grep I<expr> I<files_or_directories...>
189  
190 =back