o
    $¼"bø  ã                   @   sê   d Z ddlmZm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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mZ G dd„ deƒZe e¡G dd„ deƒƒZG dd„ deƒZG dd„ deƒZdS )uà  
A rough layout of the current storage goes as:

virtualenv-app-data
â”œâ”€â”€ py - <version> <cache information about python interpreters>
â”‚Â Â  â””â”€â”€ *.json/lock
â”œâ”€â”€ wheel <cache wheels used for seeding>
â”‚ Â  â”œâ”€â”€ house
â”‚   â”‚   â””â”€â”€ *.whl <wheels downloaded go here>
â”‚   â””â”€â”€ <python major.minor> -> 3.9
â”‚       â”œâ”€â”€ img-<version>
â”‚       â”‚   â””â”€â”€ image
â”‚       â”‚           â””â”€â”€ <install class> -> CopyPipInstall / SymlinkPipInstall
â”‚       â”‚               â””â”€â”€ <wheel name> -> pip-20.1.1-py2.py3-none-any
â”‚       â””â”€â”€ embed
â”‚           â””â”€â”€ 3 -> json format versioning
â”‚               â””â”€â”€ *.json -> for every distribution contains data about newer embed versions and releases
â””â”€â”€â”€ unzip <in zip app we cannot refer to some internal files, so first extract them>
     â””â”€â”€ <virtualenv version>
         â”œâ”€â”€ py_info.py
         â”œâ”€â”€ debug.py
         â””â”€â”€ _virtualenv.py
é    )Úabsolute_importÚunicode_literalsN)ÚABCMeta)Úcontextmanager)Úsha256)ÚReentrantFileLock)Úsafe_delete)Úensure_text)Úextract)Ú__version__é   )ÚAppDataÚContentStorec                   @   s�   e Zd ZdZdZdZdd„ Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
edd„ ƒZedd„ ƒZedd„ ƒZdd„ Zdd„ Zdd„ Zedd„ ƒZdd„ ZdS )ÚAppDataDiskFolderzH
    Store the application data on the disk within a folder layout.
    FTc                 C   s   t |ƒ| _d S ©N)r   Úlock)ÚselfÚfolder© r   úE/usr/lib/python3/dist-packages/virtualenv/app_data/via_disk_folder.pyÚ__init__4   s   zAppDataDiskFolder.__init__c                 C   s   d  t| ƒj| jj¡S )Nz{}({}))ÚformatÚtypeÚ__name__r   Úpath©r   r   r   r   Ú__repr__7   s   zAppDataDiskFolder.__repr__c                 C   s   t | jjƒS r   )Ústrr   r   r   r   r   r   Ú__str__:   ó   zAppDataDiskFolder.__str__c                 C   s    t  d| jj¡ t| jjƒ d S )Nzreset app data folder %s)ÚloggingÚdebugr   r   r   r   r   r   r   Úreset=   s   zAppDataDiskFolder.resetc                 C   s   dS )z
do nothingNr   r   r   r   r   ÚcloseA   s    zAppDataDiskFolder.closec                 c   s>   � | j | }|� |jV  W d   ƒ d S 1 sw   Y  d S r   ©r   r   )r   r   Ú	path_lockr   r   r   ÚlockedD   s
   €

"ÿzAppDataDiskFolder.lockedc                 c   sz   � |d urt |ƒ ƒ}n| jd t }| |j¡� |j|j }| ¡ s(t||ƒ |V  W d   ƒ d S 1 s6w   Y  d S )NÚunzip)r   r   r   Úlock_for_keyÚnamer   Úexistsr
   )r   r   Ú	to_folderÚrootÚdestr   r   r   r
   J   s   €
"üzAppDataDiskFolder.extractc                 C   s   | j d d S )NÚpy_infoÚ1)r   r   r   r   r   Ú
py_info_atV   s   zAppDataDiskFolder.py_info_atc                 C   s   t | j|ƒS r   )ÚPyInfoStoreDiskr0   ©r   r   r   r   r   r.   Z   r   zAppDataDiskFolder.py_infoc              
   C   s†   | j }|�4 |j ¡ D ]%}|jdkr0| |j¡� | ¡ r!| ¡  W d  ƒ n1 s+w   Y  qW d  ƒ dS 1 s<w   Y  dS )ú z.jsonN)r0   r   ÚiterdirÚsuffixr(   Ústemr*   Úunlink)r   Úpy_info_folderÚfilenamer   r   r   Úpy_info_clear]   s   
