ó
}k^c           @€  so  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
 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# e j d ƒ Z$ e j d ƒ Z% d „  Z& d „  Z' d S(   iÿÿÿÿ(   t   with_statementN(   t   existst   isdirt   join(   t	   maketrans(   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|tr|y).*)?                 # 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>T   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   loadK   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 ƒ  ƒ } | r´xË | D]À } | r| | j d | ƒ k rqí n  | d s%d  S| d j d ƒ r@| d S| d	 r¥| d
 s^| d r¥| d d k r¥t | d	 | d | d
 ƒ } d | d | d | f 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 rÇt ƒ  } xC | j d ƒ D]2 } | j ƒ  swq_n  | j | j d ƒ d ƒ q_Wt | ƒ d k rº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   versiont   standardR"   t   operators   ==s
   %s (%s %s)s   '*/%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.(   Ns   ==(#   R   t   debugR   t   Nonet
   isinstancet
   basestringR   t   ret   compileR   t   groupsR+   R3   t   REQUIRES_REt   infoR)   R*   t   gett   lowert   endswitht
   _translatet	   ci_regexpR   R   R   t   communicatet
   returncodet   setR,   R   t   addt   lenR   t   popt   sensible_pname(   t   reqR5   R   t   restt   datat   req_dt   detailst   itemt   vt   queryt   processR9   R:   R1   R   t   pname(    (    s%   /usr/share/python/debpython/pydist.pyt   guess_dependencyn   sd    	$	


		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   R[   RF   t
   writelines(   R.   t
   public_dirt   verR1   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@   RA   t   subRF   (   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.Rk   t   -s   python-i   s	   python-%s(   R+   t   replaceR   RF   (   t   egg_name(    (    s%   /usr/share/python/debpython/pydist.pyRP   á   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   RF   (   R   (    (    s%   /usr/share/python/debpython/pydist.pyRI   é   s    s   [-.]?(alpha|beta|rc|dev|a|b|c)s   \$(\d+)c         C€  s   t  j d |  ƒ S(   sc   Convert Perl RE patterns used in uscan to Python's

    >>> print _pl2py('foo$3')
    foo\g<3>
    s   \\g<\1>(   t   GROUP_RERl   (   t   pattern(    (    s%   /usr/share/python/debpython/pydist.pyt   _pl2pyò   s    c   	      C€  sc  x;| D]3} | j  d ƒ ru | j  d ƒ r1 d n d } | | d j | | ƒ } |  j t | d | d ƒ ƒ }  q | j  d ƒ r*| d j | d ƒ } t j | d ƒ } d } | d r| d } d | k rÝ d } n  d | k rt j | d t j ƒ } qn  | j t | d ƒ |  | ƒ }  q t	 j
 d	 | ƒ q W| d
 k r_t j d |  ƒ }  n  |  S(   s>  Translate Python version into Debian one.

    >>> _translate('1.C2betac', ['s/c//gi'], None)
    '1.2beta'
    >>> _translate('5-fooa1.2beta3-fooD',
    ...     ['s/^/1:/', 's/-foo//g', 's:([A-Z]):+$1:'], 'PEP386')
    '1:5~a1.2~beta3+D'
    >>> _translate('x.y.x.z', ['tr/xy/ab/', 'y,z,Z,'], None)
    'a.b.a.Z'
    t   trt   yi   i   i    t   st   gR   s   unknown rule ignored: %st   PEP386s   ~\g<1>(   Rv   Rw   (   R   R,   t	   translateR   R@   RA   t   IRl   Ru   R   t   warnt
   PRE_VER_RE(	   R5   R"   R6   t   rulet   post   tmpRt   t   countt   flags(    (    s%   /usr/share/python/debpython/pydist.pyRH   û   s(    #

	"((   t
   __future__R    t   loggingR%   R@   t   os.pathR   R   R   t   stringR   t
   subprocessR   R   t   debpython.versionR   R   R	   t   debpython.toolsR
   t	   getLoggert   __name__R   RA   R`   t   VERBOSER   RC   R   R3   R=   R[   Rj   R+   RP   RI   R~   Rs   Ru   RH   (    (    (    s%   /usr/share/python/debpython/pydist.pyt   <module>   s6   
	!H	&					