U

����� g��������������������"���@���sp��d�Z�ddddddddd	d
dd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$lZd#d$lZd#d$l
Zd#d$lZe�d%�ZG�d&d��de�ZG�d'd��de�ZG�d(d��de�ZG�d)d��de�ZG�d*d��de�ZG�d+d��de�ZG�d,d��de�ZG�d-d��de�ZG�d.d/��d/e�ZG�d0d	��d	e�ZG�d1d
��d
e�Zd2d��ZdZd3d�Zd[d4d"�Z d5d!��Z!d6d ��Z"d\d8d
�Z#d]d:d�Z$d;d��Z%d^d=d�Z&d>d��Z'd_dAd�Z(d`dBd�Z)dadDdE�Z*dbdFd�Z+dcdHd�Z,dddId�Z-dedJd�Z.dfdKd�Z/dgdLdM�Z0dhdNd�Z1didOd�Z2djdPd�Z3dkdRd�Z4dldSd�Z5G�dTd��d�Z6dmdYd�Z7d$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�FileOpErrors�CannotLinkMulti�UnexpectedAbsolutePath�InvalidExecutable�FindExeInvalidExeName�
CannotFindExe�RelativePathError�DeliveryFailed�VerificationFailed�realcwd�chdir�makedirs�remove_file�rmall�
lstat_stat�
isnonempty�
check_file�deliver_file�make_symlinks_in�find_exe�make_symlink�replace_symlink�unblock�fortcopy�norm_expand_path�
norm_abs_path�check_last_lines�wait_for_files�
FileWaiter�call_fcntrl�gribver�	netcdfver�touch�����Nzprodutil.fileopc�������������������@���s,���e�Zd�ZdZg�fdd�Zdd��Zdd��ZdS�)	r���zg!This is the superclass of several exceptions relating to
    multi-file operations in produtil.fileop.c�����������������C���s���||�_�||�_||�_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)�message�filename�more)�selfr$���r%���r&�����r(����</lfs/h1/ops/prod/packages/hmon.v3.2.7/ush/produtil/fileop.py�__init__���s����zFileOpError.__init__c�����������������C���s���d|�j�|�jf�S�)z%!A string description of the problem.z%s: %s)r%���r$����r'���r(���r(���r)����__str__3���s����zFileOpError.__str__c�����������������c���s"���|�j�D�]\}}}|||fV��qdS�)z�!Iterates over 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)r&���)r'����fromfile�tofiler$���r(���r(���r)����__iter__6���s����zFileOpError.__iter__N)�__name__�
