
    ~pi&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j6                  j8                  Zd%d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%d&dZ& G d d      Z'd'd Z(d'd!Z)	 	 	 	 	 	 	 	 d(d"Z*	 	 	 	 	 	 	 	 d)d#Z+d*d$Z,y)+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)Distributionc                \    t         j                  j                  |       j                         S N)ospathbasenameisidentifier)r   s    V/opt/services/ai/voice_agent/venv/lib/python3.12/site-packages/setuptools/discovery.py_valid_namer   ?   s     77D!..00    c                  (    e Zd ZdZddZddZddZy)_Filterz
    Given a list of patterns, create a callable that will be true only if
    the input matches at least one of the patterns.
    c                8    t         j                  |      | _        y r   )dictfromkeys	_patterns)selfpatternss     r   __init__z_Filter.__init__J   s    x0r   c                @    t        fd| j                  D              S )Nc              3  6   K   | ]  }t        |        y wr   r   ).0patitems     r   	<genexpr>z#_Filter.__call__.<locals>.<genexpr>N   s     Dc;tS)Ds   )anyr    r!   r(   s    `r   __call__z_Filter.__call__M   s    DT^^DDDr   c                    || j                   v S r   )r    r+   s     r   __contains__z_Filter.__contains__P   s    t~~%%r   N)r"   strreturnNone)r(   r/   r0   bool)__name__
__module____qualname____doc__r#   r,   r.    r   r   r   r   D   s    
1E&r   r   c                  x    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	y)_Finderz@Base class that exposes functionality for module/package findersr7   zClassVar[tuple[str, ...]]ALWAYS_EXCLUDEDEFAULT_EXCLUDEc           	         |xs | j                   }t        | j                  t        t	        |            t        g | j                  | 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:   clswhereexcludeincludes       r   findz_Finder.findZ   sW    4 0S00NNSZ(6++6g6!
 	
r   c                    t         r   )NotImplementedErrorr?   s       r   r>   z_Finder._find_iter}   s
     "!r   N).r7   )*)rA   r   rB   Iterable[str]rC   rI   r0   	list[str]rA   r   rB   r   rC   r   r0   zIterator[str])
r3   r4   r5   r6   r:   __annotations__r;   classmethodrD   r>   r7   r   r   r9   r9   T   s    J02N-213O.3 !#!'	 
 
  
 	 

 
 
  
D ""&-"8?"	" "r   r9   c                  H    e Zd ZdZdZe	 	 	 	 	 	 	 	 dd       Zedd       Zy)PackageFinderzI
    Generate a list of all Python packages found within a directory
    )ez_setupz*__pycache__c              #    K   t        j                  t        |      d      D ]  \  }}}|dd }g |dd |D ]  }t         j                  j	                  ||      }	t         j                  j                  |	|      }
|
j                  t         j                  j                  d      }d|v s| j                  |	|      s ||      r ||      s| | d|v s| d|v r|j                  |         yw)zy
        All the packages found in 'where' that pass the 'include' filter, but
        not the 'exclude' filter.
        T)followlinksNrG   rH   .*)
r   walkr/   r   joinrelpathreplacesep_looks_like_packageappend)r@   rA   rB   rC   rootdirsfilesall_dirsdir	full_pathrel_pathpackages               r   r>   zPackageFinder._find_iter   s      "$U!F 	!D$AwHDG !GGLLs3	77??9e<"**277;;< #:S%<%<Y%P 7#GG,<!M Ya=G+'"~/H C '!	!s   C1C3c                |    t         j                  j                  t         j                  j                  | d            S )z%Does a directory look like a package?__init__.py)r   r   isfilerU   )r   _package_names     r   rY   z!PackageFinder._looks_like_package   s&     ww~~bggll4?@@r   NrK   )r   r   rf   r/   r0   r2   )	r3   r4   r5   r6   r:   rM   r>   staticmethodrY   r7   r   r   rO   rO      sW     2N!!&-!8?!	! !B A Ar   rO   c                      e Zd Zedd       Zy)PEP420PackageFinderc                     y)NTr7   )_pathrf   s     r   rY   z'PEP420PackageFinder._looks_like_package   s    r   N)rk   r   rf   r/   r0   r2   )r3   r4   r5   rg   rY   r7   r   r   ri   ri      s     r   ri   c                  B    e Zd ZdZe	 	 	 	 	 	 	 	 dd       Z ee      Zy)ModuleFinderzYFind isolated Python modules.
    This function will **not** recurse subdirectories.
    c              #  6  K   t        t        j                  j                  |d            D ]i  }t        j                  j	                  t        j                  j                  |            \  }}| j                  |      sT ||      s] ||      rf| k y w)Nz*.py)r	   r   r   rU   splitextr   _looks_like_module)r@   rA   rB   rC   filemodule_exts          r   r>   zModuleFinder._find_iter   su      eV45 	D77++BGG,<,<T,BCLFD))&1vwv	s   BB	BBNrK   )	r3   r4   r5   r6   rM   r>   rg   r   rp   r7   r   r   rm   rm      sH     

