
    Ri&S                       d Z ddlmZ ddlZddlZddlmZmZmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZmZ ddlZd	d
lmZ ddlmZ ddlmZ erddlmZ ej        j        Zd5dZ G d d          Z G d d          Z  G d de           Z! G d de!          Z" G d de           Z# G d de"          Z$ G d d e#          Z%d6d%Z& G d& d'          Z'd7d)Z(d7d*Z)d8d/Z*d9d1Z+d:d4Z,dS );u_  Automatic discovery of Python modules and packages (for inclusion in the
distribution) and other config values.

For the purposes of this module, the following nomenclature is used:

- "src-layout": a directory representing a Python project that contains a "src"
  folder. Everything under the "src" folder is meant to be included in the
  distribution when packaging the project. Example::

    .
    ├── tox.ini
    ├── pyproject.toml
    └── src/
        └── mypkg/
            ├── __init__.py
            ├── mymodule.py
            └── my_data_file.txt

- "flat-layout": a Python project that does not use "src-layout" but instead
  have a directory under the project root for each package::

    .
    ├── tox.ini
    ├── pyproject.toml
    └── mypkg/
        ├── __init__.py
        ├── mymodule.py
        └── my_data_file.txt

- "single-module": a project that contains a single Python script direct under
  the project root (no directory used)::

    .
    ├── tox.ini
    ├── pyproject.toml
    └── mymodule.py

    )annotationsN)IterableIteratorMappingfnmatchcase)glob)Path)TYPE_CHECKINGClassVar   )StrPath)log)convert_path)Distributionpathr   returnboolc                d    t           j                            |                                           S N)osr   basenameisidentifier)r   s    T/Users/shanyulin/my-agent/.venv/lib/python3.11/site-packages/setuptools/discovery.py_valid_namer   ?   s$    7D!!..000    c                  *    e Zd ZdZddZdd	Zdd
ZdS )_Filterz
    Given a list of patterns, create a callable that will be true only if
    the input matches at least one of the patterns.
    patternsstrr   Nonec                D    t                               |          | _        d S r   )dictfromkeys	_patterns)selfr   s     r   __init__z_Filter.__init__J   s    x00r   itemr   c                D    t          fd| j        D                       S )Nc              3  8   K   | ]}t          |          V  d S r   r   ).0patr(   s     r   	<genexpr>z#_Filter.__call__.<locals>.<genexpr>N   s-      DDc;tS))DDDDDDr   )anyr%   r&   r(   s    `r   __call__z_Filter.__call__M   s(    DDDDT^DDDDDDr   c                    || j         v S r   )r%   r/   s     r   __contains__z_Filter.__contains__P   s    t~%%r   N)r   r    r   r!   )r(   r    r   r   )__name__
__module____qualname____doc__r'   r0   r2    r   r   r   r   D   s^         
1 1 1 1E E E E& & & & & &r   r   c                  h    e Zd ZU dZdZded<   dZded<   e	 	 	 ddd            Zedd            Z	dS )_Finderz@Base class that exposes functionality for module/package findersr7   zClassVar[tuple[str, ...]]ALWAYS_EXCLUDEDEFAULT_EXCLUDE.*wherer   excludeIterable[str]includer   	list[str]c           	         |p| j         }t          |                     t          t	          |                    t          g | j        |R  t          |                     S )aZ  Return a list of all Python items (packages or modules, depending on
        the finder implementation) found within directory ``where``.

        ``where`` is the root directory which will be searched.
        It should be supplied as a "cross-platform" (i.e. URL-style) path;
        it will be converted to the appropriate local path syntax.

        ``exclude`` is a sequence of names to exclude; ``*`` can be used
        as a wildcard in the names.
        When finding packages, ``foo.*`` will exclude all subpackages of ``foo``
        (but not ``foo`` itself).

        ``include`` is a sequence of names to include.
        If it's specified, only the named items will be included.
        If it's not specified, all found items will be included.
        ``include`` can contain shell style wildcard patterns just like
        ``exclude``.
        )r;   list
