U g"@sxdZddddddddd d d d d ddddddddddddddddddd d!d"g"Zd#d$lZd#d$lZd#d$lZd#d$lZd#d$lZd#d$lZd#d$lZd#d$l Z d#d$l Z d#d$l Z d#d$l Z d#d$l Z d#d$lZd#d$lZe d%ZGd&ddeZGd'ddeZGd(ddeZGd)ddeZGd*ddeZGd+ddeZGd,ddeZGd-ddeZGd.d/d/eZGd0d d eZGd1d d eZd2d ZdZd3d Z d[d4d"Z!d5d!Z"d6d Z#d\d8d Z$d]d:dZ%d;dZ&d^d=dZ'd>dZ(d_dAdZ)d`dBdZ*dadDdEZ+dbdFdZ,dcdHdZ-dddIdZ.dedJdZ/dfdKdZ0dgdLdMZ1dhdNdZ2didOdZ3djdPdZ4dkdRdZ5dldSdZ6GdTddZ7dmdYdZ8d$S)na*!This module provides a set of utility functions to do filesystem operations. It replaces or improves upon several os, stat, and sys module functions by working around Python bugs, providing an API layer that allows forward compatibility to future Python versions, and adding logging capabilities. FileOpError FileOpErrorsCannotLinkMultiUnexpectedAbsolutePathInvalidExecutableFindExeInvalidExeName CannotFindExeRelativePathErrorDeliveryFailedVerificationFailedrealcwdchdirmakedirs remove_filermall lstat_stat isnonempty check_file deliver_filemake_symlinks_infind_exe make_symlinkreplace_symlinkunblockfortcopynorm_expand_path norm_abs_pathcheck_last_lineswait_for_files FileWaiter call_fcntrlgribver netcdfvertouchNzprodutil.fileopc@s,eZdZdZgfddZddZddZdS) rzg!This is the superclass of several exceptions relating to multi-file operations in produtil.fileop.cCs||_||_||_dS)a<!FileOpError constructor @param message the error message @param filename the name of the problematic file @param more a list of tuples, (from,to,message) where from is the source file, to is the destination file and message is a description of the problem with that pair.N)messagefilenamemore)selfr$r%r&r(tk rj}z |j|dt|ddW5d}~XYnXdS)a!Changes to the specified directory. Please use produtil.cd.NamedDir instead. This is generally not a good idea since you will not cd back if an unhandled exception is raised. It is better to use the produtil.cd module, which provides ways to enter a directory in a "with" block and optionally delete it afterwards. Such functionality could also be implemented via a try...finally block. @param path the path to cd to @param logger a logging.Logger for log messagesNz : cd herez : cannot cd: Texc_info)strinfor<r EnvironmentErrorwarning)r=loggerer(r(r)r s c Cs&t|dt||W5QRXdS)aT!Open the file for append and set mtime and atime. Opens the specified file in append mode, but writes nothing. Sets the access and modification times. @param filename the string filename @param times A 2-tuple of numbers, of the form (atime, mtime). These are UNIX epoch times (seconds since 1970 began in UTC).aN)openr<utime)r%timesr(r(r)r"s c Cst|dx}|d}t|dkr0W5QRdS|dd}|dkrRW5QRdS|dkrhW5QRd S|d kr~W5QRd SW5QRXdS) a!What is the NetCDF version of this file? Returns one of three strings based on the NetCDF version of the given file, or returns None if the file is not NetCDF: * "CDF1" = NetCDF classic format * "CDF2" = NetCDF 64-bit offset format * "HDF5" = HDF5 file, and hence possibly a NetCDF4 file. * None = Not NetCDF and not HDF5 @param filename the name of the file to testrbNr#zCDFZCDF1zCDFZCDF2u ‰HDF  ZHDF5)rIreadlen)r%feightZfourr(r(r)r!s    c Cst|ts$tdt|jt|ft|s0dSt|dR}|d}|dkr\W5QRdS|ddd krzW5QRd SW5QRdSW5QRXdS) a!What is the GRIB version of this file? Returns the GRIB file version: 1 or 2. If the file is not a GRIB file, or if the answer is indeterminate, returns None. Only the first GRIB record is tested. @param filename the path to the file to testzJThe first argument to gribver should be a filename. You provided a %s %s.NrLrMzGRIBr#rNZGRIB) isinstancerB TypeErrortyper0r8rrIrO)r%rQrRr(r(r)r s    c Cst|D]}z0tj|s:|dk r0||dt|Wqtk r}zBtj|rfWY0dStj|rvn||dkrWYqW5d}~XYqXqdS)a!Make a directory tree, working around filesystem bugs. This makedirs implementation works around a common bug: if two processes try to recursively make a directory tree simultaneously, makedirs can fail when two processes make the same path component at the same time. This implementation automatically retries in that situation. @param filename the directory path @param numtries the number of times to retry @param logger a logging.Logger for log messagesNz: make directory and parentsTrT)ranger<r=isdirrCr rDexists)r%ZnumtriesrFnrGr(r(r)r s      Tc Cs|dks|dkstj|s dSz&|dk r:|d|ft|Wnxtk r}zZ|jtjkr|dk r|jd|t |fdd|dk r|r|jn|j}|d|fW5d}~XYnXdS)a!Deletes the specified file. Does nothing if the filename is None, is the empty string or already does not exist. Otherwise, the file is deleted. @param filename The file to delete. @param info Optional: indicates that warnings about a file already not existing should be sent to the logger at INFO level (info=True) instead of WARNING (info=False). @param logger the logging.Logger for messagesNz%s: remove filez%s: cannot remove: %sTr@z"%s: cannot remove; does not exist.) r<r=r[rCunlinkrDerrnoENOENTrErB)r%rCrFrGlogr(r(r)rs  c Os|dd}|dk r(|dt|ft}|D]H}zt|f|Wq2tk rx}z||d|fW5d}~XYq2Xq2t|dkr|ddn>t|dkrd}|dk r||t|d |dd |D|dk r|d t|fdS) a!Deletes the specified list of files. Deletes files listed in "args". Each one is passed to remove_file. Exceptions that derive from EnvironmentError are collected, and will be raised at the end, thus allowing removal of later files to continue if earlier ones failed. If only one file causes an exception, that exception will be raised, otherwise FileOpErrors will be raised @param args The files to delete. @param kwargs Keyword arguments passed to remove_file().rFNzRemoving %d files...rTr#z9Multiple exceptions caught while deleting files in rmall.,cSs g|]\}}}||t|fqSr()rB).0rHbcr(r(r) /szrmall..zDone removing %d files...) getrCrPlistrrDappendrErjoin)argskwargsrFZexargrGmsgr(r(r)rs( $     Fc Cs|dk s td\}}z.t|}t|js6||fWSt|}Wn4tk rv}z|sd|jtjkrfW5d}~XYnX||fS)a!Runs lstat and stat on a file as efficiently as possible. Returns (lstat(filename),stat(filename)) where each is None if it fails due to non-existence. Does this in as few filesystem metadata operations as possible. Will raise an exception if the stat fails for any reason other than non-existence of a file, or if the file or linked file is non-existent and raise_nonexist=True. @param filename The file to test. @param raise_nonexist Should we raise an exception if the file does not exist? @returns a tuple (L,S) where L is the lstat return value, and S is the stat return value. Each will be None if the file or link target do not exist.NNN) AssertionErrorr<lstatstatS_ISLNKst_moderDr_r`)r%raise_nonexistZxlstatZxstatrGr(r(r)r4s    cCsJ|dkr dSt|}|dks$|dkr(dSt|\}}|dk oD|jdk}|S)z!Returns True if the filename refers to an existent file that is non-empty, and False otherwise. @param filename The file to test.Nr]r#)rBrst_size)r%Zsfilelsretr(r(r)rNs rScCs`| dkrtj } | dkr&tj} |dk r2d}|dk s>t|dk sJttj|}t|dd\}}t |j r~t d|||}d}t|\}}d\}}|dk rt |j r|}tj ||}||}}|dk r|d||ft|\}}|dkrttj|}t|dkrd}|dk r4|d ||ft|dd\}}|dkrZt d ||t |j stt d |||dk r|s|dk r|d |f|dk r t |j r|dk r|d |fn0tj||r|dk r|d||fdS|dk r8|d||fn|dk r8|d||j|jk}|r|s| rt |j r|dk r|d|fnt|dk r|d||fzt||WdStk r}z$|dk r|d|t|fW5d}~XYnXd}d}zz|dkrd|d}tj|d|d}|j}|dk rL|d||f|dkr~t|d}tj|||dW5QRXn |||||d}|r|dk r|d||ft !||st"d||||dk r|d||f| rVzt#|d|j$WnJt%t&fk rT}z&|dk rD|'d||t|fW5d}~XYnX| rpt(||j |@|dk r|d||f| rt)||j*|j+ft||d}WnFt,k r}z&|dk r|-d |t|fW5d}~XYnXW5z0|dk r||r2|dk r2t|Wn$tk rX}zW5d}~XYnXXdS)!a0 !This moves or copies the file "infile" to "outfile" in a unit operation; outfile will never be seen in an incomplete state. If the caller specifies keep=False (default is True) and moveok=True, and the source and destination are on the same filesystem then the delivery is done with a simple move. Otherwise a copy is done to a temporary file on the same filesystem as the target. If verification is requested (verify=True) then the temporary file is verified by filecmp.cmp, before moving the temporary file to the final location. When requested, and when possible, the permissions and ownership are preserved. Both copy_acl and preserve_group have defaults set by the produtil.cluster module. If the cluster uses access control lists for data restriction classes, then copy_acl will be set to True, otherwise it is false. If group quotas are enabled, preserve_group is False, otherwise it is True. @note The original file is never deleted, but it may be moved to the target if keep=False. If a copy is done instead, the original file is still present. @param infile the origin file @param outfile the destination file or its parent directory @param keep If False, the original file is no longer needed. If False and moveok=True, the file might be delivered by a "mv" operation, avoiding any data duplication (no "cp"). @param verify If a "cp" is done, reopen the target and source and verify they are the same. Note that providing a copier will break the verification functionality if the copier changes the contents of the destination file (such as a copier that compresses). @param blocksize block size during copy operations @param tempprefix Prefix for temporary files during copy operations. Do not include directory paths in the tempprefix. @param permmask Permission bits to remove Default: world write (002) @param removefailed If True, delete temporary files if the delivery fails @param logger the logging.Logger for log messages. @param preserve_perms If True, copy the old file's permissions to the new file @param preserve_times If True, copy the old file's timestamps to the new file @param preserve_group If True, copy the old file's group ID to the new file @param copy_acl If True, copy the access control lists from one file to another @param moveok If True, delivery by "mv" is allowed. Must also set keep=False. @param force If False, delivery will be aborted (raise TargetFileExists) if the target file already exists. @param copier If present, this function or callable object is used to copy data from the source file to the temporary file before moving it to the target. The copier is called as copier(infile,temp_file_name,temp_file_object) Where the temp_file_name is the name of the destination file and the temp_file_object is an object that can be used to write to the file. The copier should NOT close the temp_file_object. NTruz+This subroutine cannot deliver directories.roz%s: is a directory; file is %srT.z6%s: exists, so parent %s must exist and be a directoryz;Target does not exist, and parent of target does not exist.z?Target does not exist, and parent of target is not a directory.zA%s: exists and overwrite (force) is disabled. Aborting delivery.z5%s: destination is a link, will recopy as a non-link.z%s: same as %sz%s: exists, replacing with %sz%s: does not existz:%s: cannot deliver via "os.rename" since source is a link.z%s: move from %sz&%s: could not deliver by os.rename: %sztmp.z.part.F)prefixdeletedirz%s: copy to temporary %srL)Zlengthz%s: verify copy %szfilecmp.cmp returned Falsez'%s: copy group ID and permissions to %sz!%s: cannot copy groupid to %s: %sz%s: delivery failed: %s).produtilclusterZ group_quotasZuse_acl_for_rstdatarpr<r=basenamerrrS_ISDIRrtr rjdebugdirnamerPrsrCsamestatst_devrenamerDrBcloser^tempfileZNamedTemporaryFilenamerIshutilZ copyfileobjfilecmpZcmpr chownst_gidIOErrorOSErrorrEchmodrJst_atimest_mtime Exceptionerror)ZinfileZoutfilekeepZverifyZ blocksizeZ tempprefixZpermmaskZ removefailedrFZpreserve_permsZpreserve_timesZpreserve_groupZcopy_aclZmoveokforceZcopierZinbaseZilstatZistatZactual_outfileZoutdirZoflstatZofstatZodlstatZodstatZsamefsrGZtemptempnameZindatar(r(r)rZs"=                                         cCstj|}||kr$td|f||dkr %sz-FATAL ERROR: source: "%s" not exist, Exiting.rSz-Target is a directory. Symlink to %s insteadz_FATAL ERROR: FILESYSTEM FAILURE: Cannot symlink "%s" -> "%s". Instead, the symlink is to "%s".z-target exists - using replace_symlink insteadrF) module_loggerrCr<r=r[criticalsysexitrZrjrsymlinkreadlinkr5rDr_ZEEXISTr)rrrrF max_triescontentrnrGr(r(r)rs6          c CsF|dkr t}tjtj|dtj|tdtdf}z|dk r\|d||ft ||t |}||krd|||f}|dk r| |t |||dk r|d||ft ||Wnttk r@}zTt|t rz(|dk r|d|ft|Wntk r*YnX|W5d}~XYnXdS)a|!Do not call this routine directly: you want make_symlink instead. This routine creates a new symbolic link and renames that link to "target." That always replaces target with a symbolic link to source, even if target did not already exist. @param source the file to link from @param target the file to link to @param logger a logging.Logger for messagesNztmp.%s.%06x.%06x.tmp rzRFILESYSTEM FAILURE: Cannot symlink "%s" -> "%s". Instead, the symlink is to "%s".zrename %s to %szfailed: delete %s)rr<r=rjrrrandom getrandbitsrCrrrr5rrrUremoverD)rrrFrrrrnrGr(r(r)rs>       cCsttjd|dS)a+!Attempts to modify the given stream to be non-blocking. This only works with streams that have an underlying POSIX fileno, such as those from open. Will re-raise any exception received, other than AttributeError and EnvironmentError. Hence, I/O errors and attempts to make a non-fileno stream non-blocking will produce a False return value, while anything else will raise an exception. @param stream the stream to unblock @param logger a logging.Logger for log messages @returns True on success, False otherwise.r#N)rr< O_NONBLOCK)streamrFr(r(r)rs c Cszt|tr|}n|}WnRttfk rn}z0|dk rX|jdt|t|fddWYdSd}~XYnXz.t|tj }t|tj ||B|@WdStk r}z0|dk r|j dt|t|fddWYdSd}~XYnXdS)z!Internal function that implements unblock() @param stream the stream to modify @param on flags to turn on @param off flags to turn off @param logger a logging.Logger for messages @returns True on success, False otherwise.Nz?%s: stream has no fileno, cannot switch to non-blocking I/O: %sTr@Fz)%s: cannot switch to non-blocking I/O: %s) rUintfilenoAttributeErrorrDrEr8rBfcntlF_GETFLF_SETFLr)rZonZoffrFfdeeflagsr(r(r)rs0  cCs|dk r.|dt|t|t|t|f|D]J\}}t|tsLtdt|f}|dk rptj |t }t ||||dq6dS)a!This is a convenience routine that makes many symbolic links to fort.N files for various integers N using make_symlink. It works similarly to fortcopy. The optional basedir is the relative directory. The optional force argument is passed on to make_symlink and has the usual meaning: replace existing files. Call like this: @code fortlink({ 15:"/usr/local/share/file1", 23:"./file2"}) @endcode And you will create these symbolic links: @code{.unformatted} ./fort.15 -> /usr/local/share/file1 ./fort.23 -> ./file2 @endcode as with other symlink routines in this module, set force=True to remove target fort.N files if they already exist. @param forts Mapping from Fortran unit number to link target. @param force Remove target files if they exist. @param basedir Where to make the links instead of the current directory. @param logger A logging.Logger for log messages.Nz3in fortlink, forts=%s force=%s basedir=%s logger=%sfort.%dr) rr8itemsrUrBrprr<r=rjwherer)fortsrbasedirrFir%linkr(r(r)fortlinksrc Ks|D]\}}dt|f}|dk r4tj|t}zt||fd|i|Wqtk r}z.|dk r|d||t |f|snW5d}~XYqXqdS)aA!A convenience function for copying files to local fort.N files for various integers N using deliver_file(...,keep=True). It works similarly to fortlink. The force= argument tells fortcopy to overwrite existing files. Otherwise, an exception will be raised if the destination file already exists. The optional basedir argument is the parent directory of the fort.N. Call like this: @code fortcopy({ 15:"/usr/local/share/file1", 23:"./file2"}) @endcode And you will create files: @code{.unformatted} ./fort.15 (copied from /usr/local/share/file1) ./fort.23 (copied from ./file2) @endcode All other keyword arguments are sent to deliver_file. @param forts Mapping from Fortran unit number to copy target. @param basedir Where to put the files instead of the current directory. @param logger A logging.Logger for log messages. @param only_log_errors Only log failed operations instead of logging everything. @param kwargs All other keyword arguments are passed to deliver_file()rNrFz%%s: fortcopy could not copy to %s: %s) rrr<r=rjrrrDrErB) rrrFZonly_log_errorsrlrr%Znewfilerr(r(r)r5s cCs\|dkrt}tjtj|}|rrr)r=Zfullnormrr(r(r)r_s  cCstj|t|S)a!Return relative path. This routine generates relative file paths (using os.path.relpath) that are relative to the specified "from" directory fromdir. The fromdir will be first sent through norm_expand_path to eliminate system-specific weirdness, such as a/./b, a/../b, ~username and so on. This will raise RelativePathError if the resulting path is not absolute. @param rel the path @param fromdir the directory from which we want the relative path)r<r=relpathr)ZrelZfromdirr(r(r)rws 'c Cstt|d}z|| tjWn@tk rd}z"|dk rT|d|t|fW5d}~XYnXd}|D]*}|d7}||dkrnW5QRdSqn|dk r|d||fW5QRdSQRXdS) a!Checks the last few bytes of a file to see if the specified search string is present. Returns True if the string is present or False if the file existed but the string was not present. Will raise an exception if the file is non-existent or cannot be read. @param filename The file to search (a string). @param searchstr The string to search for. Must not contain end-of-line chars. @param lastbytes The number of bytes at the end of the file to check. Can be larger than the file size. @param logger A logging.Logger for log messages. @returns True if the file contains the given string, False otherwiserNz%s: probably not an error: %sr#rTTz%s: read %d linesF)rIrBseekr<SEEK_ENDrDrCfind)r%Z searchstrZ lastbytesrFrQrGrliner(r(r)rs  c Csrzt|}|j|kr6|dk r0|d|fWdS|dk sN|dk sN|dk rtt}|dk r||j|ks|dk r|d|fWdS|dk r||j|ks|dk r|d|fWdS|dk r||j|ks|dk r|d|fWdS|dk r|d|fWdSt k rl}z8|j t j krZ|dk rP|d |fWY dSW5d}~XYnXdS) a!Determines whether the specified file exists, and meets additional requirements. @param filename The file to analyze. @param min_size If present, the file must be at least this many bytes. @param min_mtime_age If specified, the file must have been modified more than this many seconds in the past. @param min_atime_age if specified, the file atime must be at least this many seconds old. The meaning of atime varies, but usually means the last access time. @param min_ctime_age If specified, the file ctime must be at least this many seconds old. The meaning of ctime varies between platforms and file types, but usually means the file creation or inode change time. See stat(2) for details. @param logger a logging.Logger for log messages. @note This routine can also be used on directories, but one should avoid the min_size option when doing that. @returns True if requirements are met, False otherwise. Nz %s: too smallFz&%s: not old enough (modification time)z %s: not old enough (access time)z&%s: not old enough (inode change time)z%s: file meets requirementsTz%s: does not exist (ENOENT)) r<rrrvrCrtimerrst_ctimerDr_r`) r%min_size min_mtime_age min_atime_age min_ctime_agerFrxnowrGr(r(r)rsP      c@sVeZdZdZdddZddZddd Zd d Zd d ZddZ ddZ dddZ dS)rz8!A class that waits for files to meet some requirements.N?cCsPt|_t|_t|_||_||_||_||_t ||_ |dk rL| |dS)a!Constructor for the FileWaiter. Most arguments have the same meaning as check_file() @param flist the file or list of files to wait for. This is simply sent into self.add. @param min_size minimum file size @param min_mtime_age minimum modification time age, @param min_atime_age minimum access time age. @param min_ctime_age time since last file status change (see stat(2)) @param min_fraction the minimum fraction of the provided files that must match the above requirements in order for FileWaiter.wait to return True. Default is 1.0, which means all of them.N) rh_flistset_fset_foundrrrrfloat min_fractionadd)r'flistrrrrrr(r(r)r*s zFileWaiter.__init__cCsJt|tr2||jkrdS|j||j|n|D]}||q6dS)z!Adds a file, or iterable that iterates over files, to the list of files to wait for. If the same filename is received a second time, it is ignored. @param flist a filename (string) or list of filenamesN)rUrBrrrir)r'rfiler(r(r)rs   zFileWaiter.addcCst||j|j|j|j|dS)a!Checks to see if one file meets the requirements set in the constructor. This default implementation calls check_file. This is in a separate member function so that a subclass can override the file checking method. @returns True if the file is "ready," and False if it is not. @param filename the path to the file to check @param logger a logging.Logger for messagesr)rrrrr)r'r%rFr(r(r)checks  zFileWaiter.checkcCs t|_dS)zF!Resets internal information about which files have been seen.N)rrr+r(r(r)reset szFileWaiter.resetccs|jD] }|VqdS)z)!Iterates over all files that were found.N)r)r'r%r(r(r) iterfound%s zFileWaiter.iterfoundcCs t|jS)z-!Returns the number of files that were found.)rPrr+r(r(r) countfound)szFileWaiter.countfoundcCst|jt|jS)z1!Returns the number of files that were NOT found.)rPrrr+r(r(r) countmissing,szFileWaiter.countmissingrTc Cst|}tt}|}d}|r&|}nd}ddkrt|jdkrX|dk rT|ddSt|jt|j} tt}t|j} t|j} t| | } t|j | } | |j dkr|ddS|||kr|ddS|st dt ||||d }|d kr|ddS|dk r|d t|jt|j|j d | | d krPd ndf|dkrj|jn|j }|dt|ft ||dk r|dd}|jD]\}||jkrq|j||dr|j||dk r|d|t|jt|jfqq*t|jt|jkS)aw!Looks for the requested files. Will loop, checking over and over up to maxwait seconds, sleeping sleeptime seconds between checks. @param maxwait maximum seconds to wait @param sleeptime sleep time in seconds between checks @param logger a logging.Logger for messages @param log_each_file log messages about each file checkedTNr#zNo files to check.gh㈵>z Have required fraction of files.zWaited too long. Giving up.FrTgMbP?zCStill need files: have %d of %d, but need %g%% of them (%g file%s).gY@rxr]zSleeping %g seconds...zDone sleeping.rz$%s: found this one (%d of %d found).)rrrPrrCrrmathceilrmaxminrsleeprrr)r'maxwait sleeptimerF log_each_filestartrfirstZfloggerleftZnfilesZnfoundZfracZ needfilesZsleepnowZlogfunr%r(r(r) checkfiles0sn                      zFileWaiter.checkfiles)NNNNNr)N)rrNT) r0r1r2r3r*rrrrrrrr(r(r(r)rs  ( rrTrc Cs"t||||||} | |||| S)a !Waits for files to meet requirements. This is a simple wrapper around the FileWaiter class for convenience. It is equivalent to creating a FileWaiter with the provided arguments, and calling its checkfiles routine. @param flist the file or list of files to wait for. This is simply sent into self.add. @param logger a logging.Logger for messages @param maxwait maximum seconds to wait @param sleeptime sleep time in seconds between checks @param min_size minimum file size @param min_mtime_age minimum modification time age, @param min_atime_age minimum access time age. @param min_ctime_age time since last file status change (see stat(2)) @param min_fraction the minimum fraction of the provided files that must match the above requirements in order for FileWaiter.wait to return True. Default is 1.0, which means all of them. @param log_each_file log messages about each file checked )rr) rrFrrrrrrrrwaiterr(r(r)rss  )N)N)rXN)TN)F)TFrzNrSTNTTNNTTN)NT)rN)FNNF)FNr)Nr)N)N)FNN)NNF)NF)N)rN)NNNNN) NrrrTrNNrT)9r3__all__r<rrrrrrr_rrrrloggingprodutil.clusterrprodutil.pipeline getLoggerrrrrrrrrrrr5r r r r r"r!r r rrrrrrrrrrrrrrrrrrrrr(r(r(r)s ` !       [   : & $   " *   ! 8