U .e @ s d dl mZ d dlZd dlZd dlZd dlZd dlZd dlm Z d dl mZ er\d dlm Z eeZG dd deZG dd d eZdS ) )absolute_importN)rmtree)MYPY_CHECK_RUNNING)Optionalc sV e Zd ZdZd fdd Zedd Zdd Zd d Zdd Z dd Z dd Z ZS ) 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. Ntempc sP t t| |d kr"|d kr"d}|d kr4| |}|| _d| _|| _|| _d S )NTF)superr __init___create_path_deleteddeletekind)selfpathr r __class__ @/usr/lib/python3.8/site-packages/pip/_internal/utils/temp_dir.pyr + s zTempDirectory.__init__c C s | j S N)r r r r r r @ s zTempDirectory.pathc C s d | jj| jS )Nz <{} {!r}>)formatr __name__r r r r r __repr__H s zTempDirectory.__repr__c C s | S r r r r r r __enter__K s zTempDirectory.__enter__c C s | j r| d S r )r cleanup)r excvaluetbr r r __exit__N s zTempDirectory.__exit__c C s. t jtjd|d}td| |S )zECreate a temporary directory and store its path in self.path pip-{}-prefixCreated temporary directory: {})osr realpathtempfilemkdtempr loggerdebug)r r r r r r r R s zTempDirectory._createc C s" d| _ tj| jrt| j dS )z?Remove the temporary directory created and reset state TN)r r$ r existsr r r r r r r _ s zTempDirectory.cleanup)NNr ) r __module____qualname____doc__r propertyr r r r r r __classcell__r r r r r s r c s: e Zd ZdZdZd fdd Zedd Zdd 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| _tt| j|d d S )Nz/\)r )rstriporiginalr r0 r )r r2 r r r r r | s zAdjacentTempDirectory.__init__c c s t dt|D ]D}t| j|d D ],}dd| ||d }||kr$|V q$qt t| jD ]8}t| j|D ]$}dd| | }||krt|V qtqbdS )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)rangelen itertoolscombinations_with_replacement LEADING_CHARSjoin)clsnamei candidatenew_namer r r _generate_names s z%AdjacentTempDirectory._generate_namesc C s t j| j\}}| |D ]b}t j||}zt | W n0 tk rl } z|jtj kr\ W 5 d }~X Y qX t j |} qqt j tjd |d}td | |S )Nr r! r# )r$ r splitr2 rA r; mkdirOSErrorerrnoZEEXISTr% r&