o
    7f4                     @  s   U d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	m
Z
mZ e	r+ddlZG dd dZdd
dZG dd deZdZg Zded< dddZG dd deZdS )a:  Interfaces for accessing metadata.

We provide two implementations.
 * The "classic" file system implementation, which uses a directory
   structure of files.
 * A hokey sqlite backed implementation, which basically simulates
   the file system in an effort to work around poor file system performance
   on OS X.
    )annotationsN)abstractmethod)TYPE_CHECKINGAnyIterablec                   @  sf   e Zd ZdZedddZeddd	ZeddddZedddZedddZ	edddZ
d
S ) MetadataStorez'Generic interface for metadata storage.namestrreturnfloatc                 C     dS )zgRead the mtime of a metadata entry..

        Raises FileNotFound if the entry does not exist.
        N selfr   r   r   G/home/ertert/spirit/venv/lib/python3.10/site-packages/mypy/metastore.pygetmtime       zMetadataStore.getmtimec                 C  r   )ziRead the contents of a metadata entry.

        Raises FileNotFound if the entry does not exist.
        Nr   r   r   r   r   read#   r   zMetadataStore.readNdatamtimefloat | Noneboolc                 C  r   )zWrite a metadata entry.

        If mtime is specified, set it as the mtime of the entry. Otherwise,
        the current time is used.

        Returns True if the entry is successfully written, False otherwise.
        Nr   )r   r   r   r   r   r   r   write*   r   zMetadataStore.writeNonec                 C  r   )zDelete a metadata entryNr   r   r   r   r   remove4   r   zMetadataStore.removec                 C  r   )zIf the backing store requires a commit, do it.

        But N.B. that this is not *guaranteed* to do anything, and
        there is no guarantee that changes are not made until it is
        called.
        Nr   r   r   r   r   commit8   r   zMetadataStore.commitIterable[str]c                 C     d S Nr   r   r   r   r   list_allA      zMetadataStore.list_allr   r	   r
   r   r   r	   r
   r	   r   r   r	   r   r	   r   r   r
   r   r   r	   r
   r   r
   r   r
   r   )__name__
__module____qualname____doc__r   r   r   r   r   r   r    r   r   r   r   r      s    	r   r
   r	   c                   C  s   t tddS )N   ascii)binasciihexlifyosurandomdecoder   r   r   r   random_stringE   s   r3   c                   @  sT   e Zd ZdddZdd	d
ZdddZddddZd ddZd!ddZd"ddZ	dS )#FilesystemMetadataStorecache_dir_prefixr	   r
   r   c                 C  s    | tjrd | _d S || _d S r   )
startswithr0   devnullr5   r   r5   r   r   r   __init__J   s   

z FilesystemMetadataStore.__init__r   r   c                 C  s(   | j st ttjtj| j |S r   )r5   FileNotFoundErrorintr0   pathr   joinr   r   r   r   r   S   s   z FilesystemMetadataStore.getmtimec                 C  sl   t j|t j|ksJ d| jst tt j| j|}| W  d    S 1 s/w   Y  d S )NDon't use absolute paths!)	r0   r<   normpathabspathr5   r:   openr=   r   )r   r   fr   r   r   r   Y   s    $zFilesystemMetadataStore.readNr   r   r   r   c                 C  s   t j|t j|ksJ d| jsdS t j| j|}|d t  }z>t jt j|dd t	|d}|
| W d    n1 sEw   Y  t || |d ur`t j|||fd W dS W dS  tyl   Y dS w )Nr>   F.Texist_okw)times)r0   r<   r?   r@   r5   r=   r3   makedirsdirnamerA   r   replaceutimeOSError)r   r   r   r   r<   tmp_filenamerB   r   r   r   r   b   s&    zFilesystemMetadataStore.writec                 C  s&   | j st ttj| j | d S r   )r5   r:   r0   r   r<   r=   r   r   r   r   r   v   s   zFilesystemMetadataStore.removec                 C  r   r   r   r   r   r   r   r   |   r!   zFilesystemMetadataStore.commitr   c                 c  sR    | j sd S t| j D ]\}}}tj|| j }|D ]
}tj||V  qqd S r   )r5   r0   walkr<   relpathr=   )r   dir_filesfiler   r   r   r       s   z FilesystemMetadataStore.list_allr5   r	   r
   r   r"   r#   r   r$   r%   r&   r'   )
r(   r)   r*   r9   r   r   r   r   r   r    r   r   r   r   r4   I   s    

	
	

r4   z
CREATE TABLE IF NOT EXISTS files (
    path TEXT UNIQUE NOT NULL,
    mtime REAL,
    data TEXT
);
CREATE INDEX IF NOT EXISTS path_idx on files(path);
z	list[str]
MIGRATIONSdb_filesqlite3.Connectionc              	   C  sN   dd l }|j| }|t tD ]}z|| W q |jy$   Y qw |S )Nr   )sqlite3.dbapi2dbapi2connectexecutescriptSCHEMArU   OperationalError)rV   sqlite3dbmigrr   r   r   
connect_db   s   
ra   c                   @  s^   e Zd ZdddZd d
dZd!ddZd"ddZd#d$ddZd%ddZd&ddZ	d'ddZ
dS )(SqliteMetadataStorer5   r	   r
   r   c                 C  s<   | tjrd | _d S tj|dd ttj|d| _d S )NTrD   zcache.db)r6   r0   r7   r_   rH   ra   r<   r=   r8   r   r   r   r9      s
   zSqliteMetadataStore.__init__r   fieldr   c                 C  sR   | j st | j d| d|f}| }|st t|dks#J |d d S )NzSELECT z FROM files WHERE path = ?   r   )r_   r:   executefetchalllen)r   r   rc   curresultsr   r   r   _query   s   zSqliteMetadataStore._queryr   c                 C     |  |d}t|tsJ |S )Nr   )rj   
isinstancer   )r   r   r   r   r   r   r         zSqliteMetadataStore.getmtimec                 C  rk   )Nr   )rj   rl   r	   )r   r   r   r   r   r   r      rm   zSqliteMetadataStore.readNr   r   r   r   c                 C  sT   dd l }| js	dS z|d u rt }| jd|||f W dS  |jy)   Y dS w )Nr   Fz?INSERT OR REPLACE INTO files(path, mtime, data) VALUES(?, ?, ?)T)r^   r_   timere   r]   )r   r   r   r   r^   r   r   r   r      s   zSqliteMetadataStore.writec                 C  s    | j st | j d|f d S )Nz DELETE FROM files WHERE path = ?)r_   r:   re   r   r   r   r   r      s   zSqliteMetadataStore.removec                 C  s   | j r
| j   d S d S r   )r_   r   r   r   r   r   r      s   zSqliteMetadataStore.commitr   c                 c  s,    | j r| j dD ]	}|d V  q
d S d S )NzSELECT path FROM filesr   )r_   re   )r   rowr   r   r   r       s   zSqliteMetadataStore.list_allrT   )r   r	   rc   r	   r
   r   r"   r#   r   r$   r%   r&   r'   )r(   r)   r*   r9   rj   r   r   r   r   r   r    r   r   r   r   rb      s    





rb   )r
   r	   )rV   r	   r
   rW   )r+   
__future__r   r.   r0   rn   abcr   typingr   r   r   r^   r   r3   r4   r\   rU   __annotations__ra   rb   r   r   r   r   <module>   s     

,@	