__module__�__qualname__�__doc__r*���r,���r/���r(���r(���r(���r)���r������s���c�������������������@���s���e�Zd�ZdZdS�)r���zn!This exception is raised when an operation that processes
    multiple files catches more than one exception.N�r0���r1���r2���r3���r(���r(���r(���r)���r���<���s���c�������������������@���s���e�Zd�ZdZdS�)r���z�!This exception is raised when the caller tries to create
    multiple symlinks in a single target, but the target is not a
    directory.Nr4���r(���r(���r(���r)���r���?���s���c�������������������@���s���e�Zd�ZdZdS�)r���zk!This exception indicates that the renamer function sent to
    make_symlinks_in returned an absolute path.Nr4���r(���r(���r(���r)���r���C���s���c�������������������@���s���e�Zd�ZdZdS�)r���z!Thrown when a find_exe fails.Nr4���r(���r(���r(���r)���r���F���s���c�������������������@���s���e�Zd�ZdZdS�)r���zU!Thrown when find_exe is given an executable name that contains
    a directory path.Nr4���r(���r(���r(���r)���r���H���s���c�������������������@���s���e�Zd�ZdZdS�)r���zR!Thrown when find_exe cannot find an executable in the path or
    directory list.Nr4���r(���r(���r(���r)���r���K���s���c�������������������@���s���e�Zd�ZdZdS�)r���zL!Raised when a relative path is given, but an absolute path is
    expected.Nr4���r(���r(���r(���r)���r���N���s���c�������������������@���s���e�Zd�ZdZdS�)�WrongSymlinkz�!Raised when os.symlink makes a symlink to a target other than
    the one that was requested.  This is present to detect a bug in
    Cray where os.symlink randomly makes a symlink to the wrong
    place.Nr4���r(���r(���r(���r)���r5���R���s���r5���c�������������������@���s(���e�Zd�ZdZdd��Zdd��Zdd��ZdS�)	r	���z:!This exception is raised when a file cannot be delivered.c�����������������C���s���||�_�||�_||�_dS�)z�!DeliveryFailed constructor.
        @param message why the delivery failed
        @param fromfile what was being delivered
        @param tofile delivery destinationN)r$���r-���r.���)r'���r$���r-���r.���r(���r(���r)���r*���Z���s����zDeliveryFailed.__init__c�����������������C���s���d|�j�|�j|�jf�S�)�*!Human-readable description of this error.z %s: cannot deliver (from %s): %s)r.���r-���r$���r+���r(���r(���r)���r,���k���s
�������zDeliveryFailed.__str__c�����������������C���s ���dt�|�j�t�|�j�t�|�j�f�S�)�'!Pythonic representation of this error.zDeliveryFailed(%s,%s,%s))�reprr$���r-���r.���r+���r(���r(���r)����__repr__o���s�����zDeliveryFailed.__repr__N�r0���r1���r2���r3���r*���r,���r9���r(���r(���r(���r)���r	���X���s���c�������������������@���s(���e�Zd�ZdZdd��Zdd��Zdd��ZdS�)	r
���z\!This exception is raised when a copy of a file has different
    content than the original.c�����������������C���s���t��|�|||��||�_dS�)z�!VerificationFailed constructor.
        @param message why the delivery failed
        @param fromfile what was being delivered
        @param tofile delivery destination
        @param verifyfile temporary file that failed verificationN)r	���r*����
verifyfile)r'���r$���r-���r.���r;���r(���r(���r)���r*���w���s����zVerificationFailed.__init__c�����������������C���s���d|�j�|�j|�j|�jf�S�)r6���z:%s: verification failed on temporary file %s (from %s): %s)r.���r;���r-���r$���r+���r(���r(���r)���r,�������s�����zVerificationFailed.__str__c�����������������C���s(���dt�|�j�t�|�j�t�|�j�t�|�j�f�S�)r7���zVerificationFailed(%s,%s,%s,%s))r8���r$���r-���r.���r;���r+���r(���r(���r)���r9�������s����"�zVerificationFailed.__repr__Nr:���r(���r(���r(���r)���r
���t���s���c�������������������C���s���t�j�t�����S�)zI!Returns the current working directory, expanding any symbolic
    links.)�os�path�realpath�getcwdr(���r(���r(���r)���r�������s����c��������������
���C���sp���t�|��}�z$|dk	r |�|�d���t�|���W�n>�tk
rj�}�z |j|�d�t�|��dd����W�5�d}~X�Y�nX�dS�)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: T��exc_info)�str�infor<���r����EnvironmentError�warning)r=����logger�er(���r(���r)���r�������s�����c��������������	���C���s&���t�|�d���t�|�|��W�5�Q�R�X�dS�)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��������������	���C���s����t�|�d��x}|�d�}t|�dk�r0W�5�Q�R���dS�|dd��}|dkrRW�5�Q�R���dS�|dkrhW�5�Q�R���d	S�|d
kr~W�5�Q�R���dS�W�5�Q�R�X�dS�)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 test�rb��������Nr#���zCDFZCDF1zCDFZCDF2u	���‰HDF