_find_iterr   r    r   r:   clsr?   r@   rB   s       r   findz_Finder.findZ   sf    4 0S0NNSZZ((6+6g666! 
 
 	
r   r   Iterator[str]c                    t           r   )NotImplementedErrorrG   s       r   rF   z_Finder._find_iter}   s
     "!r   N)r<   r7   r=   )r?   r   r@   rA   rB   rA   r   rC   r?   r   r@   r   rB   r   r   rJ   )
r3   r4   r5   r6   r:   __annotations__r;   classmethodrI   rF   r7   r   r   r9   r9   T   s         JJ02N222213O3333 !#!'	 
  
  
  
 [ 
D " " " [" " "r   r9   c                  F    e Zd ZdZdZedd
            Zedd            ZdS )PackageFinderzI
    Generate a list of all Python packages found within a directory
    )ez_setupz*__pycache__r?   r   r@   r   rB   r   rJ   c              #    K   t          j        t          |          d          D ]\  }}}|dd         }g |dd<   |D ]}t           j                            ||          }	t           j                            |	|          }
|
                    t           j        j        d          }d|v s|                     |	|          s ||          r ||          s|V  | d|v s| d|v r|	                    |           dS )zy
        All the packages found in 'where' that pass the 'include' filter, but
        not the 'exclude' filter.
        T)followlinksNr<   r>   .*)
