ó
}k^c           @€  s¦  d  d l  m 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 d  d l	 m
 Z
 m Z m Z m Z d  d l m Z d  d l m Z m Z d  d l m Z m Z m Z e j e ƒ Z e j d	 ƒ Z e j d
 ƒ Z e j d ƒ Z e j d e e j ƒ Z e j d e e j ƒ Z  d e" d „ Z# d „  Z$ d „  Z% d d „ Z& d „  Z' d „  Z( d „  Z) d e* f d „  ƒ  YZ+ d „  Z, d „  Z- d S(   iÿÿÿÿ(   t   with_statementN(   t   dumps(   t   glob(   t   existst   isdirt   joint   split(   t   rmtree(   t   PIPEt   Popen(   t   RANGE_PATTERNt   getvert   get_requested_versionss/   (.*?)(-py\d\.\d(?:-[^.]*)?)?(\.egg-info|\.pth)$sA   ^#!\s*(.*?/bin/.*?)(python(2\.\d+)?(?:-dbg)?)(?:\s(.*))?[$\r\n].*s   NEEDED.*libpython(\d\.\d)s¸   
    (?P<pattern>.+?)  # file pattern
    (?:\s+  # optional Python module name:
    (?P<module>[A-Za-z][A-Za-z0-9_.]*)?
    )?
    \s*  # optional version range:
    (?P<vrange>%s)?$
s^   
    (?P<pattern>.+?)  # file pattern
    \s*  # optional version range:
    (?P<vrange>%s)?$
