
    )uycK                     :   d Z ddlZddlZddlZddlZddlZddlmZ 	 ddl	m
Z
mZmZmZmZmZmZmZmZmZmZmZ ddlmZ n# e$ r
 dZesd ZY nw xY wddlmZmZmZ dd	lmZ dd
lm Z  dZ!dZ"g dZ#dZ$g dZ%dZ&dZ'dZ(dZ) G d de          Z* G d de+          Z, G d de,          Z- G d de,          Z. G d de          Z/e0dk    rP e/ej1        d                    Z2e2j3        4                                Z4 e5e46                    d                     dS dS )!zX Representation of Debian binary package (.deb) files


Debfile Classes
===============
    N)Path)AnyBinaryIODictIOIteratorListOptionalTextTypeVarUnionoverloadTYPE_CHECKING)LiteralFc                     d S N )fs    0/usr/lib/python3/dist-packages/debian/debfile.py<lambda>r   9   s    T     )ArFileArErrorArMember)	Changelog)Deb822zdata.tarzcontrol.tar)gzbz2xzlzmazstzdebian-binary)preinstpostinstprermpostrmconfigcontrolzusr/share/doc/%s/changelog.gzz$usr/share/doc/%s/changelog.Debian.gzmd5sumsc                       e Zd ZdS )DebErrorN__name__
__module____qualname__r   r   r   r*   r*   M   s        Dr   r*   c                       e Zd ZdZd Zd Zed             Zd ZddZ	e
dd	            Ze
dd
            ZddZe
	 	 	 dd            Ze
	 	 dd            Z	 	 	 ddZd Zd Zd Zd ZdS )DebParta  '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                 "    || _         d | _        d S r   )_DebPart__member_DebPart__tgz)selfmembers     r   __init__zDebPart.__init__c   s    


r   c                      fd} j         ׉ j        j        }t          j                            |          d         dd         }|t          v s|t          k    s|t          k    rm|dk    r |ddg          }n j        }	 t          j
        |d           _         nG# t          j        t          j        f$ r}t          d	|z            d}~ww xY wt          d
|z             j         S )zReturn 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.
        c                    	 dd l }dd l|                    | |j        |j        dfd          }nB# t          t
          f$ r.}t          dd                    |           d|d          d }~ww xY w|                    j	        
                                          d         }|j        dk    r.t          d	d                    |           d
|j        d          t          j        |          S )Nr   Fc                  D                           j         j                  S r   )signalSIGPIPESIG_DFL)r:   s   r   r   z9DebPart.tgz.<locals>._custom_decompress.<locals>.<lambda>}   s    MM&.&.AA r   )stdinstdoutuniversal_newlines
preexec_fnzerror while running command ' z' as subprocess: ''z	command 'z' has failed with code ')
subprocessr:   PopenPIPEOSError
ValueErrorr*   joincommunicater2   read
returncodeioBytesIO)command_listrC   procedatar:   r4   s        @r   _custom_decompressz'DebPart.tgz.<locals>._custom_decompressq   s>   <!!!! "'' $/*/', B  B  B  B	 (   Z( < < <h # 6 6 6 6 ; < < << ##DM$6$6$8$899!<D!##h # 6 6 6 6 I J J J :d###s   /4 A3)A..A3N   r!   unzstdz--stdoutzr:*)fileobjmodez#tarfile has returned an error: '%s'z"part '%s' has unexpected extension)r3   r2   nameospathsplitext	PART_EXTS	DATA_PART	CTRL_PARTtarfileopen	ReadErrorCompressionErrorr*   )r4   rR   rW   	extensionbufferrP   s   `     r   tgzzDebPart.tgzh   s   	$ 	$ 	$ 	$ 	$4 :=%D((..q1!""5II%%):):di>O>O %%//:0FGGFF!]FN!(f5!I!I!IDJJ)7+CD N N N"#H1#LMMMN CdJKKKzs   B C9CCc                     t          |                               dd          } |                     d          r| S |                     d          rd| z   S d| z   S )zw try (not so hard) to obtain a member file name in a form that is
        stored in the .tar.gz, i.e. starting with ./ \/z./.)strreplace
startswith)fnames    r   __normalize_memberzDebPart.__normalize_member   sd     E

""4--D!! 	LC   	;e|r   c                    	 g }|                     d          dd         D ]}|                    |           t          j                            d                    |                    }t                              |          }|                                 	                    |          }|
                                rF|j                            d          r"|j                             d          }|j        }|j        |d<   n# t          $ r Y dS w xY wt                              t          j                            |                    S )z walk the path following symlinks

        returns:
            resolved_path, info

        if the path is not found even after following symlinks within the
        archive, then None is returned.
        rg   rS   N)splitappendrX   rY   normpathrH   r0   _DebPart__normalize_memberrd   	getmemberissymlinknamerk   KeyError)r4   rY   resolved_path_partspathpartcurrpathtinfos         r   __resolve_symlinkszDebPart.__resolve_symlinks   s@   	"$ JJsOOABB/ A A#**84447++CHH5H,I,IJJ"55h??

