0adc263d0e001f8a24d2e329d45f0029ff132b30
larrym
  Mon Mar 14 14:25:59 2011 -0700
Use version based links in production sites, mtime based links in dev sites (see redmine #3170)
diff --git src/hg/utils/cpAndLinkToResourceFiles.pl src/hg/utils/cpAndLinkToResourceFiles.pl
index 8ad153b..3bf6c95 100755
--- src/hg/utils/cpAndLinkToResourceFiles.pl
+++ src/hg/utils/cpAndLinkToResourceFiles.pl
@@ -7,88 +7,129 @@
 use Cwd;
 use Getopt::Long;
 
 sub usage
 {
     my ($msg) = @_;
     print STDERR <<END;
 $msg
 
 Usage:
 
 cpAndLinkToResourceFiles.pl [-exclude=...] -destDir=... files
 END
 }
 
-my ($exclude, $destDir, $debug);
+my ($exclude, $destDir, $debug, $versionFile, $cgiVersion, $useMtimes);
 my %exclude;
+my $forceVersionNumbers;   # You can use this option to test CGI-versioned links on dev server
 
-GetOptions("exclude=s" => \$exclude, "destDir=s" => \$destDir, "debug" => \$debug);
+GetOptions("exclude=s" => \$exclude, "destDir=s" => \$destDir, "debug" => \$debug, 
+           "versionFile=s" => \$versionFile,  "forceVersionNumbers" =>  \$forceVersionNumbers);
 
 if($exclude) {
     %exclude = map { $_ => 1} split(/\s*,\s*/, $exclude);
 }
 
 usage("Missing/invalid destDir '$destDir'") if(!$destDir || !(-d $destDir));
 
+my $host = $ENV{HOST};
+if(!defined($host)) {
+    $host = `/bin/hostname`;
+    chomp($host);
+}
+
+# Use version based links in production sites, mtime based links in dev sites (see redmine #3170)
+if($forceVersionNumbers) {
+    $useMtimes = 0;
+} else {
+    $useMtimes = $host eq 'hgwdev' || $host eq 'genome-test' || $host eq 'genome-preview';
+}
+
+if(!$useMtimes) {
+    if(defined($versionFile)) {
+        open(FILE, $versionFile) || die "Couldn't open '$versionFile'; err: $!";
+        while(<FILE>) {
+            if(/CGI_VERSION \"([^\"]+)\"/) {
+                $cgiVersion = $1;
+            }
+        }
+        close(FILE);
+        if(!defined($cgiVersion)) {
+            die "Couldn't find CGI_VERSION in '$versionFile'";
+        }
+    } else {
+        die "Missing required versionFile parameter";
+    }
+}
+
 $destDir =~ s/\/$//;
 
 my @destFiles;
-for (`ls $destDir`) {
-    chomp;
-    push(@destFiles, $_);
+opendir(DIR, $destDir);
+for my $file (readdir(DIR)) {
+    if($file !~ /^\./) {
+        push(@destFiles, $file);
 }
+}
+closedir(DIR);
 
 # To avoid screwing up mirrors who run "rsync --links", we chdir into the resource directory and make non-absolute softlinks there.
 my $cwd = getcwd();
 chdir($destDir) || die "Couldn't chdir into '$destDir'; err: $!";
 
 for my $file (@ARGV)
 {
     if(!$exclude{$file}) {
         my @stat = stat("$cwd/$file") or die "Couldn't stat '$file'; err: $!";
         my $mtime = $stat[9];
         
         # update destination file as appropriate
         my $update = 0;
         my $destFile = $file;
         if(-e $destFile) {
             my @destStat = stat("$destFile") or die "Couldn't stat '$destFile'; err: $!";
             $update = ($destStat[9] < $mtime);
         } else {
             $update = 1;
         }
         if($update) {
             if (-e $destFile) {
                 unlink($destFile) || die "Couldn't unlink $destFile'; err: $!";
             }
+            if($debug) {
+                print STDERR "cp -p $cwd/$file $destFile\n";
+            }
             !system("cp -p $cwd/$file $destFile") || die "Couldn't cp $cwd/file to $destFile: err: $!";
         }
 
-        if($file =~ /(.+)\.[a-z]+$/) {
+        if($file =~ /(.+)\.([a-z]+)$/) {
             my $prefix = $1;
+            my $suffix = $2;
             # make sure time is right, in case file; file might have been newer,
             # speculation that cp -p silently failed if user doesn't own destDir
             @stat = stat($destFile) or die "Couldn't stat '$destFile'; err: $!";
             $mtime = $stat[9];
 
             my $softLink = $file;
-            #$softLink =~ s/\..js/-$mtime.js/;
-            $softLink =~ s/\.([a-z]+)$/-$mtime.$1/;
-            #$softLink =~ s/\..([css|js])/-$mtime.$1/;
+            if($useMtimes) {
+                $softLink =~ s/\.${suffix}$/-$mtime.${suffix}/;
+            } else {
+                $softLink =~ s/\.${suffix}$/-v$cgiVersion.${suffix}/;
+            }
             # Delete obsolete symlinks
             for my $f (@destFiles) {
-                if($f =~ /^$prefix-(\d+)\..*$/) {
+                if(($useMtimes && $f =~ /^$prefix-\d+\.$suffix$/) || ($useMtimes && $f =~ /^$prefix-v\d+\.$suffix$/)) {
                     if($f ne $softLink) {
                         print STDERR "Deleting old soft-link $f\n" if($debug);
                         unlink($f) || die "Couldn't unlink obsolete softlink '$softLink'; err: $!";
                     }
                 }
             }
             # create new symlink
             if(!(-l "$softLink")) {
                 print STDERR "ln -s $softLink\n" if($debug);
                 !system("ln -s $file $softLink") || die "Couldn't ln -s $file; err: $!";
             }
         }
     }
 }
