
    X-Ph*                        d Z ddlmZ ddlZddlZddlmZ ddlmZ  ed           G d d	                      Z	ddZ
dS )a  Interface for accessing the file system with automatic caching.

The idea is to cache the results of any file system state reads during
a single transaction. This has two main benefits:

* This avoids redundant syscalls, as we won't perform the same OS
  operations multiple times.

* This makes it easier to reason about concurrent FS updates, as different
  operations targeting the same paths can't report different state during
  a transaction.

Note that this only deals with reading state, not writing.

Properties maintained by the API:

* The contents of the file are always from the same or later time compared
  to the reported mtime of the file, even if mtime is queried after reading
  a file.

* Repeating an operation produces the same result as the first one during
  a transaction.

* Call flush() to start a new transaction (flush the caches).

The API is a bit limited. It's easy to add new cached operations, however.
You should perform all file system reads through the API to actually take
advantage of the benefits.
    )annotationsN)
mypyc_attr)hash_digestT)allow_interpreted_subclassesc                      e Zd ZddZddZddZd dZd!dZd"dZd#dZ	d!dZ
d$dZd$dZd!dZd!dZd%dZd&dZd'dZdS )(FileSystemCachereturnNonec                <    g | _         |                                  d S N)package_rootflushselfs    L/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/mypy/fscache.py__init__zFileSystemCache.__init__+   s     (*

    r   	list[str]c                    || _         d S r   )r   )r   r   s     r   set_package_rootz FileSystemCache.set_package_root1   s    (r   c                    i | _         i | _        i | _        i | _        i | _        i | _        i | _        i | _        t                      | _	        dS )z/Start another transaction and empty all caches.N)
stat_or_none_cachelistdir_cachelistdir_error_cacheisfile_case_cacheexists_case_cache
read_cacheread_error_cache
hash_cachesetfake_package_cacher   s    r   r   zFileSystemCache.flush4   sR    DF3579 2424,.68*,,/EEr   pathstros.stat_result | Nonec                   || j         v r| j         |         S d }	 t          j        |          }nL# t          $ r? |                     |          r'	 |                     |          }n# t          $ r Y nw xY wY nw xY w|| j         |<   |S r   )r   osstatOSErrorinit_under_package_root
_fake_initr   r"   sts      r   stat_or_nonezFileSystemCache.stat_or_noneA   s    4****400	BB 	 	 	++D11 ..BB   D		 )+%	s3   / A8A%$A8%
A2/A81A22A87A8boolc                   | j         sdS t          j                            |          \  }}|dk    rdS t          j                            |                                          sdS |                     |          }|dS t          j        |j	                  sdS d}t          j        
                    |          \  }}t          j                            |          rt          j                            |          }t          j                            |          }| j         D ]&}|                    |          r|||z   k    rd} nd}'|S )a  Is this path an __init__.py under a package root?

        This is used to detect packages that don't contain __init__.py
        files, which is needed to support Bazel.  The function should
        only be called for non-existing files.

        It will return True if it refers to a __init__.py file that
        Bazel would create, so that at runtime Python would think the
        directory containing it is a package.  For this to work you
        must pass one or more package roots using the --package-root
        flag.

        As an exceptional case, any directory that is a package root
        itself will not be considered to contain a __init__.py file.
        This is different from the rules Bazel itself applies, but is
        necessary for mypy to properly distinguish packages from other
        directories.

        See https://docs.bazel.build/versions/master/be/python.html,
        where this behavior is described under legacy_create_init.
        F__init__.pyNT)r   r&   r"   splitbasenameisidentifierr-   r'   S_ISDIRst_mode
splitdriveisabsrelpathnormpath
startswith)r   r"   dirnamer2   r,   okdriveroots           r   r)   z'FileSystemCache.init_under_package_rootR   sA   ,   	5GMM$//}$$5w((5577 	5w'':5<
++ ug((..t7== 	)7??4((Dw%%% 	 	Dt$$ 4(?**BEB	r   os.stat_resultc                .   t           j                            |          \  }}|dk    s
J |            t           j                            |          r
J |            t           j                            |          }t          j        |          }t          |          }t
          j        dz  |t
          j        <   d|t
          j	        <   d|t
          j
        <   d|t
          j        <   t          j        |          }| j                            |           |S )zPrime the cache with a fake __init__.py file.

        This makes code that looks for path believe an empty file by
        that name exists.  Should only be called after
        init_under_package_root() returns True.
        r0   i$     r   )r&   r"   r1   existsr9   r'   listS_IFREGST_MODEST_INOST_NLINKST_SIZEstat_resultr!   add)r   r"   r;   r2   r,   seqs         r   r*   zFileSystemCache._fake_init   s     GMM$//=((($(((7>>$''-----'""7++WW88 L50DLDKDMDL^C  ##G,,,	r   c                   t           j                            |          }|| j        v r1| j        |         }|| j        v rd|vr|                    d           |S || j        v rt          | j        |                   	 t          j        |          }n+# t          $ r}t          |          | j        |<   |d }~ww xY w|| j        |<   || j        v rd|vr|                    d           |S )Nr0   )