r   walkr    r   joinrelpathreplacesep_looks_like_packageappend)rH   r?   r@   rB   rootdirsfilesall_dirsdir	full_pathrel_pathpackages               r   rF   zPackageFinder._find_iter   s;      "$U!F!F!F 	! 	!D$AAAwHDG ! !GLLs33	7??9e<<"**27;<< #::S%<%<Y%P%P: 77## "GGG,<,< "!MMM ===G++'~~~/H/H C    '!	! 	!r   r   _package_namer    r   c                |    t           j                            t           j                            | d                    S )z%Does a directory look like a package?__init__.py)r   r   isfilerW   )r   re   s     r   r[   z!PackageFinder._looks_like_package   s(     w~~bgll4??@@@r   NrM   )r   r   re   r    r   r   )	r3   r4   r5   r6   r:   rO   rF   staticmethodr[   r7   r   r   rQ   rQ      sj          2N! ! ! [!B A A A \A A Ar   rQ   c                  &    e Zd Zed	d            ZdS )
PEP420PackageFinder_pathr   re   r    r   r   c                    dS )NTr7   )rl   re   s     r   r[   z'PEP420PackageFinder._looks_like_package   s    tr   N)rl   r   re   r    r   r   )r3   r4   r5   ri   r[   r7   r   r   rk   rk      s2           \  r   rk   c                  @    e Zd ZdZedd	            Z ee          Zd
S )ModuleFinderzYFind isolated Python modules.
    This function will **not** recurse subdirectories.
    r?   r   r@   r   rB   r   rJ   c              #  H  K   t          t          j                            |d                    D ]q}t          j                            t          j                            |                    \  }}|                     |          sW ||          r ||          s|V  rd S )Nz*.py)r	   r   r   rW   splitextr   _looks_like_module)rH   r?   r@   rB   filemodule_exts          r   rF   zModuleFinder._find_iter   s       eV4455 	 	D7++BG,<,<T,B,BCCLFD))&11 wv wwv 	 	r   NrM   )	r3   r4   r5   r6   rO   rF   ri   r   rr   r7   r   r   ro   ro      sL          
 
 
 [
 &k22r   ro   c                  h    e Zd ZdZ e ed eD                                 Z	 edd	            Zd
S )FlatLayoutPackageFinder)#cibindebiandocdocsdocumentationmanpagesnewsnewsfragments	changelogtesttests	unit_test
unit_testsexampleexamplesscriptstoolsutilutilspythonbuilddistvenvenvrequirementstasksfabfile
site_scons	benchmark
benchmarksexercise	exerciseshtmlcov[._]*c              #  $   K   | ]}|| d fV  dS )rU   Nr7   )r+   ps     r   r-   z!FlatLayoutPackageFinder.<genexpr>   s,      &G&Ga888}&G&G&G&G&G&Gr   rl   r   package_namer    r   r   c                    |                     d          }|d                                         p|d                             d          }|o t          d |dd          D                       S )Nr<   r   -stubsc              3  >   K   | ]}|                                 V  d S r   )r   )r+   names     r   r-   z>FlatLayoutPackageFinder._looks_like_package.<locals>.<genexpr>  s.      (S(S):):)<)<(S(S(S(S(S(Sr   r   )splitr   endswithall)rl   r   namesroot_pkg_is_valids       r   r[   z+FlatLayoutPackageFinder._looks_like_package   sl    ""3''!!H1133RuQx7H7H7R7R SS(S(Sqrr(S(S(S%S%SSr   N)rl   r   r   r    r   r   )	r3   r4   r5   _EXCLUDEtuple
chain_iterr;   ri   r[   r7   r   r   rw   rw      sm        'HR eJJ&G&Gh&G&G&GGGHHO T T T \T T Tr   rw   c                      e Zd ZdZdS )FlatLayoutModuleFinder)setupconftestr   r   r   r   r   toxfilenoxfilepavementdodor   r   z[Ss][Cc]onstruct	conanfilemanager   r   r   r   r   N)r3   r4   r5   r;   r7   r   r   r   r     s        O4 *)r   r   root_pkgr    pkg_dirrC   c                \     t                               |          } g fd|D             z   S )Nc                >    g | ]}d                      |f          S )r<   )rW   )r+   nr   s     r   
<listcomp>z)_find_packages_within.<locals>.<listcomp>'  s)    AAAQ8Q-00AAAr   )rk   rI   )r   r   nesteds   `  r   _find_packages_withinr   %  s8     %%g..F:AAAA&AAAAAr   c                      e Zd ZdZd%dZd Zd Zed&d
            Zed'd            Z		 d(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 )/ConfigDiscoveryzFill-in metadata and options that can be automatically derived
    (from other metadata/options, the file system or conventions)
    distributionr   r   r!   c                >    || _         d| _        d| _        d| _        d S )NF)r   _called	_disabled_skip_ext_modules)r&   r   s     r   r'   zConfigDiscovery.__init__/  s%     	!&r   c                    d| _         dS )z+Internal API to disable automatic discoveryTN)r   r&   s    r   _disablezConfigDiscovery._disable5  s    r   c                    d| _         dS )a  Internal API to disregard ext_modules.

        Normally auto-discovery would not be triggered if ``ext_modules`` are set
        (this is done for backward compatibility with existing packages relying on
        ``setup.py`` or ``setup.cfg``). However, ``setuptools`` can call this function
        to ignore given ``ext_modules`` and proceed with the auto-discovery if
        ``packages`` and ``py_modules`` are not given (e.g. when using pyproject.toml
        metadata).
        TN)r   r   s    r   _ignore_ext_modulesz#ConfigDiscovery._ignore_ext_modules9  s     "&r   r   c                2    | j         j        pt          j        S r   )r   src_rootr   curdirr   s    r   	_root_dirzConfigDiscovery._root_dirE  s     y!.RY.r   dict[str, str]c                6    | j         j        i S | j         j        S r   )r   package_dirr   s    r   _package_dirzConfigDiscovery._package_dirJ  s    9 (Iy$$r   FTforcer   r   ignore_ext_modulesc                    |du r| j         s| j        rdS |                     |           |r|                                  d| _         dS )a  Automatically discover missing configuration fields
        and modifies the given ``distribution`` object in-place.

        Note that by default this will only have an effect the first time the
        ``ConfigDiscovery`` object is called.

        To repeatedly invoke automatic discovery (e.g. when the project
        directory changes), please use ``force=True`` (or create a new
        ``ConfigDiscovery`` instance).
        FNT)r   r   _analyse_package_layoutanalyse_name)r&   r   r   r   s       r   r0   zConfigDiscovery.__call__P  sX     E>>t|>t~>F$$%7888 	 r   c                    |p| j         }| j        j        du p| }| j        j        dup0| j        j        dup"|p t          | j        d          o| j        j        S )zF``True`` if the user has specified some form of package/module listingNconfiguration)r   r   ext_modulespackages