ZHDF5)rI����read�len)r%����f�eightZfourr(���r(���r)���r!�������s����

c��������������	���C���s����t�|�t�s$tdt|��jt|��f���t|��s0dS�t|�d��R}|�d�}|dkr\W�5�Q�R���dS�|dd��d	krzW�5�Q�R���d
S�W�5�Q�R���dS�W�5�Q�R�X�dS�)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.NrL���rM���zGRIB�������r#���rN���ZGRIB����)	�
isinstancerB����	TypeError�typer0���r8���r���rI���rO���)r%���rQ���rR���r(���r(���r)���r �������s����
�
�
���c�����������������C���s����t�|�D�]�}z0tj�|��s:|dk	r0|�|�d���t�|���W�q�tk
r��}�zBtj�|��rfW�Y��0�dS�tj�|��rv��n||d�kr�W�Y��q��W�5�d}~X�Y�qX�qdS�)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=����isdirrC���r
���rD����exists)r%���ZnumtriesrF����nrG���r(���r(���r)���r
�������s����Tc��������������
���C���s����|�dks|�dkrdS�z&|dk	r.|��d|�f���t�|���W�n~�tk
r��}�z`|jtjkr||dk	rz|jd|�t|�f�dd����|dk	r�|r�|j�n|j}|d|�t|�f���W�5�d}~X�Y�nX�dS�)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 messagesN��z%s: remove filez%s: cannot remove: %sTr@���z%%s: cannot remove; does not exist: %s)rC���r<����unlinkrD����errno�ENOENTrE���rB���)r%���rC���rF���rG����logr(���r(���r)���r�������s ����
��c������������������O���s����|��dd�}|dk	r(|�dt|��f���t��}|�D�]H}zt|f|��W�q2�tk
rx�}�z|�|d|f��W�5�d}~X�Y�q2X�q2t|�dkr�|d�d��n>t|�dkr�d}|dk	r�|�|��t|d�	|��dd	��|D����|dk	r�|�d
t|��f���dS�)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().rF���NzRemoving %d files...rT���r#���z9Multiple exceptions caught while deleting files in rmall.�,c�����������������S���s ���g�|�]\}}}||t�|�f�qS�r(���)rB���)�.0rH����b�cr(���r(���r)����
<listcomp>.��s�����zrmall.<locals>.<listcomp>zDone removing %d files...)
�getrC���rP����listr���rD����appendrE���r����join)�args�kwargsrF����ex�argrG����msgr(���r(���r)���r�����s(����$�
�Fc��������������
���C���s����|�dk	st��d\}}z.t�|��}t�|j�s6||fW�S�t�|��}W�n4�tk
rv�}�z|sd|jtjkrf��W�5�d}~X�Y�nX�||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.N�NN)	�AssertionErrorr<����lstat�stat�S_ISLNK�st_moderD���r_���r`���)r%����raise_nonexistZxlstatZxstatrG���r(���r(���r)���r���3��s����

c�����������������C���sJ���|�dkrdS�t�|��}|dks$|dkr(dS�t|��\}}|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#���)rB���r����st_size)r%���Zsfile�l�s�retr(���r(���r)���r���M��s����������rS���c�����������������C���s`��|dkrt�j����}|dkr&t�j���}|dk	r2d}|�dk	s>t�|dk	sJt�tj�|��}t|�dd�\}}t	�
|j�r~td|�|��|}d}t|�\}}d\}}|dk	r�t	�
|j�r�|}tj�
||�}||�}}|dk	r�|�d||f���t|�\}}|dk�rttj�|�}t|�dk��rd}|dk	�r4|�d	||f���t|dd�\}}|dk�rZtd
|�|��t	�
|j��sttd|�|��|dk	�r�|�s�|dk	�r�|�d|f���|dk	�r t	�|j��r�|dk	�r|�d
|f���n0tj�||��r|dk	�r�|�d||�f���dS�|dk	�r8|�d||�f���n|dk	�r8|�d|���|j|jk}|�r�|�s�|
�r�t	�|j��r�|dk	�r�|�d|�f���nt|dk	�r�|�d||�f���zt�|�|��W�dS��tk
�r��}�z$|dk	�r�|�d|t|�f���W�5�d}~X�Y�nX�d}d}�z�z�|dk�rd|�d�}tj|d|d�}|j}|dk	�rL|�d|�|f���|dk�r~t|�d��}tj|||d��W�5�Q�R�X�n||�||��|����d}|�r�|dk	�r�|�d|�|f���t �!|�|��s�t"d|�||��|dk	�r�|�d|�|f���|�rVzt�#|d|j$��W�nJ�t%t&fk
�rT�}�z&|dk	�rD|�'d|�|t|�f���W�5�d}~X�Y�nX�|	�rpt�(||j|�@���|dk	�r�|�d||f���|
�r�t�)||j*|j+f��t�||��d}W�nF�t,k
�r��}�z&|dk	�r�|�-d |�t|�f�����W�5�d}~X�Y�nX�W�5�z0|dk	�r|����|�r2|dk	�r2t�|��W�n$�tk
�rX�}�zW�5�d}~X�Y�nX�X�dS�)!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. NT�rv���z+This subroutine cannot deliver directories.rp���z%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)�prefix�delete�dirz%s: copy to temporary %srL���)�lengthz%s: verify copy %szfilecmp.cmp returned Falsez'%s: copy group ID and permissions to %s�����z!%s: cannot copy groupid to %s: %sz%s: delivery failed: %s).�produtilZclusterZgroup_quotasZuse_acl_for_rstdatarq���r<���r=����basenamer���rs����S_ISDIRru���r	���rj����debug�dirnamerP���rt���rC����samestat�st_dev�renamerD���rB����closer^����tempfileZNamedTemporaryFile�namerI����shutil�copyfileobj�filecmpZcmpr
����chown�st_gid�IOError�OSErrorrE����chmodrJ����st_atime�st_mtime�	Exception�error)�infile�outfile�keepZverify�	blocksizeZ
tempprefixZpermmaskZremovefailedrF���Zpreserve_permsZpreserve_timesZpreserve_groupZcopy_aclZmoveok�force�copierZinbaseZilstatZistatZactual_outfile�outdirZoflstatZofstatZodlstatZodstatZsamefsrG����temp�tempnameZindatar(���r(���r)���r���Y��s"���=
��
�
�
�
����
�

�

��


�


�
��


���
�
�


c�����������������C���s����t�j�|��}||�kr$td|f�|���|dkr<t�jd��d�}|D�]@}|dkrPd}t�j�||��}t�j�|�r@t��|t�j	�r@|��S�q@|s�dS�t
d|���dS�)a���!Searches the $PATH or a specified iterable of directory names
    to find an executable file with the given name.  

    Returns the exectuable's location.  If the executable cannot be
    found, and raise_missing=True, raises CannotFindExe, otherwise
    returns None.  Raises FindExeInvalidExeName if "name" is not the
    same as its os.path.basename.

    @param name The name of the executable to find.
    @param dirlist The list of directories to search, or None to search $PATH
    @param raise_missing If True, the CannotFindExe exception is
      raised for executables that cannot be found.  If False, return
      None in that situation.zIexecutable name is not the same as its basename in find_exe (basename=%s)N�PATH�:r]���r}���zcannot find executable)r<���r=���r����r����environ�splitrj����isfile�access�X_OKr���)r����ZdirlistZ
