| 1 |
#!/usr/bin/perl -w |
| 2 |
# |
| 3 |
# Substitutes one string for another in a set of files |
| 4 |
# |
| 5 |
# $Id: install-N-sub 41 2003-08-20 15:36:48Z brianfinley $ |
| 6 |
# |
| 7 |
# ================================================================ |
| 8 |
# |
| 9 |
# Description: |
| 10 |
# |
| 11 |
# This program substitutes one string for another string in a |
| 12 |
# set of files. What to substitute is based on the first argument |
| 13 |
# to the program. The rest of the arguments are files on which to |
| 14 |
# do the substitution. |
| 15 |
# |
| 16 |
# ================================================================ |
| 17 |
# |
| 18 |
# Substitution Code by: |
| 19 |
# Alan Bailey, [email protected] |
| 20 |
# |
| 21 |
# Other Code by: |
| 22 |
# Brian Elliott Finley <[email protected]> (2003.05.12) |
| 23 |
# |
| 24 |
# ================================================================ |
| 25 |
# |
| 26 |
|
| 27 |
use strict; |
| 28 |
use Getopt::Long; |
| 29 |
use File::Basename; |
| 30 |
use File::Copy; |
| 31 |
use File::Path; |
| 32 |
|
| 33 |
(my $program = $0) =~ s:.*/::; |
| 34 |
my $VERSION = "0.1.1"; |
| 35 |
|
| 36 |
# set version information |
| 37 |
my $version_info = <<"EOF"; |
| 38 |
$program v$VERSION |
| 39 |
EOF |
| 40 |
|
| 41 |
# set help information |
| 42 |
my $help_info = $version_info . <<"EOF"; |
| 43 |
|
| 44 |
Usage: $program --config FILE [OPTION]... FILE... |
| 45 |
|
| 46 |
If --install and --dest are not specified, substitutions are applied |
| 47 |
to FILE(s) directly. |
| 48 |
|
| 49 |
If --install and --dest are specified, then FILE(s) are left alone |
| 50 |
where they reside, and substitutions are only made to the copies of |
| 51 |
FILE(s) as they are being installed. |
| 52 |
|
| 53 |
Options: (options can be presented in any order) |
| 54 |
|
| 55 |
--help Display this output. |
| 56 |
--version Display version and copyright information. |
| 57 |
--config FILE Substitution configuration file. Format is: |
| 58 |
|
| 59 |
KEY_STRING what_to_put_in_it's_place |
| 60 |
|
| 61 |
--install 'ARGS' Arguments to be passed to the install program. |
| 62 |
Example: --install '-m 755' |
| 63 |
See \"man install\" for details. |
| 64 |
|
| 65 |
--dest [FILE|DIR] Destination file or directory name. |
| 66 |
|
| 67 |
EOF |
| 68 |
|
| 69 |
# interpret command line options |
| 70 |
GetOptions( |
| 71 |
"help" => \my $help, |
| 72 |
"version" => \my $version, |
| 73 |
"config=s" => \my $config_file, |
| 74 |
"install=s" => \my $install_args, |
| 75 |
"dest=s" => \my $dest, |
| 76 |
) or die qq($help_info); |
| 77 |
|
| 78 |
# if requested, print help information |
| 79 |
if($help) { |
| 80 |
print qq($help_info); |
| 81 |
exit 0; |
| 82 |
} |
| 83 |
|
| 84 |
# if requested, print version and copyright information |
| 85 |
if($version) { |
| 86 |
print qq($version_info); |
| 87 |
exit 0; |
| 88 |
} |
| 89 |
|
| 90 |
# if requested, print version and copyright information |
| 91 |
if(!$config_file) { |
| 92 |
print qq($help_info); |
| 93 |
exit 1; |
| 94 |
} |
| 95 |
|
| 96 |
# install and dest must both be specified, or we conservatively exit. |
| 97 |
if(($install_args) and (!$dest)) { |
| 98 |
print qq($help_info); |
| 99 |
exit 1; |
| 100 |
} |
| 101 |
if(($dest) and (!$install_args)) { |
| 102 |
print qq($help_info); |
| 103 |
exit 1; |
| 104 |
} |
| 105 |
|
| 106 |
my @files = @ARGV; |
| 107 |
|
| 108 |
if(! -r $config_file) { |
| 109 |
die "$program: Cannot read $config_file\n"; |
| 110 |
} |
| 111 |
|
| 112 |
my %subs = parse_config_file($config_file); |
| 113 |
|
| 114 |
if(($dest) and ($install_args)) { |
| 115 |
|
| 116 |
my @tmp_files; |
| 117 |
my $tmp_dir = "/tmp/.$program.$$"; |
| 118 |
|
| 119 |
if((! -d $dest) and (@files > 1)) { |
| 120 |
print "FATAL: You have multiple source files, but have specified one of the\n"; |
| 121 |
print " following as your destination:\n"; |
| 122 |
print " - a non-existent destination directory\n"; |
| 123 |
print " - a single destination file\n"; |
| 124 |
exit 1; |
| 125 |
} |
| 126 |
|
| 127 |
foreach my $file (@files) { |
| 128 |
# |
| 129 |
# get list of tmp file names |
| 130 |
# |
| 131 |
my $tmp_file = $tmp_dir . "/" . $file; |
| 132 |
push @tmp_files, $tmp_file; |
| 133 |
|
| 134 |
# |
| 135 |
# copy files into tmp dir |
| 136 |
# |
| 137 |
my $tmp_path = dirname($tmp_file); |
| 138 |
eval { mkpath($tmp_path) }; |
| 139 |
if ($@) { |
| 140 |
print "Couldn't create $tmp_path $@"; |
| 141 |
} |
| 142 |
copy($file, $tmp_path) or die("Couldn't copy $file to $tmp_path!"); |
| 143 |
} |
| 144 |
|
| 145 |
# |
| 146 |
# Do substitutions on the tmp_files |
| 147 |
# |
| 148 |
run_through_files(@tmp_files); |
| 149 |
|
| 150 |
foreach my $tmp_file (@tmp_files) { |
| 151 |
# |
| 152 |
# install file(s) |
| 153 |
# |
| 154 |
my $cmd = "install $install_args $tmp_file $dest"; |
| 155 |
!system($cmd) or die "$cmd failed!"; |
| 156 |
} |
| 157 |
|
| 158 |
rmtree($tmp_dir); |
| 159 |
|
| 160 |
} else { |
| 161 |
# |
| 162 |
# Do substitutions in place on source files. |
| 163 |
# |
| 164 |
run_through_files(@files); |
| 165 |
} |
| 166 |
|
| 167 |
exit(0); |
| 168 |
|
| 169 |
|
| 170 |
################################################################################ |
| 171 |
# |
| 172 |
# Subroutines |
| 173 |
# |
| 174 |
################################################################################ |
| 175 |
|
| 176 |
# |
| 177 |
# Take each file, run substitutions on each line of the file, |
| 178 |
# and writes the file back. |
| 179 |
# |
| 180 |
# Usage: run_through_files(@files); |
| 181 |
sub run_through_files { |
| 182 |
|
| 183 |
my @files = @_; |
| 184 |
|
| 185 |
my($write_it, $error); |
| 186 |
|
| 187 |
foreach my $file (@files) { |
| 188 |
$write_it = $error = 0; |
| 189 |
|
| 190 |
# Skip this file if it's not a text file. |
| 191 |
if(! -T $file) { |
| 192 |
print "$program skipping $file -- not text.\n"; |
| 193 |
next; |
| 194 |
} |
| 195 |
|
| 196 |
# Suck in this file... |
| 197 |
open OLDFILE, "<$file" || die "$program: Cannot open $file"; |
| 198 |
my @lines = <OLDFILE>; |
| 199 |
close OLDFILE; |
| 200 |
|
| 201 |
# Check for matches |
| 202 |
foreach (@lines) { |
| 203 |
foreach my $string (keys %subs) { |
| 204 |
if(s/$string/$subs{$string}/g) { |
| 205 |
$write_it = 1; |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
# Write it if we need to. |
| 211 |
if($write_it) { |
| 212 |
open NEWFILE, ">$file" || die "$program: Cannot open $file"; |
| 213 |
foreach (@lines) { |
| 214 |
$error = !(print NEWFILE); |
| 215 |
} |
| 216 |
close NEWFILE; |
| 217 |
if($error) { |
| 218 |
print "Error writing $file\n"; |
| 219 |
} else { |
| 220 |
print "Updated $file\n"; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
# |
| 229 |
# Parses through the substitution file and puts all of the associations |
| 230 |
# into a hash, %subs |
| 231 |
# |
| 232 |
# Usage: my %subs = parse_config_file($config_file); |
| 233 |
sub parse_config_file { |
| 234 |
|
| 235 |
my $file = shift; |
| 236 |
|
| 237 |
my %subs; |
| 238 |
my $string; |
| 239 |
my $substitution; |
| 240 |
|
| 241 |
open FILE, "<$file" || die "$program: Cannot open $file for reading."; |
| 242 |
while (<FILE>) { |
| 243 |
chop; |
| 244 |
next if (/^\s*\#/); |
| 245 |
next if (/^\s*$/); |
| 246 |
|
| 247 |
if (/^\s*\"/) { |
| 248 |
s/^\s*\"([^\"]*)\"\s+//; |
| 249 |
$string = $1; |
| 250 |
} else { |
| 251 |
s/^\s*(\S*)\s+//; |
| 252 |
$string = $1; |
| 253 |
} |
| 254 |
|
| 255 |
if (/^\"/) { |
| 256 |
s/^\"(.*)\"//; |
| 257 |
$substitution = $1; |
| 258 |
} elsif (/^\`/) { |
| 259 |
s/^\`(.*)\`//; |
| 260 |
chomp($substitution = `$1`); # execute it |
| 261 |
} else { |
| 262 |
$substitution = $_; |
| 263 |
} |
| 264 |
$subs{$string} = $substitution; |
| 265 |
} |
| 266 |
close FILE; |
| 267 |
|
| 268 |
return %subs; |
| 269 |
} |