Difference between revisions of "CopyImages.pl"
(Maintenance script to copy wiki images) |
(Bug fixes etc) |
||
| Line 10: | Line 10: | ||
my $debug = 0; # debugging switch | my $debug = 0; # debugging switch | ||
| − | + | my @matches; # A list of images obtained from 'xmlFile' | |
| − | my @matches; # A list of images obtained from ' | + | my @wantedPaths; # A list of wanted image paths which exist from 'xmlFile' |
| − | |||
my %options = ( # Set options using Getopt::Long | my %options = ( # Set options using Getopt::Long | ||
| − | + | xmlFile => "", | |
wikiImagePath => "", | wikiImagePath => "", | ||
| − | + | tmpDir => "/tmp/wikiImages", # Could use wiki/images path too | |
debug => 0, | debug => 0, | ||
| − | + | rmTmpDir => 0, | |
); | ); | ||
| − | |||
| − | |||
# 1) ------------------------------ Get options ------------------------------ # | # 1) ------------------------------ Get options ------------------------------ # | ||
| Line 27: | Line 24: | ||
GetOptions( | GetOptions( | ||
\%options, | \%options, | ||
| − | ' | + | 'xmlFile=s', |
'wikiImagePath=s', | 'wikiImagePath=s', | ||
| − | ' | + | 'tmpDir=s', |
'debug', | 'debug', | ||
| − | ' | + | 'rmTmpDir', |
); | ); | ||
| + | |||
| + | if( $options{'rmTmpDir'} && ($options{'tmpDir'} =~ m/^\/tmp/)) { | ||
| + | print("[ Removing temporary directory $options{'tmpDir'} ]\n"); | ||
| + | system("rm -rf $options{'tmpDir'}\n") && die("Could not delete $options{'tmpDir'}: $!"); | ||
| + | } | ||
# Create tmp subdirectory | # Create tmp subdirectory | ||
| − | mkdir $options{' | + | mkdir $options{'tmpDir'} || die("Could not create directory"); |
if ($options{'debug'}) { | if ($options{'debug'}) { | ||
| Line 44: | Line 46: | ||
} | } | ||
| − | if( $options{' | + | if( $options{'xmlFile'} eq "" ) { |
| − | print "[ Copying ALL images to: $options{' | + | print "[ Copying ALL images to: $options{'tmpDir'} ]\n"; |
# find . | egrep '\..{3,4}$' | egrep -v '(thumb|archive)' | while read i; do cp $i /tmp/wikiimages/; done | # find . | egrep '\..{3,4}$' | egrep -v '(thumb|archive)' | while read i; do cp $i /tmp/wikiimages/; done | ||
| − | my $ | + | my $cmd = "find $options{'wikiImagePath'} " # find files in image path |
. q(| egrep '\..{3,4}$' ) # 3-4 letter extensions only | . q(| egrep '\..{3,4}$' ) # 3-4 letter extensions only | ||
| − | . q(| egrep -v '(thumb|archive)') # Remove thumb archive directories | + | . q(| egrep -v '(thumb|archive)'); # Remove thumb archive directories |
| − | |||
if ($options{'debug'} ) { | if ($options{'debug'} ) { | ||
print"== Executing command ==\n"; | print"== Executing command ==\n"; | ||
| − | print "$ | + | print "$cmd\n"; |
} | } | ||
| − | + | # Saving list of all images | |
| − | system($ | + | system($cmd . " > /tmp/allImages"); |
| − | # | + | |
| − | + | # Copying images | |
| + | system($cmd . q(| while read i; do cp $i ) . $options{'tmpDir'} . q(; done)); | ||
exit; | exit; | ||
} | } | ||
| Line 68: | Line 70: | ||
# 2) ---------------- Open input XML file and list of images ------------------ # | # 2) ---------------- Open input XML file and list of images ------------------ # | ||
| − | open(XMLFILE, $options{' | + | open(XMLFILE, $options{'xmlFile'}) || die("Could not open file ($options{'xmlFile'}): $!\n"); |
| + | open(ALLPATHS, "/tmp/allImages") || die("Could not open file /tmp/allImages: $!\n"); | ||
| + | |||
# 3) ------------------------------ Grab images ------------------------------- # | # 3) ------------------------------ Grab images ------------------------------- # | ||
| Line 102: | Line 106: | ||
# Convert space separated image names to '_'s | # Convert space separated image names to '_'s | ||
$imageToMatch =~ s/ /_/g; | $imageToMatch =~ s/ /_/g; | ||
| − | # Matching | + | # Matching xmlFile images against images in ALLPATHS |
foreach my $eachImagePath (@allImagePaths) { | foreach my $eachImagePath (@allImagePaths) { | ||
$eachImagePath =~ m/$imageToMatch/ && push @wantedPaths, $eachImagePath; | $eachImagePath =~ m/$imageToMatch/ && push @wantedPaths, $eachImagePath; | ||
| Line 113: | Line 117: | ||
if ( scalar(@wantedPaths) == 0 ) { | if ( scalar(@wantedPaths) == 0 ) { | ||
| − | die("[ No images found in XML file $options{' | + | die("[ No images found in XML file $options{'xmlFile'} ]\n"); |
} | } | ||
| − | print "[ Copying images found in XML file $options{' | + | print "[ Copying images found in XML file $options{'xmlFile'} to: $options{'tmpDir'} ]\n"; |
# 5) --------------------- Copy subset of wanted images ----------------------- # | # 5) --------------------- Copy subset of wanted images ----------------------- # | ||
| Line 127: | Line 131: | ||
# Copy files | # Copy files | ||
| − | copy($fullPath, $options{' | + | copy($fullPath, $options{'tmpDir'}) || dir("Could not copy $fullPath files to $options{'tmpDir'}: $!\n") |
} | } | ||
Revision as of 09:50, 1 December 2008
- !/usr/bin/perl -w
use strict; use Getopt::Long; use File::Copy;
- If there is no XML file the script should grab all the images and put them into
- /tmp/wikiimages Template:PERL
- 0) ------------------------------ Globals ----------------------------------- #
my $debug = 0; # debugging switch my @matches; # A list of images obtained from 'xmlFile' my @wantedPaths; # A list of wanted image paths which exist from 'xmlFile' my %options = ( # Set options using Getopt::Long
xmlFile => "", wikiImagePath => "", tmpDir => "/tmp/wikiImages", # Could use wiki/images path too debug => 0, rmTmpDir => 0,
);
- 1) ------------------------------ Get options ------------------------------ #
GetOptions(
\%options, 'xmlFile=s', 'wikiImagePath=s', 'tmpDir=s', 'debug', 'rmTmpDir',
);
if( $options{'rmTmpDir'} && ($options{'tmpDir'} =~ m/^\/tmp/)) {
print("[ Removing temporary directory $options{'tmpDir'} ]\n");
system("rm -rf $options{'tmpDir'}\n") && die("Could not delete $options{'tmpDir'}: $!");
}
- Create tmp subdirectory
mkdir $options{'tmpDir'} || die("Could not create directory");
if ($options{'debug'}) {
print "== Input arguements ==\n";
foreach my$values ( keys %options ) {
print "$values => $options{$values}\n";
}
}
if( $options{'xmlFile'} eq "" ) {
print "[ Copying ALL images to: $options{'tmpDir'} ]\n";
- find . | egrep '\..{3,4}$' | egrep -v '(thumb|archive)' | while read i; do cp $i /tmp/wikiimages/; done
my $cmd = "find $options{'wikiImagePath'} " # find files in image path
. q(| egrep '\..{3,4}$' ) # 3-4 letter extensions only
. q(| egrep -v '(thumb|archive)'); # Remove thumb archive directories
if ($options{'debug'} ) {
print"== Executing command ==\n";
print "$cmd\n";
}
# Saving list of all images
system($cmd . " > /tmp/allImages");
# Copying images
system($cmd . q(| while read i; do cp $i ) . $options{'tmpDir'} . q(; done));
exit;
}
- 2) ---------------- Open input XML file and list of images ------------------ #
open(XMLFILE, $options{'xmlFile'}) || die("Could not open file ($options{'xmlFile'}): $!\n"); open(ALLPATHS, "/tmp/allImages") || die("Could not open file /tmp/allImages: $!\n");
- 3) ------------------------------ Grab images ------------------------------- #
{
# Not scalable local $/=undef; my $xmlImage = <XMLFILE>;
# ($xmlImage =~ m/\<nowiki(.+?)\&nowiki\\gt;/) && (print $1);
if ( $options{'debug'} ) {
print "== XML nowiki sections ==\n";
while ($xmlImage =~ m/\<nowiki\>(.+?)<\/nowiki\>/g) {print "$1\n"};
}
# Remove content in between <nowiki>'s
$xmlImage =~ s/\<nowiki\>(.+?)<\/nowiki\>//g;
# Grab image matches (don't forget |'s and ]]'s in image tags)
@matches = $xmlImage =~ m/\[{2}Image:(.+?)(?=\||\]{2})/g;
}
if($options{'debug'}) {
local $" = "\n"; print "== \@matches ==\n @matches\n";
}
- Not scalable
my @allImagePaths=<ALLPATHS>;
- 4) ----------------- Test $imageToMatch against $eachImagePath -------------- #
foreach my $imageToMatch (@matches) {
chop $imageToMatch;
# Convert space separated image names to '_'s
$imageToMatch =~ s/ /_/g;
# Matching xmlFile images against images in ALLPATHS
foreach my $eachImagePath (@allImagePaths) {
$eachImagePath =~ m/$imageToMatch/ && push @wantedPaths, $eachImagePath;
}
}
if ( $options{'debug'} ) {
print "==Wanted image paths==\n@wantedPaths";
}
if ( scalar(@wantedPaths) == 0 ) {
die("[ No images found in XML file $options{'xmlFile'} ]\n");
}
print "[ Copying images found in XML file $options{'xmlFile'} to: $options{'tmpDir'} ]\n";
- 5) --------------------- Copy subset of wanted images ----------------------- #
foreach my $wantedPath ( @wantedPaths ) {
chomp($wantedPath);
my $fullPath = $options{'wikiImagePath'} . substr($wantedPath, 1, length $wantedPath);
print "$fullPath\n";
# Copy files
copy($fullPath, $options{'tmpDir'}) || dir("Could not copy $fullPath files to $options{'tmpDir'}: $!\n")
}



