B
    Kkd                 @   sn   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	 e
eZG dd deZG dd deZdS )    )absolute_importN)rmtreec                   sJ   e Zd ZdZd fdd	Zdd Zdd	 Zd
d Zdd Zdd Z	  Z
S )TempDirectorya'  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 or None
        delete
            Whether the directory should be deleted when exiting
            (when used as a contextmanager)

    Methods:
        create()
            Creates a temporary directory and stores its path in the path
            attribute.
        cleanup()
            Deletes the temporary directory and sets path attribute to None

    When used as a context manager, a temporary directory is created on
    entering the context and, if the delete attribute is True, on exiting the
    context the created directory is deleted.
    Ntempc                s8   t t|   |d kr"|d kr"d}|| _|| _|| _d S )NT)superr   __init__pathdeletekind)selfr   r	   r
   )	__class__ =/tmp/pip-install-gxxfd9b7/pip/pip/_internal/utils/temp_dir.pyr   '   s    zTempDirectory.__init__c             C   s   d | jj| jS )Nz	<{} {!r}>)formatr   __name__r   )r   r   r   r   __repr__3   s    zTempDirectory.__repr__c             C   s   |    | S )N)create)r   r   r   r   	__enter__6   s    zTempDirectory.__enter__c             C   s   | j r|   d S )N)r	   cleanup)r   excvaluetbr   r   r   __exit__:   s    zTempDirectory.__exit__c             C   sT   | j dk	r td| j  dS tj tjd| jd| _ td| j  dS )zECreate a temporary directory and store its path in self.path
        Nz+Skipped creation of temporary directory: {}zpip-{}-)prefixzCreated temporary directory: {})	r   loggerdebugr   osrealpathtempfilemkdtempr
   )r   r   r   r   r   >   s    
zTempDirectory.createc             C   s,   | j dk	r"tj | j r"t| j  d| _ dS )z?Remove the temporary directory created and reset state
        N)r   r   existsr   )r   r   r   r   r   O   s    
zTempDirectory.cleanup)NNr   )r   
__module____qualname____doc__r   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"   t t| j|d |d| _d S )N)r	   z/\)r   r%   r   rstriporiginal)r   r'   r	   )r   r   r   r   l   s    zAdjacentTempDirectory.__init__c             c   s   xZt dt|D ]H}xBt| j|d D ],}dd| ||d  }||kr(|V  q(W qW xNt t| jD ]<}x6t| j|D ]$}dd| | }||kr|V  qW qlW d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_namesp   s    	z%AdjacentTempDirectory._generate_namesc             C   s   t j| j\}}xp| |D ]b}t j||}yt | W n0 tk
rn } z|jtj	kr^ W d d }~X Y qX t j
|| _P qW | jst j
tjd| jd| _td| j d S )Nzpip-{}-)r   zCreated temporary directory: {})r   r   splitr'   r6   r0   mkdirOSErrorerrnoEEXISTr   r   r   r   r
   r   r   )r   rootr2   r4   r   exr   r   r   r      s    zAdjacentTempDirectory.create)N)
r   r!   r"   r#   r/   r   classmethodr6   r   r$   r   r   )r   r   r%   W   s
   r%   )
__future__r   r:   r-   loggingos.pathr   r   pip._internal.utils.miscr   	getLoggerr   r   objectr   r%   r   r   r   r   <module>   s   
I