[LON-CAPA-cvs] cvs: modules /raeburn changedom.pl
raeburn
raeburn@source.lon-capa.org
Wed, 24 Jun 2009 07:33:11 -0000
raeburn Wed Jun 24 07:33:11 2009 EDT
Added files:
/modules/raeburn changedom.pl
Log:
- Script to change instancles of /olddomain/oldusername/ within Guy Ashkenazi's chemistry resources (to facilitate migration from the old domain).
Index: modules/raeburn/changedom.pl
+++ modules/raeburn/changedom.pl
#! /usr/bin/perl
use strict;
use vars qw( $log );
my $dest = '/home/raeburn/guyashkenazi';
my $logfile = "convert.log";
if (open($log,">$logfile")) {
my $dir = 'public_html';
if (-d $dir) {
&readtree($dir,$dest);
} else {
print "$dir is not a directory\n";
}
close($log);
} else {
print "Could not open $logfile for writing\n";
}
sub readtree {
my ($dir,$dest) = @_;
if (opendir(DIR,$dir)) {
my @items = grep(!/^\.\.?/,readdir(DIR));
closedir(DIR);
foreach my $item (@items) {
if (-d "$dir/$item") {
&readtree("$dir/$item",$dest);
} else {
&convert_file($dir,$item,$dest);
}
}
} else {
print "Could not open directory $dir for reading\n";
}
}
sub convert_file {
my ($dir,$item,$dest) = @_;
if ($item =~ /\~/) {
print $log "$dir/$item skipped\n";
return;
}
if ($item =~ /\.(bak|save|log)$/) {
print $log "$dir/$item skipped\n";
return;
}
if ($item =~ /\.jar$/) {
print $log "$dir/$item is a .jar file\n";
}
&make_dirs($dir,$dest);
if (-T "$dir/$item") {
return;
if (open(my $fh,"<$dir/$item")) {
if (open(my $newfh,">$dest/$dir/$item.tmp")) {
my $changes = 0;
while (<$fh>) {
if (m{/huji/guy/}) {
$changes ++;
s{/huji/guy/}{/author/guyashkenazi/}g;
print $log "Changed /huji/guy/ -> /author/guyashkenazi/ in $dir/$item in $_\n";
}
if (/guy\@huji/) {
$changes ++;
s/guy\@huji/guyashkenazi\@author/g;
print $log "Changed guy\@huji -> guyashkenazi\@author in $dir/$item in $_\n";
}
print $newfh $_;
}
close($newfh);
if ($changes) {
system("mv '$dest/$dir/$item.tmp' '$dest/$dir/$item'");
print $log "Modifed $dir/$item written to $dest/$dir/$item\n";
} else {
unlink("$dest/$dir/$item.tmp");
system("cp -p '$dir/$item' '$dest/$dir/$item'");
print $log "Copied $dir/$item to $dest/$dir/$item\n";
}
} else {
print "could not open $dest/$dir/$item for writing\n";
}
close($fh);
} else {
print "could not open $dir/$item for reading\n";
}
} else {
system("cp -p '$dir/$item' '$dest/$dir/$item'");
print $log "Copied non-text file from $dir/$item to $dest/$dir/$item\n";
}
}
sub make_dirs {
my ($dir,$dest) = @_;
my @dirs = split('/',$dir);
my $path = $dest;
foreach my $dir (@dirs) {
$path .= '/'.$dir;
if (!-d $path) {
mkdir($path,0755);
print $log "Created directory $path\n";
}
}
return;
}