c         C€  s„   t  |  t ƒ r1 t d „  |  j d ƒ Dƒ ƒ }  n  |  d	 k rJ d |  } n
 d |  } | rg d | } n  | r€ d | | f } n  | S(
   sà   Return path to site-packages directory.

    >>> sitedir((2, 5))
    '/usr/lib/python2.5/site-packages/'
    >>> sitedir((2, 7), 'python-foo', True)
    'debian/python-foo/usr/lib/debug/usr/lib/python2.7/dist-packages/'
    c         s€  s   |  ] } t  | ƒ Vq d  S(   N(   t   int(   t   .0t   i(    (    s$   /usr/share/python/debpython/tools.pys	   <genexpr>>   s    t   .i   i   s#   /usr/lib/python%d.%d/dist-packages/s#   /usr/lib/python%d.%d/site-packages/s   /usr/lib/debug%ss   debian/%s%s(   i   i   (   t
   isinstancet
   basestringt   tupleR   (   t   versiont   packaget   gdbt   path(    (    s$   /usr/share/python/debpython/tools.pyt   sitedir5   s    "
c         C€  si   |  j  d ƒ } | j  d ƒ } x& | d | d k rF | d =| d =q! Wd j d g t | ƒ d | ƒ S(   s~   Return relative path.

    >>> relpath('/usr/share/python-foo/foo.py', '/usr/bin/foo', )
    '../share/python-foo/foo.py'
    t   /i    s   ..i   (   R   R   t   len(   t   targett   linkt   tt   l(    (    s$   /usr/share/python/debpython/tools.pyt   relpathM   s
    c         C€  s   t  j t |  | ƒ | ƒ S(   s   Create relative symlink.(   t   ost   symlinkR   (   R   R   (    (    s$   /usr/share/python/debpython/tools.pyt   relative_symlinkZ   s    c   	      C€  s`  y? t  |  ƒ  } | j ƒ  } Wd QX| s> t j d |  ƒ d SWn" t k
 rc t j d |  ƒ t SXt j	 | d ƒ } | s d S| s| j
 ƒ  \ } } } } | d k r¸ d | } n  | d k rÍ d } n  | d	 k râ d } n  | r| r| d
 | 7} qn  | r\t j d |  | d ƒ t  |  d ƒ ) } | j d | ƒ | j | d ƒ Wd QXn  t S(   sm   Normalize file's shebang.

    :param replacement: new shebang command (path to interpreter and options)
    Ns$   fix_shebang: ignoring empty file: %ss   cannot open %si    s   /usr/bins   /usr/bin/%st   pythons   /usr/bin/python2t   python2s    %ss   replacing shebang in %s (%s)t   ws   #! %s
i   (   t   opent	   readlinest   logt   debugt   Nonet   IOErrort   errort   Falset
   SHEBANG_REt   matcht   groupst   infot   writet
   writelinest   True(	   t   fpatht   replacementt   fpt   fcontentR/   R   t   interpreterR   t   argv(    (    s$   /usr/share/python/debpython/tools.pyt   fix_shebang_   s8    		c         C€  s¸   y t  |  ƒ ~ } | j d ƒ } t j | ƒ } | s: d S| j ƒ  } | d d !d k r‰ | d r{ | d t | d ƒ f S| d d f SWd QXWn! t k
 r³ t j	 d |  ƒ n Xd S(   sp   Check file's shebang.

    :rtype: tuple
    :returns: pair of Python interpreter string and Python version
    i    i   i   i   Ns   cannot open %s(   NN(
   R&   t   readR.   R/   R*   R0   R   R+   R(   R,   (   R5   R7   t   dataR/   t   res(    (    s$   /usr/share/python/debpython/tools.pyt   shebang2pyver…   s    
c         C€  sX   d |  } t  | d t d t ƒ} t j | j j ƒ  ƒ } | rT t | j ƒ  d ƒ Sd S(   sh   Return libpython version file is linked to or None.

    :rtype: tuple
    :returns: Python version
    s   readelf -Wd '%s't   stdoutt   shelli    N(	   R	   R   R4   t   SHAREDLIB_REt   searchR@   R<   R   R0   (   R5   t   cmdt   processR/   (    (    s$   /usr/share/python/debpython/tools.pyt   so2pyverš   s
    
c         C€  sG   t  j |  ƒ } | rC | j d ƒ d k	 rC d j | j d d ƒ ƒ S|  S(   s  Remove Python version and platform name from Egg files/dirs.

    >>> clean_egg_name('python_pipeline-0.1.3_py3k-py3.1.egg-info')
    'python_pipeline-0.1.3_py3k.egg-info'
    >>> clean_egg_name('Foo-1.2-py2.7-linux-x86_64.egg-info')
    'Foo-1.2.egg-info'
    i   t    i   i   N(   t
   EGGnPTH_RER/   t   groupR*   R   (   t   nameR/   (    (    s$   /usr/share/python/debpython/tools.pyt   clean_egg_name¨   s    t   memoizec           B€  s   e  Z d  „  Z d „  Z RS(   c         C€  s   | |  _  i  |  _ d  S(   N(   t   funct   cache(   t   selfRM   (    (    s$   /usr/share/python/debpython/tools.pyt   __init__·   s    	c         O€  sH   t  | | f ƒ } | |  j k r= |  j | | Ž  |  j | <n  |  j | S(   N(   R   RN   RM   (   RO   t   argst   kwargst   key(    (    s$   /usr/share/python/debpython/tools.pyt   __call__»   s    (   t   __name__t
   __module__RP   RT   (    (    (    s$   /usr/share/python/debpython/tools.pyRL   ¶   s   	c         C€  s·  t  } d |  } t | ƒ s  | St | ƒ } x„t j | d d ƒD]m} | sB | j d ƒ rd qB n  t j | ƒ } | s˜ t } t	 j
 d |  | ƒ qB n  | j ƒ  } | d rË | d j d d ƒ | d <n  | t | d	 ƒ @} | sþ t	 j d
 |  | ƒ qB n  t | d ƒ } | s7t } t	 j d |  | d ƒ qB n  xu| D]m}	 |	 j d ƒ }	 | d r}t | d t |	 ƒ d ƒ }
 n" |	 j d ƒ r™|	 d }
 n |	 }
 x	| D]} t t | |  ƒ |
 ƒ } t | ƒ d } t | ƒ sy t j | ƒ Wqt k
 rt	 j d | ƒ t SXn  t | ƒ rfy t j | ƒ Wqft k
 rbt } t	 j d | ƒ q¦qfXn  y t j |	 | ƒ Wq¦t k
 r¦t } t	 j d |	 | ƒ q¦Xq¦Wq>WqB W| S(   sD   Install local files listed in pkg.pyinstall files as public modules.s   ./debian/%s.pyinstallt   encodings   utf-8t   #s#   %s.pyinstall: unrecognized line: %st   moduleR   R   t   vranges.   %s.pyinstall: no matching versions for line %st   patterns    %s.pyinstall: file not found: %ss   /.i   s   debian/i   i    s   cannot create %s directorys   cannot replace %s files   cannot copy %s file to %s(   R4   R   R   t   codecsR&   t
   startswitht
   INSTALL_RER/   R-   R(   t   warnt	   groupdictt   replaceR)   R   R,   t   lstripR   R   R   R    t   makedirst	   Exceptiont   removeR   (   R   RZ   t   statust   srcfpatht   versionst   linet   detailst   myverst   filesR5   t   dstnameR   t   dstfpatht   dstdir(    (    s$   /usr/share/python/debpython/tools.pyt	   pyinstallÂ   sr    
	

	
	
 
#c      	   C€  sÊ  t  } d |  } t | ƒ s  | St | ƒ } x—t j | d d ƒD]€} | sB | j d ƒ rd qB n  t j | ƒ } | s˜ t } t	 j
 d |  | ƒ qB n  | j ƒ  } | t | d ƒ @} | s× t	 j d |  | ƒ qB n  xè | D]à } t t | |  ƒ | d ƒ }	 |	 s't	 j d	 |  | | d ƒ qÞ n  x” |	 D]Œ }
 t |
 ƒ rzy t |
 ƒ Wqºt k
 rv} t } t	 j | ƒ qºXq.y t j |
 ƒ Wq.t t f k
 r¹} t } t	 j | ƒ q.Xq.WqÞ WqB W| S(
   s2   Remove public modules listed in pkg.pyremove file.s   ./debian/%s.pyremoveRW   s   utf-8RX   s"   %s.pyremove: unrecognized line: %sRZ   s-   %s.pyremove: no matching versions for line %sR[   s/   %s.pyremove: nothing to remove: python%d.%d, %s(   R4   R   R   R\   R&   R]   t	   REMOVE_RER/   R-   R(   R_   R`   R)   R   R   R   R   Rd   R,   R    Re   R+   t   OSError(   R   RZ   Rf   Rg   Rh   Ri   Rj   Rk   R   Rl   R5   t   e(    (    s$   /usr/share/python/debpython/tools.pyt   pyremove  sN    
	
	
	(.   t
   __future__R    R\   t   loggingR    t   ret   cPickleR   R   t   os.pathR   R   R   R   t   shutilR   t
   subprocessR   R	   t   debpython.versionR
   R   R   t	   getLoggerRU   R(   t   compileRH   R.   RB   t   VERBOSER^   Rq   R*   R-   R   R   R"   R;   R?   RF   RK   t   objectRL   Rp   Rt   (    (    (    s$   /usr/share/python/debpython/tools.pyt   <module>   s8   "		&				?