,,X66 ;;== A~0055 
A.3n.B.B3.G.G+#(> 38.+B/%A(  	 	 	44	 ))"'*:*:8*D*DEEEs   DD 
DDFc                     t                               |          }|                                                                 }||v rdS |r|                     |          }|duS ||v S )zmCheck if this part contains a given file name.

        Symlinks within the archive can be followed.
        TN)r0   rs   rd   getnames_DebPart__resolve_symlinks)r4   rl   follow_symlinksnames
fname_reals        r   has_filezDebPart.has_file   sn     **511

##%%E>>4 	*0077JT))~r   Nc                     d S r   r   r4   rl   encodingerrorsr   s        r   get_filezDebPart.get_file   	     	r   c                     d S r   r   r   s        r   r   zDebPart.get_file   r   r   c                 n   t                               |          }|r(|                     |          }|t          d          |}	 |                                                     |          }n# t          $ r t          d          w xY w|t          d          |t          j        |||          S |S )a  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.

        If follow_symlinks is True, then symlinks within the archive will be
        followed.
        NzFile not found inside packager   r   )	r0   rs   r   r*   rd   extractfilerw   rL   TextIOWrapper)r4   rl   r   r   r   r   fobjs          r   r   zDebPart.get_file   s     **511 	0077J!>???E	<88::))%00DD 	< 	< 	<:;;;	< <:;;;#D8FKKKKs   'A. .Bc                     d S r   r   r   s        r   get_contentzDebPart.get_content  	     	r   c                     d S r   r   r   s        r   r   zDebPart.get_content  r   r   c                     |                      t          |          |||          }d}|r(|                                }|                                 |S )a5  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.

        If follow_symlinks is True, then symlinks within the archive will be
        followed.
        )r   r   r   N)r   ri   rJ   close)r4   rl   r   r   r   r   contents          r   r   zDebPart.get_content$  s[      MMJJf+  
 

  	ffhhGGGIIIr   c                 h    t          |                                                                           S r   )iterrd   r~   r4   s    r   __iter__zDebPart.__iter__A  s$    DHHJJ''))***r   c                 ,    |                      |          S r   )r   r4   rl   s     r   __contains__zDebPart.__contains__E  s    }}U###r   c                 ,    |                      |          S r   )r   r   s     r   __getitem__zDebPart.__getitem__I  s    &&&r   c                 8    | j                                          d S r   )r2   r   r   s    r   r   zDebPart.closeM  s    r   )F)NNF)NF)r,   r-   r.   __doc__r6   rd   staticmethodrs   r   r   r   r   r   r   r   r   r   r   r   r   r0   r0   Q   s        "  
4 4 4l   \$F $F $FL   $    X    X   @  "$)	   X   $)	   X "$)	   :+ + +$ $ $' ' '    r   r0   c                       e Zd ZdS )DebDataNr+   r   r   r   r   r   R  s        Dr   r   c                   R    e Zd Zd Zd Zedd            Zedd            ZddZdS )	
DebControlc                     i }t           D ]3}|                     |          r|                     |          }||||<   4|S )zo Return a dictionary of maintainer scripts (postinst, prerm, ...)
        mapping script names to script text. )MAINT_SCRIPTSr   r   )r4   scriptsrl   rQ   s       r   r   zDebControl.scriptsY  sT    
 " 	* 	*E}}U## *''..#%)GENr   c                 P    t          |                     t                              S )z 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   CONTROL_FILEr   s    r   
