__init__.py 127 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835
  1. """
  2. Package resource API
  3. --------------------
  4. A resource is a logical file contained within a package, or a logical
  5. subdirectory thereof. The package resource API expects resource names
  6. to have their path parts separated with ``/``, *not* whatever the local
  7. path separator is. Do not use os.path operations to manipulate resource
  8. names being passed into the API.
  9. The package resource API is designed to work with normal filesystem packages,
  10. .egg files, and unpacked .egg files. It can also work in a limited way with
  11. .zip files and with custom PEP 302 loaders that support the ``get_data()``
  12. method.
  13. This module is deprecated. Users are directed to :mod:`importlib.resources`,
  14. :mod:`importlib.metadata` and :pypi:`packaging` instead.
  15. """
  16. from __future__ import annotations
  17. import sys
  18. if sys.version_info < (3, 9): # noqa: UP036 # Check for unsupported versions
  19. raise RuntimeError("Python 3.9 or later is required")
  20. import _imp
  21. import collections
  22. import email.parser
  23. import errno
  24. import functools
  25. import importlib
  26. import importlib.abc
  27. import importlib.machinery
  28. import inspect
  29. import io
  30. import ntpath
  31. import operator
  32. import os
  33. import pkgutil
  34. import platform
  35. import plistlib
  36. import posixpath
  37. import re
  38. import stat
  39. import tempfile
  40. import textwrap
  41. import time
  42. import types
  43. import warnings
  44. import zipfile
  45. import zipimport
  46. from collections.abc import Iterable, Iterator, Mapping, MutableSequence
  47. from pkgutil import get_importer
  48. from typing import (
  49. TYPE_CHECKING,
  50. Any,
  51. BinaryIO,
  52. Callable,
  53. Literal,
  54. NamedTuple,
  55. NoReturn,
  56. Protocol,
  57. TypeVar,
  58. Union,
  59. overload,
  60. )
  61. sys.path.extend(((vendor_path := os.path.join(os.path.dirname(os.path.dirname(__file__)), 'setuptools', '_vendor')) not in sys.path) * [vendor_path]) # fmt: skip
  62. # workaround for #4476
  63. sys.modules.pop('backports', None)
  64. # capture these to bypass sandboxing
  65. from os import open as os_open, utime # isort: skip
  66. from os.path import isdir, split # isort: skip
  67. try:
  68. from os import mkdir, rename, unlink
  69. WRITE_SUPPORT = True
  70. except ImportError:
  71. # no write support, probably under GAE
  72. WRITE_SUPPORT = False
  73. import packaging.markers
  74. import packaging.requirements
  75. import packaging.specifiers
  76. import packaging.utils
  77. import packaging.version
  78. from jaraco.text import drop_comment, join_continuation, yield_lines
  79. from platformdirs import user_cache_dir as _user_cache_dir
  80. if TYPE_CHECKING:
  81. from _typeshed import BytesPath, StrOrBytesPath, StrPath
  82. from _typeshed.importlib import LoaderProtocol
  83. from typing_extensions import Self, TypeAlias
  84. warnings.warn(
  85. "pkg_resources is deprecated as an API. "
  86. "See https://setuptools.pypa.io/en/latest/pkg_resources.html",
  87. DeprecationWarning,
  88. stacklevel=2,
  89. )
  90. _T = TypeVar("_T")
  91. _DistributionT = TypeVar("_DistributionT", bound="Distribution")
  92. # Type aliases
  93. _NestedStr: TypeAlias = Union[str, Iterable[Union[str, Iterable["_NestedStr"]]]]
  94. _StrictInstallerType: TypeAlias = Callable[["Requirement"], "_DistributionT"]
  95. _InstallerType: TypeAlias = Callable[["Requirement"], Union["Distribution", None]]
  96. _PkgReqType: TypeAlias = Union[str, "Requirement"]
  97. _EPDistType: TypeAlias = Union["Distribution", _PkgReqType]
  98. _MetadataType: TypeAlias = Union["IResourceProvider", None]
  99. _ResolvedEntryPoint: TypeAlias = Any # Can be any attribute in the module
  100. _ResourceStream: TypeAlias = Any # TODO / Incomplete: A readable file-like object
  101. # Any object works, but let's indicate we expect something like a module (optionally has __loader__ or __file__)
  102. _ModuleLike: TypeAlias = Union[object, types.ModuleType]
  103. # Any: Should be _ModuleLike but we end up with issues where _ModuleLike doesn't have _ZipLoaderModule's __loader__
  104. _ProviderFactoryType: TypeAlias = Callable[[Any], "IResourceProvider"]
  105. _DistFinderType: TypeAlias = Callable[[_T, str, bool], Iterable["Distribution"]]
  106. _NSHandlerType: TypeAlias = Callable[[_T, str, str, types.ModuleType], Union[str, None]]
  107. _AdapterT = TypeVar(
  108. "_AdapterT", _DistFinderType[Any], _ProviderFactoryType, _NSHandlerType[Any]
  109. )
  110. class _ZipLoaderModule(Protocol):
  111. __loader__: zipimport.zipimporter
  112. _PEP440_FALLBACK = re.compile(r"^v?(?P<safe>(?:[0-9]+!)?[0-9]+(?:\.[0-9]+)*)", re.I)
  113. class PEP440Warning(RuntimeWarning):
  114. """
  115. Used when there is an issue with a version or specifier not complying with
  116. PEP 440.
  117. """
  118. parse_version = packaging.version.Version
  119. _state_vars: dict[str, str] = {}
  120. def _declare_state(vartype: str, varname: str, initial_value: _T) -> _T:
  121. _state_vars[varname] = vartype
  122. return initial_value
  123. def __getstate__() -> dict[str, Any]:
  124. state = {}
  125. g = globals()
  126. for k, v in _state_vars.items():
  127. state[k] = g['_sget_' + v](g[k])
  128. return state
  129. def __setstate__(state: dict[str, Any]) -> dict[str, Any]:
  130. g = globals()
  131. for k, v in state.items():
  132. g['_sset_' + _state_vars[k]](k, g[k], v)
  133. return state
  134. def _sget_dict(val):
  135. return val.copy()
  136. def _sset_dict(key, ob, state) -> None:
  137. ob.clear()
  138. ob.update(state)
  139. def _sget_object(val):
  140. return val.__getstate__()
  141. def _sset_object(key, ob, state) -> None:
  142. ob.__setstate__(state)
  143. _sget_none = _sset_none = lambda *args: None
  144. def get_supported_platform():
  145. """Return this platform's maximum compatible version.
  146. distutils.util.get_platform() normally reports the minimum version
  147. of macOS that would be required to *use* extensions produced by
  148. distutils. But what we want when checking compatibility is to know the
  149. version of macOS that we are *running*. To allow usage of packages that
  150. explicitly require a newer version of macOS, we must also know the
  151. current version of the OS.
  152. If this condition occurs for any other platform with a version in its
  153. platform strings, this function should be extended accordingly.
  154. """
  155. plat = get_build_platform()
  156. m = macosVersionString.match(plat)
  157. if m is not None and sys.platform == "darwin":
  158. try:
  159. major_minor = '.'.join(_macos_vers()[:2])
  160. build = m.group(3)
  161. plat = f'macosx-{major_minor}-{build}'
  162. except ValueError:
  163. # not macOS
  164. pass
  165. return plat
  166. __all__ = [
  167. # Basic resource access and distribution/entry point discovery
  168. 'require',
  169. 'run_script',
  170. 'get_provider',
  171. 'get_distribution',
  172. 'load_entry_point',
  173. 'get_entry_map',
  174. 'get_entry_info',
  175. 'iter_entry_points',
  176. 'resource_string',
  177. 'resource_stream',
  178. 'resource_filename',
  179. 'resource_listdir',
  180. 'resource_exists',
  181. 'resource_isdir',
  182. # Environmental control
  183. 'declare_namespace',
  184. 'working_set',
  185. 'add_activation_listener',
  186. 'find_distributions',
  187. 'set_extraction_path',
  188. 'cleanup_resources',
  189. 'get_default_cache',
  190. # Primary implementation classes
  191. 'Environment',
  192. 'WorkingSet',
  193. 'ResourceManager',
  194. 'Distribution',
  195. 'Requirement',
  196. 'EntryPoint',
  197. # Exceptions
  198. 'ResolutionError',
  199. 'VersionConflict',
  200. 'DistributionNotFound',
  201. 'UnknownExtra',
  202. 'ExtractionError',
  203. # Warnings
  204. 'PEP440Warning',
  205. # Parsing functions and string utilities
  206. 'parse_requirements',
  207. 'parse_version',
  208. 'safe_name',
  209. 'safe_version',
  210. 'get_platform',
  211. 'compatible_platforms',
  212. 'yield_lines',
  213. 'split_sections',
  214. 'safe_extra',
  215. 'to_filename',
  216. 'invalid_marker',
  217. 'evaluate_marker',
  218. # filesystem utilities
  219. 'ensure_directory',
  220. 'normalize_path',
  221. # Distribution "precedence" constants
  222. 'EGG_DIST',
  223. 'BINARY_DIST',
  224. 'SOURCE_DIST',
  225. 'CHECKOUT_DIST',
  226. 'DEVELOP_DIST',
  227. # "Provider" interfaces, implementations, and registration/lookup APIs
  228. 'IMetadataProvider',
  229. 'IResourceProvider',
  230. 'FileMetadata',
  231. 'PathMetadata',
  232. 'EggMetadata',
  233. 'EmptyProvider',
  234. 'empty_provider',
  235. 'NullProvider',
  236. 'EggProvider',
  237. 'DefaultProvider',
  238. 'ZipProvider',
  239. 'register_finder',
  240. 'register_namespace_handler',
  241. 'register_loader_type',
  242. 'fixup_namespace_packages',
  243. 'get_importer',
  244. # Warnings
  245. 'PkgResourcesDeprecationWarning',
  246. # Deprecated/backward compatibility only
  247. 'run_main',
  248. 'AvailableDistributions',
  249. ]
  250. class ResolutionError(Exception):
  251. """Abstract base for dependency resolution errors"""
  252. def __repr__(self) -> str:
  253. return self.__class__.__name__ + repr(self.args)
  254. class VersionConflict(ResolutionError):
  255. """
  256. An already-installed version conflicts with the requested version.
  257. Should be initialized with the installed Distribution and the requested
  258. Requirement.
  259. """
  260. _template = "{self.dist} is installed but {self.req} is required"
  261. @property
  262. def dist(self) -> Distribution:
  263. return self.args[0]
  264. @property
  265. def req(self) -> Requirement:
  266. return self.args[1]
  267. def report(self):
  268. return self._template.format(**locals())
  269. def with_context(
  270. self, required_by: set[Distribution | str]
  271. ) -> Self | ContextualVersionConflict:
  272. """
  273. If required_by is non-empty, return a version of self that is a
  274. ContextualVersionConflict.
  275. """
  276. if not required_by:
  277. return self
  278. args = self.args + (required_by,)
  279. return ContextualVersionConflict(*args)
  280. class ContextualVersionConflict(VersionConflict):
  281. """
  282. A VersionConflict that accepts a third parameter, the set of the
  283. requirements that required the installed Distribution.
  284. """
  285. _template = VersionConflict._template + ' by {self.required_by}'
  286. @property
  287. def required_by(self) -> set[str]:
  288. return self.args[2]
  289. class DistributionNotFound(ResolutionError):
  290. """A requested distribution was not found"""
  291. _template = (
  292. "The '{self.req}' distribution was not found "
  293. "and is required by {self.requirers_str}"
  294. )
  295. @property
  296. def req(self) -> Requirement:
  297. return self.args[0]
  298. @property
  299. def requirers(self) -> set[str] | None:
  300. return self.args[1]
  301. @property
  302. def requirers_str(self):
  303. if not self.requirers:
  304. return 'the application'
  305. return ', '.join(self.requirers)
  306. def report(self):
  307. return self._template.format(**locals())
  308. def __str__(self) -> str:
  309. return self.report()
  310. class UnknownExtra(ResolutionError):
  311. """Distribution doesn't have an "extra feature" of the given name"""
  312. _provider_factories: dict[type[_ModuleLike], _ProviderFactoryType] = {}
  313. PY_MAJOR = f'{sys.version_info.major}.{sys.version_info.minor}'
  314. EGG_DIST = 3
  315. BINARY_DIST = 2
  316. SOURCE_DIST = 1
  317. CHECKOUT_DIST = 0
  318. DEVELOP_DIST = -1
  319. def register_loader_type(
  320. loader_type: type[_ModuleLike], provider_factory: _ProviderFactoryType
  321. ) -> None:
  322. """Register `provider_factory` to make providers for `loader_type`
  323. `loader_type` is the type or class of a PEP 302 ``module.__loader__``,
  324. and `provider_factory` is a function that, passed a *module* object,
  325. returns an ``IResourceProvider`` for that module.
  326. """
  327. _provider_factories[loader_type] = provider_factory
  328. @overload
  329. def get_provider(moduleOrReq: str) -> IResourceProvider: ...
  330. @overload
  331. def get_provider(moduleOrReq: Requirement) -> Distribution: ...
  332. def get_provider(moduleOrReq: str | Requirement) -> IResourceProvider | Distribution:
  333. """Return an IResourceProvider for the named module or requirement"""
  334. if isinstance(moduleOrReq, Requirement):
  335. return working_set.find(moduleOrReq) or require(str(moduleOrReq))[0]
  336. try:
  337. module = sys.modules[moduleOrReq]
  338. except KeyError:
  339. __import__(moduleOrReq)
  340. module = sys.modules[moduleOrReq]
  341. loader = getattr(module, '__loader__', None)
  342. return _find_adapter(_provider_factories, loader)(module)
  343. @functools.cache
  344. def _macos_vers():
  345. version = platform.mac_ver()[0]
  346. # fallback for MacPorts
  347. if version == '':
  348. plist = '/System/Library/CoreServices/SystemVersion.plist'
  349. if os.path.exists(plist):
  350. with open(plist, 'rb') as fh:
  351. plist_content = plistlib.load(fh)
  352. if 'ProductVersion' in plist_content:
  353. version = plist_content['ProductVersion']
  354. return version.split('.')
  355. def _macos_arch(machine):
  356. return {'PowerPC': 'ppc', 'Power_Macintosh': 'ppc'}.get(machine, machine)
  357. def get_build_platform():
  358. """Return this platform's string for platform-specific distributions
  359. XXX Currently this is the same as ``distutils.util.get_platform()``, but it
  360. needs some hacks for Linux and macOS.
  361. """
  362. from sysconfig import get_platform
  363. plat = get_platform()
  364. if sys.platform == "darwin" and not plat.startswith('macosx-'):
  365. try:
  366. version = _macos_vers()
  367. machine = _macos_arch(os.uname()[4].replace(" ", "_"))
  368. return f"macosx-{version[0]}.{version[1]}-{machine}"
  369. except ValueError:
  370. # if someone is running a non-Mac darwin system, this will fall
  371. # through to the default implementation
  372. pass
  373. return plat
  374. macosVersionString = re.compile(r"macosx-(\d+)\.(\d+)-(.*)")
  375. darwinVersionString = re.compile(r"darwin-(\d+)\.(\d+)\.(\d+)-(.*)")
  376. # XXX backward compat
  377. get_platform = get_build_platform
  378. def compatible_platforms(provided: str | None, required: str | None) -> bool:
  379. """Can code for the `provided` platform run on the `required` platform?
  380. Returns true if either platform is ``None``, or the platforms are equal.
  381. XXX Needs compatibility checks for Linux and other unixy OSes.
  382. """
  383. if provided is None or required is None or provided == required:
  384. # easy case
  385. return True
  386. # macOS special cases
  387. reqMac = macosVersionString.match(required)
  388. if reqMac:
  389. provMac = macosVersionString.match(provided)
  390. # is this a Mac package?
  391. if not provMac:
  392. # this is backwards compatibility for packages built before
  393. # setuptools 0.6. All packages built after this point will
  394. # use the new macOS designation.
  395. provDarwin = darwinVersionString.match(provided)
  396. if provDarwin:
  397. dversion = int(provDarwin.group(1))
  398. macosversion = f"{reqMac.group(1)}.{reqMac.group(2)}"
  399. if (
  400. dversion == 7
  401. and macosversion >= "10.3"
  402. or dversion == 8
  403. and macosversion >= "10.4"
  404. ):
  405. return True
  406. # egg isn't macOS or legacy darwin
  407. return False
  408. # are they the same major version and machine type?
  409. if provMac.group(1) != reqMac.group(1) or provMac.group(3) != reqMac.group(3):
  410. return False
  411. # is the required OS major update >= the provided one?
  412. if int(provMac.group(2)) > int(reqMac.group(2)):
  413. return False
  414. return True
  415. # XXX Linux and other platforms' special cases should go here
  416. return False
  417. @overload
  418. def get_distribution(dist: _DistributionT) -> _DistributionT: ...
  419. @overload
  420. def get_distribution(dist: _PkgReqType) -> Distribution: ...
  421. def get_distribution(dist: Distribution | _PkgReqType) -> Distribution:
  422. """Return a current distribution object for a Requirement or string"""
  423. if isinstance(dist, str):
  424. dist = Requirement.parse(dist)
  425. if isinstance(dist, Requirement):
  426. dist = get_provider(dist)
  427. if not isinstance(dist, Distribution):
  428. raise TypeError("Expected str, Requirement, or Distribution", dist)
  429. return dist
  430. def load_entry_point(dist: _EPDistType, group: str, name: str) -> _ResolvedEntryPoint:
  431. """Return `name` entry point of `group` for `dist` or raise ImportError"""
  432. return get_distribution(dist).load_entry_point(group, name)
  433. @overload
  434. def get_entry_map(
  435. dist: _EPDistType, group: None = None
  436. ) -> dict[str, dict[str, EntryPoint]]: ...
  437. @overload
  438. def get_entry_map(dist: _EPDistType, group: str) -> dict[str, EntryPoint]: ...
  439. def get_entry_map(dist: _EPDistType, group: str | None = None):
  440. """Return the entry point map for `group`, or the full entry map"""
  441. return get_distribution(dist).get_entry_map(group)
  442. def get_entry_info(dist: _EPDistType, group: str, name: str) -> EntryPoint | None:
  443. """Return the EntryPoint object for `group`+`name`, or ``None``"""
  444. return get_distribution(dist).get_entry_info(group, name)
  445. class IMetadataProvider(Protocol):
  446. def has_metadata(self, name: str) -> bool:
  447. """Does the package's distribution contain the named metadata?"""
  448. ...
  449. def get_metadata(self, name: str) -> str:
  450. """The named metadata resource as a string"""
  451. ...
  452. def get_metadata_lines(self, name: str) -> Iterator[str]:
  453. """Yield named metadata resource as list of non-blank non-comment lines
  454. Leading and trailing whitespace is stripped from each line, and lines
  455. with ``#`` as the first non-blank character are omitted."""
  456. ...
  457. def metadata_isdir(self, name: str) -> bool:
  458. """Is the named metadata a directory? (like ``os.path.isdir()``)"""
  459. ...
  460. def metadata_listdir(self, name: str) -> list[str]:
  461. """List of metadata names in the directory (like ``os.listdir()``)"""
  462. ...
  463. def run_script(self, script_name: str, namespace: dict[str, Any]) -> None:
  464. """Execute the named script in the supplied namespace dictionary"""
  465. ...
  466. class IResourceProvider(IMetadataProvider, Protocol):
  467. """An object that provides access to package resources"""
  468. def get_resource_filename(
  469. self, manager: ResourceManager, resource_name: str
  470. ) -> str:
  471. """Return a true filesystem path for `resource_name`
  472. `manager` must be a ``ResourceManager``"""
  473. ...
  474. def get_resource_stream(
  475. self, manager: ResourceManager, resource_name: str
  476. ) -> _ResourceStream:
  477. """Return a readable file-like object for `resource_name`
  478. `manager` must be a ``ResourceManager``"""
  479. ...
  480. def get_resource_string(
  481. self, manager: ResourceManager, resource_name: str
  482. ) -> bytes:
  483. """Return the contents of `resource_name` as :obj:`bytes`
  484. `manager` must be a ``ResourceManager``"""
  485. ...
  486. def has_resource(self, resource_name: str) -> bool:
  487. """Does the package contain the named resource?"""
  488. ...
  489. def resource_isdir(self, resource_name: str) -> bool:
  490. """Is the named resource a directory? (like ``os.path.isdir()``)"""
  491. ...
  492. def resource_listdir(self, resource_name: str) -> list[str]:
  493. """List of resource names in the directory (like ``os.listdir()``)"""
  494. ...
  495. class WorkingSet:
  496. """A collection of active distributions on sys.path (or a similar list)"""
  497. def __init__(self, entries: Iterable[str] | None = None) -> None:
  498. """Create working set from list of path entries (default=sys.path)"""
  499. self.entries: list[str] = []
  500. self.entry_keys: dict[str | None, list[str]] = {}
  501. self.by_key: dict[str, Distribution] = {}
  502. self.normalized_to_canonical_keys: dict[str, str] = {}
  503. self.callbacks: list[Callable[[Distribution], object]] = []
  504. if entries is None:
  505. entries = sys.path
  506. for entry in entries:
  507. self.add_entry(entry)
  508. @classmethod
  509. def _build_master(cls):
  510. """
  511. Prepare the master working set.
  512. """
  513. ws = cls()
  514. try:
  515. from __main__ import __requires__
  516. except ImportError:
  517. # The main program does not list any requirements
  518. return ws
  519. # ensure the requirements are met
  520. try:
  521. ws.require(__requires__)
  522. except VersionConflict:
  523. return cls._build_from_requirements(__requires__)
  524. return ws
  525. @classmethod
  526. def _build_from_requirements(cls, req_spec):
  527. """
  528. Build a working set from a requirement spec. Rewrites sys.path.
  529. """
  530. # try it without defaults already on sys.path
  531. # by starting with an empty path
  532. ws = cls([])
  533. reqs = parse_requirements(req_spec)
  534. dists = ws.resolve(reqs, Environment())
  535. for dist in dists:
  536. ws.add(dist)
  537. # add any missing entries from sys.path
  538. for entry in sys.path:
  539. if entry not in ws.entries:
  540. ws.add_entry(entry)
  541. # then copy back to sys.path
  542. sys.path[:] = ws.entries
  543. return ws
  544. def add_entry(self, entry: str) -> None:
  545. """Add a path item to ``.entries``, finding any distributions on it
  546. ``find_distributions(entry, True)`` is used to find distributions
  547. corresponding to the path entry, and they are added. `entry` is
  548. always appended to ``.entries``, even if it is already present.
  549. (This is because ``sys.path`` can contain the same value more than
  550. once, and the ``.entries`` of the ``sys.path`` WorkingSet should always
  551. equal ``sys.path``.)
  552. """
  553. self.entry_keys.setdefault(entry, [])
  554. self.entries.append(entry)
  555. for dist in find_distributions(entry, True):
  556. self.add(dist, entry, False)
  557. def __contains__(self, dist: Distribution) -> bool:
  558. """True if `dist` is the active distribution for its project"""
  559. return self.by_key.get(dist.key) == dist
  560. def find(self, req: Requirement) -> Distribution | None:
  561. """Find a distribution matching requirement `req`
  562. If there is an active distribution for the requested project, this
  563. returns it as long as it meets the version requirement specified by
  564. `req`. But, if there is an active distribution for the project and it
  565. does *not* meet the `req` requirement, ``VersionConflict`` is raised.
  566. If there is no active distribution for the requested project, ``None``
  567. is returned.
  568. """
  569. dist = self.by_key.get(req.key)
  570. if dist is None:
  571. canonical_key = self.normalized_to_canonical_keys.get(req.key)
  572. if canonical_key is not None:
  573. req.key = canonical_key
  574. dist = self.by_key.get(canonical_key)
  575. if dist is not None and dist not in req:
  576. # XXX add more info
  577. raise VersionConflict(dist, req)
  578. return dist
  579. def iter_entry_points(
  580. self, group: str, name: str | None = None
  581. ) -> Iterator[EntryPoint]:
  582. """Yield entry point objects from `group` matching `name`
  583. If `name` is None, yields all entry points in `group` from all
  584. distributions in the working set, otherwise only ones matching
  585. both `group` and `name` are yielded (in distribution order).
  586. """
  587. return (
  588. entry
  589. for dist in self
  590. for entry in dist.get_entry_map(group).values()
  591. if name is None or name == entry.name
  592. )
  593. def run_script(self, requires: str, script_name: str) -> None:
  594. """Locate distribution for `requires` and run `script_name` script"""
  595. ns = sys._getframe(1).f_globals
  596. name = ns['__name__']
  597. ns.clear()
  598. ns['__name__'] = name
  599. self.require(requires)[0].run_script(script_name, ns)
  600. def __iter__(self) -> Iterator[Distribution]:
  601. """Yield distributions for non-duplicate projects in the working set
  602. The yield order is the order in which the items' path entries were
  603. added to the working set.
  604. """
  605. seen = set()
  606. for item in self.entries:
  607. if item not in self.entry_keys:
  608. # workaround a cache issue
  609. continue
  610. for key in self.entry_keys[item]:
  611. if key not in seen:
  612. seen.add(key)
  613. yield self.by_key[key]
  614. def add(
  615. self,
  616. dist: Distribution,
  617. entry: str | None = None,
  618. insert: bool = True,
  619. replace: bool = False,
  620. ) -> None:
  621. """Add `dist` to working set, associated with `entry`
  622. If `entry` is unspecified, it defaults to the ``.location`` of `dist`.
  623. On exit from this routine, `entry` is added to the end of the working
  624. set's ``.entries`` (if it wasn't already present).
  625. `dist` is only added to the working set if it's for a project that
  626. doesn't already have a distribution in the set, unless `replace=True`.
  627. If it's added, any callbacks registered with the ``subscribe()`` method
  628. will be called.
  629. """
  630. if insert:
  631. dist.insert_on(self.entries, entry, replace=replace)
  632. if entry is None:
  633. entry = dist.location
  634. keys = self.entry_keys.setdefault(entry, [])
  635. keys2 = self.entry_keys.setdefault(dist.location, [])
  636. if not replace and dist.key in self.by_key:
  637. # ignore hidden distros
  638. return
  639. self.by_key[dist.key] = dist
  640. normalized_name = packaging.utils.canonicalize_name(dist.key)
  641. self.normalized_to_canonical_keys[normalized_name] = dist.key
  642. if dist.key not in keys:
  643. keys.append(dist.key)
  644. if dist.key not in keys2:
  645. keys2.append(dist.key)
  646. self._added_new(dist)
  647. @overload
  648. def resolve(
  649. self,
  650. requirements: Iterable[Requirement],
  651. env: Environment | None,
  652. installer: _StrictInstallerType[_DistributionT],
  653. replace_conflicting: bool = False,
  654. extras: tuple[str, ...] | None = None,
  655. ) -> list[_DistributionT]: ...
  656. @overload
  657. def resolve(
  658. self,
  659. requirements: Iterable[Requirement],
  660. env: Environment | None = None,
  661. *,
  662. installer: _StrictInstallerType[_DistributionT],
  663. replace_conflicting: bool = False,
  664. extras: tuple[str, ...] | None = None,
  665. ) -> list[_DistributionT]: ...
  666. @overload
  667. def resolve(
  668. self,
  669. requirements: Iterable[Requirement],
  670. env: Environment | None = None,
  671. installer: _InstallerType | None = None,
  672. replace_conflicting: bool = False,
  673. extras: tuple[str, ...] | None = None,
  674. ) -> list[Distribution]: ...
  675. def resolve(
  676. self,
  677. requirements: Iterable[Requirement],
  678. env: Environment | None = None,
  679. installer: _InstallerType | None | _StrictInstallerType[_DistributionT] = None,
  680. replace_conflicting: bool = False,
  681. extras: tuple[str, ...] | None = None,
  682. ) -> list[Distribution] | list[_DistributionT]:
  683. """List all distributions needed to (recursively) meet `requirements`
  684. `requirements` must be a sequence of ``Requirement`` objects. `env`,
  685. if supplied, should be an ``Environment`` instance. If
  686. not supplied, it defaults to all distributions available within any
  687. entry or distribution in the working set. `installer`, if supplied,
  688. will be invoked with each requirement that cannot be met by an
  689. already-installed distribution; it should return a ``Distribution`` or
  690. ``None``.
  691. Unless `replace_conflicting=True`, raises a VersionConflict exception
  692. if
  693. any requirements are found on the path that have the correct name but
  694. the wrong version. Otherwise, if an `installer` is supplied it will be
  695. invoked to obtain the correct version of the requirement and activate
  696. it.
  697. `extras` is a list of the extras to be used with these requirements.
  698. This is important because extra requirements may look like `my_req;
  699. extra = "my_extra"`, which would otherwise be interpreted as a purely
  700. optional requirement. Instead, we want to be able to assert that these
  701. requirements are truly required.
  702. """
  703. # set up the stack
  704. requirements = list(requirements)[::-1]
  705. # set of processed requirements
  706. processed = set()
  707. # key -> dist
  708. best: dict[str, Distribution] = {}
  709. to_activate: list[Distribution] = []
  710. req_extras = _ReqExtras()
  711. # Mapping of requirement to set of distributions that required it;
  712. # useful for reporting info about conflicts.
  713. required_by = collections.defaultdict[Requirement, set[str]](set)
  714. while requirements:
  715. # process dependencies breadth-first
  716. req = requirements.pop(0)
  717. if req in processed:
  718. # Ignore cyclic or redundant dependencies
  719. continue
  720. if not req_extras.markers_pass(req, extras):
  721. continue
  722. dist = self._resolve_dist(
  723. req, best, replace_conflicting, env, installer, required_by, to_activate
  724. )
  725. # push the new requirements onto the stack
  726. new_requirements = dist.requires(req.extras)[::-1]
  727. requirements.extend(new_requirements)
  728. # Register the new requirements needed by req
  729. for new_requirement in new_requirements:
  730. required_by[new_requirement].add(req.project_name)
  731. req_extras[new_requirement] = req.extras
  732. processed.add(req)
  733. # return list of distros to activate
  734. return to_activate
  735. def _resolve_dist(
  736. self, req, best, replace_conflicting, env, installer, required_by, to_activate
  737. ) -> Distribution:
  738. dist = best.get(req.key)
  739. if dist is None:
  740. # Find the best distribution and add it to the map
  741. dist = self.by_key.get(req.key)
  742. if dist is None or (dist not in req and replace_conflicting):
  743. ws = self
  744. if env is None:
  745. if dist is None:
  746. env = Environment(self.entries)
  747. else:
  748. # Use an empty environment and workingset to avoid
  749. # any further conflicts with the conflicting
  750. # distribution
  751. env = Environment([])
  752. ws = WorkingSet([])
  753. dist = best[req.key] = env.best_match(
  754. req, ws, installer, replace_conflicting=replace_conflicting
  755. )
  756. if dist is None:
  757. requirers = required_by.get(req, None)
  758. raise DistributionNotFound(req, requirers)
  759. to_activate.append(dist)
  760. if dist not in req:
  761. # Oops, the "best" so far conflicts with a dependency
  762. dependent_req = required_by[req]
  763. raise VersionConflict(dist, req).with_context(dependent_req)
  764. return dist
  765. @overload
  766. def find_plugins(
  767. self,
  768. plugin_env: Environment,
  769. full_env: Environment | None,
  770. installer: _StrictInstallerType[_DistributionT],
  771. fallback: bool = True,
  772. ) -> tuple[list[_DistributionT], dict[Distribution, Exception]]: ...
  773. @overload
  774. def find_plugins(
  775. self,
  776. plugin_env: Environment,
  777. full_env: Environment | None = None,
  778. *,
  779. installer: _StrictInstallerType[_DistributionT],
  780. fallback: bool = True,
  781. ) -> tuple[list[_DistributionT], dict[Distribution, Exception]]: ...
  782. @overload
  783. def find_plugins(
  784. self,
  785. plugin_env: Environment,
  786. full_env: Environment | None = None,
  787. installer: _InstallerType | None = None,
  788. fallback: bool = True,
  789. ) -> tuple[list[Distribution], dict[Distribution, Exception]]: ...
  790. def find_plugins(
  791. self,
  792. plugin_env: Environment,
  793. full_env: Environment | None = None,
  794. installer: _InstallerType | None | _StrictInstallerType[_DistributionT] = None,
  795. fallback: bool = True,
  796. ) -> tuple[
  797. list[Distribution] | list[_DistributionT],
  798. dict[Distribution, Exception],
  799. ]:
  800. """Find all activatable distributions in `plugin_env`
  801. Example usage::
  802. distributions, errors = working_set.find_plugins(
  803. Environment(plugin_dirlist)
  804. )
  805. # add plugins+libs to sys.path
  806. map(working_set.add, distributions)
  807. # display errors
  808. print('Could not load', errors)
  809. The `plugin_env` should be an ``Environment`` instance that contains
  810. only distributions that are in the project's "plugin directory" or
  811. directories. The `full_env`, if supplied, should be an ``Environment``
  812. contains all currently-available distributions. If `full_env` is not
  813. supplied, one is created automatically from the ``WorkingSet`` this
  814. method is called on, which will typically mean that every directory on
  815. ``sys.path`` will be scanned for distributions.
  816. `installer` is a standard installer callback as used by the
  817. ``resolve()`` method. The `fallback` flag indicates whether we should
  818. attempt to resolve older versions of a plugin if the newest version
  819. cannot be resolved.
  820. This method returns a 2-tuple: (`distributions`, `error_info`), where
  821. `distributions` is a list of the distributions found in `plugin_env`
  822. that were loadable, along with any other distributions that are needed
  823. to resolve their dependencies. `error_info` is a dictionary mapping
  824. unloadable plugin distributions to an exception instance describing the
  825. error that occurred. Usually this will be a ``DistributionNotFound`` or
  826. ``VersionConflict`` instance.
  827. """
  828. plugin_projects = list(plugin_env)
  829. # scan project names in alphabetic order
  830. plugin_projects.sort()
  831. error_info: dict[Distribution, Exception] = {}
  832. distributions: dict[Distribution, Exception | None] = {}
  833. if full_env is None:
  834. env = Environment(self.entries)
  835. env += plugin_env
  836. else:
  837. env = full_env + plugin_env
  838. shadow_set = self.__class__([])
  839. # put all our entries in shadow_set
  840. list(map(shadow_set.add, self))
  841. for project_name in plugin_projects:
  842. for dist in plugin_env[project_name]:
  843. req = [dist.as_requirement()]
  844. try:
  845. resolvees = shadow_set.resolve(req, env, installer)
  846. except ResolutionError as v:
  847. # save error info
  848. error_info[dist] = v
  849. if fallback:
  850. # try the next older version of project
  851. continue
  852. else:
  853. # give up on this project, keep going
  854. break
  855. else:
  856. list(map(shadow_set.add, resolvees))
  857. distributions.update(dict.fromkeys(resolvees))
  858. # success, no need to try any more versions of this project
  859. break
  860. sorted_distributions = list(distributions)
  861. sorted_distributions.sort()
  862. return sorted_distributions, error_info
  863. def require(self, *requirements: _NestedStr) -> list[Distribution]:
  864. """Ensure that distributions matching `requirements` are activated
  865. `requirements` must be a string or a (possibly-nested) sequence
  866. thereof, specifying the distributions and versions required. The
  867. return value is a sequence of the distributions that needed to be
  868. activated to fulfill the requirements; all relevant distributions are
  869. included, even if they were already activated in this working set.
  870. """
  871. needed = self.resolve(parse_requirements(requirements))
  872. for dist in needed:
  873. self.add(dist)
  874. return needed
  875. def subscribe(
  876. self, callback: Callable[[Distribution], object], existing: bool = True
  877. ) -> None:
  878. """Invoke `callback` for all distributions
  879. If `existing=True` (default),
  880. call on all existing ones, as well.
  881. """
  882. if callback in self.callbacks:
  883. return
  884. self.callbacks.append(callback)
  885. if not existing:
  886. return
  887. for dist in self:
  888. callback(dist)
  889. def _added_new(self, dist) -> None:
  890. for callback in self.callbacks:
  891. callback(dist)
  892. def __getstate__(
  893. self,
  894. ) -> tuple[
  895. list[str],
  896. dict[str | None, list[str]],
  897. dict[str, Distribution],
  898. dict[str, str],
  899. list[Callable[[Distribution], object]],
  900. ]:
  901. return (
  902. self.entries[:],
  903. self.entry_keys.copy(),
  904. self.by_key.copy(),
  905. self.normalized_to_canonical_keys.copy(),
  906. self.callbacks[:],
  907. )
  908. def __setstate__(self, e_k_b_n_c) -> None:
  909. entries, keys, by_key, normalized_to_canonical_keys, callbacks = e_k_b_n_c
  910. self.entries = entries[:]
  911. self.entry_keys = keys.copy()
  912. self.by_key = by_key.copy()
  913. self.normalized_to_canonical_keys = normalized_to_canonical_keys.copy()
  914. self.callbacks = callbacks[:]
  915. class _ReqExtras(dict["Requirement", tuple[str, ...]]):
  916. """
  917. Map each requirement to the extras that demanded it.
  918. """
  919. def markers_pass(self, req: Requirement, extras: tuple[str, ...] | None = None):
  920. """
  921. Evaluate markers for req against each extra that
  922. demanded it.
  923. Return False if the req has a marker and fails
  924. evaluation. Otherwise, return True.
  925. """
  926. return not req.marker or any(
  927. req.marker.evaluate({'extra': extra})
  928. for extra in self.get(req, ()) + (extras or ("",))
  929. )
  930. class Environment:
  931. """Searchable snapshot of distributions on a search path"""
  932. def __init__(
  933. self,
  934. search_path: Iterable[str] | None = None,
  935. platform: str | None = get_supported_platform(),
  936. python: str | None = PY_MAJOR,
  937. ) -> None:
  938. """Snapshot distributions available on a search path
  939. Any distributions found on `search_path` are added to the environment.
  940. `search_path` should be a sequence of ``sys.path`` items. If not
  941. supplied, ``sys.path`` is used.
  942. `platform` is an optional string specifying the name of the platform
  943. that platform-specific distributions must be compatible with. If
  944. unspecified, it defaults to the current platform. `python` is an
  945. optional string naming the desired version of Python (e.g. ``'3.6'``);
  946. it defaults to the current version.
  947. You may explicitly set `platform` (and/or `python`) to ``None`` if you
  948. wish to map *all* distributions, not just those compatible with the
  949. running platform or Python version.
  950. """
  951. self._distmap: dict[str, list[Distribution]] = {}
  952. self.platform = platform
  953. self.python = python
  954. self.scan(search_path)
  955. def can_add(self, dist: Distribution) -> bool:
  956. """Is distribution `dist` acceptable for this environment?
  957. The distribution must match the platform and python version
  958. requirements specified when this environment was created, or False
  959. is returned.
  960. """
  961. py_compat = (
  962. self.python is None
  963. or dist.py_version is None
  964. or dist.py_version == self.python
  965. )
  966. return py_compat and compatible_platforms(dist.platform, self.platform)
  967. def remove(self, dist: Distribution) -> None:
  968. """Remove `dist` from the environment"""
  969. self._distmap[dist.key].remove(dist)
  970. def scan(self, search_path: Iterable[str] | None = None) -> None:
  971. """Scan `search_path` for distributions usable in this environment
  972. Any distributions found are added to the environment.
  973. `search_path` should be a sequence of ``sys.path`` items. If not
  974. supplied, ``sys.path`` is used. Only distributions conforming to
  975. the platform/python version defined at initialization are added.
  976. """
  977. if search_path is None:
  978. search_path = sys.path
  979. for item in search_path:
  980. for dist in find_distributions(item):
  981. self.add(dist)
  982. def __getitem__(self, project_name: str) -> list[Distribution]:
  983. """Return a newest-to-oldest list of distributions for `project_name`
  984. Uses case-insensitive `project_name` comparison, assuming all the
  985. project's distributions use their project's name converted to all
  986. lowercase as their key.
  987. """
  988. distribution_key = project_name.lower()
  989. return self._distmap.get(distribution_key, [])
  990. def add(self, dist: Distribution) -> None:
  991. """Add `dist` if we ``can_add()`` it and it has not already been added"""
  992. if self.can_add(dist) and dist.has_version():
  993. dists = self._distmap.setdefault(dist.key, [])
  994. if dist not in dists:
  995. dists.append(dist)
  996. dists.sort(key=operator.attrgetter('hashcmp'), reverse=True)
  997. @overload
  998. def best_match(
  999. self,
  1000. req: Requirement,
  1001. working_set: WorkingSet,
  1002. installer: _StrictInstallerType[_DistributionT],
  1003. replace_conflicting: bool = False,
  1004. ) -> _DistributionT: ...
  1005. @overload
  1006. def best_match(
  1007. self,
  1008. req: Requirement,
  1009. working_set: WorkingSet,
  1010. installer: _InstallerType | None = None,
  1011. replace_conflicting: bool = False,
  1012. ) -> Distribution | None: ...
  1013. def best_match(
  1014. self,
  1015. req: Requirement,
  1016. working_set: WorkingSet,
  1017. installer: _InstallerType | None | _StrictInstallerType[_DistributionT] = None,
  1018. replace_conflicting: bool = False,
  1019. ) -> Distribution | None:
  1020. """Find distribution best matching `req` and usable on `working_set`
  1021. This calls the ``find(req)`` method of the `working_set` to see if a
  1022. suitable distribution is already active. (This may raise
  1023. ``VersionConflict`` if an unsuitable version of the project is already
  1024. active in the specified `working_set`.) If a suitable distribution
  1025. isn't active, this method returns the newest distribution in the
  1026. environment that meets the ``Requirement`` in `req`. If no suitable
  1027. distribution is found, and `installer` is supplied, then the result of
  1028. calling the environment's ``obtain(req, installer)`` method will be
  1029. returned.
  1030. """
  1031. try:
  1032. dist = working_set.find(req)
  1033. except VersionConflict:
  1034. if not replace_conflicting:
  1035. raise
  1036. dist = None
  1037. if dist is not None:
  1038. return dist
  1039. for dist in self[req.key]:
  1040. if dist in req:
  1041. return dist
  1042. # try to download/install
  1043. return self.obtain(req, installer)
  1044. @overload
  1045. def obtain(
  1046. self,
  1047. requirement: Requirement,
  1048. installer: _StrictInstallerType[_DistributionT],
  1049. ) -> _DistributionT: ...
  1050. @overload
  1051. def obtain(
  1052. self,
  1053. requirement: Requirement,
  1054. installer: Callable[[Requirement], None] | None = None,
  1055. ) -> None: ...
  1056. @overload
  1057. def obtain(
  1058. self,
  1059. requirement: Requirement,
  1060. installer: _InstallerType | None = None,
  1061. ) -> Distribution | None: ...
  1062. def obtain(
  1063. self,
  1064. requirement: Requirement,
  1065. installer: Callable[[Requirement], None]
  1066. | _InstallerType
  1067. | None
  1068. | _StrictInstallerType[_DistributionT] = None,
  1069. ) -> Distribution | None:
  1070. """Obtain a distribution matching `requirement` (e.g. via download)
  1071. Obtain a distro that matches requirement (e.g. via download). In the
  1072. base ``Environment`` class, this routine just returns
  1073. ``installer(requirement)``, unless `installer` is None, in which case
  1074. None is returned instead. This method is a hook that allows subclasses
  1075. to attempt other ways of obtaining a distribution before falling back
  1076. to the `installer` argument."""
  1077. return installer(requirement) if installer else None
  1078. def __iter__(self) -> Iterator[str]:
  1079. """Yield the unique project names of the available distributions"""
  1080. for key in self._distmap.keys():
  1081. if self[key]:
  1082. yield key
  1083. def __iadd__(self, other: Distribution | Environment) -> Self:
  1084. """In-place addition of a distribution or environment"""
  1085. if isinstance(other, Distribution):
  1086. self.add(other)
  1087. elif isinstance(other, Environment):
  1088. for project in other:
  1089. for dist in other[project]:
  1090. self.add(dist)
  1091. else:
  1092. raise TypeError(f"Can't add {other!r} to environment")
  1093. return self
  1094. def __add__(self, other: Distribution | Environment) -> Self:
  1095. """Add an environment or distribution to an environment"""
  1096. new = self.__class__([], platform=None, python=None)
  1097. for env in self, other:
  1098. new += env
  1099. return new
  1100. # XXX backward compatibility
  1101. AvailableDistributions = Environment
  1102. class ExtractionError(RuntimeError):
  1103. """An error occurred extracting a resource
  1104. The following attributes are available from instances of this exception:
  1105. manager
  1106. The resource manager that raised this exception
  1107. cache_path
  1108. The base directory for resource extraction
  1109. original_error
  1110. The exception instance that caused extraction to fail
  1111. """
  1112. manager: ResourceManager
  1113. cache_path: str
  1114. original_error: BaseException | None
  1115. class ResourceManager:
  1116. """Manage resource extraction and packages"""
  1117. extraction_path: str | None = None
  1118. def __init__(self) -> None:
  1119. # acts like a set
  1120. self.cached_files: dict[str, Literal[True]] = {}
  1121. def resource_exists(
  1122. self, package_or_requirement: _PkgReqType, resource_name: str
  1123. ) -> bool:
  1124. """Does the named resource exist?"""
  1125. return get_provider(package_or_requirement).has_resource(resource_name)
  1126. def resource_isdir(
  1127. self, package_or_requirement: _PkgReqType, resource_name: str
  1128. ) -> bool:
  1129. """Is the named resource an existing directory?"""
  1130. return get_provider(package_or_requirement).resource_isdir(resource_name)
  1131. def resource_filename(
  1132. self, package_or_requirement: _PkgReqType, resource_name: str
  1133. ) -> str:
  1134. """Return a true filesystem path for specified resource"""
  1135. return get_provider(package_or_requirement).get_resource_filename(
  1136. self, resource_name
  1137. )
  1138. def resource_stream(
  1139. self, package_or_requirement: _PkgReqType, resource_name: str
  1140. ) -> _ResourceStream:
  1141. """Return a readable file-like object for specified resource"""
  1142. return get_provider(package_or_requirement).get_resource_stream(
  1143. self, resource_name
  1144. )
  1145. def resource_string(
  1146. self, package_or_requirement: _PkgReqType, resource_name: str
  1147. ) -> bytes:
  1148. """Return specified resource as :obj:`bytes`"""
  1149. return get_provider(package_or_requirement).get_resource_string(
  1150. self, resource_name
  1151. )
  1152. def resource_listdir(
  1153. self, package_or_requirement: _PkgReqType, resource_name: str
  1154. ) -> list[str]:
  1155. """List the contents of the named resource directory"""
  1156. return get_provider(package_or_requirement).resource_listdir(resource_name)
  1157. def extraction_error(self) -> NoReturn:
  1158. """Give an error message for problems extracting file(s)"""
  1159. old_exc = sys.exc_info()[1]
  1160. cache_path = self.extraction_path or get_default_cache()
  1161. tmpl = textwrap.dedent(
  1162. """
  1163. Can't extract file(s) to egg cache
  1164. The following error occurred while trying to extract file(s)
  1165. to the Python egg cache:
  1166. {old_exc}
  1167. The Python egg cache directory is currently set to:
  1168. {cache_path}
  1169. Perhaps your account does not have write access to this directory?
  1170. You can change the cache directory by setting the PYTHON_EGG_CACHE
  1171. environment variable to point to an accessible directory.
  1172. """
  1173. ).lstrip()
  1174. err = ExtractionError(tmpl.format(**locals()))
  1175. err.manager = self
  1176. err.cache_path = cache_path
  1177. err.original_error = old_exc
  1178. raise err
  1179. def get_cache_path(self, archive_name: str, names: Iterable[StrPath] = ()) -> str:
  1180. """Return absolute location in cache for `archive_name` and `names`
  1181. The parent directory of the resulting path will be created if it does
  1182. not already exist. `archive_name` should be the base filename of the
  1183. enclosing egg (which may not be the name of the enclosing zipfile!),
  1184. including its ".egg" extension. `names`, if provided, should be a
  1185. sequence of path name parts "under" the egg's extraction location.
  1186. This method should only be called by resource providers that need to
  1187. obtain an extraction location, and only for names they intend to
  1188. extract, as it tracks the generated names for possible cleanup later.
  1189. """
  1190. extract_path = self.extraction_path or get_default_cache()
  1191. target_path = os.path.join(extract_path, archive_name + '-tmp', *names)
  1192. try:
  1193. _bypass_ensure_directory(target_path)
  1194. except Exception:
  1195. self.extraction_error()
  1196. self._warn_unsafe_extraction_path(extract_path)
  1197. self.cached_files[target_path] = True
  1198. return target_path
  1199. @staticmethod
  1200. def _warn_unsafe_extraction_path(path) -> None:
  1201. """
  1202. If the default extraction path is overridden and set to an insecure
  1203. location, such as /tmp, it opens up an opportunity for an attacker to
  1204. replace an extracted file with an unauthorized payload. Warn the user
  1205. if a known insecure location is used.
  1206. See Distribute #375 for more details.
  1207. """
  1208. if os.name == 'nt' and not path.startswith(os.environ['windir']):
  1209. # On Windows, permissions are generally restrictive by default
  1210. # and temp directories are not writable by other users, so
  1211. # bypass the warning.
  1212. return
  1213. mode = os.stat(path).st_mode
  1214. if mode & stat.S_IWOTH or mode & stat.S_IWGRP:
  1215. msg = (
  1216. "Extraction path is writable by group/others "
  1217. "and vulnerable to attack when "
  1218. "used with get_resource_filename ({path}). "
  1219. "Consider a more secure "
  1220. "location (set with .set_extraction_path or the "
  1221. "PYTHON_EGG_CACHE environment variable)."
  1222. ).format(**locals())
  1223. warnings.warn(msg, UserWarning)
  1224. def postprocess(self, tempname: StrOrBytesPath, filename: StrOrBytesPath) -> None:
  1225. """Perform any platform-specific postprocessing of `tempname`
  1226. This is where Mac header rewrites should be done; other platforms don't
  1227. have anything special they should do.
  1228. Resource providers should call this method ONLY after successfully
  1229. extracting a compressed resource. They must NOT call it on resources
  1230. that are already in the filesystem.
  1231. `tempname` is the current (temporary) name of the file, and `filename`
  1232. is the name it will be renamed to by the caller after this routine
  1233. returns.
  1234. """
  1235. if os.name == 'posix':
  1236. # Make the resource executable
  1237. mode = ((os.stat(tempname).st_mode) | 0o555) & 0o7777
  1238. os.chmod(tempname, mode)
  1239. def set_extraction_path(self, path: str) -> None:
  1240. """Set the base path where resources will be extracted to, if needed.
  1241. If you do not call this routine before any extractions take place, the
  1242. path defaults to the return value of ``get_default_cache()``. (Which
  1243. is based on the ``PYTHON_EGG_CACHE`` environment variable, with various
  1244. platform-specific fallbacks. See that routine's documentation for more
  1245. details.)
  1246. Resources are extracted to subdirectories of this path based upon
  1247. information given by the ``IResourceProvider``. You may set this to a
  1248. temporary directory, but then you must call ``cleanup_resources()`` to
  1249. delete the extracted files when done. There is no guarantee that
  1250. ``cleanup_resources()`` will be able to remove all extracted files.
  1251. (Note: you may not change the extraction path for a given resource
  1252. manager once resources have been extracted, unless you first call
  1253. ``cleanup_resources()``.)
  1254. """
  1255. if self.cached_files:
  1256. raise ValueError("Can't change extraction path, files already extracted")
  1257. self.extraction_path = path
  1258. def cleanup_resources(self, force: bool = False) -> list[str]:
  1259. """
  1260. Delete all extracted resource files and directories, returning a list
  1261. of the file and directory names that could not be successfully removed.
  1262. This function does not have any concurrency protection, so it should
  1263. generally only be called when the extraction path is a temporary
  1264. directory exclusive to a single process. This method is not
  1265. automatically called; you must call it explicitly or register it as an
  1266. ``atexit`` function if you wish to ensure cleanup of a temporary
  1267. directory used for extractions.
  1268. """
  1269. # XXX
  1270. return []
  1271. def get_default_cache() -> str:
  1272. """
  1273. Return the ``PYTHON_EGG_CACHE`` environment variable
  1274. or a platform-relevant user cache dir for an app
  1275. named "Python-Eggs".
  1276. """
  1277. return os.environ.get('PYTHON_EGG_CACHE') or _user_cache_dir(appname='Python-Eggs')
  1278. def safe_name(name: str) -> str:
  1279. """Convert an arbitrary string to a standard distribution name
  1280. Any runs of non-alphanumeric/. characters are replaced with a single '-'.
  1281. """
  1282. return re.sub('[^A-Za-z0-9.]+', '-', name)
  1283. def safe_version(version: str) -> str:
  1284. """
  1285. Convert an arbitrary string to a standard version string
  1286. """
  1287. try:
  1288. # normalize the version
  1289. return str(packaging.version.Version(version))
  1290. except packaging.version.InvalidVersion:
  1291. version = version.replace(' ', '.')
  1292. return re.sub('[^A-Za-z0-9.]+', '-', version)
  1293. def _forgiving_version(version) -> str:
  1294. """Fallback when ``safe_version`` is not safe enough
  1295. >>> parse_version(_forgiving_version('0.23ubuntu1'))
  1296. <Version('0.23.dev0+sanitized.ubuntu1')>
  1297. >>> parse_version(_forgiving_version('0.23-'))
  1298. <Version('0.23.dev0+sanitized')>
  1299. >>> parse_version(_forgiving_version('0.-_'))
  1300. <Version('0.dev0+sanitized')>
  1301. >>> parse_version(_forgiving_version('42.+?1'))
  1302. <Version('42.dev0+sanitized.1')>
  1303. >>> parse_version(_forgiving_version('hello world'))
  1304. <Version('0.dev0+sanitized.hello.world')>
  1305. """
  1306. version = version.replace(' ', '.')
  1307. match = _PEP440_FALLBACK.search(version)
  1308. if match:
  1309. safe = match["safe"]
  1310. rest = version[len(safe) :]
  1311. else:
  1312. safe = "0"
  1313. rest = version
  1314. local = f"sanitized.{_safe_segment(rest)}".strip(".")
  1315. return f"{safe}.dev0+{local}"
  1316. def _safe_segment(segment):
  1317. """Convert an arbitrary string into a safe segment"""
  1318. segment = re.sub('[^A-Za-z0-9.]+', '-', segment)
  1319. segment = re.sub('-[^A-Za-z0-9]+', '-', segment)
  1320. return re.sub(r'\.[^A-Za-z0-9]+', '.', segment).strip(".-")
  1321. def safe_extra(extra: str) -> str:
  1322. """Convert an arbitrary string to a standard 'extra' name
  1323. Any runs of non-alphanumeric characters are replaced with a single '_',
  1324. and the result is always lowercased.
  1325. """
  1326. return re.sub('[^A-Za-z0-9.-]+', '_', extra).lower()
  1327. def to_filename(name: str) -> str:
  1328. """Convert a project or version name to its filename-escaped form
  1329. Any '-' characters are currently replaced with '_'.
  1330. """
  1331. return name.replace('-', '_')
  1332. def invalid_marker(text: str) -> SyntaxError | Literal[False]:
  1333. """
  1334. Validate text as a PEP 508 environment marker; return an exception
  1335. if invalid or False otherwise.
  1336. """
  1337. try:
  1338. evaluate_marker(text)
  1339. except SyntaxError as e:
  1340. e.filename = None
  1341. e.lineno = None
  1342. return e
  1343. return False
  1344. def evaluate_marker(text: str, extra: str | None = None) -> bool:
  1345. """
  1346. Evaluate a PEP 508 environment marker.
  1347. Return a boolean indicating the marker result in this environment.
  1348. Raise SyntaxError if marker is invalid.
  1349. This implementation uses the 'pyparsing' module.
  1350. """
  1351. try:
  1352. marker = packaging.markers.Marker(text)
  1353. return marker.evaluate()
  1354. except packaging.markers.InvalidMarker as e:
  1355. raise SyntaxError(e) from e
  1356. class NullProvider:
  1357. """Try to implement resources and metadata for arbitrary PEP 302 loaders"""
  1358. egg_name: str | None = None
  1359. egg_info: str | None = None
  1360. loader: LoaderProtocol | None = None
  1361. def __init__(self, module: _ModuleLike) -> None:
  1362. self.loader = getattr(module, '__loader__', None)
  1363. self.module_path = os.path.dirname(getattr(module, '__file__', ''))
  1364. def get_resource_filename(
  1365. self, manager: ResourceManager, resource_name: str
  1366. ) -> str:
  1367. return self._fn(self.module_path, resource_name)
  1368. def get_resource_stream(
  1369. self, manager: ResourceManager, resource_name: str
  1370. ) -> BinaryIO:
  1371. return io.BytesIO(self.get_resource_string(manager, resource_name))
  1372. def get_resource_string(
  1373. self, manager: ResourceManager, resource_name: str
  1374. ) -> bytes:
  1375. return self._get(self._fn(self.module_path, resource_name))
  1376. def has_resource(self, resource_name: str) -> bool:
  1377. return self._has(self._fn(self.module_path, resource_name))
  1378. def _get_metadata_path(self, name):
  1379. return self._fn(self.egg_info, name)
  1380. def has_metadata(self, name: str) -> bool:
  1381. if not self.egg_info:
  1382. return False
  1383. path = self._get_metadata_path(name)
  1384. return self._has(path)
  1385. def get_metadata(self, name: str) -> str:
  1386. if not self.egg_info:
  1387. return ""
  1388. path = self._get_metadata_path(name)
  1389. value = self._get(path)
  1390. try:
  1391. return value.decode('utf-8')
  1392. except UnicodeDecodeError as exc:
  1393. # Include the path in the error message to simplify
  1394. # troubleshooting, and without changing the exception type.
  1395. exc.reason += f' in {name} file at path: {path}'
  1396. raise
  1397. def get_metadata_lines(self, name: str) -> Iterator[str]:
  1398. return yield_lines(self.get_metadata(name))
  1399. def resource_isdir(self, resource_name: str) -> bool:
  1400. return self._isdir(self._fn(self.module_path, resource_name))
  1401. def metadata_isdir(self, name: str) -> bool:
  1402. return bool(self.egg_info and self._isdir(self._fn(self.egg_info, name)))
  1403. def resource_listdir(self, resource_name: str) -> list[str]:
  1404. return self._listdir(self._fn(self.module_path, resource_name))
  1405. def metadata_listdir(self, name: str) -> list[str]:
  1406. if self.egg_info:
  1407. return self._listdir(self._fn(self.egg_info, name))
  1408. return []
  1409. def run_script(self, script_name: str, namespace: dict[str, Any]) -> None:
  1410. script = 'scripts/' + script_name
  1411. if not self.has_metadata(script):
  1412. raise ResolutionError(
  1413. "Script {script!r} not found in metadata at {self.egg_info!r}".format(
  1414. **locals()
  1415. ),
  1416. )
  1417. script_text = self.get_metadata(script).replace('\r\n', '\n')
  1418. script_text = script_text.replace('\r', '\n')
  1419. script_filename = self._fn(self.egg_info, script)
  1420. namespace['__file__'] = script_filename
  1421. if os.path.exists(script_filename):
  1422. source = _read_utf8_with_fallback(script_filename)
  1423. code = compile(source, script_filename, 'exec')
  1424. exec(code, namespace, namespace)
  1425. else:
  1426. from linecache import cache
  1427. cache[script_filename] = (
  1428. len(script_text),
  1429. 0,
  1430. script_text.split('\n'),
  1431. script_filename,
  1432. )
  1433. script_code = compile(script_text, script_filename, 'exec')
  1434. exec(script_code, namespace, namespace)
  1435. def _has(self, path) -> bool:
  1436. raise NotImplementedError(
  1437. "Can't perform this operation for unregistered loader type"
  1438. )
  1439. def _isdir(self, path) -> bool:
  1440. raise NotImplementedError(
  1441. "Can't perform this operation for unregistered loader type"
  1442. )
  1443. def _listdir(self, path) -> list[str]:
  1444. raise NotImplementedError(
  1445. "Can't perform this operation for unregistered loader type"
  1446. )
  1447. def _fn(self, base: str | None, resource_name: str):
  1448. if base is None:
  1449. raise TypeError(
  1450. "`base` parameter in `_fn` is `None`. Either override this method or check the parameter first."
  1451. )
  1452. self._validate_resource_path(resource_name)
  1453. if resource_name:
  1454. return os.path.join(base, *resource_name.split('/'))
  1455. return base
  1456. @staticmethod
  1457. def _validate_resource_path(path) -> None:
  1458. """
  1459. Validate the resource paths according to the docs.
  1460. https://setuptools.pypa.io/en/latest/pkg_resources.html#basic-resource-access
  1461. >>> warned = getfixture('recwarn')
  1462. >>> warnings.simplefilter('always')
  1463. >>> vrp = NullProvider._validate_resource_path
  1464. >>> vrp('foo/bar.txt')
  1465. >>> bool(warned)
  1466. False
  1467. >>> vrp('../foo/bar.txt')
  1468. >>> bool(warned)
  1469. True
  1470. >>> warned.clear()
  1471. >>> vrp('/foo/bar.txt')
  1472. >>> bool(warned)
  1473. True
  1474. >>> vrp('foo/../../bar.txt')
  1475. >>> bool(warned)
  1476. True
  1477. >>> warned.clear()
  1478. >>> vrp('foo/f../bar.txt')
  1479. >>> bool(warned)
  1480. False
  1481. Windows path separators are straight-up disallowed.
  1482. >>> vrp(r'\\foo/bar.txt')
  1483. Traceback (most recent call last):
  1484. ...
  1485. ValueError: Use of .. or absolute path in a resource path \
  1486. is not allowed.
  1487. >>> vrp(r'C:\\foo/bar.txt')
  1488. Traceback (most recent call last):
  1489. ...
  1490. ValueError: Use of .. or absolute path in a resource path \
  1491. is not allowed.
  1492. Blank values are allowed
  1493. >>> vrp('')
  1494. >>> bool(warned)
  1495. False
  1496. Non-string values are not.
  1497. >>> vrp(None)
  1498. Traceback (most recent call last):
  1499. ...
  1500. AttributeError: ...
  1501. """
  1502. invalid = (
  1503. os.path.pardir in path.split(posixpath.sep)
  1504. or posixpath.isabs(path)
  1505. or ntpath.isabs(path)
  1506. or path.startswith("\\")
  1507. )
  1508. if not invalid:
  1509. return
  1510. msg = "Use of .. or absolute path in a resource path is not allowed."
  1511. # Aggressively disallow Windows absolute paths
  1512. if (path.startswith("\\") or ntpath.isabs(path)) and not posixpath.isabs(path):
  1513. raise ValueError(msg)
  1514. # for compatibility, warn; in future
  1515. # raise ValueError(msg)
  1516. issue_warning(
  1517. msg[:-1] + " and will raise exceptions in a future release.",
  1518. DeprecationWarning,
  1519. )
  1520. def _get(self, path) -> bytes:
  1521. if hasattr(self.loader, 'get_data') and self.loader:
  1522. # Already checked get_data exists
  1523. return self.loader.get_data(path) # type: ignore[attr-defined]
  1524. raise NotImplementedError(
  1525. "Can't perform this operation for loaders without 'get_data()'"
  1526. )
  1527. register_loader_type(object, NullProvider)
  1528. def _parents(path):
  1529. """
  1530. yield all parents of path including path
  1531. """
  1532. last = None
  1533. while path != last:
  1534. yield path
  1535. last = path
  1536. path, _ = os.path.split(path)
  1537. class EggProvider(NullProvider):
  1538. """Provider based on a virtual filesystem"""
  1539. def __init__(self, module: _ModuleLike) -> None:
  1540. super().__init__(module)
  1541. self._setup_prefix()
  1542. def _setup_prefix(self):
  1543. # Assume that metadata may be nested inside a "basket"
  1544. # of multiple eggs and use module_path instead of .archive.
  1545. eggs = filter(_is_egg_path, _parents(self.module_path))
  1546. egg = next(eggs, None)
  1547. egg and self._set_egg(egg)
  1548. def _set_egg(self, path: str) -> None:
  1549. self.egg_name = os.path.basename(path)
  1550. self.egg_info = os.path.join(path, 'EGG-INFO')
  1551. self.egg_root = path
  1552. class DefaultProvider(EggProvider):
  1553. """Provides access to package resources in the filesystem"""
  1554. def _has(self, path) -> bool:
  1555. return os.path.exists(path)
  1556. def _isdir(self, path) -> bool:
  1557. return os.path.isdir(path)
  1558. def _listdir(self, path):
  1559. return os.listdir(path)
  1560. def get_resource_stream(
  1561. self, manager: object, resource_name: str
  1562. ) -> io.BufferedReader:
  1563. return open(self._fn(self.module_path, resource_name), 'rb')
  1564. def _get(self, path) -> bytes:
  1565. with open(path, 'rb') as stream:
  1566. return stream.read()
  1567. @classmethod
  1568. def _register(cls) -> None:
  1569. loader_names = (
  1570. 'SourceFileLoader',
  1571. 'SourcelessFileLoader',
  1572. )
  1573. for name in loader_names:
  1574. loader_cls = getattr(importlib.machinery, name, type(None))
  1575. register_loader_type(loader_cls, cls)
  1576. DefaultProvider._register()
  1577. class EmptyProvider(NullProvider):
  1578. """Provider that returns nothing for all requests"""
  1579. # A special case, we don't want all Providers inheriting from NullProvider to have a potentially None module_path
  1580. module_path: str | None = None # type: ignore[assignment]
  1581. _isdir = _has = lambda self, path: False
  1582. def _get(self, path) -> bytes:
  1583. return b''
  1584. def _listdir(self, path):
  1585. return []
  1586. def __init__(self) -> None:
  1587. pass
  1588. empty_provider = EmptyProvider()
  1589. class ZipManifests(dict[str, "MemoizedZipManifests.manifest_mod"]):
  1590. """
  1591. zip manifest builder
  1592. """
  1593. # `path` could be `StrPath | IO[bytes]` but that violates the LSP for `MemoizedZipManifests.load`
  1594. @classmethod
  1595. def build(cls, path: str) -> dict[str, zipfile.ZipInfo]:
  1596. """
  1597. Build a dictionary similar to the zipimport directory
  1598. caches, except instead of tuples, store ZipInfo objects.
  1599. Use a platform-specific path separator (os.sep) for the path keys
  1600. for compatibility with pypy on Windows.
  1601. """
  1602. with zipfile.ZipFile(path) as zfile:
  1603. items = (
  1604. (
  1605. name.replace('/', os.sep),
  1606. zfile.getinfo(name),
  1607. )
  1608. for name in zfile.namelist()
  1609. )
  1610. return dict(items)
  1611. load = build
  1612. class MemoizedZipManifests(ZipManifests):
  1613. """
  1614. Memoized zipfile manifests.
  1615. """
  1616. class manifest_mod(NamedTuple):
  1617. manifest: dict[str, zipfile.ZipInfo]
  1618. mtime: float
  1619. def load(self, path: str) -> dict[str, zipfile.ZipInfo]: # type: ignore[override] # ZipManifests.load is a classmethod
  1620. """
  1621. Load a manifest at path or return a suitable manifest already loaded.
  1622. """
  1623. path = os.path.normpath(path)
  1624. mtime = os.stat(path).st_mtime
  1625. if path not in self or self[path].mtime != mtime:
  1626. manifest = self.build(path)
  1627. self[path] = self.manifest_mod(manifest, mtime)
  1628. return self[path].manifest
  1629. class ZipProvider(EggProvider):
  1630. """Resource support for zips and eggs"""
  1631. eagers: list[str] | None = None
  1632. _zip_manifests = MemoizedZipManifests()
  1633. # ZipProvider's loader should always be a zipimporter or equivalent
  1634. loader: zipimport.zipimporter
  1635. def __init__(self, module: _ZipLoaderModule) -> None:
  1636. super().__init__(module)
  1637. self.zip_pre = self.loader.archive + os.sep
  1638. def _zipinfo_name(self, fspath):
  1639. # Convert a virtual filename (full path to file) into a zipfile subpath
  1640. # usable with the zipimport directory cache for our target archive
  1641. fspath = fspath.rstrip(os.sep)
  1642. if fspath == self.loader.archive:
  1643. return ''
  1644. if fspath.startswith(self.zip_pre):
  1645. return fspath[len(self.zip_pre) :]
  1646. raise AssertionError(f"{fspath} is not a subpath of {self.zip_pre}")
  1647. def _parts(self, zip_path):
  1648. # Convert a zipfile subpath into an egg-relative path part list.
  1649. # pseudo-fs path
  1650. fspath = self.zip_pre + zip_path
  1651. if fspath.startswith(self.egg_root + os.sep):
  1652. return fspath[len(self.egg_root) + 1 :].split(os.sep)
  1653. raise AssertionError(f"{fspath} is not a subpath of {self.egg_root}")
  1654. @property
  1655. def zipinfo(self):
  1656. return self._zip_manifests.load(self.loader.archive)
  1657. def get_resource_filename(
  1658. self, manager: ResourceManager, resource_name: str
  1659. ) -> str:
  1660. if not self.egg_name:
  1661. raise NotImplementedError(
  1662. "resource_filename() only supported for .egg, not .zip"
  1663. )
  1664. # no need to lock for extraction, since we use temp names
  1665. zip_path = self._resource_to_zip(resource_name)
  1666. eagers = self._get_eager_resources()
  1667. if '/'.join(self._parts(zip_path)) in eagers:
  1668. for name in eagers:
  1669. self._extract_resource(manager, self._eager_to_zip(name))
  1670. return self._extract_resource(manager, zip_path)
  1671. @staticmethod
  1672. def _get_date_and_size(zip_stat):
  1673. size = zip_stat.file_size
  1674. # ymdhms+wday, yday, dst
  1675. date_time = zip_stat.date_time + (0, 0, -1)
  1676. # 1980 offset already done
  1677. timestamp = time.mktime(date_time)
  1678. return timestamp, size
  1679. # FIXME: 'ZipProvider._extract_resource' is too complex (12)
  1680. def _extract_resource(self, manager: ResourceManager, zip_path) -> str: # noqa: C901
  1681. if zip_path in self._index():
  1682. for name in self._index()[zip_path]:
  1683. last = self._extract_resource(manager, os.path.join(zip_path, name))
  1684. # return the extracted directory name
  1685. return os.path.dirname(last)
  1686. timestamp, _size = self._get_date_and_size(self.zipinfo[zip_path])
  1687. if not WRITE_SUPPORT:
  1688. raise OSError(
  1689. '"os.rename" and "os.unlink" are not supported on this platform'
  1690. )
  1691. try:
  1692. if not self.egg_name:
  1693. raise OSError(
  1694. '"egg_name" is empty. This likely means no egg could be found from the "module_path".'
  1695. )
  1696. real_path = manager.get_cache_path(self.egg_name, self._parts(zip_path))
  1697. if self._is_current(real_path, zip_path):
  1698. return real_path
  1699. outf, tmpnam = _mkstemp(
  1700. ".$extract",
  1701. dir=os.path.dirname(real_path),
  1702. )
  1703. os.write(outf, self.loader.get_data(zip_path))
  1704. os.close(outf)
  1705. utime(tmpnam, (timestamp, timestamp))
  1706. manager.postprocess(tmpnam, real_path)
  1707. try:
  1708. rename(tmpnam, real_path)
  1709. except OSError:
  1710. if os.path.isfile(real_path):
  1711. if self._is_current(real_path, zip_path):
  1712. # the file became current since it was checked above,
  1713. # so proceed.
  1714. return real_path
  1715. # Windows, del old file and retry
  1716. elif os.name == 'nt':
  1717. unlink(real_path)
  1718. rename(tmpnam, real_path)
  1719. return real_path
  1720. raise
  1721. except OSError:
  1722. # report a user-friendly error
  1723. manager.extraction_error()
  1724. return real_path
  1725. def _is_current(self, file_path, zip_path):
  1726. """
  1727. Return True if the file_path is current for this zip_path
  1728. """
  1729. timestamp, size = self._get_date_and_size(self.zipinfo[zip_path])
  1730. if not os.path.isfile(file_path):
  1731. return False
  1732. stat = os.stat(file_path)
  1733. if stat.st_size != size or stat.st_mtime != timestamp:
  1734. return False
  1735. # check that the contents match
  1736. zip_contents = self.loader.get_data(zip_path)
  1737. with open(file_path, 'rb') as f:
  1738. file_contents = f.read()
  1739. return zip_contents == file_contents
  1740. def _get_eager_resources(self):
  1741. if self.eagers is None:
  1742. eagers = []
  1743. for name in ('native_libs.txt', 'eager_resources.txt'):
  1744. if self.has_metadata(name):
  1745. eagers.extend(self.get_metadata_lines(name))
  1746. self.eagers = eagers
  1747. return self.eagers
  1748. def _index(self):
  1749. try:
  1750. return self._dirindex
  1751. except AttributeError:
  1752. ind = {}
  1753. for path in self.zipinfo:
  1754. parts = path.split(os.sep)
  1755. while parts:
  1756. parent = os.sep.join(parts[:-1])
  1757. if parent in ind:
  1758. ind[parent].append(parts[-1])
  1759. break
  1760. else:
  1761. ind[parent] = [parts.pop()]
  1762. self._dirindex = ind
  1763. return ind
  1764. def _has(self, fspath) -> bool:
  1765. zip_path = self._zipinfo_name(fspath)
  1766. return zip_path in self.zipinfo or zip_path in self._index()
  1767. def _isdir(self, fspath) -> bool:
  1768. return self._zipinfo_name(fspath) in self._index()
  1769. def _listdir(self, fspath):
  1770. return list(self._index().get(self._zipinfo_name(fspath), ()))
  1771. def _eager_to_zip(self, resource_name: str):
  1772. return self._zipinfo_name(self._fn(self.egg_root, resource_name))
  1773. def _resource_to_zip(self, resource_name: str):
  1774. return self._zipinfo_name(self._fn(self.module_path, resource_name))
  1775. register_loader_type(zipimport.zipimporter, ZipProvider)
  1776. class FileMetadata(EmptyProvider):
  1777. """Metadata handler for standalone PKG-INFO files
  1778. Usage::
  1779. metadata = FileMetadata("/path/to/PKG-INFO")
  1780. This provider rejects all data and metadata requests except for PKG-INFO,
  1781. which is treated as existing, and will be the contents of the file at
  1782. the provided location.
  1783. """
  1784. def __init__(self, path: StrPath) -> None:
  1785. self.path = path
  1786. def _get_metadata_path(self, name):
  1787. return self.path
  1788. def has_metadata(self, name: str) -> bool:
  1789. return name == 'PKG-INFO' and os.path.isfile(self.path)
  1790. def get_metadata(self, name: str) -> str:
  1791. if name != 'PKG-INFO':
  1792. raise KeyError("No metadata except PKG-INFO is available")
  1793. with open(self.path, encoding='utf-8', errors="replace") as f:
  1794. metadata = f.read()
  1795. self._warn_on_replacement(metadata)
  1796. return metadata
  1797. def _warn_on_replacement(self, metadata) -> None:
  1798. replacement_char = '�'
  1799. if replacement_char in metadata:
  1800. tmpl = "{self.path} could not be properly decoded in UTF-8"
  1801. msg = tmpl.format(**locals())
  1802. warnings.warn(msg)
  1803. def get_metadata_lines(self, name: str) -> Iterator[str]:
  1804. return yield_lines(self.get_metadata(name))
  1805. class PathMetadata(DefaultProvider):
  1806. """Metadata provider for egg directories
  1807. Usage::
  1808. # Development eggs:
  1809. egg_info = "/path/to/PackageName.egg-info"
  1810. base_dir = os.path.dirname(egg_info)
  1811. metadata = PathMetadata(base_dir, egg_info)
  1812. dist_name = os.path.splitext(os.path.basename(egg_info))[0]
  1813. dist = Distribution(basedir, project_name=dist_name, metadata=metadata)
  1814. # Unpacked egg directories:
  1815. egg_path = "/path/to/PackageName-ver-pyver-etc.egg"
  1816. metadata = PathMetadata(egg_path, os.path.join(egg_path,'EGG-INFO'))
  1817. dist = Distribution.from_filename(egg_path, metadata=metadata)
  1818. """
  1819. def __init__(self, path: str, egg_info: str) -> None:
  1820. self.module_path = path
  1821. self.egg_info = egg_info
  1822. class EggMetadata(ZipProvider):
  1823. """Metadata provider for .egg files"""
  1824. def __init__(self, importer: zipimport.zipimporter) -> None:
  1825. """Create a metadata provider from a zipimporter"""
  1826. self.zip_pre = importer.archive + os.sep
  1827. self.loader = importer
  1828. if importer.prefix:
  1829. self.module_path = os.path.join(importer.archive, importer.prefix)
  1830. else:
  1831. self.module_path = importer.archive
  1832. self._setup_prefix()
  1833. _distribution_finders: dict[type, _DistFinderType[Any]] = _declare_state(
  1834. 'dict', '_distribution_finders', {}
  1835. )
  1836. def register_finder(
  1837. importer_type: type[_T], distribution_finder: _DistFinderType[_T]
  1838. ) -> None:
  1839. """Register `distribution_finder` to find distributions in sys.path items
  1840. `importer_type` is the type or class of a PEP 302 "Importer" (sys.path item
  1841. handler), and `distribution_finder` is a callable that, passed a path
  1842. item and the importer instance, yields ``Distribution`` instances found on
  1843. that path item. See ``pkg_resources.find_on_path`` for an example."""
  1844. _distribution_finders[importer_type] = distribution_finder
  1845. def find_distributions(path_item: str, only: bool = False) -> Iterable[Distribution]:
  1846. """Yield distributions accessible via `path_item`"""
  1847. importer = get_importer(path_item)
  1848. finder = _find_adapter(_distribution_finders, importer)
  1849. return finder(importer, path_item, only)
  1850. def find_eggs_in_zip(
  1851. importer: zipimport.zipimporter, path_item: str, only: bool = False
  1852. ) -> Iterator[Distribution]:
  1853. """
  1854. Find eggs in zip files; possibly multiple nested eggs.
  1855. """
  1856. if importer.archive.endswith('.whl'):
  1857. # wheels are not supported with this finder
  1858. # they don't have PKG-INFO metadata, and won't ever contain eggs
  1859. return
  1860. metadata = EggMetadata(importer)
  1861. if metadata.has_metadata('PKG-INFO'):
  1862. yield Distribution.from_filename(path_item, metadata=metadata)
  1863. if only:
  1864. # don't yield nested distros
  1865. return
  1866. for subitem in metadata.resource_listdir(''):
  1867. if _is_egg_path(subitem):
  1868. subpath = os.path.join(path_item, subitem)
  1869. dists = find_eggs_in_zip(zipimport.zipimporter(subpath), subpath)
  1870. yield from dists
  1871. elif subitem.lower().endswith(('.dist-info', '.egg-info')):
  1872. subpath = os.path.join(path_item, subitem)
  1873. submeta = EggMetadata(zipimport.zipimporter(subpath))
  1874. submeta.egg_info = subpath
  1875. yield Distribution.from_location(path_item, subitem, submeta)
  1876. register_finder(zipimport.zipimporter, find_eggs_in_zip)
  1877. def find_nothing(
  1878. importer: object | None, path_item: str | None, only: bool | None = False
  1879. ):
  1880. return ()
  1881. register_finder(object, find_nothing)
  1882. def find_on_path(importer: object | None, path_item, only=False):
  1883. """Yield distributions accessible on a sys.path directory"""
  1884. path_item = _normalize_cached(path_item)
  1885. if _is_unpacked_egg(path_item):
  1886. yield Distribution.from_filename(
  1887. path_item,
  1888. metadata=PathMetadata(path_item, os.path.join(path_item, 'EGG-INFO')),
  1889. )
  1890. return
  1891. entries = (os.path.join(path_item, child) for child in safe_listdir(path_item))
  1892. # scan for .egg and .egg-info in directory
  1893. for entry in sorted(entries):
  1894. fullpath = os.path.join(path_item, entry)
  1895. factory = dist_factory(path_item, entry, only)
  1896. yield from factory(fullpath)
  1897. def dist_factory(path_item, entry, only):
  1898. """Return a dist_factory for the given entry."""
  1899. lower = entry.lower()
  1900. is_egg_info = lower.endswith('.egg-info')
  1901. is_dist_info = lower.endswith('.dist-info') and os.path.isdir(
  1902. os.path.join(path_item, entry)
  1903. )
  1904. is_meta = is_egg_info or is_dist_info
  1905. return (
  1906. distributions_from_metadata
  1907. if is_meta
  1908. else find_distributions
  1909. if not only and _is_egg_path(entry)
  1910. else resolve_egg_link
  1911. if not only and lower.endswith('.egg-link')
  1912. else NoDists()
  1913. )
  1914. class NoDists:
  1915. """
  1916. >>> bool(NoDists())
  1917. False
  1918. >>> list(NoDists()('anything'))
  1919. []
  1920. """
  1921. def __bool__(self) -> Literal[False]:
  1922. return False
  1923. def __call__(self, fullpath: object):
  1924. return iter(())
  1925. def safe_listdir(path: StrOrBytesPath):
  1926. """
  1927. Attempt to list contents of path, but suppress some exceptions.
  1928. """
  1929. try:
  1930. return os.listdir(path)
  1931. except (PermissionError, NotADirectoryError):
  1932. pass
  1933. except OSError as e:
  1934. # Ignore the directory if does not exist, not a directory or
  1935. # permission denied
  1936. if e.errno not in (errno.ENOTDIR, errno.EACCES, errno.ENOENT):
  1937. raise
  1938. return ()
  1939. def distributions_from_metadata(path: str):
  1940. root = os.path.dirname(path)
  1941. if os.path.isdir(path):
  1942. if len(os.listdir(path)) == 0:
  1943. # empty metadata dir; skip
  1944. return
  1945. metadata: _MetadataType = PathMetadata(root, path)
  1946. else:
  1947. metadata = FileMetadata(path)
  1948. entry = os.path.basename(path)
  1949. yield Distribution.from_location(
  1950. root,
  1951. entry,
  1952. metadata,
  1953. precedence=DEVELOP_DIST,
  1954. )
  1955. def non_empty_lines(path):
  1956. """
  1957. Yield non-empty lines from file at path
  1958. """
  1959. for line in _read_utf8_with_fallback(path).splitlines():
  1960. line = line.strip()
  1961. if line:
  1962. yield line
  1963. def resolve_egg_link(path):
  1964. """
  1965. Given a path to an .egg-link, resolve distributions
  1966. present in the referenced path.
  1967. """
  1968. referenced_paths = non_empty_lines(path)
  1969. resolved_paths = (
  1970. os.path.join(os.path.dirname(path), ref) for ref in referenced_paths
  1971. )
  1972. dist_groups = map(find_distributions, resolved_paths)
  1973. return next(dist_groups, ())
  1974. if hasattr(pkgutil, 'ImpImporter'):
  1975. register_finder(pkgutil.ImpImporter, find_on_path)
  1976. register_finder(importlib.machinery.FileFinder, find_on_path)
  1977. _namespace_handlers: dict[type, _NSHandlerType[Any]] = _declare_state(
  1978. 'dict', '_namespace_handlers', {}
  1979. )
  1980. _namespace_packages: dict[str | None, list[str]] = _declare_state(
  1981. 'dict', '_namespace_packages', {}
  1982. )
  1983. def register_namespace_handler(
  1984. importer_type: type[_T], namespace_handler: _NSHandlerType[_T]
  1985. ) -> None:
  1986. """Register `namespace_handler` to declare namespace packages
  1987. `importer_type` is the type or class of a PEP 302 "Importer" (sys.path item
  1988. handler), and `namespace_handler` is a callable like this::
  1989. def namespace_handler(importer, path_entry, moduleName, module):
  1990. # return a path_entry to use for child packages
  1991. Namespace handlers are only called if the importer object has already
  1992. agreed that it can handle the relevant path item, and they should only
  1993. return a subpath if the module __path__ does not already contain an
  1994. equivalent subpath. For an example namespace handler, see
  1995. ``pkg_resources.file_ns_handler``.
  1996. """
  1997. _namespace_handlers[importer_type] = namespace_handler
  1998. def _handle_ns(packageName, path_item):
  1999. """Ensure that named package includes a subpath of path_item (if needed)"""
  2000. importer = get_importer(path_item)
  2001. if importer is None:
  2002. return None
  2003. # use find_spec (PEP 451) and fall-back to find_module (PEP 302)
  2004. try:
  2005. spec = importer.find_spec(packageName)
  2006. except AttributeError:
  2007. # capture warnings due to #1111
  2008. with warnings.catch_warnings():
  2009. warnings.simplefilter("ignore")
  2010. loader = importer.find_module(packageName)
  2011. else:
  2012. loader = spec.loader if spec else None
  2013. if loader is None:
  2014. return None
  2015. module = sys.modules.get(packageName)
  2016. if module is None:
  2017. module = sys.modules[packageName] = types.ModuleType(packageName)
  2018. module.__path__ = []
  2019. _set_parent_ns(packageName)
  2020. elif not hasattr(module, '__path__'):
  2021. raise TypeError("Not a package:", packageName)
  2022. handler = _find_adapter(_namespace_handlers, importer)
  2023. subpath = handler(importer, path_item, packageName, module)
  2024. if subpath is not None:
  2025. path = module.__path__
  2026. path.append(subpath)
  2027. importlib.import_module(packageName)
  2028. _rebuild_mod_path(path, packageName, module)
  2029. return subpath
  2030. def _rebuild_mod_path(orig_path, package_name, module: types.ModuleType) -> None:
  2031. """
  2032. Rebuild module.__path__ ensuring that all entries are ordered
  2033. corresponding to their sys.path order
  2034. """
  2035. sys_path = [_normalize_cached(p) for p in sys.path]
  2036. def safe_sys_path_index(entry):
  2037. """
  2038. Workaround for #520 and #513.
  2039. """
  2040. try:
  2041. return sys_path.index(entry)
  2042. except ValueError:
  2043. return float('inf')
  2044. def position_in_sys_path(path):
  2045. """
  2046. Return the ordinal of the path based on its position in sys.path
  2047. """
  2048. path_parts = path.split(os.sep)
  2049. module_parts = package_name.count('.') + 1
  2050. parts = path_parts[:-module_parts]
  2051. return safe_sys_path_index(_normalize_cached(os.sep.join(parts)))
  2052. new_path = sorted(orig_path, key=position_in_sys_path)
  2053. new_path = [_normalize_cached(p) for p in new_path]
  2054. if isinstance(module.__path__, list):
  2055. module.__path__[:] = new_path
  2056. else:
  2057. module.__path__ = new_path
  2058. def declare_namespace(packageName: str) -> None:
  2059. """Declare that package 'packageName' is a namespace package"""
  2060. msg = (
  2061. f"Deprecated call to `pkg_resources.declare_namespace({packageName!r})`.\n"
  2062. "Implementing implicit namespace packages (as specified in PEP 420) "
  2063. "is preferred to `pkg_resources.declare_namespace`. "
  2064. "See https://setuptools.pypa.io/en/latest/references/"
  2065. "keywords.html#keyword-namespace-packages"
  2066. )
  2067. warnings.warn(msg, DeprecationWarning, stacklevel=2)
  2068. _imp.acquire_lock()
  2069. try:
  2070. if packageName in _namespace_packages:
  2071. return
  2072. path: MutableSequence[str] = sys.path
  2073. parent, _, _ = packageName.rpartition('.')
  2074. if parent:
  2075. declare_namespace(parent)
  2076. if parent not in _namespace_packages:
  2077. __import__(parent)
  2078. try:
  2079. path = sys.modules[parent].__path__
  2080. except AttributeError as e:
  2081. raise TypeError("Not a package:", parent) from e
  2082. # Track what packages are namespaces, so when new path items are added,
  2083. # they can be updated
  2084. _namespace_packages.setdefault(parent or None, []).append(packageName)
  2085. _namespace_packages.setdefault(packageName, [])
  2086. for path_item in path:
  2087. # Ensure all the parent's path items are reflected in the child,
  2088. # if they apply
  2089. _handle_ns(packageName, path_item)
  2090. finally:
  2091. _imp.release_lock()
  2092. def fixup_namespace_packages(path_item: str, parent: str | None = None) -> None:
  2093. """Ensure that previously-declared namespace packages include path_item"""
  2094. _imp.acquire_lock()
  2095. try:
  2096. for package in _namespace_packages.get(parent, ()):
  2097. subpath = _handle_ns(package, path_item)
  2098. if subpath:
  2099. fixup_namespace_packages(subpath, package)
  2100. finally:
  2101. _imp.release_lock()
  2102. def file_ns_handler(
  2103. importer: object,
  2104. path_item: StrPath,
  2105. packageName: str,
  2106. module: types.ModuleType,
  2107. ):
  2108. """Compute an ns-package subpath for a filesystem or zipfile importer"""
  2109. subpath = os.path.join(path_item, packageName.split('.')[-1])
  2110. normalized = _normalize_cached(subpath)
  2111. for item in module.__path__:
  2112. if _normalize_cached(item) == normalized:
  2113. break
  2114. else:
  2115. # Only return the path if it's not already there
  2116. return subpath
  2117. if hasattr(pkgutil, 'ImpImporter'):
  2118. register_namespace_handler(pkgutil.ImpImporter, file_ns_handler)
  2119. register_namespace_handler(zipimport.zipimporter, file_ns_handler)
  2120. register_namespace_handler(importlib.machinery.FileFinder, file_ns_handler)
  2121. def null_ns_handler(
  2122. importer: object,
  2123. path_item: str | None,
  2124. packageName: str | None,
  2125. module: _ModuleLike | None,
  2126. ) -> None:
  2127. return None
  2128. register_namespace_handler(object, null_ns_handler)
  2129. @overload
  2130. def normalize_path(filename: StrPath) -> str: ...
  2131. @overload
  2132. def normalize_path(filename: BytesPath) -> bytes: ...
  2133. def normalize_path(filename: StrOrBytesPath) -> str | bytes:
  2134. """Normalize a file/dir name for comparison purposes"""
  2135. return os.path.normcase(os.path.realpath(os.path.normpath(_cygwin_patch(filename))))
  2136. def _cygwin_patch(filename: StrOrBytesPath): # pragma: nocover
  2137. """
  2138. Contrary to POSIX 2008, on Cygwin, getcwd (3) contains
  2139. symlink components. Using
  2140. os.path.abspath() works around this limitation. A fix in os.getcwd()
  2141. would probably better, in Cygwin even more so, except
  2142. that this seems to be by design...
  2143. """
  2144. return os.path.abspath(filename) if sys.platform == 'cygwin' else filename
  2145. if TYPE_CHECKING:
  2146. # https://github.com/python/mypy/issues/16261
  2147. # https://github.com/python/typeshed/issues/6347
  2148. @overload
  2149. def _normalize_cached(filename: StrPath) -> str: ...
  2150. @overload
  2151. def _normalize_cached(filename: BytesPath) -> bytes: ...
  2152. def _normalize_cached(filename: StrOrBytesPath) -> str | bytes: ...
  2153. else:
  2154. @functools.cache
  2155. def _normalize_cached(filename):
  2156. return normalize_path(filename)
  2157. def _is_egg_path(path):
  2158. """
  2159. Determine if given path appears to be an egg.
  2160. """
  2161. return _is_zip_egg(path) or _is_unpacked_egg(path)
  2162. def _is_zip_egg(path):
  2163. return (
  2164. path.lower().endswith('.egg')
  2165. and os.path.isfile(path)
  2166. and zipfile.is_zipfile(path)
  2167. )
  2168. def _is_unpacked_egg(path):
  2169. """
  2170. Determine if given path appears to be an unpacked egg.
  2171. """
  2172. return path.lower().endswith('.egg') and os.path.isfile(
  2173. os.path.join(path, 'EGG-INFO', 'PKG-INFO')
  2174. )
  2175. def _set_parent_ns(packageName) -> None:
  2176. parts = packageName.split('.')
  2177. name = parts.pop()
  2178. if parts:
  2179. parent = '.'.join(parts)
  2180. setattr(sys.modules[parent], name, sys.modules[packageName])
  2181. MODULE = re.compile(r"\w+(\.\w+)*$").match
  2182. EGG_NAME = re.compile(
  2183. r"""
  2184. (?P<name>[^-]+) (
  2185. -(?P<ver>[^-]+) (
  2186. -py(?P<pyver>[^-]+) (
  2187. -(?P<plat>.+)
  2188. )?
  2189. )?
  2190. )?
  2191. """,
  2192. re.VERBOSE | re.IGNORECASE,
  2193. ).match
  2194. class EntryPoint:
  2195. """Object representing an advertised importable object"""
  2196. def __init__(
  2197. self,
  2198. name: str,
  2199. module_name: str,
  2200. attrs: Iterable[str] = (),
  2201. extras: Iterable[str] = (),
  2202. dist: Distribution | None = None,
  2203. ) -> None:
  2204. if not MODULE(module_name):
  2205. raise ValueError("Invalid module name", module_name)
  2206. self.name = name
  2207. self.module_name = module_name
  2208. self.attrs = tuple(attrs)
  2209. self.extras = tuple(extras)
  2210. self.dist = dist
  2211. def __str__(self) -> str:
  2212. s = f"{self.name} = {self.module_name}"
  2213. if self.attrs:
  2214. s += ':' + '.'.join(self.attrs)
  2215. if self.extras:
  2216. extras = ','.join(self.extras)
  2217. s += f' [{extras}]'
  2218. return s
  2219. def __repr__(self) -> str:
  2220. return f"EntryPoint.parse({str(self)!r})"
  2221. @overload
  2222. def load(
  2223. self,
  2224. require: Literal[True] = True,
  2225. env: Environment | None = None,
  2226. installer: _InstallerType | None = None,
  2227. ) -> _ResolvedEntryPoint: ...
  2228. @overload
  2229. def load(
  2230. self,
  2231. require: Literal[False],
  2232. *args: Any,
  2233. **kwargs: Any,
  2234. ) -> _ResolvedEntryPoint: ...
  2235. def load(
  2236. self,
  2237. require: bool = True,
  2238. *args: Environment | _InstallerType | None,
  2239. **kwargs: Environment | _InstallerType | None,
  2240. ) -> _ResolvedEntryPoint:
  2241. """
  2242. Require packages for this EntryPoint, then resolve it.
  2243. """
  2244. if not require or args or kwargs:
  2245. warnings.warn(
  2246. "Parameters to load are deprecated. Call .resolve and "
  2247. ".require separately.",
  2248. PkgResourcesDeprecationWarning,
  2249. stacklevel=2,
  2250. )
  2251. if require:
  2252. # We could pass `env` and `installer` directly,
  2253. # but keeping `*args` and `**kwargs` for backwards compatibility
  2254. self.require(*args, **kwargs) # type: ignore[arg-type]
  2255. return self.resolve()
  2256. def resolve(self) -> _ResolvedEntryPoint:
  2257. """
  2258. Resolve the entry point from its module and attrs.
  2259. """
  2260. module = __import__(self.module_name, fromlist=['__name__'], level=0)
  2261. try:
  2262. return functools.reduce(getattr, self.attrs, module)
  2263. except AttributeError as exc:
  2264. raise ImportError(str(exc)) from exc
  2265. def require(
  2266. self,
  2267. env: Environment | None = None,
  2268. installer: _InstallerType | None = None,
  2269. ) -> None:
  2270. if not self.dist:
  2271. error_cls = UnknownExtra if self.extras else AttributeError
  2272. raise error_cls("Can't require() without a distribution", self)
  2273. # Get the requirements for this entry point with all its extras and
  2274. # then resolve them. We have to pass `extras` along when resolving so
  2275. # that the working set knows what extras we want. Otherwise, for
  2276. # dist-info distributions, the working set will assume that the
  2277. # requirements for that extra are purely optional and skip over them.
  2278. reqs = self.dist.requires(self.extras)
  2279. items = working_set.resolve(reqs, env, installer, extras=self.extras)
  2280. list(map(working_set.add, items))
  2281. pattern = re.compile(
  2282. r'\s*'
  2283. r'(?P<name>.+?)\s*'
  2284. r'=\s*'
  2285. r'(?P<module>[\w.]+)\s*'
  2286. r'(:\s*(?P<attr>[\w.]+))?\s*'
  2287. r'(?P<extras>\[.*\])?\s*$'
  2288. )
  2289. @classmethod
  2290. def parse(cls, src: str, dist: Distribution | None = None) -> Self:
  2291. """Parse a single entry point from string `src`
  2292. Entry point syntax follows the form::
  2293. name = some.module:some.attr [extra1, extra2]
  2294. The entry name and module name are required, but the ``:attrs`` and
  2295. ``[extras]`` parts are optional
  2296. """
  2297. m = cls.pattern.match(src)
  2298. if not m:
  2299. msg = "EntryPoint must be in 'name=module:attrs [extras]' format"
  2300. raise ValueError(msg, src)
  2301. res = m.groupdict()
  2302. extras = cls._parse_extras(res['extras'])
  2303. attrs = res['attr'].split('.') if res['attr'] else ()
  2304. return cls(res['name'], res['module'], attrs, extras, dist)
  2305. @classmethod
  2306. def _parse_extras(cls, extras_spec):
  2307. if not extras_spec:
  2308. return ()
  2309. req = Requirement.parse('x' + extras_spec)
  2310. if req.specs:
  2311. raise ValueError
  2312. return req.extras
  2313. @classmethod
  2314. def parse_group(
  2315. cls,
  2316. group: str,
  2317. lines: _NestedStr,
  2318. dist: Distribution | None = None,
  2319. ) -> dict[str, Self]:
  2320. """Parse an entry point group"""
  2321. if not MODULE(group):
  2322. raise ValueError("Invalid group name", group)
  2323. this: dict[str, Self] = {}
  2324. for line in yield_lines(lines):
  2325. ep = cls.parse(line, dist)
  2326. if ep.name in this:
  2327. raise ValueError("Duplicate entry point", group, ep.name)
  2328. this[ep.name] = ep
  2329. return this
  2330. @classmethod
  2331. def parse_map(
  2332. cls,
  2333. data: str | Iterable[str] | dict[str, str | Iterable[str]],
  2334. dist: Distribution | None = None,
  2335. ) -> dict[str, dict[str, Self]]:
  2336. """Parse a map of entry point groups"""
  2337. _data: Iterable[tuple[str | None, str | Iterable[str]]]
  2338. if isinstance(data, dict):
  2339. _data = data.items()
  2340. else:
  2341. _data = split_sections(data)
  2342. maps: dict[str, dict[str, Self]] = {}
  2343. for group, lines in _data:
  2344. if group is None:
  2345. if not lines:
  2346. continue
  2347. raise ValueError("Entry points must be listed in groups")
  2348. group = group.strip()
  2349. if group in maps:
  2350. raise ValueError("Duplicate group name", group)
  2351. maps[group] = cls.parse_group(group, lines, dist)
  2352. return maps
  2353. def _version_from_file(lines):
  2354. """
  2355. Given an iterable of lines from a Metadata file, return
  2356. the value of the Version field, if present, or None otherwise.
  2357. """
  2358. def is_version_line(line):
  2359. return line.lower().startswith('version:')
  2360. version_lines = filter(is_version_line, lines)
  2361. line = next(iter(version_lines), '')
  2362. _, _, value = line.partition(':')
  2363. return safe_version(value.strip()) or None
  2364. class Distribution:
  2365. """Wrap an actual or potential sys.path entry w/metadata"""
  2366. PKG_INFO = 'PKG-INFO'
  2367. def __init__(
  2368. self,
  2369. location: str | None = None,
  2370. metadata: _MetadataType = None,
  2371. project_name: str | None = None,
  2372. version: str | None = None,
  2373. py_version: str | None = PY_MAJOR,
  2374. platform: str | None = None,
  2375. precedence: int = EGG_DIST,
  2376. ) -> None:
  2377. self.project_name = safe_name(project_name or 'Unknown')
  2378. if version is not None:
  2379. self._version = safe_version(version)
  2380. self.py_version = py_version
  2381. self.platform = platform
  2382. self.location = location
  2383. self.precedence = precedence
  2384. self._provider = metadata or empty_provider
  2385. @classmethod
  2386. def from_location(
  2387. cls,
  2388. location: str,
  2389. basename: StrPath,
  2390. metadata: _MetadataType = None,
  2391. **kw: int, # We could set `precedence` explicitly, but keeping this as `**kw` for full backwards and subclassing compatibility
  2392. ) -> Distribution:
  2393. project_name, version, py_version, platform = [None] * 4
  2394. basename, ext = os.path.splitext(basename)
  2395. if ext.lower() in _distributionImpl:
  2396. cls = _distributionImpl[ext.lower()]
  2397. match = EGG_NAME(basename)
  2398. if match:
  2399. project_name, version, py_version, platform = match.group(
  2400. 'name', 'ver', 'pyver', 'plat'
  2401. )
  2402. return cls(
  2403. location,
  2404. metadata,
  2405. project_name=project_name,
  2406. version=version,
  2407. py_version=py_version,
  2408. platform=platform,
  2409. **kw,
  2410. )._reload_version()
  2411. def _reload_version(self):
  2412. return self
  2413. @property
  2414. def hashcmp(self):
  2415. return (
  2416. self._forgiving_parsed_version,
  2417. self.precedence,
  2418. self.key,
  2419. self.location,
  2420. self.py_version or '',
  2421. self.platform or '',
  2422. )
  2423. def __hash__(self) -> int:
  2424. return hash(self.hashcmp)
  2425. def __lt__(self, other: Distribution) -> bool:
  2426. return self.hashcmp < other.hashcmp
  2427. def __le__(self, other: Distribution) -> bool:
  2428. return self.hashcmp <= other.hashcmp
  2429. def __gt__(self, other: Distribution) -> bool:
  2430. return self.hashcmp > other.hashcmp
  2431. def __ge__(self, other: Distribution) -> bool:
  2432. return self.hashcmp >= other.hashcmp
  2433. def __eq__(self, other: object) -> bool:
  2434. if not isinstance(other, self.__class__):
  2435. # It's not a Distribution, so they are not equal
  2436. return False
  2437. return self.hashcmp == other.hashcmp
  2438. def __ne__(self, other: object) -> bool:
  2439. return not self == other
  2440. # These properties have to be lazy so that we don't have to load any
  2441. # metadata until/unless it's actually needed. (i.e., some distributions
  2442. # may not know their name or version without loading PKG-INFO)
  2443. @property
  2444. def key(self):
  2445. try:
  2446. return self._key
  2447. except AttributeError:
  2448. self._key = key = self.project_name.lower()
  2449. return key
  2450. @property
  2451. def parsed_version(self):
  2452. if not hasattr(self, "_parsed_version"):
  2453. try:
  2454. self._parsed_version = parse_version(self.version)
  2455. except packaging.version.InvalidVersion as ex:
  2456. info = f"(package: {self.project_name})"
  2457. if hasattr(ex, "add_note"):
  2458. ex.add_note(info) # PEP 678
  2459. raise
  2460. raise packaging.version.InvalidVersion(f"{str(ex)} {info}") from None
  2461. return self._parsed_version
  2462. @property
  2463. def _forgiving_parsed_version(self):
  2464. try:
  2465. return self.parsed_version
  2466. except packaging.version.InvalidVersion as ex:
  2467. self._parsed_version = parse_version(_forgiving_version(self.version))
  2468. notes = "\n".join(getattr(ex, "__notes__", [])) # PEP 678
  2469. msg = f"""!!\n\n
  2470. *************************************************************************
  2471. {str(ex)}\n{notes}
  2472. This is a long overdue deprecation.
  2473. For the time being, `pkg_resources` will use `{self._parsed_version}`
  2474. as a replacement to avoid breaking existing environments,
  2475. but no future compatibility is guaranteed.
  2476. If you maintain package {self.project_name} you should implement
  2477. the relevant changes to adequate the project to PEP 440 immediately.
  2478. *************************************************************************
  2479. \n\n!!
  2480. """
  2481. warnings.warn(msg, DeprecationWarning)
  2482. return self._parsed_version
  2483. @property
  2484. def version(self):
  2485. try:
  2486. return self._version
  2487. except AttributeError as e:
  2488. version = self._get_version()
  2489. if version is None:
  2490. path = self._get_metadata_path_for_display(self.PKG_INFO)
  2491. msg = f"Missing 'Version:' header and/or {self.PKG_INFO} file at path: {path}"
  2492. raise ValueError(msg, self) from e
  2493. return version
  2494. @property
  2495. def _dep_map(self):
  2496. """
  2497. A map of extra to its list of (direct) requirements
  2498. for this distribution, including the null extra.
  2499. """
  2500. try:
  2501. return self.__dep_map
  2502. except AttributeError:
  2503. self.__dep_map = self._filter_extras(self._build_dep_map())
  2504. return self.__dep_map
  2505. @staticmethod
  2506. def _filter_extras(
  2507. dm: dict[str | None, list[Requirement]],
  2508. ) -> dict[str | None, list[Requirement]]:
  2509. """
  2510. Given a mapping of extras to dependencies, strip off
  2511. environment markers and filter out any dependencies
  2512. not matching the markers.
  2513. """
  2514. for extra in list(filter(None, dm)):
  2515. new_extra: str | None = extra
  2516. reqs = dm.pop(extra)
  2517. new_extra, _, marker = extra.partition(':')
  2518. fails_marker = marker and (
  2519. invalid_marker(marker) or not evaluate_marker(marker)
  2520. )
  2521. if fails_marker:
  2522. reqs = []
  2523. new_extra = safe_extra(new_extra) or None
  2524. dm.setdefault(new_extra, []).extend(reqs)
  2525. return dm
  2526. def _build_dep_map(self):
  2527. dm = {}
  2528. for name in 'requires.txt', 'depends.txt':
  2529. for extra, reqs in split_sections(self._get_metadata(name)):
  2530. dm.setdefault(extra, []).extend(parse_requirements(reqs))
  2531. return dm
  2532. def requires(self, extras: Iterable[str] = ()) -> list[Requirement]:
  2533. """List of Requirements needed for this distro if `extras` are used"""
  2534. dm = self._dep_map
  2535. deps: list[Requirement] = []
  2536. deps.extend(dm.get(None, ()))
  2537. for ext in extras:
  2538. try:
  2539. deps.extend(dm[safe_extra(ext)])
  2540. except KeyError as e:
  2541. raise UnknownExtra(f"{self} has no such extra feature {ext!r}") from e
  2542. return deps
  2543. def _get_metadata_path_for_display(self, name):
  2544. """
  2545. Return the path to the given metadata file, if available.
  2546. """
  2547. try:
  2548. # We need to access _get_metadata_path() on the provider object
  2549. # directly rather than through this class's __getattr__()
  2550. # since _get_metadata_path() is marked private.
  2551. path = self._provider._get_metadata_path(name)
  2552. # Handle exceptions e.g. in case the distribution's metadata
  2553. # provider doesn't support _get_metadata_path().
  2554. except Exception:
  2555. return '[could not detect]'
  2556. return path
  2557. def _get_metadata(self, name):
  2558. if self.has_metadata(name):
  2559. yield from self.get_metadata_lines(name)
  2560. def _get_version(self):
  2561. lines = self._get_metadata(self.PKG_INFO)
  2562. return _version_from_file(lines)
  2563. def activate(self, path: list[str] | None = None, replace: bool = False) -> None:
  2564. """Ensure distribution is importable on `path` (default=sys.path)"""
  2565. if path is None:
  2566. path = sys.path
  2567. self.insert_on(path, replace=replace)
  2568. if path is sys.path and self.location is not None:
  2569. fixup_namespace_packages(self.location)
  2570. for pkg in self._get_metadata('namespace_packages.txt'):
  2571. if pkg in sys.modules:
  2572. declare_namespace(pkg)
  2573. def egg_name(self):
  2574. """Return what this distribution's standard .egg filename should be"""
  2575. filename = f"{to_filename(self.project_name)}-{to_filename(self.version)}-py{self.py_version or PY_MAJOR}"
  2576. if self.platform:
  2577. filename += '-' + self.platform
  2578. return filename
  2579. def __repr__(self) -> str:
  2580. if self.location:
  2581. return f"{self} ({self.location})"
  2582. else:
  2583. return str(self)
  2584. def __str__(self) -> str:
  2585. try:
  2586. version = getattr(self, 'version', None)
  2587. except ValueError:
  2588. version = None
  2589. version = version or "[unknown version]"
  2590. return f"{self.project_name} {version}"
  2591. def __getattr__(self, attr: str):
  2592. """Delegate all unrecognized public attributes to .metadata provider"""
  2593. if attr.startswith('_'):
  2594. raise AttributeError(attr)
  2595. return getattr(self._provider, attr)
  2596. def __dir__(self):
  2597. return list(
  2598. set(super().__dir__())
  2599. | set(attr for attr in self._provider.__dir__() if not attr.startswith('_'))
  2600. )
  2601. @classmethod
  2602. def from_filename(
  2603. cls,
  2604. filename: StrPath,
  2605. metadata: _MetadataType = None,
  2606. **kw: int, # We could set `precedence` explicitly, but keeping this as `**kw` for full backwards and subclassing compatibility
  2607. ) -> Distribution:
  2608. return cls.from_location(
  2609. _normalize_cached(filename), os.path.basename(filename), metadata, **kw
  2610. )
  2611. def as_requirement(self):
  2612. """Return a ``Requirement`` that matches this distribution exactly"""
  2613. if isinstance(self.parsed_version, packaging.version.Version):
  2614. spec = f"{self.project_name}=={self.parsed_version}"
  2615. else:
  2616. spec = f"{self.project_name}==={self.parsed_version}"
  2617. return Requirement.parse(spec)
  2618. def load_entry_point(self, group: str, name: str) -> _ResolvedEntryPoint:
  2619. """Return the `name` entry point of `group` or raise ImportError"""
  2620. ep = self.get_entry_info(group, name)
  2621. if ep is None:
  2622. raise ImportError(f"Entry point {(group, name)!r} not found")
  2623. return ep.load()
  2624. @overload
  2625. def get_entry_map(self, group: None = None) -> dict[str, dict[str, EntryPoint]]: ...
  2626. @overload
  2627. def get_entry_map(self, group: str) -> dict[str, EntryPoint]: ...
  2628. def get_entry_map(self, group: str | None = None):
  2629. """Return the entry point map for `group`, or the full entry map"""
  2630. if not hasattr(self, "_ep_map"):
  2631. self._ep_map = EntryPoint.parse_map(
  2632. self._get_metadata('entry_points.txt'), self
  2633. )
  2634. if group is not None:
  2635. return self._ep_map.get(group, {})
  2636. return self._ep_map
  2637. def get_entry_info(self, group: str, name: str) -> EntryPoint | None:
  2638. """Return the EntryPoint object for `group`+`name`, or ``None``"""
  2639. return self.get_entry_map(group).get(name)
  2640. # FIXME: 'Distribution.insert_on' is too complex (13)
  2641. def insert_on( # noqa: C901
  2642. self,
  2643. path: list[str],
  2644. loc=None,
  2645. replace: bool = False,
  2646. ) -> None:
  2647. """Ensure self.location is on path
  2648. If replace=False (default):
  2649. - If location is already in path anywhere, do nothing.
  2650. - Else:
  2651. - If it's an egg and its parent directory is on path,
  2652. insert just ahead of the parent.
  2653. - Else: add to the end of path.
  2654. If replace=True:
  2655. - If location is already on path anywhere (not eggs)
  2656. or higher priority than its parent (eggs)
  2657. do nothing.
  2658. - Else:
  2659. - If it's an egg and its parent directory is on path,
  2660. insert just ahead of the parent,
  2661. removing any lower-priority entries.
  2662. - Else: add it to the front of path.
  2663. """
  2664. loc = loc or self.location
  2665. if not loc:
  2666. return
  2667. nloc = _normalize_cached(loc)
  2668. bdir = os.path.dirname(nloc)
  2669. npath = [(p and _normalize_cached(p) or p) for p in path]
  2670. for p, item in enumerate(npath):
  2671. if item == nloc:
  2672. if replace:
  2673. break
  2674. else:
  2675. # don't modify path (even removing duplicates) if
  2676. # found and not replace
  2677. return
  2678. elif item == bdir and self.precedence == EGG_DIST:
  2679. # if it's an .egg, give it precedence over its directory
  2680. # UNLESS it's already been added to sys.path and replace=False
  2681. if (not replace) and nloc in npath[p:]:
  2682. return
  2683. if path is sys.path:
  2684. self.check_version_conflict()
  2685. path.insert(p, loc)
  2686. npath.insert(p, nloc)
  2687. break
  2688. else:
  2689. if path is sys.path:
  2690. self.check_version_conflict()
  2691. if replace:
  2692. path.insert(0, loc)
  2693. else:
  2694. path.append(loc)
  2695. return
  2696. # p is the spot where we found or inserted loc; now remove duplicates
  2697. while True:
  2698. try:
  2699. np = npath.index(nloc, p + 1)
  2700. except ValueError:
  2701. break
  2702. else:
  2703. del npath[np], path[np]
  2704. # ha!
  2705. p = np
  2706. return
  2707. def check_version_conflict(self):
  2708. if self.key == 'setuptools':
  2709. # ignore the inevitable setuptools self-conflicts :(
  2710. return
  2711. nsp = dict.fromkeys(self._get_metadata('namespace_packages.txt'))
  2712. loc = normalize_path(self.location)
  2713. for modname in self._get_metadata('top_level.txt'):
  2714. if (
  2715. modname not in sys.modules
  2716. or modname in nsp
  2717. or modname in _namespace_packages
  2718. ):
  2719. continue
  2720. if modname in ('pkg_resources', 'setuptools', 'site'):
  2721. continue
  2722. fn = getattr(sys.modules[modname], '__file__', None)
  2723. if fn and (
  2724. normalize_path(fn).startswith(loc) or fn.startswith(self.location)
  2725. ):
  2726. continue
  2727. issue_warning(
  2728. f"Module {modname} was already imported from {fn}, "
  2729. f"but {self.location} is being added to sys.path",
  2730. )
  2731. def has_version(self) -> bool:
  2732. try:
  2733. self.version
  2734. except ValueError:
  2735. issue_warning("Unbuilt egg for " + repr(self))
  2736. return False
  2737. except SystemError:
  2738. # TODO: remove this except clause when python/cpython#103632 is fixed.
  2739. return False
  2740. return True
  2741. def clone(self, **kw: str | int | IResourceProvider | None) -> Self:
  2742. """Copy this distribution, substituting in any changed keyword args"""
  2743. names = 'project_name version py_version platform location precedence'
  2744. for attr in names.split():
  2745. kw.setdefault(attr, getattr(self, attr, None))
  2746. kw.setdefault('metadata', self._provider)
  2747. # Unsafely unpacking. But keeping **kw for backwards and subclassing compatibility
  2748. return self.__class__(**kw) # type:ignore[arg-type]
  2749. @property
  2750. def extras(self):
  2751. return [dep for dep in self._dep_map if dep]
  2752. class EggInfoDistribution(Distribution):
  2753. def _reload_version(self):
  2754. """
  2755. Packages installed by distutils (e.g. numpy or scipy),
  2756. which uses an old safe_version, and so
  2757. their version numbers can get mangled when
  2758. converted to filenames (e.g., 1.11.0.dev0+2329eae to
  2759. 1.11.0.dev0_2329eae). These distributions will not be
  2760. parsed properly
  2761. downstream by Distribution and safe_version, so
  2762. take an extra step and try to get the version number from
  2763. the metadata file itself instead of the filename.
  2764. """
  2765. md_version = self._get_version()
  2766. if md_version:
  2767. self._version = md_version
  2768. return self
  2769. class DistInfoDistribution(Distribution):
  2770. """
  2771. Wrap an actual or potential sys.path entry
  2772. w/metadata, .dist-info style.
  2773. """
  2774. PKG_INFO = 'METADATA'
  2775. EQEQ = re.compile(r"([\(,])\s*(\d.*?)\s*([,\)])")
  2776. @property
  2777. def _parsed_pkg_info(self):
  2778. """Parse and cache metadata"""
  2779. try:
  2780. return self._pkg_info
  2781. except AttributeError:
  2782. metadata = self.get_metadata(self.PKG_INFO)
  2783. self._pkg_info = email.parser.Parser().parsestr(metadata)
  2784. return self._pkg_info
  2785. @property
  2786. def _dep_map(self):
  2787. try:
  2788. return self.__dep_map
  2789. except AttributeError:
  2790. self.__dep_map = self._compute_dependencies()
  2791. return self.__dep_map
  2792. def _compute_dependencies(self) -> dict[str | None, list[Requirement]]:
  2793. """Recompute this distribution's dependencies."""
  2794. self.__dep_map: dict[str | None, list[Requirement]] = {None: []}
  2795. reqs: list[Requirement] = []
  2796. # Including any condition expressions
  2797. for req in self._parsed_pkg_info.get_all('Requires-Dist') or []:
  2798. reqs.extend(parse_requirements(req))
  2799. def reqs_for_extra(extra):
  2800. for req in reqs:
  2801. if not req.marker or req.marker.evaluate({'extra': extra}):
  2802. yield req
  2803. common = types.MappingProxyType(dict.fromkeys(reqs_for_extra(None)))
  2804. self.__dep_map[None].extend(common)
  2805. for extra in self._parsed_pkg_info.get_all('Provides-Extra') or []:
  2806. s_extra = safe_extra(extra.strip())
  2807. self.__dep_map[s_extra] = [
  2808. r for r in reqs_for_extra(extra) if r not in common
  2809. ]
  2810. return self.__dep_map
  2811. _distributionImpl = {
  2812. '.egg': Distribution,
  2813. '.egg-info': EggInfoDistribution,
  2814. '.dist-info': DistInfoDistribution,
  2815. }
  2816. def issue_warning(*args, **kw):
  2817. level = 1
  2818. g = globals()
  2819. try:
  2820. # find the first stack frame that is *not* code in
  2821. # the pkg_resources module, to use for the warning
  2822. while sys._getframe(level).f_globals is g:
  2823. level += 1
  2824. except ValueError:
  2825. pass
  2826. warnings.warn(stacklevel=level + 1, *args, **kw)
  2827. def parse_requirements(strs: _NestedStr) -> map[Requirement]:
  2828. """
  2829. Yield ``Requirement`` objects for each specification in `strs`.
  2830. `strs` must be a string, or a (possibly-nested) iterable thereof.
  2831. """
  2832. return map(Requirement, join_continuation(map(drop_comment, yield_lines(strs))))
  2833. class RequirementParseError(packaging.requirements.InvalidRequirement):
  2834. "Compatibility wrapper for InvalidRequirement"
  2835. class Requirement(packaging.requirements.Requirement):
  2836. # prefer variable length tuple to set (as found in
  2837. # packaging.requirements.Requirement)
  2838. extras: tuple[str, ...] # type: ignore[assignment]
  2839. def __init__(self, requirement_string: str) -> None:
  2840. """DO NOT CALL THIS UNDOCUMENTED METHOD; use Requirement.parse()!"""
  2841. super().__init__(requirement_string)
  2842. self.unsafe_name = self.name
  2843. project_name = safe_name(self.name)
  2844. self.project_name, self.key = project_name, project_name.lower()
  2845. self.specs = [(spec.operator, spec.version) for spec in self.specifier]
  2846. self.extras = tuple(map(safe_extra, self.extras))
  2847. self.hashCmp = (
  2848. self.key,
  2849. self.url,
  2850. self.specifier,
  2851. frozenset(self.extras),
  2852. str(self.marker) if self.marker else None,
  2853. )
  2854. self.__hash = hash(self.hashCmp)
  2855. def __eq__(self, other: object) -> bool:
  2856. return isinstance(other, Requirement) and self.hashCmp == other.hashCmp
  2857. def __ne__(self, other: object) -> bool:
  2858. return not self == other
  2859. def __contains__(
  2860. self, item: Distribution | packaging.specifiers.UnparsedVersion
  2861. ) -> bool:
  2862. if isinstance(item, Distribution):
  2863. if item.key != self.key:
  2864. return False
  2865. version = item.version
  2866. else:
  2867. version = item
  2868. # Allow prereleases always in order to match the previous behavior of
  2869. # this method. In the future this should be smarter and follow PEP 440
  2870. # more accurately.
  2871. return self.specifier.contains(
  2872. version,
  2873. prereleases=True,
  2874. )
  2875. def __hash__(self) -> int:
  2876. return self.__hash
  2877. def __repr__(self) -> str:
  2878. return f"Requirement.parse({str(self)!r})"
  2879. @staticmethod
  2880. def parse(s: str | Iterable[str]) -> Requirement:
  2881. (req,) = parse_requirements(s)
  2882. return req
  2883. def _always_object(classes):
  2884. """
  2885. Ensure object appears in the mro even
  2886. for old-style classes.
  2887. """
  2888. if object not in classes:
  2889. return classes + (object,)
  2890. return classes
  2891. def _find_adapter(registry: Mapping[type, _AdapterT], ob: object) -> _AdapterT:
  2892. """Return an adapter factory for `ob` from `registry`"""
  2893. types = _always_object(inspect.getmro(getattr(ob, '__class__', type(ob))))
  2894. for t in types:
  2895. if t in registry:
  2896. return registry[t]
  2897. # _find_adapter would previously return None, and immediately be called.
  2898. # So we're raising a TypeError to keep backward compatibility if anyone depended on that behaviour.
  2899. raise TypeError(f"Could not find adapter for {registry} and {ob}")
  2900. def ensure_directory(path: StrOrBytesPath) -> None:
  2901. """Ensure that the parent directory of `path` exists"""
  2902. dirname = os.path.dirname(path)
  2903. os.makedirs(dirname, exist_ok=True)
  2904. def _bypass_ensure_directory(path) -> None:
  2905. """Sandbox-bypassing version of ensure_directory()"""
  2906. if not WRITE_SUPPORT:
  2907. raise OSError('"os.mkdir" not supported on this platform.')
  2908. dirname, filename = split(path)
  2909. if dirname and filename and not isdir(dirname):
  2910. _bypass_ensure_directory(dirname)
  2911. try:
  2912. mkdir(dirname, 0o755)
  2913. except FileExistsError:
  2914. pass
  2915. def split_sections(s: _NestedStr) -> Iterator[tuple[str | None, list[str]]]:
  2916. """Split a string or iterable thereof into (section, content) pairs
  2917. Each ``section`` is a stripped version of the section header ("[section]")
  2918. and each ``content`` is a list of stripped lines excluding blank lines and
  2919. comment-only lines. If there are any such lines before the first section
  2920. header, they're returned in a first ``section`` of ``None``.
  2921. """
  2922. section = None
  2923. content: list[str] = []
  2924. for line in yield_lines(s):
  2925. if line.startswith("["):
  2926. if line.endswith("]"):
  2927. if section or content:
  2928. yield section, content
  2929. section = line[1:-1].strip()
  2930. content = []
  2931. else:
  2932. raise ValueError("Invalid section heading", line)
  2933. else:
  2934. content.append(line)
  2935. # wrap up last segment
  2936. yield section, content
  2937. def _mkstemp(*args, **kw):
  2938. old_open = os.open
  2939. try:
  2940. # temporarily bypass sandboxing
  2941. os.open = os_open
  2942. return tempfile.mkstemp(*args, **kw)
  2943. finally:
  2944. # and then put it back
  2945. os.open = old_open
  2946. # Yandex resource support
  2947. from __res import Y_PYTHON_SOURCE_ROOT, ResourceImporter, executable
  2948. from library.python import resource
  2949. class UnionProvider(EmptyProvider):
  2950. def __init__(self, *pp):
  2951. self.module_path = [(p, p.module_path) for p in pp]
  2952. def _has(self, path):
  2953. for p, pp in path:
  2954. if p._has(pp):
  2955. return True
  2956. return False
  2957. def _fn(self, base, resource_name):
  2958. return [(p, p._fn(pp, resource_name)) for p, pp in base]
  2959. def _get(self, path):
  2960. for p, pp in path:
  2961. if p._has(pp):
  2962. return p._get(pp)
  2963. raise IOError(path)
  2964. def _itdir(self, path):
  2965. for p, pp in path:
  2966. if not p._isdir(pp):
  2967. continue
  2968. for np in p._listdir(pp):
  2969. yield np
  2970. def _listdir(self, path):
  2971. return list(self._itdir(path))
  2972. def _isdir(self, path):
  2973. for p, pp in path:
  2974. if p._has(pp):
  2975. return p._isdir(pp)
  2976. return False
  2977. def get_resource_filename(self, manager, resource_name):
  2978. return self._fn(self.module_path, resource_name)[0][1]
  2979. class ResProvider(EmptyProvider):
  2980. _resource_fs = {}
  2981. def __init__(self, prefix):
  2982. if hasattr(prefix, '__file__'):
  2983. key = prefix.__file__.rsplit('/', 1)[0]
  2984. self.module_path = 'resfs/file/{}/'.format(key)
  2985. # Метаданные лежат на уровень выше самого пакета
  2986. key = key.rsplit('/', 1)[0]
  2987. self.egg_info = 'resfs/file/{}/.dist-info/'.format(key)
  2988. else:
  2989. # Сюда попадаем только из ResDistribution, который работает
  2990. # только метаданными, поэтому self.module_path не используется
  2991. self.egg_info = prefix
  2992. @staticmethod
  2993. def from_module(module):
  2994. if Y_PYTHON_SOURCE_ROOT:
  2995. return UnionProvider(DefaultProvider(module), ResProvider(module))
  2996. else:
  2997. return ResProvider(module)
  2998. def _fn(self, base, resource_name):
  2999. return base + resource_name
  3000. def _has(self, path):
  3001. return resource.find(path) is not None
  3002. def _get(self, path):
  3003. result = resource.find(path)
  3004. if result is None:
  3005. raise IOError(path)
  3006. return result
  3007. @classmethod
  3008. def _init_resource_fs(cls):
  3009. for path in resource.iterkeys(b'resfs/file/'):
  3010. path_str = path.decode('utf-8')
  3011. components = path_str.split('/')
  3012. for l in range(len(components)):
  3013. subpath = os.path.normpath('/'.join(components[:l]))
  3014. cls._resource_fs.setdefault(subpath, set()).add(components[l])
  3015. def __lookup(self, path):
  3016. if not self._resource_fs:
  3017. self._init_resource_fs()
  3018. path = os.path.normpath(path)
  3019. return self._resource_fs.get(path)
  3020. def _listdir(self, path):
  3021. result = self.__lookup(path)
  3022. if result is None:
  3023. return []
  3024. return list(result)
  3025. def _isdir(self, path):
  3026. return bool(self.__lookup(path))
  3027. class ResDistribution(DistInfoDistribution):
  3028. def __init__(self, prefix):
  3029. super(ResDistribution, self).__init__(
  3030. location=executable,
  3031. metadata=ResProvider(prefix),
  3032. precedence=BINARY_DIST,
  3033. )
  3034. self.project_name = self._parsed_pkg_info.get('Name', self.project_name)
  3035. def find_in_res(importer, path_item, only=False):
  3036. for key in resource.iterkeys():
  3037. if key.endswith('.dist-info/METADATA') and not key.startswith('resfs/src/'):
  3038. yield ResDistribution(key[:-8])
  3039. register_finder(ResourceImporter, find_in_res)
  3040. register_loader_type(ResourceImporter, ResProvider.from_module)
  3041. # Silence the PEP440Warning by default, so that end users don't get hit by it
  3042. # randomly just because they use pkg_resources. We want to append the rule
  3043. # because we want earlier uses of filterwarnings to take precedence over this
  3044. # one.
  3045. warnings.filterwarnings("ignore", category=PEP440Warning, append=True)
  3046. class PkgResourcesDeprecationWarning(Warning):
  3047. """
  3048. Base class for warning about deprecations in ``pkg_resources``
  3049. This class is not derived from ``DeprecationWarning``, and as such is
  3050. visible by default.
  3051. """
  3052. # Ported from ``setuptools`` to avoid introducing an import inter-dependency:
  3053. _LOCALE_ENCODING = "locale" if sys.version_info >= (3, 10) else None
  3054. # This must go before calls to `_call_aside`. See https://github.com/pypa/setuptools/pull/4422
  3055. def _read_utf8_with_fallback(file: str, fallback_encoding=_LOCALE_ENCODING) -> str:
  3056. """See setuptools.unicode_utils._read_utf8_with_fallback"""
  3057. try:
  3058. with open(file, "r", encoding="utf-8") as f:
  3059. return f.read()
  3060. except UnicodeDecodeError: # pragma: no cover
  3061. msg = f"""\
  3062. ********************************************************************************
  3063. `encoding="utf-8"` fails with {file!r}, trying `encoding={fallback_encoding!r}`.
  3064. This fallback behaviour is considered **deprecated** and future versions of
  3065. `setuptools/pkg_resources` may not implement it.
  3066. Please encode {file!r} with "utf-8" to ensure future builds will succeed.
  3067. If this file was produced by `setuptools` itself, cleaning up the cached files
  3068. and re-building/re-installing the package with a newer version of `setuptools`
  3069. (e.g. by updating `build-system.requires` in its `pyproject.toml`)
  3070. might solve the problem.
  3071. ********************************************************************************
  3072. """
  3073. # TODO: Add a deadline?
  3074. # See comment in setuptools.unicode_utils._Utf8EncodingNeeded
  3075. warnings.warn(msg, PkgResourcesDeprecationWarning, stacklevel=2)
  3076. with open(file, "r", encoding=fallback_encoding) as f:
  3077. return f.read()
  3078. # from jaraco.functools 1.3
  3079. def _call_aside(f, *args, **kwargs):
  3080. f(*args, **kwargs)
  3081. return f
  3082. @_call_aside
  3083. def _initialize(g=globals()) -> None:
  3084. "Set up global resource manager (deliberately not state-saved)"
  3085. manager = ResourceManager()
  3086. g['_manager'] = manager
  3087. g.update(
  3088. (name, getattr(manager, name))
  3089. for name in dir(manager)
  3090. if not name.startswith('_')
  3091. )
  3092. @_call_aside
  3093. def _initialize_master_working_set() -> None:
  3094. """
  3095. Prepare the master working set and make the ``require()``
  3096. API available.
  3097. This function has explicit effects on the global state
  3098. of pkg_resources. It is intended to be invoked once at
  3099. the initialization of this module.
  3100. Invocation by other packages is unsupported and done
  3101. at their own risk.
  3102. """
  3103. working_set = _declare_state('object', 'working_set', WorkingSet._build_master())
  3104. require = working_set.require
  3105. iter_entry_points = working_set.iter_entry_points
  3106. add_activation_listener = working_set.subscribe
  3107. run_script = working_set.run_script
  3108. # backward compatibility
  3109. run_main = run_script
  3110. # Activate all distributions already on sys.path with replace=False and
  3111. # ensure that all distributions added to the working set in the future
  3112. # (e.g. by calling ``require()``) will get activated as well,
  3113. # with higher priority (replace=True).
  3114. tuple(dist.activate(replace=False) for dist in working_set)
  3115. add_activation_listener(
  3116. lambda dist: dist.activate(replace=True),
  3117. existing=False,
  3118. )
  3119. working_set.entries = []
  3120. # match order
  3121. list(map(working_set.add_entry, sys.path))
  3122. globals().update(locals())
  3123. if TYPE_CHECKING:
  3124. # All of these are set by the @_call_aside methods above
  3125. __resource_manager = ResourceManager() # Won't exist at runtime
  3126. resource_exists = __resource_manager.resource_exists
  3127. resource_isdir = __resource_manager.resource_isdir
  3128. resource_filename = __resource_manager.resource_filename
  3129. resource_stream = __resource_manager.resource_stream
  3130. resource_string = __resource_manager.resource_string
  3131. resource_listdir = __resource_manager.resource_listdir
  3132. set_extraction_path = __resource_manager.set_extraction_path
  3133. cleanup_resources = __resource_manager.cleanup_resources
  3134. working_set = WorkingSet()
  3135. require = working_set.require
  3136. iter_entry_points = working_set.iter_entry_points
  3137. add_activation_listener = working_set.subscribe
  3138. run_script = working_set.run_script
  3139. run_main = run_script