
¡\\Í  ã               @   sY   d  Z  d d l Z d d l Z d d l m Z e j e ƒ Z d d „  Z d d „  Z	 d S)zPlugin utilities.é    N)Úutilc             C   sk   t  j j |  ƒ } g  } xL t | ƒ d k rf | j | ƒ t  j j | ƒ \ } } | | d k r Pq W| S)a‡  Retrieves all possible path prefixes of a path, in descending order
    of length. For instance,
        (linux) /a/b/c returns ['/a/b/c', '/a/b', '/a', '/']
        (windows) C:\a\b\c returns ['C:\a\b\c', 'C:\a\b', 'C:\a', 'C:']
    :param str path: the path to break into prefixes

    :returns: all possible path prefixes of given path in descending order
    :rtype: `list` of `str`
    r   é   éÿÿÿÿ)ÚosÚpathÚnormpathÚlenÚappendÚsplit)r   ÚprefixÚprefixesÚ_© r   ú6/usr/lib/python3/dist-packages/certbot/plugins/util.pyÚget_prefixes	   s    
r   c             C   sÕ   d } t  j d } g  } x8 | D]0 } | | k r  | t  j | 7} | j | ƒ q  Wt | ƒ rŒ t j d |  t  j j | ƒ ƒ | t  j d <t j	 |  ƒ rŸ d St | ƒ r± d n d } t j d	 |  | | ƒ d
 Sd S)zöAttempt to perform PATH surgery to find cmd

    Mitigates https://github.com/certbot/certbot/issues/1833

    :param str cmd: the command that is being searched for in the PATH

    :returns: True if the operation succeeded, False otherwise
    ú	/usr/sbinú/usr/local/binú/usr/local/sbinÚPATHz6Can't find %s, attempting PATH mitigation by adding %sTz	 expandedÚ z*Failed to find executable %s in%s PATH: %sFN)r   r   r   )
r   ÚenvironÚpathsepr	   ÚanyÚloggerÚdebugÚjoinr   Z
exe_exists)ÚcmdÚdirsr   ZaddedÚdZexpandedr   r   r   Úpath_surgery   s"    		
r   )
Ú__doc__Zloggingr   Zcertbotr   Z	getLoggerÚ__name__r   r   r   r   r   r   r   Ú<module>   s   