575fd05526889a61d457a5a31f96c86de0d637c8
hiram
  Fri Oct 18 13:19:55 2024 -0700
adding a tmpDir function to properly choose a temporary directory no redmine

diff --git src/hg/utils/automation/HgAutomate.pm src/hg/utils/automation/HgAutomate.pm
index b00d7e1..12f5f30 100755
--- src/hg/utils/automation/HgAutomate.pm
+++ src/hg/utils/automation/HgAutomate.pm
@@ -19,31 +19,31 @@
 
 # This is a listing of the public methods and variables (which should be
 # treated as constants) exported by this module:
 @EXPORT_OK = (
     # Support for common command line options:
     qw( getCommonOptionHelp processCommonOptions
 	@commonOptionVars @commonOptionSpec
       ),
     # Some basic smarts about our compute infrastructure:
     qw( choosePermanentStorage
 	chooseWorkhorse chooseFileServer
 	chooseClusterByBandwidth chooseSmallClusterByBandwidth
 	chooseFilesystemsForCluster checkClusterPath
       ),
     # General-purpose utility routines:
-    qw( checkCleanSlate checkExistsUnlessDebug closeStdin
+    qw( tmpDir checkCleanSlate checkExistsUnlessDebug closeStdin
 	getAssemblyInfo getSpecies hubDateName gensub2 machineHasFile
 	databaseExists dbTableExists makeGsub mustMkdir asmHubBuildDir
 	asmHubDownloadDir mustOpen nfsNoodge paraRun run verbose
       ),
     # Hardcoded paths/commands/constants:
     qw( $centralDbSql $git
 	$clusterData $trackBuild $goldenPath $images $gbdb
 	$splitThreshold $runSSH $setMachtype
       ),
 );
 
 #########################################################################
 # A simple model of our local compute environment with some subroutines
 # for checking the validity of path+machine combos and for suggesting
 # appropriate storage and machines.
@@ -556,30 +556,64 @@
 # This is the max number of sequences in an assembly that we will consider
 # "chrom-based" (allow split tables; per-seq files can fit in one directory)
 # as opposed to "scaffold-based" (no split tables; multi-level directory for
 # per-seq files, or use set of multi-seq files).
 $splitThreshold = 100;
 
 # ssh command and its options, the extra -o options prevent asking
 # questions about adding machines to known hosts
 $runSSH = "ssh -x -o 'StrictHostKeyChecking = no' -o 'BatchMode = yes'";
 
 $setMachtype = "setenv MACHTYPE `uname -m | sed -e 's/i686/i386/;'`";
 
 #########################################################################
 # General utility subroutines:
 
+### decide on an appropriate temporary directory
+sub tmpDir {
+  my $tDir="/tmp";	# default, most likely overridden
+  if (defined($ENV{'TMPDIR'}) && -d $ENV{'TMPDIR'}) {	# TMPDIR above all else
+    $tDir = $ENV{'TMPDIR'};
+  } elsif ( -d "/data/tmp" ) {	# UCSC local file system
+    $tDir = "/data/tmp";
+  } elsif ( -d "/scratch/tmp" ) {	# UCSC cluster node local file system
+    $tDir = "/scratch/tmp";
+  } else {
+    my $tmpSz = `df --output=avail -k /tmp | tail -1`;
+    my $shmSz = `df --output=avail -k /dev/shm | tail -1`;
+    chomp ($tmpSz, $shmSz);
+    if ( $shmSz > $tmpSz ) {	# use /dev/shm when it is the larger one
+       my $shmTmp = "/dev/shm/tmp";
+       my $saveUmask = umask(0000);	# do not allow user's umask to interfere
+       if (! mkdir($shmTmp, 0777)) {
+          my $errString = $!;
+          if ($errString =~ /File exists/i) {	# this error OK
+            if ( -w "${shmTmp}" ) {	# use only when writable
+                $tDir = $shmTmp;
+            }
+          }	# mkdir other errors cause use of default
+       } else {				# no error on the mkdir
+          if ( -w "${shmTmp}" ) {	# use only when writable
+            $tDir = $shmTmp;
+          }
+       }
+       umask($saveUmask);	# restore
+    }
+  }
+return $tDir;
+}
+
 sub checkCleanSlate {
   # Exit with an error message if it looks like this step has already been run
   # based on the existence of the given file(s) or directory(ies).
   my ($step, $nextStep, @files) = @_;
   confess "Must have at least 3 arguments" if (scalar(@_) < 3);
   confess "undef input" if (! defined $step || ! defined $nextStep);
   my $problem = 0;
   foreach my $f (@files) {
     confess "undef input" if (! defined $f);
     if (-e $f) {
       warn "$step: looks like this was run successfully already " .
 	"($f exists).  Either run with -continue $nextStep or some later " .
 	"step, or move aside/remove $f and run again.\n";
       $problem = 1;
     }