py_moduleshasattrr   )r&   r   r   s      r   _explicitly_specifiedz%ConfigDiscovery._explicitly_specifiedg  sw    /I43I90D8N<NOId* (y#4/(( ty/22 (	'	
r   c                    |                      |          rdS t          j        d           |                                 p'|                                 p|                                 S )NTzLNo `packages` or `py_modules` configuration, performing automatic discovery.)r   r   debug_analyse_explicit_layout_analyse_src_layout_analyse_flat_layout)r&   r   s     r   r   z'ConfigDiscovery._analyse_package_layoutt  sy    %%&899 	 4	#	
 	
 	
 ))++ +''))+ ((**		
r   c                x   | j                                         }|                    dd           | j        |sdS t	          j        d|            t          fd|                                D                       }t          |          | j	        _
        t	          j        d| j	        j
                    dS )zAThe user can explicitly give a package layout via ``package_dir`` NFz(`explicit-layout` detected -- analysing c              3  z   K   | ]5\  }}t          |t          j                            |                    V  6d S r   )r   r   r   rW   )r+   pkg
parent_dirroot_dirs      r   r-   z;ConfigDiscovery._analyse_explicit_layout.<locals>.<genexpr>  sU       
 
Z "#rw||Hj'I'IJJ
 
 
 
 
 
r   discovered packages -- T)r   copypopr   r   r   r   itemsrE   r   r   )r&   r   pkgsr   s      @r   r   z(ConfigDiscovery._analyse_explicit_layout  s    ',,..D!!!> 	5	J[JJKKK 
 
 
 
#.#4#4#6#6
 
 
 
 
 "$ZZ		@DI,>@@AAAtr   c                   | j         }t          j                            | j        |                    dd                    }t          j                            |          sdS t          j        d|            |	                    dt          j        
                    |                     || j        _        t                              |          | j        _        t                               |          | j        _        t          j        d| j        j                    t          j        d| j        j                    dS )a  Try to find all packages or modules under the ``src`` directory
        (or anything pointed by ``package_dir[""]``).

        The "src-layout" is relatively safe for automatic discovery.
        We assume that everything within is meant to be included in the
        distribution.

        If ``package_dir[""]`` is not given, but the ``src`` directory exists,
        this function will set ``package_dir[""] = "src"``.
        r   srcFz#`src-layout` detected -- analysing r   discovered py_modules -- T)r   r   r   rW   r   getisdirr   r   
setdefaultr   r   r   rk   rI   r   ro   r   )r&   r   src_dirs      r   r   z#ConfigDiscovery._analyse_src_layout  s    '',,t~{r5/I/IJJw}}W%% 	5	AAABBBr27#3#3G#<#<=== +	055g>>	+0099		@DI,>@@AAA	Ddi.BDDEEEtr   c                    t          j        d| j                    |                                 p|                                 S )a  Try to find all packages and modules under the project root.

        Since the ``flat-layout`` is more dangerous in terms of accidentally including
        extra files/directories, this function is more conservative and will raise an
        error if multiple packages or modules are found.

        This assumes that multi-package dists are uncommon and refuse to support that
        use case in order to be able to prevent unintended errors.
        z$`flat-layout` detected -- analysing )r   r   r   _analyse_flat_packages_analyse_flat_modulesr   s    r   r   z$ConfigDiscovery._analyse_flat_layout  sB     		IIIJJJ**,,L0J0J0L0LLr   c                ,   t                               | j                  | j        _        t          t          | j        j                            }t          j        d| j        j                    | 	                    |d           t          |          S )Nr   r   )rw   rI   r   r   r   remove_nested_packagesremove_stubsr   r   _ensure_no_accidental_inclusionr   )r&   	top_levels     r   r   z&ConfigDiscovery._analyse_flat_packages  sv    499$.II	*<	8J+K+KLL		@DI,>@@AAA,,Y