&-
8?
	
 
 &k2r   rm   c                  N    e Zd ZdZ e ed eD                    Z	 edd       Zy)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  yw)rS   Nr7   )r&   ps     r   r)   z!FlatLayoutPackageFinder.<genexpr>   s     &GaS8}&Gs   c                    |j                  d      }|d   j                         xs |d   j                  d      }|xr t        d |dd  D              S )NrG   r   -stubsc              3  <   K   | ]  }|j                           y wr   )r   )r&   names     r   r)   z>FlatLayoutPackageFinder._looks_like_package.<locals>.<genexpr>  s     (S):):)<(Ss   r   )splitr   endswithall)rk   package_namenamesroot_pkg_is_valids       r   rY   z+FlatLayoutPackageFinder._looks_like_package   sU    ""3'!!H113RuQx7H7H7R SS(Sqr(S%SSr   N)rk   r   r   r/   r0   r2   )	r3   r4   r5   _EXCLUDEtuple
chain_iterr;   rg   rY   r7   r   r   ru   ru      s8    'HR J&Gh&GGHO T Tr   ru   c                      e Zd ZdZy)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   c                    t         j                  |      }| g|D cg c]  }dj                  | |f       c}z   S c c}w )NrG   )ri   rD   rU   )root_pkgpkg_dirnestedns       r   _find_packages_withinr   %  s:     %%g.F:&AQ8Q-0AAAAs   ;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y)ConfigDiscoveryzFill-in metadata and options that can be automatically derived
    (from other metadata/options, the file system or conventions)
    c                <    || _         d| _        d| _        d| _        y )NF)r   _called	_disabled_skip_ext_modules)r!   distributions     r   r#   zConfigDiscovery.__init__/  s     	!&r   c                    d| _         y)z+Internal API to disable automatic discoveryTN)r   r!   s    r   _disablezConfigDiscovery._disable5  s	    r   c                    d| _         y)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   c                R    | j                   j                  xs t        j                  S r   )r   src_rootr   curdirr   s    r   	_root_dirzConfigDiscovery._root_dirE  s     yy!!.RYY.r   c                ^    | j                   j                  i S | j                   j                  S r   )r   package_dirr   s    r   _package_dirzConfigDiscovery._package_dirJ  s'    99  (Iyy$$$r   c                    |du r| j                   s| j                  ry| j                  |       |r| j                          d| _         y)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!   forcer   ignore_ext_moduless       r   r,   zConfigDiscovery.__call__P  s?     E>t||t~~$$%78r   c                (   |xs | j                   }| j                  j                  du xs | }| j                  j                  duxsL | j                  j                  duxs2 |xs. t        | j                  d      xr | 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  s    /I43I3I9900D8N<NOIId* (yy##4/(( tyy/2 (		''	
r   c                    | j                  |      ryt        j                  d       | j                         xs" | j	                         xs | j                         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  s^    %%&89 		#	
 ))+ +'')+ ((*		
r   c                |   | j                   j                         }|j                  dd       | j                  |syt	        j
                  d|        t        fd|j                         D              }t        |      | j                  _
        t	        j
                  d| j                  j                          y)zAThe user can explicitly give a package layout via ``package_dir`` NFz(`explicit-layout` detected -- analysing c              3  x   K   | ]1  \  }}t        |t        j                  j                  |             3 y wr   )r   r   r   rU   )r&   pkg
