U
    P^                     @   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Zd dlm	Z	 d dl
mZ er\d dlmZ eeZG dd deZG dd	 d	eZdS )
    )absolute_importN)rmtree)MYPY_CHECK_RUNNING)Optionalc                       sV   e Zd ZdZd fdd	Zedd Zdd	 Zd
d Zdd Z	dd Z
dd Z  ZS )TempDirectoryaM  Helper class that owns and cleans up a temporary directory.

    This class can be used as a context manager or as an OO representation of a
    temporary directory.

    Attributes:
        path
            Location to the created temporary directory
        delete
            Whether the directory should be deleted when exiting
            (when used as a contextmanager)

    Methods:
        cleanup()
            Deletes the temporary directory

    When used as a context manager, if the delete attribute is True, on
    exiting the context the temporary directory is deleted.
    Ntempc                    sP   t t|   |d kr"|d kr"d}|d kr4| |}|| _d| _|| _|| _d S )NTF)superr   __init___create_path_deleteddeletekind)selfpathr   r   	__class__ =/tmp/pip-install-220asx0h/pip/pip/_internal/utils/temp_dir.pyr	   +   s    
zTempDirectory.__init__c                 C   s   | j rtd| j| jS )Nz$Attempted to access deleted path: {})r   AssertionErrorformatr   r   r   r   r   r   @   s    
zTempDirectory.pathc                 C   s   d | jj| jS )Nz	<{} {!r}>)r   r   __name__r   r   r   r   r   __repr__H   s    zTempDirectory.__repr__c                 C   s   | S Nr   r   r   r   r   	__enter__K   s    zTempDirectory.__enter__c                 C   s   | j r|   d S r   )r   cleanup)r   excvaluetbr   r   r   __exit__N   s    zTempDirectory.__exit__c                 C   s.   t jtjd|d}td| |S )zECreate a temporary directory and store its path in self.path
        pip-{}-prefixCreated temporary directory: {})osr   realpathtempfilemkdtempr   loggerdebug)r   r   r   r   r   r   r
   R   s
    zTempDirectory._createc                 C   s"   d| _ tj| jrt| j dS )z?Remove the temporary directory created and reset state
        TN)r   r%   r   existsr   r   r   r   r   r   r   _   s    zTempDirectory.cleanup)NNr   )r   
__module____qualname____doc__r	   propertyr   r   r   r    r
   r   __classcell__r   r   r   r   r      s      
r   c                       s:   e Zd ZdZdZd
 fdd	Zedd Zdd	 Z  Z	S )AdjacentTempDirectorya  Helper class that creates a temporary directory adjacent to a real one.

    Attributes:
        original
            The original directory to create a temp directory for.
        path
            After calling create() or entering, contains the full
            path to the temporary directory.
        delete
            Whether the directory should be deleted when exiting
            (when used as a contextmanager)

    z-~.=%0123456789Nc                    s"   | d| _tt| j|d d S )Nz/\)r   )rstriporiginalr   r1   r	   )r   r3   r   r   r   r   r	   |   s    zAdjacentTempDirectory.__init__c                 c   s   t dt|D ]D}t| j|d D ],}dd| ||d  }||kr$|V  q$qt t| jD ]8}t| j|D ]$}dd| | }||krt|V  qtqbdS )a  Generates a series of temporary names.

        The algorithm replaces the leading characters in the name
        with ones that are valid filesystem characters, but are not
        valid package names (for both Python and pip definitions of
        package).
           ~ N)rangelen	itertoolscombinations_with_replacementLEADING_CHARSjoin)clsnamei	candidatenew_namer   r   r   _generate_names   s     	 
 z%AdjacentTempDirectory._generate_namesc                 C   s   t j| j\}}| |D ]b}t j||}zt | W n0 tk
rl } z|jtj	kr\ W 5 d }~X Y qX t j
|} qqt j
tjd|d}td| |S )Nr!   r"   r$   )r%   r   splitr3   rB   r<   mkdirOSErrorerrnoEEXISTr&   r'   r(   r   r)   r*   )r   r   rootr>   r@   r   exr   r   r   r
      s    zAdjacentTempDirectory._create)N)
r   r,   r-   r.   r;   r	   classmethodrB   r
   r0   r   r   r   r   r1   g   s   
r1   )
__future__r   rF   r9   loggingos.pathr%   r'   pip._internal.utils.miscr   pip._internal.utils.typingr   typingr   	getLoggerr   r)   objectr   r1   r   r   r   r   <module>   s   
Q