ó
8Nc           @€  s/  d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l m Z m Z m Z d  d l	 m
 Z
 m Z d  d l m Z m Z m Z d  d l m Z e j e ƒ Z e j d ƒ Z e j d e j ƒ Z e j d	 e j ƒ Z d
 „  Z e d d d d „ ƒ Z d d „ Z d „  Z d „  Z d „  Z  d „  Z! d S(   iÿÿÿÿ(   t   with_statementN(   t   existst   isdirt   join(   t   PIPEt   Popen(   t   vreprt   getvert   get_requested_versions(   t   memoizes/   .*?/usr/lib/python(\d.\d+)/(site|dist)-packagessÍ  
    (?P<name>[A-Za-z][A-Za-z0-9_.]*)             # Python distribution name
    \s*
    (?P<vrange>(?:-?\d\.\d+(?:-(?:\d\.\d+)?)?)?) # version range
    \s*
    (?P<dependency>(?:[a-z][^;]*)?)              # Debian dependency
    (?:  # optional upstream version -> Debian version translator
        ;\s*
        (?P<standard>PEP386)?                    # PEP-386 mode
        \s*
        (?P<rules>s/.*)?                         # translator rules
    )?
    s  
    (?P<name>[A-Za-z][A-Za-z0-9_.]*)     # Python distribution name
    \s*
    (?P<enabled_extras>(?:\[[^\]]*\])?)  # ignored for now
    \s*
    (?:  # optional minimum/maximum version
        (?P<operator><=?|>=?|==|!=)
        \s*
        (?P<version>(\w|[-.])+)
    )?
    c         C€  s‹   t  |  ƒ y } xo | D]g } | j d ƒ } | j d ƒ s | rG q n  t j | ƒ s t j d |  j d d ƒ d | ƒ t Sq WWd QXt	 S(   s    Check if pydist file looks good.s   
t   #s"   invalid pydist data in file %s: %st   /i   iÿÿÿÿN(
   t   opent   stript
   startswitht	   PYDIST_REt   matcht   logt   errort   rsplitt   Falset   True(   t   fpatht   fpt   line(    (    s%   /usr/share/python/debpython/pydist.pyt   validate<   s    	s   /usr/share/python/dist/s   debian/pydist-overridess   /usr/share/python/dist_fallbackc   
      €  s”  t  | ƒ r | g } n g  } t ˆ  ƒ rS | j ‡  f d †  t j ˆ  ƒ Dƒ ƒ n  t  | ƒ ro | j | ƒ n  i  } x| D]} t | ƒ ü } xò | D]ê } | j d ƒ } | j d ƒ s˜ | rÉ q˜ n  t	 j
 | ƒ } | s÷ t d | | f ƒ ‚ n  | j ƒ  } t | d ƒ }	 t | d ƒ | d <| d j ƒ  | d <| d	 r_| d	 j d
 ƒ | d	 <n
 g  | d	 <| j |	 g  ƒ j | ƒ q˜ WWd QXq| W| S(   s5   Load iformation about installed Python distributions.c         3€  s   |  ] } t  ˆ  | ƒ Vq d  S(   N(   R   (   t   .0t   i(   t   dname(    s%   /usr/share/python/debpython/pydist.pys	   <genexpr>S   s    s   
R
   s   invalid pydist line: %s (in %s)t   namet   vranget   versionst
   dependencyt   rulest   ;N(   R   R   t   extendt   ost   listdirt   appendR   R   R   R   t   searcht	   Exceptiont	   groupdictt	   safe_nameR   t   splitt
   setdefault(
   R   t   fnamet   fbnamet   to_checkt   resultR   R   R   t   distR   (    (   R   s%   /usr/share/python/debpython/pydist.pyt   loadJ   s4    )

'c         C€  s³  t  j d |  | r t | ƒ n d  ƒ t | t ƒ rC t | ƒ } n  t j d ƒ j	 |  ƒ j
 ƒ  \ } } t | ƒ | }  t ƒ  } t j	 |  ƒ } | sµ t  j d ƒ t d |  ƒ ‚ n  | j ƒ  } | d } | j | j ƒ  ƒ } | raxx | D]m } | r| | j d | ƒ k rqí n  | d s%d  S| d j d ƒ r@| d S| d	 rR| d S| d Sqí Wn  d
 t t | ƒ ƒ } | r–d | t | ƒ f } n
 d | } t  j d | ƒ t d | d t d t d t ƒ}	 |	 j ƒ  \ }
 } |	 j d k rtt ƒ  } xC |
 j d ƒ D]2 } | j ƒ  s$qn  | j | j d ƒ d ƒ qWt | ƒ d k rgt  j d | ƒ q‡| j  ƒ  Sn t  j d | | ƒ t! | ƒ } t  j" d | | t | ƒ ƒ | S(   Ns-   trying to guess dependency for %s (python=%s)s   ([^><= \[]+)(.*)s[   please ask dh_python2 author to fix REQUIRES_RE or your upstream author to fix requires.txts   requirement is not valid: %sR   R   R    t   )t   versions   '*/%s-?*\.egg-info's"   %s | grep '/python%s/\|/pyshared/'s$   %s | grep '/python2\../\|/pyshared/'s   invoking dpkg -S %ss   /usr/bin/dpkg -S %st   shellt   stdoutt   stderri    s   
