σ
ψ7Tc           @@ s  d  d l  m Z m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l m	 Z	 m
 Z
 d  d l m Z d  d l m Z d Z d Z d d	 d
 g Z d Z d d d d d g Z d Z d Z d Z d Z d e
 f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e	 f d     YZ e d k rd  d l Z e d  e j d!  Z  e  j! j"   Z" e# e" j$ d   n  d S("   i    (   t   absolute_importt   print_functionN(   t   ArFilet   ArError(   t	   Changelog(   t   Deb822s   data.tars   control.tart   gzt   bz2t   xzs   debian-binaryt   preinstt   postinstt   prermt   postrmt   configt   controls   usr/share/doc/%s/changelog.gzs$   usr/share/doc/%s/changelog.Debian.gzt   md5sumst   DebErrorc           B@ s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyR   )   s   t   DebPartc           B@ s   e  Z d  Z d   Z d   Z e d    Z d   Z d d d  Z	 d d d  Z
 d   Z d   Z e j d	 k  r d
   Z n  d   Z d   Z RS(   s€  'Part' of a .deb binary package.
    
    A .deb package is considered as made of 2 parts: a 'data' part
    (corresponding to the possibly compressed 'data.tar' archive embedded
    in a .deb) and a 'control' part (the 'control.tar.gz' archive). Each of
    them is represented by an instance of this class. Each archive should
    be a compressed tar archive although an uncompressed data.tar is permitted;
    supported compression formats are: .tar.gz, .tar.bz2, .tar.xz .

    When referring to file members of the underlying .tar.gz archive, file
    names can be specified in one of 3 formats "file", "./file", "/file". In
    all cases the file is considered relative to the root of the archive. For
    the control part the preferred mechanism is the first one (as in
    deb.control.get_content('control') ); for the data part the preferred
    mechanism is the third one (as in deb.data.get_file('/etc/vim/vimrc') ).
    c         C@ s   | |  _  d  |  _ d  S(   N(   t   _DebPart__membert   Nonet   _DebPart__tgz(   t   selft   member(    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyt   __init__?   s    	c   	      @ sΒ  |  j  d k r»|  j j } t j j |  d d } | t k sM | t k r¨t	 j
 d k  rK| d k rKyd d d l } d d l   d d l } | j d d g d | j d	 | j d
 t d   f d   } Wn) t t f k
 rχ } t d |   n X| j |  j j    d } | j d k r9t d | j   n  | j |  } n	 |  j } y t j d | d d  |  _  WqΈt j t j f k
 r€} t d |   qΈXq»t d |   n  |  j  S(   sΥ   Return a TarFile object corresponding to this part of a .deb
        package.

        Despite the name, this method gives access to various kind of
        compressed tar archives, not only gzipped ones.
        i   i   R   i    Nt   unxzs   --stdoutt   stdint   stdoutt   universal_newlinest
   preexec_fnc           @ s     j    j   j  S(   N(   t   signalt   SIGPIPEt   SIG_DFL(    (   R   (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyt   <lambda>Y   s    s   %ss!   command has failed with code '%s't   fileobjt   modes   r:*s#   tarfile has returned an error: '%s's"   part '%s' has unexpected extension(   i   i   (   R   R   R   t   namet   ost   patht   splitextt	   PART_EXTSt	   DATA_PARTt   syst   version_infot
   subprocessR   t   iot   Popent   PIPEt   Falset   OSErrort
   ValueErrorR   t   communicatet   readt
   returncodet   BytesIOt   tarfilet   opent	   ReadErrort   CompressionError(	   R   R%   t	   extensionR-   R.   t   proct   et   datat   buffer(    (   R   s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyt   tgzC   s6    	c         C@ s<   |  j  d  r |  d }  n |  j  d  r8 |  d }  n  |  S(   sw    try (not so hard) to obtain a member file name in a form relative
        to the .tar.gz root and with no heading '.' s   ./i   t   /i   (   t
   startswith(   t   fname(    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyt   __normalize_membero   s
    c         C@ s;   t  j |  } |  j   j   } d | | k p: | | k S(   s.   Check if this part contains a given file name.s   ./(   R   t   _DebPart__normalize_memberRA   t   getnames(   R   RD   t   names(    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyt   has_file   s    c         C@ sυ   t  j |  } y |  j   j d |  } Wn& t k
 rT |  j   j |  } n X| d k	 rν t j d k r³ d d l } t	 | d  s d   | _
 n  | j | d | d | Sd d l } | d k rΤ d	 } n  | j | | d | Sn | Sd S(
   sΎ   Return a file object corresponding to a given file name.

        If encoding is given, then the file object will return Unicode data;
        otherwise, it will return binary data.
        s   ./t   3i    Nt   flushc           S@ s   d  S(   N(   R   (    (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyR"      s    t   encodingt   errorst   strict(   R   RF   RA   t   extractfilet   KeyErrorR   R+   t   versionR.   t   hasattrRK   t   TextIOWrappert   codecst   EncodedFile(   R   RD   RL   RM   t   fobjR.   RT   (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyt   get_file   s     	c         C@ sD   |  j  | d | d | } d } | r@ | j   } | j   n  | S(   sΥ   Return the string content of a given file, or None (e.g. for
        directories).

        If encoding is given, then the content will be a Unicode object;
        otherwise, it will contain binary data.
        RL   RM   N(   RW   R   R5   t   close(   R   RD   RL   RM   t   ft   content(    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyt   get_content£   s    c         C@ s   t  |  j   j    S(   N(   t   iterRA   RG   (   R   (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyt   __iter__΄   s    c         C@ s   |  j  |  S(   N(   RI   (   R   RD   (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyt   __contains__·   s    RJ   c         C@ s   |  j  |  S(   N(   RI   (   R   RD   (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyt   has_key»   s    c         C@ s   |  j  |  S(   N(   R[   (   R   RD   (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyt   __getitem__Ύ   s    c         C@ s   |  j  j   d  S(   N(   R   RX   (   R   (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyRX   Α   s    N(   R   R   t   __doc__R   RA   t   staticmethodRF   RI   R   RW   R[   R]   R^   R+   RQ   R_   R`   RX   (    (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyR   -   s   		,				t   DebDatac           B@ s   e  Z RS(    (   R   R   (    (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyRc   Ε   s   t
   DebControlc           B@ s)   e  Z d    Z d   Z d d d  Z RS(   c         C@ s@   i  } x3 t  D]+ } |  j |  r |  j |  | | <q q W| S(   so    Return a dictionary of maintainer scripts (postinst, prerm, ...)
        mapping script names to script text. (   t   MAINT_SCRIPTSRI   R[   (   R   t   scriptsRD   (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyRf   Μ   s
    c         C@ s   t  |  j t   S(   sΎ    Return the debian/control as a Deb822 (a Debian-specific dict-like
        class) object.
        
        For a string representation of debian/control try
        .get_content('control') (   R   R[   t   CONTROL_FILE(   R   (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyt
   debcontrolΧ   s    c   	      C@ sί   |  j  t  s" t d t   n  |  j t d | d | } i  } | d k rX d } n d } xp | j   D]b } | j |  j d d  \ } } t j	 d k rΓ t
 | t  rΓ | j   | | <qk | | | <qk W| j   | S(   sσ   Return a dictionary mapping filenames (of the data part) to
        md5sums. Fails if the control part does not contain a 'md5sum' file.

        Keys of the returned dictionary are the left-hand side values of lines
        in the md5sums member of control.tar.gz, usually file names relative to
        the file system root (without heading '/' or './').

        The returned keys are Unicode objects if an encoding is specified,
        otherwise binary. The returned values are always Unicode.s(   '%s' file not found, can't list MD5 sumsRL   RM   s   
i   RJ   N(   RI   t   MD5_FILER   RW   R   t	   readlinest   rstript   splitR+   RQ   t
   isinstancet   bytest   decodeRX   (	   R   RL   RM   t   md5_filet   sumst   newlinet   linet   md5RD   (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyR   ΰ   s    	!
N(   R   R   Rf   Rh   R   R   (    (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyRd   Κ   s   			t   DebFilec           B@ s   e  Z d  Z d d d d  Z d   Z e d    Z e d    Z e d    Z	 d   Z
 d   Z d d d	  Z d
   Z d   Z RS(   sΪ  Representation of a .deb file (a Debian binary package)

    DebFile objects have the following (read-only) properties:
        - version       debian .deb file format version (not related with the
                        contained package version), 2.0 at the time of writing
                        for all .deb packages in the Debian archive
        - data          DebPart object corresponding to the data.tar.gz (or
                        other compressed or uncompressed tar) archive contained
                        in the .deb file
        - control       DebPart object corresponding to the control.tar.gz (or
                        other compressed tar) archive contained in the .deb
                        file
    t   rc         @ sή   t  j |  | | |  t |  j        f d   } t   k rV t d t   n  i  |  _ t |  j | t	    |  j t	 <t
 |  j | t    |  j t <d  |  _ |  j t  } | j   j   |  _ | j   d  S(   Nc         @ s€   g  t  D] } d |  | f ^ q } |  t k r? | j |   n    j t |   } | sm t d |   n3 t |  d k r t d |   n t |  d Sd  S(   Ns   %s.%ss9   missing required part in given .deb (expected one of: %s)i   s>   too many parts in given .deb (was looking for only one of: %s)i    (   R)   R*   t   appendt   intersectiont   setR   t   lent   list(   t   basenamet   extt
   candidatest   parts(   t   actual_names(    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyt   compressed_part_name  s    #s4   missing required part in given .deb (expected: '%s')(   R   R   Ry   RG   t	   INFO_PARTR   t   _DebFile__partsRd   t	   getmembert	   CTRL_PARTRc   R*   R   t   _DebFile__pkgnameR5   t   stript   _DebFile__versionRX   (   R   t   filenameR$   R#   R   RY   (    (   R   s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyR     s    				c         C@ s   |  j    d |  _ d  S(   Nt   package(   Rh   R   (   R   (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyt   __updatePkgName0  s    c         C@ s   |  j  S(   N(   R   (   R   (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyR"   3  s    c         C@ s   |  j  t S(   N(   R   R*   (   R   (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyR"   4  s    c         C@ s   |  j  t S(   N(   R   R   (   R   (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyR"   5  s    c         C@ s   |  j  j   S(   s    See .control.debcontrol() (   R   Rh   (   R   (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyRh   9  s    c         C@ s   |  j  j   S(   s    See .control.scripts() (   R   Rf   (   R   (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyRf   =  s    c         C@ s   |  j  j d | d |  S(   s    See .control.md5sums() RL   RM   (   R   R   (   R   RL   RM   (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyR   A  s    c         C@ s   |  j  d k r |  j   n  xr t |  j  t |  j  g D]V } |  j j |  r7 t j d |  j j	 |   } | j
   } | j   t |  Sq7 Wd S(   s    Return a Changelog object for the changelog.Debian.gz of the
        present .deb package. Return None if no changelog can be found. R#   N(   R   R   t   _DebFile__updatePkgNamet   CHANGELOG_DEBIANt   CHANGELOG_NATIVER?   RI   t   gzipt   GzipFileRW   R5   RX   R   (   R   RD   R   t   raw_changelog(    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyt	   changelogE  s    
c         C@ s   |  j  j   |  j j   d  S(   N(   R   RX   R?   (   R   (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyRX   U  s    N(   R   R   Ra   R   R   R   t   propertyRQ   R?   R   Rh   Rf   R   R   RX   (    (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyRu      s   !				t   __main__R   i   (%   t
   __future__R    R   R   R8   R+   t   os.pathR&   t   debian.arfileR   R   t   debian.changelogR   t   debian.deb822R   R*   R   R)   R   Re   Rg   R   R   Ri   R   t   objectR   Rc   Rd   Ru   R   t   argvt   debR   RA   t   printR   (    (    (    s2   /usr/lib/python2.7/dist-packages/debian/debfile.pyt   <module>   s4   6Z