ó
8Nc           @€  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 „  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)$s8   ^#!\s*/usr/bin/(?:env\s+)?(python(\d+\.\d+)?(?:-dbg)?).*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  |  ƒ r } | j d ƒ } t j | ƒ } | s: d S| j ƒ  } | d k r} | d ry | d t | d ƒ f } n  | 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    Ns   cannot open %s(   NN(
   t   opent   readt
   SHEBANG_REt   matcht   Nonet   groupsR   t   IOErrort   logt   error(   t   fpatht   fpt   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   t   Truet   SHAREDLIB_REt   searchR1   R$   R   R(   (   R,   t   cmdt   processR&   (    (    s$   /usr/share/python/debpython/tools.pyt   so2pyvert   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   selfR?   (    (    s$   /usr/share/python/debpython/tools.pyt   __init__‘   s    	c         O€  sH   t  | | f ƒ } | |  j k r= |  j | | Ž  |  j | <n  |  j | S(   N(   R   R@   R?   (   RA   t   argst   kwargst   key(    (    s$   /usr/share/python/debpython/tools.pyt   __call__•   s    (   t   __name__t
   __module__RB   RF   (    (    (    s$   /usr/share/python/debpython/tools.pyR>      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(   R3   R   R   t   codecsR#   t
   startswitht
   INSTALL_RER&   t   FalseR*   t   warnt	   groupdictt   replacet   debugR   R+   t   lstripR   R   R   R    t   makedirst	   Exceptiont   removeR   (   R   RL   t   statust   srcfpatht   versionst   linet   detailst   myverst   filesR,   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.pyremoveRI   s   utf-8RJ   s"   %s.pyremove: unrecognized line: %sRL   s-   %s.pyremove: no matching versions for line %sRM   s/   %s.pyremove: nothing to remove: python%d.%d, %s(   R3   R   R   RN   R#   RO   t	   REMOVE_RER&   RQ   R*   RR   RS   RU   R   R   R   R   RX   R+   R    RY   R)   t   OSError(   R   RL   RZ   R[   R\   R]   R^   R_   R   R`   R,   t   e(    (    s$   /usr/share/python/debpython/tools.pyt   pyremoveÛ   sN    
	
	
	(-   t
   __future__R    RN   t   loggingR    t   ret   cPickleR   R   t   os.pathR   R   R   R   t   shutilR   t
   subprocessR   R	   t   debpython.versionR
   R   R   t	   getLoggerRG   R*   t   compileR:   R%   R4   t   VERBOSERP   Re   R'   RQ   R   R   R"   R0   R8   R=   t   objectR>   Rd   Rh   (    (    (    s$   /usr/share/python/debpython/tools.pyt   <module>   s6   "						?