CCCIr   c                   t                               | j                  | j        _        t          j        d| j        j                    |                     | j        j        d           t          | j        j                  S )Nr   modules)	r   rI   r   r   r   r   r   r   r   r   s    r   r   z%ConfigDiscovery._analyse_flat_modules  sh    5::4>JJ		Ddi.BDDEEE,,TY-A9MMMDI()))r   detectedrC   kindr    c                    t          |          dk    r,ddlm} ddlm} d| d| d| d} | ||                    d S )	Nr   r   )cleandoc)PackageDiscoveryErrorzMultiple top-level z discovered in a flat-layout: z.

            To avoid accidental inclusion of unwanted files or directories,
            setuptools will not proceed with this build.

            If you are trying to create a single distribution with multiple a  
            on purpose, you should not rely on automatic discovery.
            Instead, consider the following options:

            1. set up custom discovery (`find` directive with `include` or `exclude`)
            2. use a `src-layout`
            3. explicitly set `py_modules` or `packages` with a list of names

            To find more information, look for "package discovery" on setuptools docs.
            )leninspectr   setuptools.errorsr  )r&   r   r   r   r  msgs         r   r   z/ConfigDiscovery._ensure_no_accidental_inclusion  s    x==1((((((??????$  h  
 NR  C ('666) r   c                    | j         j        j        s| j         j        rdS t          j        d           |                                 p|                                 }|r|| j         j        _        dS dS )zThe packages/modules are the essential contribution of the author.
        Therefore the name of the distribution can be derived from them.
        Nz7No `name` configuration, performing automatic discovery)r   metadatar   r   r   #_find_name_single_package_or_module_find_name_from_packages)r&   r   s     r   r   zConfigDiscovery.analyse_name  s     9" 	din 	F	KLLL 4466 /,,.. 	  	+&*DI###	+ 	+r   
str | Nonec                    dD ]V}t          | j        |d          pg }|r:t          |          dk    r't          j        d|d                     |d         c S WdS )zExactly one module or package)r   r   Nr   z&Single module/package detected, name: r   )getattrr   r  r   r   )r&   fieldr   s      r   r  z3ConfigDiscovery._find_name_single_package_or_module  so    / 	  	 EDIud339rE  Uq	M58MMNNNQxtr   c                "   | j         j        sdS t          t          | j         j        t                              }| j         j        pi }t          ||| j                  }|rt          j	        d|            |S t          j
        d           dS )z<Try to find the root package that is not a PEP 420 namespaceNkeyz&Common parent package detected, name: z7No parent package detected, impossible to derive `name`)r   r   r   sortedr  r   find_parent_packager   r   r   warn)r&   r   r   
parent_pkgs       r   r	  z(ConfigDiscovery._find_name_from_packages  s    y! 	4ty'9s C C CDDi+1r(;OO
 	IKzKKLLLJKKKtr   N)r   r   r   r!   )r   r   )r   r   )FTF)r   r   r   r   r   r   r   r!   )r   r   r   r   )r   r   )r   rC   r   r    )r   r!   )r   r
  )r3   r4   r5   r6   r'   r   r   propertyr   r   r0   r   r   r   r   r   r   r   r   r   r  r	  r7   r   r   r   r   *  s        ' ' ' '  
& 
& 
& / / / X/ % % % X% RW    .
 
 
 

 
 
 
$   $   2M M M M   * * * *7 7 7 7.+ + + +"        r   r   r   c                   t          | t                    }|dd         }t          |          }t          t          |                    D ];\  }t	          fd|D                       r|                    ||z
  dz
             <|S )zRemove nested packages from a list of packages.

    >>> remove_nested_packages(["a", "a.b1", "a.b2", "a.b1.c1"])
    ['a']
    >>> remove_nested_packages(["a", "b", "c.d", "c.d.e.f", "g.h", "a.a1"])
    ['a', 'b', 'c.d', 'g.h']
    r  Nc              3  H   K   | ]}                     | d           V  dS r<   N
