ó
çiõ\c           @   s$  d  d l  Z  d  d l Z d  d l Z d  d l 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 d  d l m Z d  d l m Z d  d l m Z d  d l m Z d  d l Z d  d l m Z d d	 l m Z e rd  d
 l m Z m Z n d  d
 l m Z m Z d d g Z d& Z e j d d ƒ Z d „  Z e  d „ Z! d „  Z" d „  Z# d „  Z$ d „  Z% e j& rŸd „  Z' n e( Z' d „  Z) d „  Z* d „  Z+ e  j, d „ Z- d „  Z. d „  Z/ d „  Z0 d „  Z1 d  „  Z2 d! „  Z3 d" „  Z4 d# „  Z5 d$ „  Z6 d S('   iÿÿÿÿN(   t   reduce(   t
   expanduser(   t
   expandvars(   t   isabs(   t   sep(   t   mapi   (   t   PY36(   t   Patht   PurePathR   R   i<   i   t   joinpaths   .lockc         C   s-   |  j  ƒ  r t |  d t ƒn  |  j ƒ  d S(   s6   
    ensures the given path is an empty directory
    t   forceN(   t   existst   rmtreet   Truet   mkdir(   t   path(    (    s.   lib/python2.7/site-packages/_pytest/pathlib.pyt   ensure_reset_dir$   s    c         C   s9   | r" t  j t |  ƒ d t ƒn t  j t |  ƒ ƒ d  S(   Nt   ignore_errors(   t   shutilR   t   strR   (   R   R
   (    (    s.   lib/python2.7/site-packages/_pytest/pathlib.pyR   -   s    c         c   sG   | j  ƒ  } x4 |  j ƒ  D]& } | j j  ƒ  j | ƒ r | Vq q Wd S(   sG   finds all elements in root that begin with the prefix, case insensitiveN(   t   lowert   iterdirt   namet
   startswith(   t   roott   prefixt   l_prefixt   x(    (    s.   lib/python2.7/site-packages/_pytest/pathlib.pyt   find_prefixed6   s    c         c   s-   t  | ƒ } x |  D] } | j | Vq Wd S(   sž   
    :param iter: iterator over path names
    :param prefix: expected prefix of the path names
    :returns: the parts of the paths following the prefix
    N(   t   lenR   (   t   iterR   t   p_lent   p(    (    s.   lib/python2.7/site-packages/_pytest/pathlib.pyt   extract_suffixes>   s    c         C   s   t  t |  | ƒ | ƒ S(   s0   combines find_prefixes and extract_suffixes
    (   R!   R   (   R   R   (    (    s.   lib/python2.7/site-packages/_pytest/pathlib.pyt   find_suffixesI   s    c         C   s'   y t  |  ƒ SWn t k
 r" d SXd S(   s0   parses number path suffixes, returns -1 on erroriÿÿÿÿN(   t   intt
   ValueError(   t	   maybe_num(    (    s.   lib/python2.7/site-packages/_pytest/pathlib.pyt	   parse_numO   s    c         C   s   t  t |  | ƒ S(   s<   needed due to python2.7 lacking the default argument for max(   R    t   max(   t   iterablet   default(    (    s.   lib/python2.7/site-packages/_pytest/pathlib.pyt   _maxY   s    c         C   sZ   |  j  | ƒ } y | j ƒ  Wn t k
 r0 n Xy | j | ƒ Wn t k
 rU n Xd S(   s  helper to create the current symlink

    it's full of race conditions that are reasonably ok to ignore
    for the context of best effort linking to the latest testrun

    the presumption being thatin case of much parallelism
    the inaccuracy is going to be acceptable
    N(   R	   t   unlinkt   OSErrort
   symlink_tot	   Exception(   R   t   targett   link_tot   current_symlink(    (    s.   lib/python2.7/site-packages/_pytest/pathlib.pyt   _force_symlinkb   s    	c         C   s¼   xµ t  d ƒ D]‰ } t t t t |  | ƒ ƒ d d ƒ} | d } |  j d j | | ƒ ƒ } y | j ƒ  Wn t k
 r} q Xt	 |  | d | ƒ | Sq Wt
 d j d | d	 |  ƒ ƒ ‚ d
 S(   sJ   create a directory with an increased number as suffix for the given prefixi
   R)   iÿÿÿÿi   s   {}{}t   currentsK   could not create numbered dir with prefix {prefix} in {root} after 10 triesR   R   N(   t   rangeR*   R   R&   R"   R	   t   formatR   R.   R2   t   EnvironmentError(   R   R   t   it   max_existingt
   new_numbert   new_path(    (    s.   lib/python2.7/site-packages/_pytest/pathlib.pyt   make_numbered_dirv   s    $
	c         C   s  t  |  ƒ } y0 t j t | ƒ t j t j Bt j Bd ƒ } WnM t k
 r‹ } | j t j	 k r… t
 j t d j d |  ƒ ƒ | ƒ q‚  nv Xt j ƒ  } t | ƒ } t | t ƒ sÅ | j d ƒ } n  t j | | ƒ t j | ƒ | j ƒ  sý t d ƒ ‚ n  | Sd S(   s1   crates a lock to prevent premature folder cleanupi¤  s    cannot create lockfile in {path}R   t   asciis/   lock path got renamed after successful creationN(   t   get_lock_patht   ost   openR   t   O_WRONLYt   O_CREATt   O_EXCLR,   t   errnot   EEXISTt   sixt
   raise_fromR6   R5   t   getpidt
   isinstancet   bytest   encodet   writet   closet   is_file(   R    t	   lock_patht   fdt   et   pidt   spid(    (    s.   lib/python2.7/site-packages/_pytest/pathlib.pyt   create_cleanup_lock‹   s"    0c         C   s%   t  j ƒ  } |  | d „ } | | ƒ S(   sF   registers a cleanup function for removing a lock, by default on atexitc         S   sH   t  j ƒ  } | | k r d  Sy |  j ƒ  Wn t t f k
 rC n Xd  S(   N(   R>   RG   R+   R,   t   IOError(   RN   t   original_pidt   current_pid(    (    s.   lib/python2.7/site-packages/_pytest/pathlib.pyt   cleanup_on_exit§   s    (   R>   RG   (   RN   t   registerRQ   RW   (    (    s.   lib/python2.7/site-packages/_pytest/pathlib.pyt   register_cleanup_lock_removal£   s    
c      	   C   s¸   d } zs yT t |  ƒ } |  j } | j d j t j ƒ  ƒ ƒ } |  j | ƒ t | d t	 ƒWn t
 t f k
 rw d SXWd | d k	 r³ y | j ƒ  Wq³ t
 t f k
 r¯ q³ Xn  Xd S(   sZ   removes a numbered directory if its lock can be obtained and it does not seem to be in uses
   garbage-{}R
   N(   t   NoneRS   t   parentR	   R5   t   uuidt   uuid4t   renameR   R   R,   R6   R+   RT   (   R   RN   R[   t   garbage(    (    s.   lib/python2.7/site-packages/_pytest/pathlib.pyt   maybe_delete_a_numbered_dir´   s     		c         C   sv   |  j  ƒ  r t St |  ƒ } | j ƒ  s, t Sy | j ƒ  j } Wn t k
 rS t SX| | k  rn | j ƒ  t St Sd S(   s<   checks if a lock exists and breaks it if its considered deadN(	   t
   is_symlinkt   FalseR=   R   R   t   statt   st_mtimeR.   R+   (   R   t$   consider_lock_dead_if_created_beforet   lockt	   lock_time(    (    s.   lib/python2.7/site-packages/_pytest/pathlib.pyt   ensure_deletableÎ   s    
c         C   s    t  |  | ƒ r t |  ƒ n  d S(   s9   tries to cleanup a folder if we can ensure it's deletableN(   Rh   R`   (   R   Re   (    (    s.   lib/python2.7/site-packages/_pytest/pathlib.pyt   try_cleanupá   s    c   
      c   s¢   t  t t t |  | ƒ ƒ d d ƒ} | | } t |  | ƒ } t j | ƒ \ } } t t t | | ƒ ƒ } x1 t | | ƒ D]  \ } }	 |	 | k rz | Vqz qz Wd S(   sI   lists candidates for numbered directories to be removed - follows py.pathR)   iÿÿÿÿN(	   R*   R   R&   R"   R   t	   itertoolst   teeR!   t   zip(
   R   R   t   keepR8   t
   max_deletet   pathst   paths2t   numbersR   t   number(    (    s.   lib/python2.7/site-packages/_pytest/pathlib.pyt   cleanup_candidatesç   s    $
c         C   sU   x' t  |  | | ƒ D] } t | | ƒ q Wx$ |  j d ƒ D] } t | | ƒ q: Wd S(   s,   cleanup for lock driven numbered directoriess	   garbage-*N(   Rs   Ri   t   glob(   R   R   Rm   Re   R   (    (    s.   lib/python2.7/site-packages/_pytest/pathlib.pyt   cleanup_numbered_diró   s    c   
   
   C   s´   d } x t d ƒ D] } y) t |  | ƒ } t | ƒ } t | ƒ Wn t k
 r] } | } q X| j ƒ  j | }	 t d |  d | d | d |	 ƒ | Sq W| d k	 sª t	 ‚ | ‚ d S(   s?   creates a numbered dir with a cleanup lock and removes old onesi
   R   R   Rm   Re   N(
   RZ   R4   R;   RS   RY   R.   Rc   Rd   Ru   t   AssertionError(
   R   R   Rm   t   lock_timeoutRP   R7   R    RN   t   excRe   (    (    s.   lib/python2.7/site-packages/_pytest/pathlib.pyt   make_numbered_dir_with_cleanupû   s"    
c         C   sg   t  |  t ƒ s t d ƒ ‚ t | ƒ } t |  ƒ }  t |  ƒ }  t |  ƒ rV t |  ƒ S| j |  ƒ Sd  S(   Ns   would break on py2(   RH   R   Rv   R   R   R   R	   (   t   inputR   (    (    s.   lib/python2.7/site-packages/_pytest/pathlib.pyt   resolve_from_str  s    
c         C   sˆ   t  | ƒ } t j j d ƒ } | rQ t |  k rQ t |  k rQ |  j t t ƒ }  n  t |  k ri | j } n t j	 | ƒ } t
 j
 | |  ƒ S(   s™  FNMatcher port from py.path.common which works with PurePath() instances.

    The difference between this algorithm and PurePath.match() is that the latter matches "**" glob expressions
    for each part of the path, while this algorithm uses the whole path instead.

    For example:
        "tests/foo/bar/doc/test_foo.py" matches pattern "tests/**/doc/test*.py" with this algorithm, but not with
        PurePath.match().

    This algorithm was ported to keep backward-compatibility with existing settings which assume paths match according
    this logic.

    References:
    * https://bugs.python.org/issue29249
    * https://bugs.python.org/issue34731
    t   win(   R   t   syst   platformR   R   t	   posix_sept   replaceR   RE   t	   text_typet   fnmatch(   t   patternR   t   iswin32R   (    (    s.   lib/python2.7/site-packages/_pytest/pathlib.pyt
   fnmatch_ex  s    c            s/   |  j  t ƒ ‰  ‡  f d †  t t ˆ  ƒ ƒ Dƒ S(   Nc            s-   h  |  ]# } t  j ˆ  | d   ƒ p& t  ’ q S(   i   (   R   t   join(   t   .0R7   (   t   parts(    s.   lib/python2.7/site-packages/_pytest/pathlib.pys	   <setcomp>@  s   	 (   t   splitR   R4   R   (   t   s(    (   Rˆ   s.   lib/python2.7/site-packages/_pytest/pathlib.pyRˆ   >  s    i  i0*  (7   t   atexitRC   R‚   Rj   t   operatorR>   R   R}   R\   t	   functoolsR    t   os.pathR   R   R   R   t	   posixpathR   RE   t	   six.movesR   t   compatR   t   pathlibR   R   t   pathlib2t   __all__t   LOCK_TIMEOUTt   methodcallerR=   R   Rb   R   R   R!   R"   R&   t   PY2R*   R'   R2   R;   RS   RX   RY   R`   Rh   Ri   Rs   Ru   Ry   R{   R…   Rˆ   (    (    (    s.   lib/python2.7/site-packages/_pytest/pathlib.pyt   <module>   sZ   																			!