a oeR@sdZddlZddlZddlmZddlmZddlmZddlm Z m Z m Z m Z m Z mZmZmZmZddlZddlmZddlmZeeejfZe eZejjZe rdd lm Z ee!d d d Z"Gd ddZ#GdddZ$Gddde$Z%Gddde%Z&Gddde$Z'Gddde&Z(Gddde'Z)eee edddZ*GdddZ+e ee ed d!d"Z,e ee ed d#d$Z-e eeeefeeed%d&d'Z.eeeefeed(d)d*Z/e eee eefd+d,d-Z0dS).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 N fnmatchcase)glob)Path) TYPE_CHECKINGDictIterableIteratorListMappingOptionalTupleUnion)log) convert_path) Distribution)pathreturncCstj|SN)osrbasename isidentifier)rr5C:\Program Files\Certbot\pkgs\setuptools\discovery.py _valid_nameGsrc@s>eZdZdZedddZeedddZeeddd Zd 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. )patternscGst||_dSr)dictfromkeys _patterns)selfrrrr__init__Rsz_Filter.__init__)itemrcstfdd|jDS)Nc3s|]}t|VqdSrr).0patr"rr Vz#_Filter.__call__..)anyrr r"rr%r__call__Usz_Filter.__call__cCs ||jvSr)rr)rrr __contains__Xsz_Filter.__contains__N) __name__ __module__ __qualname____doc__strr!boolr*r+rrrrrLsrc@sxeZdZUdZdZeedfed<dZeedfed<e de e ee ee edd d Z e e eeedd d Zd S)_Finderz@Base class that exposes functionality for module/package findersr.ALWAYS_EXCLUDEDEFAULT_EXCLUDE.*whereexcludeincludercCs8|p|j}t|tt|tg|j|Rt|S)aFReturn 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'. )r4list _find_iterrr0rr3clsr9r:r;rrrfindbs  z _Finder.findcCstdSr)NotImplementedErrorr>rrrr=sz_Finder._find_iterN)r5rr6)r,r-r.r/r3r r0__annotations__r4 classmethod_Pathrr r@rStrIterr=rrrrr2\s "r2c@sBeZdZdZdZeeeeedddZ e ee e dddZ d S) PackageFinderzI Generate a list of all Python packages found within a directory )Zez_setupz *__pycache__r8c cstjt|ddD]\}}}|dd}g|dd<|D]}tj||} tj| |} | tjjd} d|vs8|| | s~q8|| r|| s| V| d|vs8| d|vrq8| |q8qdS)zy All the packages found in 'where' that pass the 'include' filter, but not the 'exclude' filter. T) followlinksNr5r7.*) rwalkr0rjoinrelpathreplacesep_looks_like_packageappend) r?r9r:r;rootdirsfilesZall_dirsdir full_pathZrel_pathpackagerrrr=s  zPackageFinder._find_iter)r _package_namercCstjtj|dS)z%Does a directory look like a package? __init__.py)rrisfilerJ)rrVrrrrNsz!PackageFinder._looks_like_packageN)r,r-r.r/r3rCrDrrEr= staticmethodr0r1rNrrrrrFs rFc@s"eZdZeeeedddZdS)PEP420PackageFinder)_pathrVrcCsdS)NTr)r[rVrrrrNsz'PEP420PackageFinder._looks_like_packageN)r,r-r.rYrDr0r1rNrrrrrZsrZc@s0eZdZdZeeeeedddZe e Z dS) ModuleFinderzYFind isolated Python modules. This function will **not** recurse subdirectories. r8ccsVttj|dD]>}tjtj|\}}||s:q||r||s|VqdS)Nz*.py)rrrrJsplitextr_looks_like_module)r?r9r:r;filemoduleZ_extrrrr=s  zModuleFinder._find_iterN) r,r-r.r/rCrDrrEr=rYrr^rrrrr\s r\c@s<eZdZdZeeddeDZeee e dddZ dS)FlatLayoutPackageFinder)#ZcibinZdebiandocZdocsZ documentationZmanpagesnewsZ newsfragmentsZ changelogtesttestsZ unit_testZ unit_testsexampleexamplesZscriptsZtoolsutilZutilsZpythonbuilddistvenvenvZ requirementstasksfabfileZ site_scons benchmark benchmarksexercise exercisesZhtmlcov[._]*ccs|]}||dfVqdS)rHNr)r#prrrr&r'z!FlatLayoutPackageFinder.)r[ package_namercCsB|d}|dp"|dd}|o@tdd|ddDS)Nr5r-stubscss|]}|VqdSr)r)r#namerrrr&r'z>FlatLayoutPackageFinder._looks_like_package..)splitrendswithall)r[rvnamesZroot_pkg_is_validrrrrNs z+FlatLayoutPackageFinder._looks_like_packageN) r,r-r.Z_EXCLUDEtuple chain_iterr4rYrDr0r1rNrrrrras)rac@seZdZdZdS)FlatLayoutModuleFinder)ZsetupZconftestrerfrgrhrjZtoxfileZnoxfileZpavementZdodornroz[Ss][Cc]onstructZ conanfileZmanagerprqrrrsrtN)r,r-r.r4rrrrr sr)root_pkgpkg_dirrcs"t|}gfdd|DS)Ncsg|]}d|fqS)r5)rJr#nrrr )r'z)_find_packages_within..)rZr@)rrnestedrrr_find_packages_within's rc@s eZdZdZddddZddZdd Zeed d d Z ee e e fd d dZ d,ddZ eedddZeedddZed ddZed ddZed ddZed ddZed d d!Zee e d"d#d$Zd%d&Zee d d'd(Zee d d)d*Zd+S)-ConfigDiscoveryzFill-in metadata and options that can be automatically derived (from other metadata/options, the file system or conventions) r) distributioncCs||_d|_d|_d|_dS)NF)rk_called _disabled_skip_ext_modules)r rrrrr!1szConfigDiscovery.__init__cCs d|_dS)z+Internal API to disable automatic discoveryTN)rr rrr_disable7szConfigDiscovery._disablecCs d|_dS)aInternal 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)rrrrr_ignore_ext_modules;s z#ConfigDiscovery._ignore_ext_modules)rcCs|jjp tjSr)rkZsrc_rootrcurdirrrrr _root_dirGszConfigDiscovery._root_dircCs|jjduriS|jjSr)rk package_dirrrrr _package_dirLs zConfigDiscovery._package_dirFTcCs8|dur|js|jrdS|||r.|d|_dS)aAutomatically 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)rr_analyse_package_layout analyse_name)r forcerxignore_ext_modulesrrrr*Rs  zConfigDiscovery.__call__)rrcCsL|p|j}|jjdup| }|jjdupJ|jjdupJ|pJt|jdoJ|jjS)zF``True`` if the user has specified some form of package/module listingN configuration)rrk ext_modulespackages py_moduleshasattrr)r rrrrr_explicitly_specifiedgs    z%ConfigDiscovery._explicitly_specifiedcCs0||rdStd|p.|p.|S)NTzLNo `packages` or `py_modules` configuration, performing automatic discovery.)rrdebug_analyse_explicit_layout_analyse_src_layout_analyse_flat_layout)r rrrrrts z'ConfigDiscovery._analyse_package_layoutcsr|j}|dd|j|s$dStd|tfdd|D}t||j _ td|j j dS) zAThe user can explicitly give a package layout via ``package_dir``NFz(`explicit-layout` detected -- analysing c3s&|]\}}t|tj|VqdSr)rrrrJ)r#pkgZ parent_dirroot_dirrrr&sz;ConfigDiscovery._analyse_explicit_layout..discovered packages -- T) rcopypoprrrritemsr<rkr)r rpkgsrrrrs    z(ConfigDiscovery._analyse_explicit_layoutcCs|j}tj|j|dd}tj|s.dStd|| dtj |||j _ t ||j _t||j _td|j jtd|j jdS)aTry 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"``. rsrcFz#`src-layout` detected -- analysing rdiscovered py_modules -- T)rrrrJrgetisdirrr setdefaultrrkrrZr@rr\r)r rZsrc_dirrrrrs  z#ConfigDiscovery._analyse_src_layoutcCs"td|j|p |S)aTry 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 )rrr_analyse_flat_packages_analyse_flat_modulesrrrrrs z$ConfigDiscovery._analyse_flat_layoutcCsHt|j|j_tt|jj}td|jj| |dt |S)Nrr) rar@rrkrremove_nested_packages remove_stubsrr_ensure_no_accidental_inclusionr1)r top_levelrrrrs  z&ConfigDiscovery._analyse_flat_packagescCs@t|j|j_td|jj||jjdt|jjS)Nrmodules) rr@rrkrrrrr1rrrrrsz%ConfigDiscovery._analyse_flat_modules)detectedkindcCsLt|dkrHddlm}ddlm}d|d|d|d}|||dS) Nryr)cleandoc)PackageDiscoveryErrorzMultiple 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. )leninspectrZsetuptools.errorsr)r rrrrmsgrrrrs   z/ConfigDiscovery._ensure_no_accidental_inclusioncCsB|jjjs|jjrdStd|p.|}|r>||jj_dS)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)rkmetadatarxrr#_find_name_single_package_or_module_find_name_from_packages)r rxrrrrs zConfigDiscovery.analyse_namecCsPdD]F}t|j|dpg}|rt|dkrtd|d|dSqdS)zExactly one module or package)rrNryz&Single module/package detected, name: r)getattrrkrrr)r Zfieldrrrrrs z3ConfigDiscovery._find_name_single_package_or_modulecCs`|jjs dStt|jjtd}|jjp*i}t|||j}|rRt d||St ddS)z>> 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'] rNc3s|]}|dVqdSr5N startswith)r#otherrxrrr&r'z)remove_nested_packages..ry)rr enumeratereversedr(r)rrrsizeirrrrs  rcCsdd|DS)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'] cSs$g|]}|ddds|qS)r5rrw)rzr{r#rrrrr&r'z remove_stubs..r)rrrrr sr)rrrrcst|td}g}t|D]8\}tfdd||ddDsFqR|q|D]2t||}tj|d}tj |rVSqVdS)z0Find the parent package that is not a namespace.rc3s|]}|dVqdSrrrrrrr&0r'z&find_parent_package..ryNrW) rrrr|rOfind_package_pathrrrJrX)rrrZcommon_ancestorsrZpkg_pathinitrrrr)s "    r)rxrrrcCs|d}tt|ddD]H}d|d|}||vr||}tjj||g||dRSq|dppd}tjj|g|d|RS)aGiven 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' r5rNr/)rzrangerrJrrr)rxrrpartsrZ partial_nameparentrrrrAs $r)r package_pathrcs$t|}t|jfdd|DS)Nc s&i|]}|dg|dqS)rr5)rJrzrprefixrr fr'z)construct_package_dir..)rrr)rrZ parent_pkgsrrrconstruct_package_dircs r)1r/ itertoolsrfnmatchrrpathlibrtypingrrrr r r r r rZ_distutils_hack.overrideZ_distutils_hackZ distutilsrZdistutils.utilrr0PathLikerDrEchain from_iterablerZ setuptoolsrr1rrr2rFrZr\rarrrrrrrrrrrrsD'   ,    .-5c   "