U
    &]^h4                     @   s   d Z ddlmZ ddlZz ddlmZmZmZmZm	Z	 W n e
k
rL   Y nX dZeeZdZdZG dd	 d	eZG d
d deZG dd deZedkredZede  dS )z Representation of ar archives for use with Debian binary packages

These classes are primarily intended to be used with the
:class:`debian.debfile.DebFile` class for working with Debian binary
packages.
    )print_functionN)AnyDictIOListOptionals   !<arch>
<   s   `
c                   @   s   e Zd ZdZdS )ArErrorz@ Common base for all exceptions raised within the arfile module N)__name__
__module____qualname____doc__ r   r   //usr/lib/python3/dist-packages/debian/arfile.pyr	   1   s   r	   c                   @   sj   e Zd ZdZdddZdd Zdd	 Zd
d ZeeZ	dd Z
dd Zdd Zdd Zdd Zdd ZdS )ArFilea   Representation of an ar archive, see man 1 ar.

    The interface of this class tries to mimic that of the TarFile module in
    the standard library.

    ArFile objects have the following (read-only) properties:
        - members       same as getmembers()
    Nrc                 C   sd   g | _ i | _|| _|| _|dkr(t }|| _|dkrJtjdkrFd}nd}|| _|dkr`| 	  dS )a   Build an ar file representation starting from either a filename or
        an existing file object. The only supported mode is 'r'.

        In Python 3, the encoding and errors parameters control how member
        names are decoded into Unicode strings. Like tarfile, the default
        encoding is sys.getfilesystemencoding() and the default error handling
        scheme is 'surrogateescape' (>= 3.2) or 'strict' (< 3.2).
        N3.2surrogateescapestrictr   )
_ArFile__members_ArFile__members_dict_ArFile__fname_ArFile__fileobjsysgetfilesystemencoding_ArFile__encodingversion_ArFile__errors_ArFile__index_archive)selffilenamemodeZfileobjencodingerrorsr   r   r   __init__?   s    
zArFile.__init__c                 C   s   | j rt| j d}n| jr"| j}ntd|ttkr@tdtj|| j | j	| j
d}|s^q| j| || j|j< |jd dkr||jd q@||jd d q@| j r|  d S )NrbzUnable to open valid filezUnable to find global header)r"   r#      r      )r   openr   r	   readGLOBAL_HEADER_LENGTHGLOBAL_HEADERArMember	from_filer   r   r   appendr   namesizeseekclose)r   fpZ	newmemberr   r   r   Z__index_archivea   s(    
zArFile.__index_archivec                 C   s
   | j | S )a   Return the (last occurrence of a) member in the archive whose name
        is 'name'. Raise KeyError if no member matches the given name.

        Note that in case of name collisions the only way to retrieve all
        members matching a given name is to use getmembers. )r   r   r/   r   r   r   	getmember|   s    zArFile.getmemberc                 C   s   | j S )z Return a list of all members contained in the archive.

        The list has the same order of members in the archive and can contain
        duplicate members (i.e. members with the same name) if they are
        duplicate in the archive itself. r   r   r   r   r   
getmembers   s    zArFile.getmembersc                 C   s   dd | j D S )z3 Return a list of all member names in the archive. c                 S   s   g | ]
}|j qS r   )r/   ).0fr   r   r   
<listcomp>   s     z#ArFile.getnames.<locals>.<listcomp>r6   r7   r   r   r   getnames   s    zArFile.getnamesc                 C   s   t dS z Not (yet) implemented. NNotImplementedErrorr7   r   r   r   
extractall   s    zArFile.extractallc                 C   s   t dS r=   r>   )r   memberpathr   r   r   extract   s    zArFile.extractc                 C   s@   | j D ]4}t|tr(|j|jkr(|  S ||jkr|  S qdS )z Return a file object corresponding to the requested member. A member
        can be specified either as a string (its name) or as a ArMember
        instance. N)r   
isinstancer,   r/   )r   rA   mr   r   r   extractfile   s    


zArFile.extractfilec                 C   s
   t | jS )z5 Iterate over the members of the present ar archive. )iterr   r7   r   r   r   __iter__   s    zArFile.__iter__c                 C   s
   |  |S )z Same as .getmember(name). )r5   r4   r   r   r   __getitem__   s    zArFile.__getitem__)Nr   NNN)r
   r   r   r   r$   r   r5   r8   propertymembersr<   r@   rC   rF   rH   rI   r   r   r   r   r   5   s"   
     
"

