Difference between revisions of "CopyImages.pl"

From Organic Design wiki
(Bugs)
m
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
{{legacy}}
 +
<source lang="perl">
 
#!/usr/bin/perl -w
 
#!/usr/bin/perl -w
 
use strict;
 
use strict;
Line 5: Line 7:
  
 
# If there is no XML file the script should grab all the images and put them into  
 
# If there is no XML file the script should grab all the images and put them into  
# /tmp/wikiimages {{PERL}}
+
# /tmp/wikiimages
  
 
# 0) ------------------------------ Globals ----------------------------------- #
 
# 0) ------------------------------ Globals ----------------------------------- #
  
my $debug = 0; # debugging switch
+
my $tmpPath = "/tmp/"; # Absolute path to tmp dir
my @matches; # A list of images obtained from 'xmlFile'
+
my $debug = 0;         # debugging switch
my @wantedPaths; # A list of wanted image paths which exist from 'xmlFile'
+
my @xmlImages;           # List of images obtained from 'xmlFile'
my %options = ( # Set options using Getopt::Long
+
my @wantedPaths;       # List of wanted image paths which exist from 'xmlFile'
  xmlFile => "",
+
 
 +
my %options = (   # Set options using Getopt::Long
 
   wikiImagePath => "",
 
   wikiImagePath => "",
   tmpDir => "/tmp/wikiImages", # Could use wiki/images path too
+
  xmlFile      => "",
  debug => 0,
+
   tmpDir       => "wikiImages", # Could use wiki/images path too
 
   rmTmpDir      => 0,
 
   rmTmpDir      => 0,
 +
  debug        => 0,
 
);
 
);
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
 
  
 
# 1) ------------------------------ Get options  ------------------------------ #
 
# 1) ------------------------------ Get options  ------------------------------ #
Line 27: Line 28:
 
GetOptions(
 
GetOptions(
 
   \%options,
 
   \%options,
 +
  'wikiImagePath=s',
 
   'xmlFile=s',
 
   'xmlFile=s',
  'wikiImagePath=s',
 
 
   'tmpDir=s',
 
   'tmpDir=s',
 +
  'rmTmpDir',
 
   'debug',
 
   'debug',
  'rmTmpDir',
 
 
);
 
);
  
if( $options{'rmTmpDir'} && ($options{'tmpDir'} =~ m/^\/tmp/)) {  
+
# Append /tmp if missing
 +
($options{'tmpDir'} =~ m/$tmpPath/) or
 +
    ($options{'tmpDir'} = $tmpPath . $options{'tmpDir'});
 +
 
 +
unless( $options{'wikiImagePath'} ) {
 +
    die("--wikiImagePath relative or absolute path required\n");
 +
}
 +
 
 +
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{'rmTmpDir'} ) {  
 
   print("[ Removing temporary directory $options{'tmpDir'} ]\n");
 
   print("[ Removing temporary directory $options{'tmpDir'} ]\n");
   system("rm -rf $options{'tmpDir'}\n") && die("Could not delete $options{'tmpDir'}: $!");  
+
   system("rm -rf $options{'tmpDir'}\n") and
 +
      die("Could not delete $options{'tmpDir'}: $!");  
 
}
 
}
  
 
# Create tmp subdirectory
 
# Create tmp subdirectory
mkdir $options{'tmpDir'} || die("Could not create directory");  
+
opendir(DIR , $options{'tmpDir'});
 +
my $rv = readdir(DIR);
 +
closedir(DIR);
 +
unless($rv) {
 +
    mkdir $options{'tmpDir'} or die("Could not create directory");  
 +
}
  
 
if ($options{'debug'}) {
 
if ($options{'debug'}) {
Line 57: Line 76:
 
   }
 
   }
 
   # Copying images
 
   # Copying images
   #system($cmd . q(| while read i; do cp $i ) . $options{'tmpDir'} . q(; done));