r&   r"   r9   r   r!   appendr   copy_os_errorlistdirr(   )r   r"   resresultserrs        r   rO   zFileSystemCache.listdir   s   w%%4%%%$T*Ct...=3K3K

=)))J4+++ 8 >???	j&&GG 	 	 	-:3-?-?D$T*I	 $+4 4***}G/K/KNN=)))s   >B 
B;B66B;c                f    |                      |          }|dS t          j        |j                  S NF)r-   r'   S_ISREGr5   r+   s      r   isfilezFileSystemCache.isfile   1    t$$:5|BJ'''r   prefixc                ^   |                      |          sdS || j        v r| j        |         S t          j                            |          \  }}|sd| j        |<   dS 	 |                     |          }||v }n# t          $ r d}Y nw xY w|r|                     ||          }|| j        |<   |S )ax  Return whether path exists and is a file.

        On case-insensitive filesystems (like Mac or Windows) this returns
        False if the case of path's last component does not exactly match
        the case found in the filesystem.

        We check also the case of other path components up to prefix.
        For example, if path is 'user-stubs/pack/mod.pyi' and prefix is 'user-stubs',
        we check that the case of 'pack' and 'mod.py' matches exactly, 'user-stubs' will be
        case insensitive on case insensitive filesystems.

        The caller must ensure that prefix is a valid file system prefix of path.
        F)rV   r   r&   r"   r1   rO   r(   exists_caser   r"   rX   headtailnamesrP   s          r   isfile_casezFileSystemCache.isfile_case   s     {{4   	54))))$//W]]4((
d 	+0D"4(5	LL&&E %-CC 	 	 	CCC	 	1""400C'*t$
s   A9 9BBc                Z   || j         v r| j         |         S t          j                            |          \  }}|                    |          r|sd| j         |<   dS 	 |                     |          }||v }n# t          $ r d}Y nw xY w|r|                     ||          }|| j         |<   |S )zoReturn whether path exists - checking path components in case sensitive
        fashion, up to prefix.
        TF)r   r&   r"   r1   r:   rO   r(   rZ   r[   s          r   rZ   zFileSystemCache.exists_case   s     4))))$//W]]4((
dv&& 	d 	+/D"4(4	LL&&E %-CC 	 	 	CCC	 	1""400C'*t$
s   A7 7BBc                f    |                      |          }|dS t          j        |j                  S rT   )r-   r'   r4   r5   r+   s      r   isdirzFileSystemCache.isdir   rW   r   c                4    |                      |          }|d uS r   )r-   r+   s      r   rB   zFileSystemCache.exists   s    t$$~r   bytesc                &   || j         v r| j         |         S || j        v r| j        |         |                     |           t          j                            |          \  }}t          j                            |          }|dk    r|| j        v rd}n[	 t          |d          5 }|	                                }d d d            n# 1 swxY w Y   n# t          $ r}|| j        |<    d }~ww xY w|| j         |<   t          |          | j        |<   |S )Nr0   r   rb)r   r   r-   r&   r"   r1   r9   r!   openreadr(   r   r   )r   r"   r;   r2   datafrR   s          r   rh   zFileSystemCache.read   se   4?""?4((4((('-- 	$GMM$//'""7++}$$D4K)K)KDD$%% $6688D$ $ $ $ $ $ $ $ $ $ $ $ $ $ $   .1%d+ !% +D 1 1s<   C &C;C CC CC 
C-C((C-c                X    || j         vr|                     |           | j         |         S r   )r   rh   )r   r"   s     r   r   zFileSystemCache.hash_digest  s+    t&&IIdOOOt$$r   f1f2c                    |                      |          }|                      |          }||dS t          j                            ||          S rT   )r-   r&   r"   samestat)r   rl   rm   s1s2s        r   samefilezFileSystemCache.samefile  sN    r""r"":5wB'''r   N)r	   r
   )r   r   r	   r
   )r"   r#   r	   r$   )r"   r#   r	   r.   )r"   r#   r	   r?   )r"   r#   r	   r   )r"   r#   rX   r#   r	   r.   )r"   r#   r	   rd   )r"   r#   r	   r#   )rl   r#   rm   r#   r	   r.   )__name__
__module____qualname__r   r   r   r-   r)   r*   rO   rV   r_   rZ   rb   rB   rh   r   rr    r   r   r   r   )   sB          ) ) ) )2 2 2 2   "2 2 2 2h   .   ,( ( ( (" " " "H   0( ( ( (      6% % % %
( ( ( ( ( (r   r   er(   r	   c                    t          | j         }| j        |_        | j        |_        | j        |_        | j        r| j        |_        |S r   )r(   argserrnostrerrorfilename	filename2)rw   news     r   rN   rN   '  sA    
16
CCI:CL:CL{ $Jr   )rw   r(   r	   r(   )__doc__
__future__r   r&   r'   mypy_extensionsr   	mypy.utilr   r   rN   rv   r   r   <module>r      s    < # " " " " " 				  & & & & & & ! ! ! ! ! ! ...z( z( z( z( z( z( z( /.z(z     r   