r   c                   @   s   e Zd ZdZdd Zed"ddZd#dd	Zd$d
dZd%ddZ	d&ddZ
dd Zdd Zdd Zdd Zdd Zedd Zedd Zedd Zedd Zedd Zed d Zed!d ZdS )'r,   a   Member of an ar archive.

    Implements most of a file object interface: read, readline, next,
    readlines, seek, tell, close.

    ArMember objects have the following (read-only) properties:
        - name      member name in an ar archive
        - mtime     modification time
        - owner     owner user
        - group     owner group
        - fmode     file permissions
        - size      size in bytes
        - fname     file namec                 C   sF   d | _ d | _d | _d | _d | _d | _d| _d | _d| _d| _	d| _
d S )N r   )_ArMember__name_ArMember__mtime_ArMember__owner_ArMember__group_ArMember__fmode_ArMember__size_ArMember__fname_ArMember__fp_ArMember__offset_ArMember__end_ArMember__curr7   r   r   r   r$      s    zArMember.__init__Nc                 C   sJ  |  t}|sdS t|tk r&td|dd tkr>tdtjdkrv|dkrXt }|dkr~tjdkrpd}q~d	}nd
}d
}t }|dd 	dd 
 }tjdkr||||_n||_t|dd |_t|dd |_t|dd |_|dd |_t|dd |_||_|s&| |_|  |_|j|j |_|j|_|S )zfp is an open File object positioned on a valid file header inside
        an ar archive. Return a new ArMember on success, None otherwise. NzIncorrect header length:   r   zIncorrect file magic3r   r   r   rL   r         /   "   (   0   )r)   FILE_HEADER_LENGTHlenIOError
FILE_MAGICr   r   r   r,   splitstripdecoderM   intrN   rO   rP   rQ   rR   rS   rT   tellrU   rV   rW   )r3   fnamer"   r#   bufr:   r/   r   r   r   r-      sB    





zArMember.from_filer   c                 C   s   | j d krt| jd| _ | j | j d|  k rB| j| j krbn n| j |}| j  | _|S | j| jksz| j| jk r~dS | j | j| j }| j  | _|S )Nr%   r       )	rT   r(   rS   r1   rW   rV   r)   rh   rU   r   r0   rj   r   r   r   r)   2  s    
 zArMember.readc                 C   s   | j d krt| jd| _ | j | j |d k	rZ| j |}| j  | _| j| jkrVdS |S | j  }| j  | _| j| jkrdS |S )Nr%   rk   )rT   r(   rS   r1   rW   readlinerh   rV   rl   r   r   r   rm   D  s    

zArMember.readlinec                 C   s&   d }g }|   }|sq"|| q|S N)rm   r.   )r   sizehintrj   linesr   r   r   	readlinesX  s    zArMember.readlinesc                 C   s|   | j | jk r| j| _ |dk r8|| j  | jk r8td| |dkrN| j | | _ n*|dkrd| j| | _ n|dkrx| j| | _ d S )Nr&   zCan't seek at %dr'   r   )rW   rU   rb   rV   )r   offsetwhencer   r   r   r1   e  s    zArMember.seekc                 C   s   | j | jk rdS | j | j S )Nr   )rW   rU   r7   r   r   r   rh   t  s    zArMember.tellc                 C   s   dS )NTr   r7   r   r   r   seekablez  s    zArMember.seekablec                 C   s(   | j d k	r$| jd k	r$| j   d | _ d S rn   )rT   rS   r2   r7   r   r   r   r2     s    
zArMember.closec                 C   s   |   S rn   rm   r7   r   r   r   next  s    zArMember.nextc                    s    fdd}t | S )Nc                  3   s      } | r| V  d S rn   ru   )liner7   r   r   nextline  s    z#ArMember.__iter__.<locals>.nextline)rG   )r   rx   r   r7   r   rH     s    zArMember.__iter__c                 C   s   | j S rn   )rM   r7   r   r   r   <lambda>  rk   zArMember.<lambda>c                 C   s   | j S rn   )rN   r7   r   r   r   ry     rk   c                 C   s   | j S rn   )rO   r7   r   r   r   ry     rk   c                 C   s   | j S rn   )rP   r7   r   r   r   ry     rk   c                 C   s   | j S rn   )rQ   r7   r   r   r   ry     rk   c                 C   s   | j S rn   )rR   r7   r   r   r   ry     rk   c                 C   s   | j S rn   )rS   r7   r   r   r   ry     rk   )NN)r   )N)r   )r   )r
   r   r   r   r$   staticmethodr-   r)   rm   rq   r1   rh   rt   r2   rv   rH   rJ   r/   mtimeownergroupZfmoder0   ri   r   r   r   r   r,      s,     E



r,   __main__ztest.ar
)r   Z
__future__r   r   typingr   r   r   r   r   ImportErrorr+   ra   r*   r`   rc   	Exceptionr	   objectr   r,   r
   aprintjoinr<   r   r   r   r   <module>   s$      V