+
   system($cmd . q(| while read i; do cp $i ) . $options{'tmpDir'} . q(; done));
 
   exit;
 
   exit;
 
} else {
 
} else {
 
   # Saving list of all images
 
   # Saving list of all images
   print $options{'xmlFile'};
+
   system($cmd . " > /tmp/allImages"); #  print ($cmd . "> /tmp/allImages\n");
  system($cmd . " > /tmp/allImages");
 
 
}
 
}
  
Line 68: Line 86:
 
# 2) ---------------- Open input XML file and list of images ------------------ #
 
# 2) ---------------- Open input XML file and list of images ------------------ #
  
open(XMLFILE, $options{'xmlFile'})     || die("Could not open file ($options{'xmlFile'}): $!\n");
+
open(XMLFILE, $options{'xmlFile'}) or die("Could not open file ($options{'xmlFile'}): $!\n");
open(ALLPATHS, "/tmp/allImages")     || die("Could not open file /tmp/allImages: $!\n");
+
open(ALLPATHS, "/tmp/allImages") or die("Could not open file /tmp/allImages: $!\n");
  
  
Line 87: Line 105:
 
   $xmlImage =~ s/\&lt;nowiki\&gt;(.+?)&lt;\/nowiki\&gt;//g;
 
   $xmlImage =~ s/\&lt;nowiki\&gt;(.+?)&lt;\/nowiki\&gt;//g;
 
   # Grab image matches (don't forget |'s and ]]'s in image tags)
 
   # Grab image matches (don't forget |'s and ]]'s in image tags)
   @matches = $xmlImage =~ m/\[{2}Image:(.+?)(?=\||\]{2})/g;  
+
   @xmlImages = $xmlImage =~ m/\[{2}Image:(.+?)(?=\||\]{2})/g;  
 
}
 
}
  
 
if($options{'debug'}) {
 
if($options{'debug'}) {
 
   local $" = "\n";
 
   local $" = "\n";
   print "== \@matches ==\n @matches\n";
+
   print "== \@xmlImages ==\n @xmlImages\n";
 
}
 
}
  
Line 100: Line 118:
 
# 4) ----------------- Test $imageToMatch against $eachImagePath -------------- #
 
# 4) ----------------- Test $imageToMatch against $eachImagePath -------------- #
  
foreach  my $imageToMatch (@matches) {
+
foreach  my $imageToMatch (@xmlImages) {
 
   chop $imageToMatch;
 
   chop $imageToMatch;
 
   # Convert space separated image names to '_'s
 
   # Convert space separated image names to '_'s
Line 106: Line 124:
 
   # Matching xmlFile images against images in ALLPATHS  
 
   # Matching xmlFile images against images in ALLPATHS  
 
   foreach my $eachImagePath (@allImagePaths) {
 
   foreach my $eachImagePath (@allImagePaths) {
     ($eachImagePath =~ m/$imageToMatch/) && (push @wantedPaths, $eachImagePath);
+
     ($eachImagePath =~ m/$imageToMatch/) and (push @wantedPaths, $eachImagePath);
 
   }
 
   }
 
}
 
}
Line 124: Line 142:
 
foreach my $wantedPath ( @wantedPaths ) {
 
foreach my $wantedPath ( @wantedPaths ) {
 
   chomp($wantedPath);
 
   chomp($wantedPath);
 +
  print "cp $wantedPath $options{'tmpDir'}\n";
 
      
 
      
  my $fullPath = $options{'wikiImagePath'} . substr($wantedPath, 1, length $wantedPath);  
+
my $fullPath = substr($wantedPath, 1, length $wantedPath);  
  print "$fullPath\n";     
+
print "$fullPath\n";     
 
      
 
      
 
   # Copy files
 
   # Copy files
   copy($fullPath, $options{'tmpDir'}) || dir("Could not copy $fullPath files to $options{'tmpDir'}: $!\n")
+
   copy($wantedPath, $options{'tmpDir'}) or die("Could not copy files to $options{'tmpDir'}: $!\n")
 
}
 
}
 +
</source>
 +
[[Category:PERL]]

Latest revision as of 03:27, 23 April 2020