t   :i   s,   more than one package name found for %s dists'   dpkg -S did not find package for %s: %ss³   Cannot find installed package that provides %s. Using %s as package name. Please add "%s correct_package_name" line to debian/pydist-overrides to override it if this is incorrect.(#   R   t   debugR   t   Nonet
   isinstancet
   basestringR   t   ret   compileR   t   groupsR*   R2   t   REQUIRES_REt   infoR(   R)   t   gett   lowert   endswitht	   ci_regexpR   R   R   t   communicatet
   returncodet   setR+   R   t   addt   lenR   t   popt   sensible_pnamet   warn(   t   reqR4   R   t   restt   datat   req_dictt   detailst   itemt   queryt   processR6   R7   R0   R   t   pname(    (    s%   /usr/share/python/debpython/pydist.pyt   guess_dependencym   s`    	$	



		c      	   C€  s  t  j |  ƒ } | r' | j d ƒ } n d  } g  } t } } g  } t |  d ƒ ñ } g  | j ƒ  D] } | j ƒ  ^ qb }	 xÂ |	 D]º }
 |
 s |
 j d ƒ r° | j	 |
 ƒ q n  |
 j d ƒ rÈ t
 } n  | rá | j	 |
 ƒ q n  t |
 | ƒ } | r.| j	 | ƒ d |
 j ƒ  k rt
 } q;| j	 |
 ƒ q | j	 |
 ƒ q WWd  QX| r}t |  d ƒ  } | j d „  | Dƒ ƒ Wd  QXn  | S(   Ni   t   rR
   t   [t
   setuptoolst   wc         s€  s   |  ] } | d  Vq d S(   s   
N(    (   R   R   (    (    s%   /usr/share/python/debpython/pydist.pys	   <genexpr>Ö   s    (   t   PUBLIC_DIR_RER   t   groupR:   R   R   t	   readlinesR   R   R&   R   RW   RC   t
   writelines(   R-   t
   public_dirt   verR0   t   modifiedt   optional_sectiont	   processedR   R   t   linesR   R    (    (    s%   /usr/share/python/debpython/pydist.pyt   parse_pydep´   s:    
%		 c         C€  s   t  j d ƒ j d |  ƒ j ƒ  S(   s   Emulate distribute's safe_name.s   [^A-Za-z0-9.]+t   _(   R=   R>   t   subRC   (   R   (    (    s%   /usr/share/python/debpython/pydist.pyR*   Ú   s    c         C€  sB   t  |  ƒ j d d ƒ }  |  j d ƒ r4 |  d }  n  d |  j ƒ  S(   s(   Guess Debian package name from Egg name.Rg   t   -s   python-i   s	   python-%s(   R*   t   replaceR   RC   (   t   egg_name(    (    s%   /usr/share/python/debpython/pydist.pyRL   ß   s    c         C€  s   d j  d „  |  j ƒ  Dƒ ƒ S(   s'   Return case insensitive dpkg -S regexp.t    c         s€  s7   |  ]- } | j  ƒ  r+ d  | j ƒ  | f n | Vq d S(   s   [%s%s]N(   t   isalphat   upper(   R   R   (    (    s%   /usr/share/python/debpython/pydist.pys	   <genexpr>é   s    (   R   RC   (   R   (    (    s%   /usr/share/python/debpython/pydist.pyRE   ç   s    ("   t
   __future__R    t   loggingR$   R=   t   os.pathR   R   R   t
   subprocessR   R   t   debpython.versionR   R   R   t   debpython.toolsR	   t	   getLoggert   __name__R   R>   R\   t   VERBOSER   R@   R   R2   R:   RW   Rf   R*   RL   RE   (    (    (    s%   /usr/share/python/debpython/pydist.pyt   <module>   s,   
	!G	&		