| 1 | #!/usr/local/bin/perl | ||
| 2 | # rep.pl doom@kzsu.stanford.edu | ||
| 3 | # 13 May 2010 | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | rep.pl - perform a series of find and replaces | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | rep.pl --backup <backup_file> --substitutions <filename> --target <file_to_act_on> | ||
| 12 | |||
| 13 | rep.pl -b <backup_file> -f <file_to_act_on> 's/foo/bar/' | ||
| 14 | |||
| 15 | =head2 USAGE | ||
| 16 | |||
| 17 | rep.pl -[options] [arguments] | ||
| 18 | |||
| 19 | Options: | ||
| 20 | |||
| 21 | -s substitutions list file | ||
| 22 | --substitutions same | ||
| 23 | -f target file name to be modified | ||
| 24 | --target same | ||
| 25 | -B backup file name | ||
| 26 | --backup same | ||
| 27 | |||
| 28 | --trial report change metadata without modifying target file | ||
| 29 | |||
| 30 | -d debug messages on | ||
| 31 | --debug same | ||
| 32 | -h help (show usage) | ||
| 33 | -v show version | ||
| 34 | --version show version | ||
| 35 | |||
| 36 | -T make no changes to the input file, but report | ||
| 37 | --trailrun metadata for changes that would've been made. | ||
| 38 | |||
| 39 | |||
| 40 | =head1 DESCRIPTION | ||
| 41 | |||
| 42 | B<rep.pl> is a script which does finds and replaces on a file, | ||
| 43 | and records the beginning and end points of the modified | ||
| 44 | strings. | ||
| 45 | |||
| 46 | It is intended to act as an intermediary between Emacs::Rep | ||
| 47 | and the emacs lisp code which drives the "rep" process. | ||
| 48 | |||
| 49 | Emacs can then use the recorded locations to highlight the | ||
| 50 | changed regions, and it can use information about what was | ||
| 51 | replaced to perform undo operations. | ||
| 52 | |||
| 53 | The elisp code must choose a unique backup file name. This makes | ||
| 54 | it possible to do reverts of an entire run of substitutions. | ||
| 55 | |||
| 56 | The script returns a data dump of the history of the changes to | ||
| 57 | the text. This is in the form of an array of hashes, serialized | ||
| 58 | using JSON. | ||
| 59 | |||
| 60 | The array is in the order in which the individual changes took | ||
| 61 | place. Each row has fields: | ||
| 62 | |||
| 63 | pass the number of the "pass" through the file | ||
| 64 | (one pass per substitution command) | ||
| 65 | beg begin point of changed string | ||
| 66 | end end point of changed string | ||
| 67 | delta the change in string length | ||
| 68 | orig the original string that was replaced | ||
| 69 | rep the replacement string that was substituted | ||
| 70 | pre up to ten characters found before the replacement | ||
| 71 | post up to ten characters found after the replacement | ||
| 72 | |||
| 73 | Note: in "beg" and "end" characters are counted from the | ||
| 74 | beginning of the text, starting with 1. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | use warnings; | ||
| 79 | use strict; | ||
| 80 | $|=1; | ||
| 81 | use Carp; | ||
| 82 | use Data::Dumper; | ||
| 83 | |||
| 84 | use File::Path qw( mkpath ); | ||
| 85 | use File::Basename qw( fileparse basename dirname ); | ||
| 86 | use File::Copy qw( copy move ); | ||
| 87 | use Fatal qw( open close mkpath copy move ); | ||
| 88 | use Cwd qw( cwd abs_path ); | ||
| 89 | use Env qw( HOME ); | ||
| 90 | use Getopt::Long qw( :config no_ignore_case bundling ); | ||
| 91 | use FindBin qw( $Bin ); | ||
| 92 | use Emacs::Rep qw( :all ); | ||
| 93 | use JSON; # encode_json | ||
| 94 | |||
| 95 | our $VERSION = 1.00; | ||
| 96 | my $prog = basename($0); | ||
| 97 | |||
| 98 | my $DEBUG = 0; # TODO set default to 0 when in production | ||
| 99 | my ( $locs_temp_file, $reps_file, $backup_file, $target_file, $trialrun_flag, | ||
| 100 | $elisp_version ); | ||
| 101 | GetOptions ("d|debug" => \$DEBUG, | ||
| 102 | "v|version" => sub{ say_version(); }, | ||
| 103 | "h|?|help" => sub{ say_usage(); }, | ||
| 104 | "s|substitutions=s" => \$reps_file, | ||
| 105 | "b|backup=s" => \$backup_file, | ||
| 106 | "f|target=s" => \$target_file, | ||
| 107 | "T|trialrun" => \$trialrun_flag, | ||
| 108 | "V|check_versions=s" =>\$elisp_version, | ||
| 109 | ) or say_usage(); | ||
| 110 | |||
| 111 | if( $elisp_version ) { | ||
| 112 | my $pl_version = $VERSION; | ||
| 113 | my $report = | ||
| 114 | check_versions( $elisp_version, $pl_version ); | ||
| 115 | if( $report =~ m{ \A Warning: \s+ }xms ) { | ||
| 116 | print $report, "\n"; | ||
| 117 | exit; | ||
| 118 | } else { | ||
| 119 | exit 1; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | |||
| 124 | # get a series of finds and replaces | ||
| 125 | # either from the substitutions file, | ||
| 126 | # or from command-line (a series of strings from @ARGV), | ||
| 127 | |||
| 128 | my $reps_text; | ||
| 129 | if( $reps_file ) { | ||
| 130 | undef $/; | ||
| 131 | open my $fh, '<', $reps_file or croak "$!"; | ||
| 132 | $reps_text = <$fh>; | ||
| 133 | } else { | ||
| 134 | $reps_text = join( "\n", @ARGV ); | ||
| 135 | } | ||
| 136 | |||
| 137 | # process the find_and_reps into an array of find and replace | ||
| 138 | # pairs (modifiers get moved inside the find.) | ||
| 139 | my $find_replaces_aref; | ||
| 140 | eval { | ||
| 141 | $find_replaces_aref = | ||
| 142 | parse_perl_substitutions( \$reps_text ); | ||
| 143 | }; | ||
| 144 | if ($@) { | ||
| 145 | carp "Problem parsing perl substitutions: $@"; | ||
| 146 | exit; | ||
| 147 | } | ||
| 148 | |||
| 149 | unless (-e $target_file) { | ||
| 150 | croak "file not found: $target_file"; | ||
| 151 | } | ||
| 152 | my $backup_file_dir = dirname( $backup_file ); | ||
| 153 | unless (-d $backup_file_dir) { | ||
| 154 | croak "directory does not exist: $backup_file_dir"; | ||
| 155 | } | ||
| 156 | |||
| 157 | if ( $trialrun_flag ) { | ||
| 158 | # During a trial run, we work on the copy of the input file, | ||
| 159 | # and never modify the original | ||
| 160 | copy( $target_file, $backup_file ) or | ||
| 161 | croak "can't copy $target_file to $backup_file: $!"; | ||
| 162 | } else { | ||
| 163 | rename( $target_file, $backup_file ) or | ||
| 164 | croak "can't move $target_file to $backup_file: $!"; | ||
| 165 | } | ||
| 166 | |||
| 167 | my $text; | ||
| 168 | { undef $/; | ||
| 169 | open my $fin, '<', $backup_file or croak "$!"; | ||
| 170 | $text = <$fin>; | ||
| 171 | close( $fin ); | ||
| 172 | } | ||
| 173 | |||
| 174 | # Apply the finds and replaces to text, recording the | ||
| 175 | # change meta-data | ||
| 176 | my $change_metadata_aref; | ||
| 177 | eval { | ||
| 178 | $change_metadata_aref = | ||
| 179 | do_finds_and_reps( \$text, $find_replaces_aref ); | ||
| 180 | }; | ||
| 181 | if ($@) { | ||
| 182 | carp "Problem applying finds and replaces: $@"; | ||
| 183 | rename( $backup_file, $target_file ); # rollback! | ||
| 184 | } else { | ||
| 185 | if ( not( $trialrun_flag ) ) { # then don't modify input file | ||
| 186 | open my $fout, '>', $target_file or croak "$!"; | ||
| 187 | print {$fout} $text; | ||
| 188 | close( $fout ); | ||
| 189 | } | ||
| 190 | # serialize the data to pass to emacs | ||
| 191 | my $chg_md_json = encode_json( $change_metadata_aref ); | ||
| 192 | print $chg_md_json; | ||
| 193 | } | ||
| 194 | |||
| 195 | ### end main, into the subs | ||
| 196 | |||
| 197 | sub say_usage { | ||
| 198 | my $usage=<<"USEME"; | ||
| 199 | $prog <-options> <arguments> | ||
| 200 | |||
| 201 | Options: | ||
| 202 | |||
| 203 | -s substitutions list file | ||
| 204 | --substitutions same | ||
| 205 | -f target file name to be modified | ||
| 206 | --target same | ||
| 207 | -b backup file name | ||
| 208 | --backup same | ||
| 209 | |||
| 210 | -d debug messages on | ||
| 211 | --debug same | ||
| 212 | -h help (show usage) | ||
| 213 | -v show version | ||
| 214 | --version show version | ||
| 215 | |||
| 216 | Typical use: | ||
| 217 | |||
| 218 | perl rep.pl -b "/tmp/edit_this.txt.bak" -f "edit_this.txt" 's/foo/bar/' | ||
| 219 | |||
| 220 | perl rep.pl --backup <backup_file> --substitutions <filename> --target <file_to_act_on> | ||
| 221 | |||
| 222 | USEME | ||
| 223 | print "$usage\n"; | ||
| 224 | exit; | ||
| 225 | } | ||
| 226 | |||
| 227 | sub say_version { | ||
| 228 | print "Running $prog version: $VERSION\n"; | ||
| 229 | exit 1; | ||
| 230 | } | ||
| 231 | |||
| 232 | |||
| 233 | |||
| 234 | __END__ |