Legacy.svg Legacy: This article describes a concept that has been superseded in the course of ongoing development on the Organic Design wiki. Please do not develop this any further or base work on this concept, now this page is for historic record only.
#!/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

# 0) ------------------------------ Globals ----------------------------------- #

my $tmpPath = "/tmp/"; # Absolute path to tmp dir
my $debug = 0;         # debugging switch
my @xmlImages;           # List of images obtained from 'xmlFile'
my @wantedPaths;       # List of wanted image paths which exist from 'xmlFile'

my %options = (   # Set options using Getopt::Long
  wikiImagePath => "",
  xmlFile       => "",
  tmpDir        => "wikiImages", # Could use wiki/images path too
  rmTmpDir      => 0,
  debug         => 0,
);

# 1) ------------------------------ Get options  ------------------------------ #

GetOptions(
  \%options,
  'wikiImagePath=s',
  'xmlFile=s',
  'tmpDir=s',
  'rmTmpDir',
  'debug',
);

# Append /tmp if missing
($options{'tmpDir'} =~ m/$tmpPath/) or
    ($options{'tmpDir'} = $tmpPath . $options{'tmpDir'});

unless( $options{'wikiImagePath'} ) {
    die("--wikiImagePath relative or absolute path required\n");
}

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{'rmTmpDir'} ) { 
  print("[ Removing temporary directory $options{'tmpDir'} ]\n");
  system("rm -rf $options{'tmpDir'}\n") and 
      die("Could not delete $options{'tmpDir'}: $!"); 
}

# Create tmp subdirectory
opendir(DIR , $options{'tmpDir'});
my $rv = readdir(DIR);
closedir(DIR);
unless($rv) {
    mkdir $options{'tmpDir'} or 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";
 
  if ($options{'debug'} ) {
    print"== Executing command ==\n";
    print "$cmd\n";
  }
  # Copying images
  system($cmd . q(| while read i; do cp $i ) . $options{'tmpDir'} . q(; done));
  exit;
} else {
  # Saving list of all images
  system($cmd . " > /tmp/allImages"); #  print ($cmd . "> /tmp/allImages\n");
}


# 2) ---------------- Open input XML file and list of images ------------------ #

open(XMLFILE, $options{'xmlFile'}) or die("Could not open file ($options{'xmlFile'}): $!\n");
open(ALLPATHS, "/tmp/allImages") or die("Could not open file /tmp/allImages: $!\n");


# 3) ------------------------------ Grab images ------------------------------- #

{ 
  # Not scalable  
  local $/=undef;
  my $xmlImage = <XMLFILE>;

  # ($xmlImage =~ m/\&lt;nowiki(.+?)\&nowiki\\gt;/) && (print $1);
  if ( $options{'debug'} ) {
    print "== XML nowiki sections ==\n";
    while ($xmlImage =~ m/\&lt;nowiki\&gt;(.+?)&lt;\/nowiki\&gt;/g) {print "$1\n"};
  }
  # Remove content in between <nowiki>'s
  $xmlImage =~ s/\&lt;nowiki\&gt;(.+?)&lt;\/nowiki\&gt;//g;
  # Grab image matches (don't forget |'s and ]]'s in image tags)
  @xmlImages = $xmlImage =~ m/\[{2}Image:(.+?)(?=\||\]{2})/g; 
}

if($options{'debug'}) {
  local $" = "\n";
  print "== \@xmlImages ==\n @xmlImages\n";
}

# Not scalable
my @allImagePaths=<ALLPATHS>;

# 4) ----------------- Test $imageToMatch against $eachImagePath -------------- #

foreach  my $imageToMatch (@xmlImages) {
  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/) and (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);
  print "cp $wantedPath $options{'tmpDir'}\n";
    
#  my $fullPath = substr($wantedPath, 1, length $wantedPath); 
#  print "$fullPath\n";    
    
  # Copy files
  copy($wantedPath, $options{'tmpDir'}) or die("Could not copy files to $options{'tmpDir'}: $!\n")
}