raise_missingZbnr����Zexenamer(���r(���r)���r���3��s&�������
��(���c��������������
���C���sj���zBt�|�d��.}|�|�}|dk	r6|�d|�t|�f���W�5�Q�R�X�W�n"�tk
rd�}�zW�5�d}~X�Y�nX�dS�)ab��!Opens the specified file for reading and attempts to read data
    to it.  Logs the process.  Will NOT raise any I/O or system
    errors; they are ignored.  This is a workaround for a bug in Cray:
    symlinks to recently created files cannot be read by the compute
    node unless the batch node reads from them first (or unless you
    wait a while).�rtNz%s: read %s)rI���rO���rC���r8���rD���)r%����readsizerF���rQ����bufrG���r(���r(���r)����symlink_read_testR��s����
$r����c�����������������C���sv��t�|dd�\}}t�|j�s&td|��g�}|�D��]&}	d}
z�|dk	r|||	�}
|
dkrVW�q.tj�|
�rltdt	��tj�
||
�}
ntj�
|tj�|	��}
|r�tj�|	�r�|r�tj�|
�r�t�
|
��t|	|
d|d��q�|dk	r�|�d|
|	f���nt|	|
||d��W�q.�tk
�rT�}�z8|�|	t|
�t|�f��|dk	�rD|jt|�dd	��W�5�d}~X�Y�q.X�q.t|�d
k�rrtd||��dS�)a���!Creates symbolic links from a set of source files to a target
    directory.  If "force" is True, then any existing files will first
    be deleted.

    The "renamer" can be a function that generates paths of the
    symlinks, relative to targetdir, for each symlink in "sources".
    If the return value from "renamer" is an absolute path, an
    exception will be thrown.  If the return value is None, then no
    link will be made.

    Example:
    make_symlinks_in(['/path/to/a','/path/to/b'],'.',
      renamer=lambda s: os.path.basename(s)+'.linkified')

    will create a.linkified, linked to /path/to/a, and b.linkified,
    linked to /path/to/b in directory "." 
    @param sources The list of files to link to.
    @param targetdir The directory in which to place the links.
    @param force Remove existing files if needed.
    @param renamer Function to generate link names.
    @param logger A logging.Logger for log messages.
    @param copy If True, files are copied instead of linked.Tr|���ztarget is not a directoryNzrenamed path is absolute)r����rF���z:%s: skip.  Am in copy mode, and source does not exist (%s)�r����rF���r@���r#���zcannot link files)r���rs���r����ru���r���r<���r=����isabsr���Zrenamedrj���r����r[���r^���r���rE���r���rD���ri���rB���rP���r���)�sourcesZ	targetdirr����ZrenamerrF����copyZtlstatZtstat�errors�source�targetrG���r(���r(���r)���r���a��sJ����

���
���
&����c��������������
���C���s��|dkrt�}|dk	r&|�d||�f���tj�|�r`tj�|tj�|���}|dk	r`|�d|f���zNt�|�|��t�|�}||�kr�d||�|f�}|dk	r�|�	|��t
||��n�W�nZ�tk
�r�}�z:|jtj
kr�|sԂ�|dk	r�|�d��t|�||d��W�Y��S�d}~X�Y�nX�dS�)a���!Creates a symbolic link "target" that points to "source".  If
    the target already exists and is NOT a directory, then the file
    will be replaced.  The replacement is done in a unit operation so
    that the target will always exist (unless the operation fails).
    @param source The file to link to.
    @param target The name of the link.
    @param force If True, and target exists, delete it first.
    @param logger a logging.Logger for log messages.N�
link %s -> %sz-Target is a directory.  Symlink to %s instead�RFILESYSTEM FAILURE: Cannot symlink "%s" -> "%s".  Instead, the symlink is to "%s".z-target exists - using replace_symlink instead�rF���)�
module_loggerrC���r<���r=���rZ���rj���r�����symlink�readlink�criticalr5���rD���r_���ZEEXISTr���)r����r����r����rF����	max_tries�contentro���rG���r(���r(���r)���r������s.����	�


c�����������������C���sF��|dkrt�}tj�tj�|�dtj�|�t�d�t�d�f��}z�|dk	r\|�d||�f���t�	|�|��t�
|�}||�kr�d||�|f�}|dk	r�|�|��t||��|dk	r�|�d||f���t�
||��W�nt�tk
�r@�}�zTt|t�r��z(|dk	�r|�d|f���t�|��W�n�tk
�r*���Y�nX�|�W�5�d}~X�Y�nX�dS�)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� ���r����r����zrename %s to %szfailed: delete %s)r����r<���r=���rj���r����r�����random�getrandbitsrC���r����r����r����r5���r����r����rU����removerD���)r����r����rF���r����r����r����ro���rG���r(���r(���r)���r������s>��������



�
�c�����������������C���s���t�tjd|��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)r���r<����
O_NONBLOCK)�streamrF���r(���r(���r)���r������s����
c��������������
���C���s����zt�|�t�r|�}n|����}W�nR�ttfk
rn�}�z0|dk	rX|jdt|��t|�f�dd��W�Y��dS�d}~X�Y�nX�z.t�|tj	�}t�|tj
||B�|�@���W�dS��tk
r��}�z0|dk	r�|jdt|��t|�f�dd��W�Y��dS�d}~X�Y�nX�dS�)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)rU����int�fileno�AttributeErrorrD���rE���r8���rB����fcntlZF_GETFLZF_SETFLr����)r����ZonZoffrF����fd�ee�flagsr(���r(���r)���r������s0����
����c�����������������C���s����|dk	r.|��dt|��t|�t|�t|�f���t|�����D�]J\}}t|t�sPt�dt|�f�}|dk	rttj	�
|t�}t||||d��q:dS�)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=%s�fort.%dr����)
r����r8���rh����itemsrU���rB���rq���r����r<���r=���rj����wherer���)�fortsr�����basedirrF����ir%����linkr(���r(���r)����fortlink��s���������r����c�����������	������K���s����t�|�����D�]�\}}dt|�f�}|dk	r8tj�|t�}zt||fd|i|���W�q�tk
r��}�z.|dk	r�|�	d||t
|�f���|s���n��W�5�d}~X�Y�qX�qdS�)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()r����NrF���z%%s: fortcopy could not copy to %s: %s)rh���r����r����r<���r=���rj���r����r���rD���rE���rB���)	r����r����rF���Zonly_log_errorsrl���r����r%���Znewfiler����r(���r(���r)���r���0��s������c�����������������C���s\���|�dkrt����}�t�j�t�j�|���}|r<t�j�t�j�|��}t�j�|�sXtd|�|f���|S�)a}��!Normalizes path and expand home directories.

    Calls os.path.normpath and os.path.expanduser on its argument, or
    on os.getcwd() if no argument is supplied (or if path=None).  This
    removes extraneous a/./b, a/../b, expands ~username and ~, and
    other system-specific expansions.  See the Python documentation of
    normpath and expanduser for details.  Will also call realpath and
    normcase if fullnorm=True.  Raises RelativePathError if the
    resulting path is not absolute.
    @param path the path to expand
    @param fullnorm If True, call os.path.normcase() and
      os.path.realpath() normapth and expanduser.Nz2%s: path is relative, not absolute (expands to %s))	r<���r?���r=����normpath�
expanduser�normcaser>���r����r���)r=���Zfullnormr����r(���r(���r)���r���Z��s����
��c�����������������C���s���t�j�|�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���)�relZfromdirr(���r(���r)���r���r��s�����'��c�����������������C���s����t�t|��d���}z|�|�tj��W�n@�tk
rd�}�z"|dk	rT|�d|�t|�f���W�5�d}~X�Y�nX�d}|D�]*}|d7�}|�|�dkrn�W�5�Q�R���dS�qn|dk	r�|�d|�|f���W�5�Q�R���dS�Q�R�X�dS�)	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 otherwiser����Nz%s: probably not an error: %sr#���rT���Tz%s: read %d linesF)rI���rB����seekr<����SEEK_ENDrD���rC����find)r%���Z	searchstrZ	lastbytesrF���rQ���rG���r�����liner(���r(���r)���r������s ����

�c�����������	���
���C���sr���zt��|��}|j|k�r6|dk	r0|�d|�f���W�dS�|dk	sN|dk	sN|dk	r�tt����}|dk	r�||j�|ks�|dk	r�|�d|�f���W�dS�|dk	r�||j�|ks�|dk	r�|�d|�f���W�dS�|dk	r�||j�|ks�|dk	r�|�d|�f���W�dS�|dk	�r|�d|�f���W�dS��t	k
�rl�}�z8|j
t
jk�rZ|dk	�rP|�d	|�f���W�Y��
dS���W�5�d}~X�Y�nX�dS�)
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<���rs���rw���rC���r�����timer����r�����st_ctimerD���r_���r`���)	r%����min_size�
min_mtime_age�
min_atime_age�
min_ctime_agerF���ry����nowrG���r(���r(���r)���r������sP����

����


c�������������������@���sV���e�Zd�ZdZddd�Zdd��Zddd	�Zd
d��Zdd
��Zdd��Z	dd��Z
ddd�ZdS�)r���z8!A class that waits for files to meet some requirements.N��������?c�����������������C���sP���t���|�_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����_flist�set�_fset�_foundr����r����r����r�����float�min_fraction�add)r'����flistr����r����r����r����r����r(���r(���r)���r*������s����
�zFileWaiter.__init__c�����������������C���sJ���t�|t�r2||�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)rU���rB���r����r����ri���r����)r'���r�����filer(���r(���r)���r������s����

zFileWaiter.addc�����������������C���s���t�||�j|�j|�j|�j|d�S�)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����)r���r����r����r����r����)r'���r%���rF���r(���r(���r)����check��s
������zFileWaiter.checkc�����������������C���s���t���|�_dS�)zF!Resets internal information about which files have been
        seen.N)r����r����r+���r(���r(���r)����reset��s����zFileWaiter.resetc�����������������c���s���|�j�D�]
}|V��qdS�)z)!Iterates over all files that were found.N)r����)r'���r%���r(���r(���r)����	iterfound ��s����
�zFileWaiter.iterfoundc�����������������C���s
���t�|�j�S�)z-!Returns the number of files that were found.)rP���r����r+���r(���r(���r)����
countfound$��s����zFileWaiter.countfoundc�����������������C���s���t�|�j�t�|�j��S�)z1!Returns the number of files that were NOT found.)rP���r����r����r+���r(���r(���r)����countmissing'��s����zFileWaiter.countmissing���r����Tc��������������	���C���s��t�|�}t�t����}|}d}|r&|}nd}ddk�rt|�j�dkrX|dk	rT|�d��dS�t|�j�t|�j��}	t�t����}t|�j�}
t|�j�}t|�|
�}t�|�j	|
��}
||�j	d�kr�|�d��dS�||�|kr�|�d��dS�|�s�t
dt|||�|�d	���}|d
k��r|�d��dS�|dk	�r�|�dt|�j�t|�j�|�j	d�|
|
d	k�rPd
ndf���|dk�rj|jn|j}|dt|�f���t�
|��|dk	�r�|d��d}|�jD�]\}||�jk�r��q�|�j||d��r�|�j�|��|dk	�r�|�d|t|�j�t|�j�f����q�q*t|�j�t|�j�kS�)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.g�h㈵��>z Have required fraction of files.zWaited too long.  Giving up.FrT���g����MbP?zCStill need files: have %d of %d, but need %g%% of them (%g file%s).g������Y@ry���r]�������zSleeping %g seconds...zDone sleeping.r����z$%s: found this one (%d of %d found).)r����r����rP���r����rC���r����r�����math�ceilr�����max�minr�����sleepr����r����r����)r'����maxwait�	sleeptimerF����
log_each_file�startr�����firstZflogger�leftZnfiles�nfoundZfracZ	needfilesZsleepnowZlogfunr%���r(���r(���r)����
checkfiles+��sn����	








���


�

��zFileWaiter.checkfiles)NNNNNr����)N)r����r����NT)r0���r1���r2���r3���r*���r����r����r����r����r����r����r��r(���r(���r(���r)���r������s ����������
(
���r����rT�������r����c
�����������������C���s"���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  )r���r��)r����rF���r����r����r����r����r����r����r����r�����waiterr(���r(���r)���r���n��s
����
��)N)N)rX���N)TN)F)TFr{���NrS���TNTTNNTTN)NT)r����N)FNNF)FNr����)Nr����)N)N)FNN)NNF)NF)N)r����N)NNNNN)	Nr����r����rT���r��NNr����T)8r3����__all__r<���r����r����rs���r����r_���r����r����r����r�����loggingZprodutil.clusterr����Zprodutil.pipeline�	getLoggerr����r����r���r���r���r���r���r���r���r���r5���r	���r
���r���r���r"���r!���r ���r
���r���r���r���r���r���r���r����r���r���r���r���r���r����r���r���r���r���r���r���r���r(���r(���r(���r)����<module>���s���������������������������X
!





��������������������
�[

���
:
"
$


"
*


!�������
8������������