parent_dirroot_dirs      r   r)   z;ConfigDiscovery._analyse_explicit_layout.<locals>.<genexpr>  s3      
Z "#rww||Hj'IJ
s   7:discovered packages -- T)r   copypopr   r   r   r   itemsr=   r   r   )r!   r   pkgsr   s      @r   r   z(ConfigDiscovery._analyse_explicit_layout  s    '',,.D!>>		<[MJK 
#.#4#4#6
 
 "$Z				+DII,>,>+?@Ar   c                   | j                   }t        j                  j                  | j                  |j                  dd            }t        j                  j                  |      syt        j                  d|        |j                  dt        j                  j                  |             || j                  _        t        j                  |      | j                  _        t         j                  |      | j                  _        t        j                  d| j                  j                          t        j                  d| j                  j"                          y)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   rU   r   getisdirr   r   
setdefaultr   r   r   ri   rD   r   rm   r   )r!   r   src_dirs      r   r   z#ConfigDiscovery._analyse_src_layout  s     '''',,t~~{r5/IJww}}W%		7yABr277#3#3G#<= +		055g>		+009				+DII,>,>+?@A		-dii.B.B-CDEr   c                    t        j                  d| j                          | j                         xs | j	                         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  s:     			88HIJ**,L0J0J0LLr   c                @   t         j                  | j                        | j                  _        t        t        | j                  j                              }t        j                  d| j                  j                          | j                  |d       t        |      S )Nr   r   )ru   rD   r   r   r   remove_nested_packagesremove_stubsr   r   _ensure_no_accidental_inclusionr2   )r!   	top_levels     r   r   z&ConfigDiscovery._analyse_flat_packages  so    499$..I		*<		8J8J+KL			+DII,>,>+?@A,,Y
CIr   c                @   t         j                  | j                        | j                  _        t        j                  d| j                  j                          | j                  | j                  j                  d       t        | j                  j                        S )Nr   modules)	r   rD   r   r   r   r   r   r   r2   r   s    r   r   z%ConfigDiscovery._analyse_flat_modules  sk    5::4>>J				-dii.B.B-CDE,,TYY-A-A9MDII(())r   c                l    t        |      dkD  r&ddlm} ddlm} d| d| d| d} | ||            y )	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!   detectedkindr   r   msgs         r   r   z/ConfigDiscovery._ensure_no_accidental_inclusion  sU    x=1(?)$/MhZ XM
 NRF 	SC (66) r   c                   | j                   j                  j                  s| j                   j                  ryt        j                  d       | j                         xs | j                         }|r|| j                   j                  _        yy)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  sq     99""diinn		KL 446 /,,. 	 &*DII# r   c                    dD ]P  }t        | j                  |d      xs g }|s!t        |      dk(  s0t        j                  d|d           |d   c S  y)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  s[    / 	 EDIIud39rEUq		B58*MNQx		  r   c                R   | j                   j                  syt        t        | j                   j                  t                    }| j                   j
                  xs i }t        ||| j                        }|rt        j                  d|        |S t        j                  d       y)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    yy!!tyy'9'9s CDii++1r(;O
II>zlKLJKr   N)r   r   r0   r1   )r0   r   )r0   dict[str, str])FTF)r   r2   r   r2   r   r2   r0   r1   )r   r2   r0   r2   )r0   r2   )r   rJ   r   r/   )r0   r1   )r0   
str | None)r3   r4   r5   r6   r#   r   r   propertyr   r   r,   r   r   r   r   r   r   r   r   r   r  r  r7   r   r   r   r   *  s    '
& / / % % RW)-JN	.

$$2M*7.+"r   r   c                    t        | t              }|dd }t        |      }t        t        |            D ]1  \  }t	        fd|D              s|j                  ||z
  dz
         3 |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  F   K   | ]  }j                  | d         ywrG   N
startswith)r&   otherr   s     r   r)   z)remove_nested_packages.<locals>.<genexpr>  s      Ct%{+C   !r   )r  r   	enumeratereversedr*   r   )r   r   r   sizeir   s        @r   r   r     si     ($DQIt9DXd^, (4CCCMM$(Q,'( r   c                t    | D cg c](  }|j                  d      d   j                  d      r'|* c}S c c}w )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']
    rG   r   r   )r   r   )r   r   s     r   r   r      s2     $PC399S>!+<+E+Eh+OCPPPs   (55c                Z   t        | t              } g }t        |       D ]2  \  }t        fd| |dz   d D              s n|j	                         4 |D ]R  t        ||      }t        j                  j                  |d      }t        j                  j                  |      sPc S  y)z0Find the parent package that is not a namespace.r	  c              3  F   K   | ]  }|j                   d         ywr  r  )r&   r   r   s     r   r)   z&find_parent_package.<locals>.<genexpr>0  s      G1<<4&
+Gr  r   Nrd   )
r  r   r  r   rZ   find_package_pathr   r   rU   re   )r   r   r   common_ancestorsr  pkg_pathinitr   s          @r   r  r  )  s     hC(HX& &4GXa!eg5FGG
 %& ! $T;Aww||Hm477>>$K	 r   c                l   | j                  d      }t        t        |      dd      D ]E  }dj                  |d|       }||v s||   }t	        j
                  j                  ||g||d  c S  |j                  d      xs d}t	        j
                  j                  |g|j                  d      | 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'
    rG   r   Nr   /)r   ranger   rU   r   r   r   )r   r   r   partsr  partial_nameparents          r   r   r   A  s    , JJsOE3u:q"% >xxbq	*;& .F77<<&=59==> __R &BF77<<=6<<#4=u==r   c           
         t        |       }t        |      j                  }|D ci c]'  }|dj                  g ||j	                  d            ) c}S c c}w )Nr&  rG   )r   r
   r(  rU   r   )r   package_pathparent_pkgsprefixr   s        r   construct_package_dirr/  c  sQ    (2K,%%FALM#C4F4SYYs^455MMMs   ,A)r   r   r0   r2   )r   r/   r   r   r0   rJ   )r   rJ   r0   rJ   )r   rJ   r   Mapping[str, str]r   r   r0   r  )r   r/   r   r0  r   r   r0   r/   )r   rJ   r,  r   r0   r  )-r6   
__future__r   	itertoolsr   collections.abcr   r   r   fnmatchr   r	   pathlibr
   typingr   r   _distutils_hack.override_distutils_hackrk   r   	distutilsr   distutils.utilr   
setuptoolsr   chainfrom_iterabler   r   r   r9   rO   ri   rm   ru   r   r   r   r   r   r  r   r/  r7   r   r   <module>r>     s  %N #  	 7 7    *    ''__**
1
& & -" -"`,AG ,A^- 37 322T1 2Tj*\ *<B
a aH$Q&7CJ0>
>->9@>>DNr   