Don't like ads? PRO users don't see any ads ;-)
Guest

Emmett / esync

By: a guest on Nov 30th, 2010  |  syntax: Perl  |  size: 6.68 KB  |  hits: 80  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/perl;
  2.  
  3. #ESYNC: SYNCHRONIZES TARGET FOLDER WITH SOURCE
  4. #EMMETT BUTLER
  5. #NOVEMBER 2010
  6.  
  7. #KNOWN BUGS:
  8. #       WHEN MOVE FLAG IS SET, FILES ARE MOVED BUT DIRECTORIES REMAIN IN SOURCE
  9. #       -C FLAG ONLY WORKS FROM INSIDE PERL SCRIPT, NOT COMMAND LINE
  10.  
  11. use File::Copy;
  12. use File::Path;
  13. use Getopt::Std;
  14.  
  15. # command line options:
  16. #       -d = delete files/directories in target that are no longer present in source
  17. #       -v = verbose notifications
  18. #       -m = move files, don't copy
  19. #       -c = copy directory contents only, not directory itself
  20. #       -t [extension] = copy/delete only the given filetypes
  21. my %options=();
  22. getopts("dvmct:", \%options);
  23.  
  24. #@extargs = split(' ', $options{t});
  25.  
  26. esync("/Users/emmettbutler/Documents/Test", "/Users/emmettbutler/Desktop/Test", 1, 1, 0, 1, 0, "");
  27.  
  28. sub esync {
  29.         my $root = shift;
  30.         my $target = shift;
  31.         my $d = shift;
  32.         my $v = shift;
  33.         my $m = shift;
  34.         my $c = shift;
  35.         my $t = shift;
  36.         my $filetype = shift;
  37.                
  38.         my $parent = ($root =~ /^(.*)(\/.*?)$/)[0];
  39.         my $topfolder = ($root =~ /^(.*)(\/.*?)$/)[1];
  40.     my $source = $parent.$topfolder;
  41.     my $targetpath = ($target =~ /^(.*)(\/.*?)$/)[0];
  42.     my $destination = $targetpath.$topfolder;
  43.        
  44.         #recursive method calls
  45.         copy_tree($source, $destination, $parent, $targetpath, $topfolder, $v, $m, $c, $t, $filetype);
  46.         if($options{d} || $d == 1){
  47.                 find_deletions($destination, $targetpath, $parent, $topfolder, $v, $c);
  48.         }
  49. }
  50.  
  51. sub copy_tree {
  52.     my $path = shift;
  53.     my $dest = shift;
  54.     my $parent = shift;
  55.     my $targetpath = shift;
  56.     my $topfolder = shift;
  57.     my $v = shift;
  58.     my $m = shift;
  59.     my $c = shift;
  60.     my $t = shift;
  61.     my $filetype = shift;
  62.    
  63.     # Open the directory.
  64.     opendir (SRCDIR, $path)
  65.         or die "Unable to open $path: $!";
  66.     my @srcfiles = grep { !/^\.{1,2}$/ } readdir (SRCDIR);
  67.     closedir (SRCDIR);
  68.    
  69.     @srcfiles = map { $path . '/' . $_ } @srcfiles;
  70.    
  71.     #create the root target directory
  72.     if($c == 0){
  73.             if(!-e $dest){
  74.                 mkdir "$dest";
  75.                 if($options{v} || $v == 1){
  76.                         print "Made dir: $dest\n";     
  77.                 }
  78.             }
  79.     }
  80.  
  81.     for (@srcfiles) { #search all files in current directory
  82.         if (-d $_) { #if the file is a directory
  83.                 $olddir = $_;
  84.                 if($c == 1){
  85.                         s/$topfolder//;
  86.                 }
  87.                 s/$parent/$targetpath/; #remove source path
  88.                 if(!-e "$_"){ #if the directory doesn't exist in the target
  89.                                 mkdir "$_"; #create it in target
  90.                                 if($options{v} || $v == 1){ #verbose
  91.                                         print "Made dir: $_\n";
  92.                                 }
  93.                 }
  94.             copy_tree ($olddir, $dest, $parent, $targetpath, $topfolder, $v, $m, $c, $t, $filetype); #recurse for this directory
  95.         } else { # if it is not a directory
  96.             $oldfile = $_;
  97.             if($c == 1){
  98.                         s/$topfolder//;
  99.                 }
  100.             s/$parent/$targetpath/; #change its path from the source to the target
  101.                         $ext = ($oldfile =~ m/([^.]+)$/)[0]; #determine filetype
  102.             $newfile = $_;
  103.                         if(-f $newfile){ #if it exists in target
  104.                                 if((-M $newfile) > (-M $oldfile)){ #if target file was modified before source file
  105.                                         if($options{m} || $m == 1){ #if move flag is set
  106.                                                 move($oldfile, $newfile); #move the file from source to target
  107.                                                 if($options{v} || $v == 1){ #verbose
  108.                                                         print "Moved $oldfile to $newfile.(Newer version was in source)\n";
  109.                                                 }
  110.                                         } else {
  111.                                                 copy($oldfile, $newfile); #copy the file from source to target
  112.                                                 if($options{v} || $v == 1){ #verbose
  113.                                                         print "Copied $oldfile to $newfile.(Newer version was in source)\n";
  114.                                                 }
  115.                                         }
  116.                                 } else {
  117.                                         if($options{v} || $v == 1){ #verbose
  118.                                                 print "Did not copy $oldfile, it exists in target.\n";
  119.                                         }
  120.                                 }      
  121.                         }
  122.                         else{ #if file does not exist in target
  123.                                 if($options{t} || $t == 1){ #if the -t flag is activated
  124.                                         if(($options{t} =~ /$ext/) || ($filetype =~ /$ext/)){ #if the current file's
  125.                                                                                                                                 #type is included in -t arguments
  126.                                                 if($options{m} || $m == 1){ #if move flag is set
  127.                                                         move($oldfile, $newfile); #move file
  128.                                                         if($options{v} || $v == 1){ #verbose
  129.                                                                 print "Moved $oldfile to $newfile\n";
  130.                                                         }
  131.                                                 } else {
  132.                                                         copy($oldfile, $newfile); #copy file
  133.                                                         if($options{v} || $v == 1){ #verbose
  134.                                                                 print "Copied $oldfile to $newfile\n";
  135.                                                         }      
  136.                                                 }
  137.                                         }
  138.                                         else{ #if the current file's type is not included in -t arguments
  139.                                                 if($options{v} || $v == 1){ #do nothing , verbose
  140.                                                         print "Did not copy $oldfile, incorrect extension.\n";
  141.                                                 }
  142.                                         }
  143.                                 }
  144.                                 else { #if the -t flag is not present
  145.                                         if($options{m} || $m == 1){ #if move flag is set
  146.                                                 move($oldfile, $newfile); #move file
  147.                                                 if($options{v} || $v == 1){ #verbose
  148.                                                         print "Moved $oldfile to $newfile\n";
  149.                                                 }
  150.                                         } else {
  151.                                                 copy($oldfile, $newfile); #copy file
  152.                                                 if($options{v} || $v == 1){ #verbose
  153.                                                         print "Copied $oldfile to $newfile\n";
  154.                                                 }      
  155.                                         }
  156.                                 }
  157.                         }      
  158.         }
  159.     }  
  160. }
  161.  
  162. sub find_deletions {
  163.         my $dest = shift;
  164.         my $targetpath = shift;
  165.         my $parent = shift;
  166.         my $topfolder = shift;
  167.         my $v = shift;
  168.         my $c = shift;
  169.        
  170.         if($c == 1){
  171.                 $dest =~ s/$topfolder//;
  172.         }
  173.        
  174.     opendir (TRGDIR, $dest)
  175.         or die "Unable to open $dest: $!";
  176.     my @trgfiles = grep { !/^\.{1,2}$/ } readdir (TRGDIR);
  177.     closedir (TRGDIR);
  178.    
  179.     for(@trgfiles){
  180.         #print "current: $_\n";
  181.     }
  182.    
  183.     @trgfiles = map { $dest . '/' . $_ } @trgfiles;
  184.    
  185.     for (@trgfiles) {
  186.         if (-d $_){ #if file is a directory
  187.                 $olddir = $_;
  188.                 if($c == 1){
  189.                         s/$targetpath/$parent$topfolder/; #replace the target's path with the source path
  190.                 } else {
  191.                         s/$targetpath/$parent/;
  192.                 }
  193.                 if(!-d $_){ #if the directory does not exist in source
  194.                         rmtree("$olddir"); #delete directory from target
  195.                         if($options{v} || $v == 1){ #verbose
  196.                                 print "Deleted directory: $olddir does not exist in source.\n";
  197.                         }
  198.                         find_deletions($dest, $targetpath, $parent, $topfolder, $v, $c);        #recurse from the root directory of target
  199.                 } else { #if the directory exists in source
  200.                         find_deletions($olddir, $targetpath, $parent, $topfolder, $v, $c); #recurse from the current directory
  201.                 }      
  202.         } else { #if file is not a directory
  203.                 $trgdelfile = $_;
  204.                 if($c == 1){
  205.                         s/$targetpath/$parent$topfolder/; #replace the target's path with the source path
  206.                 } else {
  207.                         s/$targetpath/$parent/;
  208.                 }
  209.                 if(!-f $_){ # if the file does not exist in the source
  210.                         unlink("$trgdelfile");  #delete it
  211.                         if($options{v} || $v == 1){ #verbose
  212.                                 print "Deleted file: $trgdelfile does not exist in source.\n";
  213.                         }
  214.                 }      
  215.         }              
  216.     }
  217. }