startswith)r+   otherr   s     r   r-   z)remove_nested_packages.<locals>.<genexpr>  s5      CCt%{{{++CCCCCCr   r   )r  r  	enumeratereversedr.   r   )r   r   r   sizeir   s        @r   r   r     s     ($$$DQQQIt99DXd^^,, ( (4CCCCCCCCC 	(MM$(Q,'''r   c                    d | D             S )zRemove type stubs (:pep:`561`) from a list of packages.

    >>> remove_stubs(["a", "a.b", "a-stubs", "a-stubs.b.c", "b", "c-stubs"])
    ['a', 'a.b', 'b']
    c                n    g | ]2}|                     d           d                             d          0|3S )r<   r   r   )r   r   )r+   r   s     r   r   z remove_stubs.<locals>.<listcomp>&  s:    PPPC399S>>!+<+E+Eh+O+OPCPPPr   r7   )r   s    r   r   r      s     QP8PPPPr   r   Mapping[str, str]r   r
  c                   t          | t                    } g }t          |           D ]B\  }t          fd| |dz   d         D                       s n|                               C|D ]Vt          ||          }t          j                            |d          }t          j        	                    |          rc S WdS )z0Find the parent package that is not a namespace.r  c              3  H   K   | ]}|                      d           V  dS r  r  )r+   r   r   s     r   r-   z&find_parent_package.<locals>.<genexpr>0  s5      GG1<<4


++GGGGGGr   r   Nrg   )
r  r  r  r   r\   find_package_pathr   r   rW   rh   )r   r   r   common_ancestorsr  pkg_pathinitr   s          @r   r  r  )  s     hC(((HX&& & &4GGGGXa!egg5FGGGGG 	
 E%%%%   $T;AAw||Hm447>>$ 	KKK	 4r   r   c                   |                      d          }t          t          |          dd          D ]M}d                    |d|                   }||v r*||         }t	          j        j        ||g||d         R  c S N|                    d          pd}t	          j        j        |g|                     d          |R  S )a  Given a package name, return the path where it should be found on
    disk, considering the ``package_dir`` option.

    >>> path = find_package_path("my.pkg", {"": "root/is/nested"}, ".")
    >>> path.replace(os.sep, "/")
    './root/is/nested/my/pkg'

    >>> path = find_package_path("my.pkg", {"my": "root/is/nested"}, ".")
    >>> path.replace(os.sep, "/")
    './root/is/nested/pkg'

    >>> path = find_package_path("my.pkg", {"my.pkg": "root/is/nested"}, ".")
    >>> path.replace(os.sep, "/")
    './root/is/nested'

    >>> path = find_package_path("other.pkg", {"my.pkg": "root/is/nested"}, ".")
    >>> path.replace(os.sep, "/")
    './other/pkg'
    r<   r   Nr   /)r   ranger  rW   r   r   r   )r   r   r   partsr  partial_nameparents          r   r%  r%  A  s    , JJsOOE3u::q"%% > >xxbqb	**;&& .F7<&=59====== ' __R  &BF7<=6<<#4#4=u====r   package_pathr   c                f    t          |           }t          |          j        fd|D             S )Nc           	     j    i | ]/}|d                      g |                    d                    0S )r+  r<   )rW   r   )r+   r   prefixs     r   
<dictcomp>z)construct_package_dir.<locals>.<dictcomp>f  s<    MMM#C4F4SYYs^^455MMMr   )r   r
   r-  )r   r0  parent_pkgsr3  s      @r   construct_package_dirr6  c  s:    (22K,%FMMMMMMMMr   )r   r   r   r   )r   r    r   r   r   rC   )r   rC   r   rC   )r   rC   r   r"  r   r   r   r
  )r   r    r   r"  r   r   r   r    )r   rC   r0  r   r   r   )-r6   
__future__r   	itertoolsr   collections.abcr   r   r   fnmatchr   r	   pathlibr
   typingr   r   _distutils_hack.override_distutils_hackrl   r   	distutilsr   distutils.utilr   
setuptoolsr   chainfrom_iterabler   r   r   r9   rQ   rk   ro   rw   r   r   r   r   r   r  r%  r6  r7   r   r   <module>rD     s  % %N # " " " " "     				 7 7 7 7 7 7 7 7 7 7                   * * * * * * * *                 ' ' ' ' ' ' (''''''_*
1 1 1 1
& & & & & & & & -" -" -" -" -" -" -" -"`,A ,A ,A ,A ,AG ,A ,A ,A^    -   3 3 3 3 37 3 3 322T 2T 2T 2T 2T1 2T 2T 2Tj* * * * *\ * * *<B B B B
a a a a a a a aH   $Q Q Q Q   0> > > >DN N N N N Nr   