U

����� g�����������������������@���sN���d�Z�dgZddlZddlZddlZddlZddlZddlZG�dd��de�Z	dS�)z4!Contains the Listing class, which emulates "ls -l".�Listing�����Nc�������������������@���s\���e�Zd�ZdZddd�Zdd��Zd	d
��Zdd��Zdd
d�Zdd��Z	dd��Z
dd��Zdd��ZdS�)r���a���!Imitates the shell "ls -l" program.

    Imitate ls -l, but with a longer mtime string:
    @code
       print Listing("/usr/local")
    @endcode

    To include files whose names begin with "."  add hidden=True:
    @code
       print Listing("/usr/local",hidden=True)
    @endcode

    To log messages related to failures of lstat and readlink, pass 
    a logging.Logger:
    @code
       print Listing("/usr/local",hidden=True,logger=logger)
    @endcode�.FNc�����������������C���s$���||�_�t��|�_|�jt|�|d��dS�)z�!Constructor for Listing:
        @param path The directory path to list.
        @param hidden If True, files with names beginning with "." are listed.
        @param logger A logging.Logger for error messages.)�hidden�loggerN)�_Listing__path�dict�_Listing__contents�list�bool)�self�pathr���r�����r
����=/lfs/h1/ops/prod/packages/hmon.v3.2.7/ush/produtil/listing.py�__init__���s����zListing.__init__c�����������������c���s���|�j����D�]
}|V��q
dS��z1!Iterates over filenames in the listed directory.N��contents�keys�r����namer
���r
���r����__iter__$���s����zListing.__iter__c�����������������c���s"���|�j����D�]\}}||fV��q
dS�)z�!Iterates over name,data pairs in the listed directory.  The
        "data" will be a tuple containing the output of lstat and the
        output of readlink.N)r����items)r���r����datar
���r
���r����	iteritems(���s����zListing.iteritemsc�����������������c���s���|�j����D�]
}|V��q
dS�r���r���r���r
���r
���r����iterkeys.���s����zListing.iterkeysc�����������������C���sB��t�|�}|�j}t�|�}t��}|D��]}|d�dkr:|s:q"tj�||�}zt�|�}W�nL�tk
r��}	�z.|dk	r�|j	d|t
|	�f�dd��W�Y��q"W�5�d}	~	X�Y�nX�t�|j
�s�|df||<�q"zt�|�}
W�n\�tk
�r(�}	�z<|dk	�r|j	d|t
|	�f�dd��|df||<�W�Y��q"W�5�d}	~	X�Y�nX�||
f||<�q"||�_dS�)	a��!Updates the internal data structures with a new listing of
        the directory.  Arguments are the same as for the constructor.
        @param hidden If True, files with names beginning with "." are listed.
        @param logger A logging.Logger for error messages.r���r���Nz%s: cannot lstat: %sT)�exc_infoz%s: cannot readlink: %s�
(**UNKNOWN**))r
���r����os�listdirr���r����join�lstat�EnvironmentError�info�str�stat�S_ISLNK�st_mode�readlinkr���)r���r���r���r����listingr����item�locr ����e�linkpathr
���r
���r���r	���3���s@����

��
��zListing.listc�����������������C���s����|�j�}ddddddg}t��}|���D�]N\}}|��||�}tt|�d��D�]}t||�t||���||<�qH|�|��q$d}|dd��}|t|��}t	�
��}	|D�]}|	�||���q�|	���}
|	�
���|
S�)z3!Generates an ls -l style listing of the directory.r�������z) %%%ds %%%ds %%%ds %%%ds %%%ds %%%ds %%s
N)r���r	���r����_stritem�range�len�max�append�tuple�io�StringIO�write�getvalue�close)r����cZsizesZrowsr���r)����row�col�format�s�str
���r
���r����__str__U���s"����zListing.__str__c�����������������C���s���|d�}|d�}|dkrd}|j�}d}t�|�r:|d7�}nlt�|�rN|d7�}nXt�|�rb|d7�}nDt�|�rv|d	7�}n0t�|�r�|d
7�}nt�|�r�|d7�}n|d7�}||tj@�r�d
nd7�}||tj	@�r�dnd7�}|tj
@�r�|tj@�r�|d7�}n|d7�}n|tj@��r|d7�}n|d7�}||tj@��r*d
nd7�}||tj
@��rBdnd7�}|tj@��rt|tj@��rj|d7�}n|d7�}n|tj@��r�|d7�}n|d7�}||tj@��r�d
nd7�}||tj@��r�dnd7�}|tj@��r�|tj@��r�|d7�}n|d7�}n|tj@��r|d7�}n|d7�}tt|j��}|��|j�}|��|j�}	tt|j��}
t�t�|j��}t�|j���rz||||	|
||d�|�fS�||||	|
||fS�dS�)a���!This is an internal implementation function.  Do not call
        it directly.  It generates one line of ls output as a tuple of
        strings, one string per column of information (the mtime is
        one column).  The __str__ turns the lines into one big string.
        @param name the filename
        @param item details about the file
        @returns data for each column of the listingr���r-���Nr������dr9����b�p�lr=����-�r�w�x�S�t�Tz -> ) r&���r$����S_ISDIR�S_ISCHR�S_ISBLK�S_ISFIFOr%����S_ISSOCK�S_IRUSR�S_IWUSR�S_IXUSR�S_ISUID�S_IRGRP�S_IWGRP�S_IXGRP�S_ISGID�S_IROTH�S_IWOTH�S_IXOTH�S_ISVTXr#����int�st_nlink�	_username�st_uid�
_groupname�st_gid�st_size�time�asctime�	localtime�st_mtime)r���r���r)���r ���r,����moder=���Znlink�usernameZ	groupname�size�whenr
���r
���r���r.���i���sl�����
�

�

�

�

�

�











zListing._stritemc��������������
���C���s>���zt��|�}t|j�W�S��tttfk
r8���t|��Y�S�X�dS�)z4!Return the group name for a group ID from getgrgid.N)�grp�getgrgidr#����gr_name�KeyError�
ValueErrorr!���)r����gidZgrr
���r
���r���ra�������s
����
zListing._groupnamec��������������
���C���s>���zt��|�}t|j�W�S��tttfk
r8���t|��Y�S�X�dS�)z3!Return the user name for a group ID from getpwuid.N)�pwd�getpwuidr#����pw_namero���rp���r!���)r����uid�pwr
���r
���r���r_�������s
����
zListing._username)r���FN)FN)
�__name__�
__module__�__qualname__�__doc__r���r���r���r���r	���r?���r.���ra���r_���r
���r
���r
���r���r���
���s���

"K)
rz����__all__r���r$���r4���rl���rr���rd����objectr���r
���r
���r
���r����<module>���s���0