€þ€þ"ÿzAppDataDiskFolder.py_info_clearc                 C   s   t | jd | d d |ƒS )NÚwheelÚembedÚ3)Ú EmbedDistributionUpdateStoreDiskr   )r   ÚdistributionÚfor_py_versionr   r   r   Úembed_update_logg   ó   z"AppDataDiskFolder.embed_update_logc                 C   s"   | j jd d }|jddd� |S )Nr;   ÚhouseT©ÚparentsÚexist_ok)r   r   Úmkdirr2   r   r   r   rC   j   s   zAppDataDiskFolder.housec                 C   s   | j jd | d d | S )Nr;   Úimager/   r$   )r   r@   r)   r   r   r   Úwheel_imagep   rB   zAppDataDiskFolder.wheel_imageN)r   Ú
__module__Ú__qualname__Ú__doc__Ú	transientÚ
can_updater   r   r   r"   r#   r   r&   r
   Úpropertyr0   r.   r:   rA   rC   rI   r   r   r   r   r   ,   s*    




r   c                   @   sL   e Zd Zdd„ Zedd„ ƒZdd„ Zdd„ Zd	d
„ Ze	dd„ ƒZ
dd„ ZdS )ÚJSONStoreDiskc                 C   s$   || _ || _|| _|| jf | _d S r   )Ú	in_folderÚkeyÚmsgÚfileÚmsg_args)r   rQ   rR   rS   rU   r   r   r   r   v   s   zJSONStoreDisk.__init__c                 C   s   | j jd | j¡ S )Nz{}.json)rQ   r   r   rR   r   r   r   r   rT   |   s   zJSONStoreDisk.filec                 C   s
   | j  ¡ S r   )rT   r*   r   r   r   r   r*   €   s   
zJSONStoreDisk.existsc                 C   sŽ   d\}}zt  | j ¡ ¡}tjd | j¡g| j¢R Ž  |W S  t	y(   d}Y n	 t
y0   Y nw |rEz|  ¡  W d S  tyD   Y d S w d S )N)NFzgot {} from %sT)ÚjsonÚloadsrT   Ú	read_textr    r!   r   rS   rU   Ú
ValueErrorÚ	ExceptionÚremoveÚOSError)r   ÚdataÚ
bad_formatr   r   r   Úreadƒ   s&   ÿ
þþzJSONStoreDisk.readc                 C   s*   | j  ¡  tjd | j¡g| j¢R Ž  d S )Nzremoved {} at %s)rT   r7   r    r!   r   rS   rU   r   r   r   r   r[   ”   s   
 zJSONStoreDisk.removec                 c   s<   � | j  | j¡� d V  W d   ƒ d S 1 sw   Y  d S r   )rQ   r(   rR   r   r   r   r   r&   ˜   s   €"ÿzJSONStoreDisk.lockedc                 C   sR   | j j}|jddd� | j  ttj|ddd�ƒ¡ tjd 	| j
¡g| j¢R Ž  d S )NTrD   é   )Ú	sort_keysÚindentzwrote {} at %s)rT   ÚparentrG   Ú
write_textr	   rV   Údumpsr    r!   r   rS   rU   )r   Úcontentr   r   r   r   Úwrite�   s    zJSONStoreDisk.writeN)r   rJ   rK   r   rO   rT   r*   r_   r[   r   r&   rg   r   r   r   r   rP   t   s    

rP   c                       ó   e Zd Z‡ fdd„Z‡  ZS )r1   c                    s@   t tjrt|ƒ d¡nt|ƒƒ ¡ }tt| ƒ ||d|f¡ d S )Nzutf-8zpython info of %s)	r   ÚsixÚPY3r   ÚencodeÚ	hexdigestÚsuperr1   r   )r   rQ   r   rR   ©Ú	__class__r   r   r   ¥   s   $zPyInfoStoreDisk.__init__©r   rJ   rK   r   Ú__classcell__r   r   rn   r   r1   ¤   ó    r1   c                       rh   )r>   c                    s   t t| ƒ ||d|f¡ d S )Nzembed update of distribution %s)rm   r>   r   )r   rQ   r?   rn   r   r   r   «   s   
üz)EmbedDistributionUpdateStoreDisk.__init__rp   r   r   rn   r   r>   ª   rr   r>   )rL   Ú
__future__r   r   rV   r    Úabcr   Ú
contextlibr   Úhashlibr   ri   Úvirtualenv.util.lockr   Úvirtualenv.util.pathr   Úvirtualenv.util.sixr	   Úvirtualenv.util.zipappr
   Úvirtualenv.versionr   Úbaser   r   r   Úadd_metaclassrP   r1   r>   r   r   r   r   Ú<module>   s&   H/