debcontrolzDebControl.debcontrolg  s      d&&|44555r   Nc                     d S r   r   r4   r   r   s      r   r(   zDebControl.md5sumsq  r   r   c                     d S r   r   r   s      r   r(   zDebControl.md5sumsv  r   r   c                    |                      t                    st          dt          z            |                     t          ||          }i }d}|d}|                                D ]`}|                    |                              dd          \  }}t          |t                    r|	                                ||<   [|||<   a|
                                 |S )a   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.z('%s' file not found, can't list MD5 sumsr   z
Ns   
rS   )r   MD5_FILEr*   r   	readlinesrstriprp   
isinstancebytesdecoder   )	r4   r   r   md5_filesumsnewlinelinemd5rl   s	            r   r(   zDebControl.md5sums{  s     }}X&& 	G:XEG G G ==HV=LLG&&(( 	" 	"DW--33D!<<JC#u%% "!jjllU!Ur   NNr   )r,   r-   r.   r   r   r   r(   r   r   r   r   r   W  s          6 6 6    X    X     r   r   c                       e Zd ZdZddZd Zed             Zed             Zed             Z	d	 Z
d
 Zedd            Zedd            ZddZd Zd Zd Zd ZdS )DebFilea  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
    Nrc                    t          j        | |||           t          |                                           fd}t          vrt          dt          z            i | _        t          |                      |t                                        | j        t          <   t          |                      |t                                        | j        t          <   d | _        |                     t                    }|                                                                | _        |                                 d S )Nc                 V     fdt           D             } t          t          fv r|                                                    t          |                    }|st          d|z            t          |          dk    rt          d|z            t          |          d         S )Nc                     g | ]	}d |
S )rh   r   ).0extbasenames     r   
<listcomp>zBDebFile.__init__.<locals>.compressed_part_name.<locals>.<listcomp>  s$    IIIXXXss3IIIr   z9missing required part in given .deb (expected one of: %s)rS   z>too many parts in given .deb (was looking for only one of: %s)r   )	r[   r\   r]   rq   intersectionsetr*   lenlist)r   
candidatespartsactual_namess   `  r   compressed_part_namez.DebFile.__init__.<locals>.compressed_part_name  s    IIIIyIIIJIy111!!(+++ --c*oo>>E ;-/9:; ; ; 5zzA~~9;EFG G G ;;q>!r   z4missing required part in given .deb (expected: '%s'))r   r6   r   r~   	INFO_PARTr*   _DebFile__partsr   rt   r]   r   r\   _DebFile__pkgnamerJ   strip_DebFile__versionr   )r4   filenamerV   rU   r   r   r   s         @r   r6   zDebFile.__init__  s&   hg6664==??++	" 	" 	" 	" 	"& L(($&/01 1 1 ",T^^  ++.- .- #. #.Y")$..  +++- +- #. #.YNN9%%))						r   c                 D    |                                  d         | _        d S )Npackage)r   r   r   s    r   __updatePkgNamezDebFile.__updatePkgName  s    **95r   c                     | j         S r   )r   r   s    r   versionzDebFile.version  s     ~r   c                 &    | j         t                   S r   )r   r\   r   s    r   rQ   zDebFile.data       |I&&r   c                 &    | j         t                   S r   )r   r]   r   s    r   r'   zDebFile.control  r   r   c                 4    | j                                         S )z See .control.debcontrol() )r'   r   r   s    r   r   zDebFile.debcontrol  s     |&&(((r   c                 4    | j                                         S )z See .control.scripts() )r'   r   r   s    r   r   zDebFile.scripts  s     |##%%%r   c                     d S r   r   r   s      r   r(   zDebFile.md5sums  r   r   c                     d S r   r   r   s      r   r(   zDebFile.md5sums  r   r   c                 :    | j                             ||          S )z See .control.md5sums() r   )r'   r(   r   s      r   r(   zDebFile.md5sums  s     |##Xf#EEEr   c                 |   | j         |                                  t          | j         z  t          | j         z  fD ]}	 | j                            |d          }n# t          $ r Y ,w xY wt          j        |          5 }|	                                }ddd           n# 1 swxY w Y   t          |          c S dS )z Return a Changelog object for the changelog.Debian.gz of the
        present .deb package. Return None if no changelog can be found. NT)r   )rU   )r   _DebFile__updatePkgNameCHANGELOG_DEBIANCHANGELOG_NATIVErQ   r   r*   gzipGzipFilerJ   r   )r4   rl   fhr   raw_changelogs        r   	changelogzDebFile.changelog  s   
 >!  """&7&79 		, 		,EY''t'DD    r*** *b "		* * * * * * * * * * * * * * *]+++++ts#   A
A'&A'?B  B$	'B$	c                 j    | j                                          | j                                         d S r   )r'   r   rQ   r   s    r   r   zDebFile.close  s.    	r   c                     | S r   r   r   s    r   	__enter__zDebFile.__enter__  s    r   c                 .    |                                   d S r   )r   )r4   exc_typeexc_valexc_tbs       r   __exit__zDebFile.__exit__!  s    

r   )Nr   Nr   r   )r,   r-   r.   r   r6   r   propertyr   rQ   r'   r   r   r   r(   r   r   r   r   r   r   r   r   r     sE        & & & &P6 6 6   X ' ' X' ' ' X') ) )
& & &
    X    XF F F F
  *  
      r   r   __main__rS   )r   )7r   r   rL   r^   sysos.pathrX   pathlibr   typingr   r   r   r   r   r	   r
   r   r   r   r   r   typing_extensionsr   ImportErrordebian.arfiler   r   r   debian.changelogr   debian.deb822r   r\   r]   r[   r   r   r   r   r   r   r*   objectr0   r   r   r   r,   argvdebr'   rd   printrt   r   r   r   <module>r     s   .  				  



       "                                   " " "M " ">" 4 3 3 3 3 3 3 3 3 3 & & & & & &             		...		DDD2 9 	 	 	 	 	w 	 	 	~ ~ ~ ~ ~f ~ ~ ~B	 	 	 	 	g 	 	 	
C C C C C C C CLF F F F Ff F F FR z
'38A;
'
'
'C
+//

C	E#--	
"
"##### s   "A AA