[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Editing Files In Particular Format
From: |
Systems Administrator |
Subject: |
Re: Editing Files In Particular Format |
Date: |
Mon, 20 Oct 2003 09:57:51 +1000 (EST) |
On 18 Oct 2003, Christopher Browne wrote:
> Another possibility would be to put the file editing into one file,
> and write a script (Perl? Python? Common Lisp? N'import quoi?) that
> does the gory transformations that turns 'structured' updates looking
> like
> ModifyJavaEnvar (propertyname,newvalue)
> into the gory regular expressions.
>
> Has anyone tried doing this? Any suggestions?
I'm only a novice, but what I'm doing is using a templateconv
script (included at the bottom of this e-mail) to generate the cfengine
config files. You'll need to get Text::Template from somewhere.
Basically, I do my config in a file called eg. cf.example.pt, and I put
in:
---
# cfengine config here
<?
# Perl which generates cfengine config here
?>
# more cfengine config
---
It's not the worlds most beautiful solution, but it can be useful
from time to time (I also have a script called redit, which checks a file
out of RCS, checks for a .pt version, and edits that instead, runs
"$EDITOR $filename", then, if it's a .pt file, it runs templateconv on it,
which generates the same file without the .pt).
May not be the best solution to your problem, but it's one
possibility.
:)
-----
#!/usr/bin/perl -I /usr/lib/perl5/site_perl/5.005
use Text::Template;
use Getopt::Std;
getopt('d:');
if(! $opt_d) { $opt_d = "#"; }
if($#ARGV < 0) { die "Error: List files to be processed on the command
line\n"; }
foreach $file (@ARGV) {
if($file !~ /\.pt$/) { $file .= '.pt'; }
if(! -e $file) { die "File $file does not exist\n"; }
$targetfile = $file; $targetfile =~ s/\.pt$//;
$template = new Text::Template(
TYPE => FILE,
SOURCE => $file,
DELIMITERS => ['<?', '?>'],
) or die "Can't create template: $Text::Template::ERROR\n";
$text = $template->fill_in(
BROKEN => \&callback
);
print "Output: $targetfile\n";
open(FILE, ">$targetfile") or do { warn "Error: $!\n"; next};
print FILE "$opt_d Do not edit this file -- it is generated with
templateconv\n";
print FILE $text;
close(FILE);
}
exit $templateretval ? $templateretval : 0;
sub callback {
my(%hash) = @_;
die "Error at $hash{'lineno'}: " . $hash{'error'} . "\n";
}
-----
--
Tim Nelson
Systems Administrator
Sunet Internet
Tel: +61 3 5241 1155
Fax: +61 3 5241 6187
Web: http://www.sunet.com.au/
Email: sysadmin@sunet.com.au