__init__.py 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956
  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. """
  14. from __future__ import absolute_import
  15. import sys
  16. import os
  17. import io
  18. import time
  19. import re
  20. import types
  21. import zipfile
  22. import zipimport
  23. import warnings
  24. import stat
  25. import functools
  26. import pkgutil
  27. import operator
  28. import platform
  29. import collections
  30. import plistlib
  31. import email.parser
  32. import tempfile
  33. import textwrap
  34. from pkgutil import get_importer
  35. try:
  36. import _imp
  37. except ImportError:
  38. # Python 3.2 compatibility
  39. import imp as _imp
  40. from pkg_resources.extern import six
  41. from pkg_resources.extern.six.moves import urllib, map, filter
  42. # capture these to bypass sandboxing
  43. from os import utime
  44. try:
  45. from os import mkdir, rename, unlink
  46. WRITE_SUPPORT = True
  47. except ImportError:
  48. # no write support, probably under GAE
  49. WRITE_SUPPORT = False
  50. from os import open as os_open
  51. from os.path import isdir, split
  52. try:
  53. import importlib.machinery as importlib_machinery
  54. # access attribute to force import under delayed import mechanisms.
  55. importlib_machinery.__name__
  56. except ImportError:
  57. importlib_machinery = None
  58. from pkg_resources.extern import packaging
  59. __import__('pkg_resources.extern.packaging.version')
  60. __import__('pkg_resources.extern.packaging.specifiers')
  61. __import__('pkg_resources.extern.packaging.requirements')
  62. __import__('pkg_resources.extern.packaging.markers')
  63. if (3, 0) < sys.version_info < (3, 3):
  64. msg = (
  65. "Support for Python 3.0-3.2 has been dropped. Future versions "
  66. "will fail here."
  67. )
  68. warnings.warn(msg)
  69. # declare some globals that will be defined later to
  70. # satisfy the linters.
  71. require = None
  72. working_set = None
  73. class PEP440Warning(RuntimeWarning):
  74. """
  75. Used when there is an issue with a version or specifier not complying with
  76. PEP 440.
  77. """
  78. class _SetuptoolsVersionMixin(object):
  79. def __hash__(self):
  80. return super(_SetuptoolsVersionMixin, self).__hash__()
  81. def __lt__(self, other):
  82. if isinstance(other, tuple):
  83. return tuple(self) < other
  84. else:
  85. return super(_SetuptoolsVersionMixin, self).__lt__(other)
  86. def __le__(self, other):
  87. if isinstance(other, tuple):
  88. return tuple(self) <= other
  89. else:
  90. return super(_SetuptoolsVersionMixin, self).__le__(other)
  91. def __eq__(self, other):
  92. if isinstance(other, tuple):
  93. return tuple(self) == other
  94. else:
  95. return super(_SetuptoolsVersionMixin, self).__eq__(other)
  96. def __ge__(self, other):
  97. if isinstance(other, tuple):
  98. return tuple(self) >= other
  99. else:
  100. return super(_SetuptoolsVersionMixin, self).__ge__(other)
  101. def __gt__(self, other):
  102. if isinstance(other, tuple):
  103. return tuple(self) > other
  104. else:
  105. return super(_SetuptoolsVersionMixin, self).__gt__(other)
  106. def __ne__(self, other):
  107. if isinstance(other, tuple):
  108. return tuple(self) != other
  109. else:
  110. return super(_SetuptoolsVersionMixin, self).__ne__(other)
  111. def __getitem__(self, key):
  112. return tuple(self)[key]
  113. def __iter__(self):
  114. component_re = re.compile(r'(\d+ | [a-z]+ | \.| -)', re.VERBOSE)
  115. replace = {
  116. 'pre': 'c',
  117. 'preview': 'c',
  118. '-': 'final-',
  119. 'rc': 'c',
  120. 'dev': '@',
  121. }.get
  122. def _parse_version_parts(s):
  123. for part in component_re.split(s):
  124. part = replace(part, part)
  125. if not part or part == '.':
  126. continue
  127. if part[:1] in '0123456789':
  128. # pad for numeric comparison
  129. yield part.zfill(8)
  130. else:
  131. yield '*'+part
  132. # ensure that alpha/beta/candidate are before final
  133. yield '*final'
  134. def old_parse_version(s):
  135. parts = []
  136. for part in _parse_version_parts(s.lower()):
  137. if part.startswith('*'):
  138. # remove '-' before a prerelease tag
  139. if part < '*final':
  140. while parts and parts[-1] == '*final-':
  141. parts.pop()
  142. # remove trailing zeros from each series of numeric parts
  143. while parts and parts[-1] == '00000000':
  144. parts.pop()
  145. parts.append(part)
  146. return tuple(parts)
  147. # Warn for use of this function
  148. warnings.warn(
  149. "You have iterated over the result of "
  150. "pkg_resources.parse_version. This is a legacy behavior which is "
  151. "inconsistent with the new version class introduced in setuptools "
  152. "8.0. In most cases, conversion to a tuple is unnecessary. For "
  153. "comparison of versions, sort the Version instances directly. If "
  154. "you have another use case requiring the tuple, please file a "
  155. "bug with the setuptools project describing that need.",
  156. RuntimeWarning,
  157. stacklevel=1,
  158. )
  159. for part in old_parse_version(str(self)):
  160. yield part
  161. class SetuptoolsVersion(_SetuptoolsVersionMixin, packaging.version.Version):
  162. pass
  163. class SetuptoolsLegacyVersion(_SetuptoolsVersionMixin,
  164. packaging.version.LegacyVersion):
  165. pass
  166. def parse_version(v):
  167. try:
  168. return SetuptoolsVersion(v)
  169. except packaging.version.InvalidVersion:
  170. return SetuptoolsLegacyVersion(v)
  171. _state_vars = {}
  172. def _declare_state(vartype, **kw):
  173. globals().update(kw)
  174. _state_vars.update(dict.fromkeys(kw, vartype))
  175. def __getstate__():
  176. state = {}
  177. g = globals()
  178. for k, v in _state_vars.items():
  179. state[k] = g['_sget_'+v](g[k])
  180. return state
  181. def __setstate__(state):
  182. g = globals()
  183. for k, v in state.items():
  184. g['_sset_'+_state_vars[k]](k, g[k], v)
  185. return state
  186. def _sget_dict(val):
  187. return val.copy()
  188. def _sset_dict(key, ob, state):
  189. ob.clear()
  190. ob.update(state)
  191. def _sget_object(val):
  192. return val.__getstate__()
  193. def _sset_object(key, ob, state):
  194. ob.__setstate__(state)
  195. _sget_none = _sset_none = lambda *args: None
  196. def get_supported_platform():
  197. """Return this platform's maximum compatible version.
  198. distutils.util.get_platform() normally reports the minimum version
  199. of Mac OS X that would be required to *use* extensions produced by
  200. distutils. But what we want when checking compatibility is to know the
  201. version of Mac OS X that we are *running*. To allow usage of packages that
  202. explicitly require a newer version of Mac OS X, we must also know the
  203. current version of the OS.
  204. If this condition occurs for any other platform with a version in its
  205. platform strings, this function should be extended accordingly.
  206. """
  207. plat = get_build_platform()
  208. m = macosVersionString.match(plat)
  209. if m is not None and sys.platform == "darwin":
  210. try:
  211. plat = 'macosx-%s-%s' % ('.'.join(_macosx_vers()[:2]), m.group(3))
  212. except ValueError:
  213. # not Mac OS X
  214. pass
  215. return plat
  216. __all__ = [
  217. # Basic resource access and distribution/entry point discovery
  218. 'require', 'run_script', 'get_provider', 'get_distribution',
  219. 'load_entry_point', 'get_entry_map', 'get_entry_info',
  220. 'iter_entry_points',
  221. 'resource_string', 'resource_stream', 'resource_filename',
  222. 'resource_listdir', 'resource_exists', 'resource_isdir',
  223. # Environmental control
  224. 'declare_namespace', 'working_set', 'add_activation_listener',
  225. 'find_distributions', 'set_extraction_path', 'cleanup_resources',
  226. 'get_default_cache',
  227. # Primary implementation classes
  228. 'Environment', 'WorkingSet', 'ResourceManager',
  229. 'Distribution', 'Requirement', 'EntryPoint',
  230. # Exceptions
  231. 'ResolutionError', 'VersionConflict', 'DistributionNotFound',
  232. 'UnknownExtra', 'ExtractionError',
  233. # Warnings
  234. 'PEP440Warning',
  235. # Parsing functions and string utilities
  236. 'parse_requirements', 'parse_version', 'safe_name', 'safe_version',
  237. 'get_platform', 'compatible_platforms', 'yield_lines', 'split_sections',
  238. 'safe_extra', 'to_filename', 'invalid_marker', 'evaluate_marker',
  239. # filesystem utilities
  240. 'ensure_directory', 'normalize_path',
  241. # Distribution "precedence" constants
  242. 'EGG_DIST', 'BINARY_DIST', 'SOURCE_DIST', 'CHECKOUT_DIST', 'DEVELOP_DIST',
  243. # "Provider" interfaces, implementations, and registration/lookup APIs
  244. 'IMetadataProvider', 'IResourceProvider', 'FileMetadata',
  245. 'PathMetadata', 'EggMetadata', 'EmptyProvider', 'empty_provider',
  246. 'NullProvider', 'EggProvider', 'DefaultProvider', 'ZipProvider',
  247. 'register_finder', 'register_namespace_handler', 'register_loader_type',
  248. 'fixup_namespace_packages', 'get_importer',
  249. # Deprecated/backward compatibility only
  250. 'run_main', 'AvailableDistributions',
  251. ]
  252. class ResolutionError(Exception):
  253. """Abstract base for dependency resolution errors"""
  254. def __repr__(self):
  255. return self.__class__.__name__+repr(self.args)
  256. class VersionConflict(ResolutionError):
  257. """
  258. An already-installed version conflicts with the requested version.
  259. Should be initialized with the installed Distribution and the requested
  260. Requirement.
  261. """
  262. _template = "{self.dist} is installed but {self.req} is required"
  263. @property
  264. def dist(self):
  265. return self.args[0]
  266. @property
  267. def req(self):
  268. return self.args[1]
  269. def report(self):
  270. return self._template.format(**locals())
  271. def with_context(self, required_by):
  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):
  288. return self.args[2]
  289. class DistributionNotFound(ResolutionError):
  290. """A requested distribution was not found"""
  291. _template = ("The '{self.req}' distribution was not found "
  292. "and is required by {self.requirers_str}")
  293. @property
  294. def req(self):
  295. return self.args[0]
  296. @property
  297. def requirers(self):
  298. return self.args[1]
  299. @property
  300. def requirers_str(self):
  301. if not self.requirers:
  302. return 'the application'
  303. return ', '.join(self.requirers)
  304. def report(self):
  305. return self._template.format(**locals())
  306. def __str__(self):
  307. return self.report()
  308. class UnknownExtra(ResolutionError):
  309. """Distribution doesn't have an "extra feature" of the given name"""
  310. _provider_factories = {}
  311. PY_MAJOR = sys.version[:3]
  312. EGG_DIST = 3
  313. BINARY_DIST = 2
  314. SOURCE_DIST = 1
  315. CHECKOUT_DIST = 0
  316. DEVELOP_DIST = -1
  317. def register_loader_type(loader_type, provider_factory):
  318. """Register `provider_factory` to make providers for `loader_type`
  319. `loader_type` is the type or class of a PEP 302 ``module.__loader__``,
  320. and `provider_factory` is a function that, passed a *module* object,
  321. returns an ``IResourceProvider`` for that module.
  322. """
  323. _provider_factories[loader_type] = provider_factory
  324. def get_provider(moduleOrReq):
  325. """Return an IResourceProvider for the named module or requirement"""
  326. if isinstance(moduleOrReq, Requirement):
  327. return working_set.find(moduleOrReq) or require(str(moduleOrReq))[0]
  328. try:
  329. module = sys.modules[moduleOrReq]
  330. except KeyError:
  331. __import__(moduleOrReq)
  332. module = sys.modules[moduleOrReq]
  333. loader = getattr(module, '__loader__', None)
  334. return _find_adapter(_provider_factories, loader)(module)
  335. def _macosx_vers(_cache=[]):
  336. if not _cache:
  337. version = platform.mac_ver()[0]
  338. # fallback for MacPorts
  339. if version == '':
  340. plist = '/System/Library/CoreServices/SystemVersion.plist'
  341. if os.path.exists(plist):
  342. if hasattr(plistlib, 'readPlist'):
  343. plist_content = plistlib.readPlist(plist)
  344. if 'ProductVersion' in plist_content:
  345. version = plist_content['ProductVersion']
  346. _cache.append(version.split('.'))
  347. return _cache[0]
  348. def _macosx_arch(machine):
  349. return {'PowerPC': 'ppc', 'Power_Macintosh': 'ppc'}.get(machine, machine)
  350. def get_build_platform():
  351. """Return this platform's string for platform-specific distributions
  352. XXX Currently this is the same as ``distutils.util.get_platform()``, but it
  353. needs some hacks for Linux and Mac OS X.
  354. """
  355. try:
  356. # Python 2.7 or >=3.2
  357. from sysconfig import get_platform
  358. except ImportError:
  359. from distutils.util import get_platform
  360. plat = get_platform()
  361. if sys.platform == "darwin" and not plat.startswith('macosx-'):
  362. try:
  363. version = _macosx_vers()
  364. machine = os.uname()[4].replace(" ", "_")
  365. return "macosx-%d.%d-%s" % (int(version[0]), int(version[1]),
  366. _macosx_arch(machine))
  367. except ValueError:
  368. # if someone is running a non-Mac darwin system, this will fall
  369. # through to the default implementation
  370. pass
  371. return plat
  372. macosVersionString = re.compile(r"macosx-(\d+)\.(\d+)-(.*)")
  373. darwinVersionString = re.compile(r"darwin-(\d+)\.(\d+)\.(\d+)-(.*)")
  374. # XXX backward compat
  375. get_platform = get_build_platform
  376. def compatible_platforms(provided, required):
  377. """Can code for the `provided` platform run on the `required` platform?
  378. Returns true if either platform is ``None``, or the platforms are equal.
  379. XXX Needs compatibility checks for Linux and other unixy OSes.
  380. """
  381. if provided is None or required is None or provided==required:
  382. # easy case
  383. return True
  384. # Mac OS X special cases
  385. reqMac = macosVersionString.match(required)
  386. if reqMac:
  387. provMac = macosVersionString.match(provided)
  388. # is this a Mac package?
  389. if not provMac:
  390. # this is backwards compatibility for packages built before
  391. # setuptools 0.6. All packages built after this point will
  392. # use the new macosx designation.
  393. provDarwin = darwinVersionString.match(provided)
  394. if provDarwin:
  395. dversion = int(provDarwin.group(1))
  396. macosversion = "%s.%s" % (reqMac.group(1), reqMac.group(2))
  397. if dversion == 7 and macosversion >= "10.3" or \
  398. dversion == 8 and macosversion >= "10.4":
  399. return True
  400. # egg isn't macosx or legacy darwin
  401. return False
  402. # are they the same major version and machine type?
  403. if provMac.group(1) != reqMac.group(1) or \
  404. provMac.group(3) != reqMac.group(3):
  405. return False
  406. # is the required OS major update >= the provided one?
  407. if int(provMac.group(2)) > int(reqMac.group(2)):
  408. return False
  409. return True
  410. # XXX Linux and other platforms' special cases should go here
  411. return False
  412. def run_script(dist_spec, script_name):
  413. """Locate distribution `dist_spec` and run its `script_name` script"""
  414. ns = sys._getframe(1).f_globals
  415. name = ns['__name__']
  416. ns.clear()
  417. ns['__name__'] = name
  418. require(dist_spec)[0].run_script(script_name, ns)
  419. # backward compatibility
  420. run_main = run_script
  421. def get_distribution(dist):
  422. """Return a current distribution object for a Requirement or string"""
  423. if isinstance(dist, six.string_types):
  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 string, Requirement, or Distribution", dist)
  429. return dist
  430. def load_entry_point(dist, group, name):
  431. """Return `name` entry point of `group` for `dist` or raise ImportError"""
  432. return get_distribution(dist).load_entry_point(group, name)
  433. def get_entry_map(dist, group=None):
  434. """Return the entry point map for `group`, or the full entry map"""
  435. return get_distribution(dist).get_entry_map(group)
  436. def get_entry_info(dist, group, name):
  437. """Return the EntryPoint object for `group`+`name`, or ``None``"""
  438. return get_distribution(dist).get_entry_info(group, name)
  439. class IMetadataProvider:
  440. def has_metadata(name):
  441. """Does the package's distribution contain the named metadata?"""
  442. def get_metadata(name):
  443. """The named metadata resource as a string"""
  444. def get_metadata_lines(name):
  445. """Yield named metadata resource as list of non-blank non-comment lines
  446. Leading and trailing whitespace is stripped from each line, and lines
  447. with ``#`` as the first non-blank character are omitted."""
  448. def metadata_isdir(name):
  449. """Is the named metadata a directory? (like ``os.path.isdir()``)"""
  450. def metadata_listdir(name):
  451. """List of metadata names in the directory (like ``os.listdir()``)"""
  452. def run_script(script_name, namespace):
  453. """Execute the named script in the supplied namespace dictionary"""
  454. class IResourceProvider(IMetadataProvider):
  455. """An object that provides access to package resources"""
  456. def get_resource_filename(manager, resource_name):
  457. """Return a true filesystem path for `resource_name`
  458. `manager` must be an ``IResourceManager``"""
  459. def get_resource_stream(manager, resource_name):
  460. """Return a readable file-like object for `resource_name`
  461. `manager` must be an ``IResourceManager``"""
  462. def get_resource_string(manager, resource_name):
  463. """Return a string containing the contents of `resource_name`
  464. `manager` must be an ``IResourceManager``"""
  465. def has_resource(resource_name):
  466. """Does the package contain the named resource?"""
  467. def resource_isdir(resource_name):
  468. """Is the named resource a directory? (like ``os.path.isdir()``)"""
  469. def resource_listdir(resource_name):
  470. """List of resource names in the directory (like ``os.listdir()``)"""
  471. class WorkingSet(object):
  472. """A collection of active distributions on sys.path (or a similar list)"""
  473. def __init__(self, entries=None):
  474. """Create working set from list of path entries (default=sys.path)"""
  475. self.entries = []
  476. self.entry_keys = {}
  477. self.by_key = {}
  478. self.callbacks = []
  479. if entries is None:
  480. entries = sys.path
  481. for entry in entries:
  482. self.add_entry(entry)
  483. @classmethod
  484. def _build_master(cls):
  485. """
  486. Prepare the master working set.
  487. """
  488. ws = cls()
  489. try:
  490. from __main__ import __requires__
  491. except ImportError:
  492. # The main program does not list any requirements
  493. return ws
  494. # ensure the requirements are met
  495. try:
  496. ws.require(__requires__)
  497. except VersionConflict:
  498. return cls._build_from_requirements(__requires__)
  499. return ws
  500. @classmethod
  501. def _build_from_requirements(cls, req_spec):
  502. """
  503. Build a working set from a requirement spec. Rewrites sys.path.
  504. """
  505. # try it without defaults already on sys.path
  506. # by starting with an empty path
  507. ws = cls([])
  508. reqs = parse_requirements(req_spec)
  509. dists = ws.resolve(reqs, Environment())
  510. for dist in dists:
  511. ws.add(dist)
  512. # add any missing entries from sys.path
  513. for entry in sys.path:
  514. if entry not in ws.entries:
  515. ws.add_entry(entry)
  516. # then copy back to sys.path
  517. sys.path[:] = ws.entries
  518. return ws
  519. def add_entry(self, entry):
  520. """Add a path item to ``.entries``, finding any distributions on it
  521. ``find_distributions(entry, True)`` is used to find distributions
  522. corresponding to the path entry, and they are added. `entry` is
  523. always appended to ``.entries``, even if it is already present.
  524. (This is because ``sys.path`` can contain the same value more than
  525. once, and the ``.entries`` of the ``sys.path`` WorkingSet should always
  526. equal ``sys.path``.)
  527. """
  528. self.entry_keys.setdefault(entry, [])
  529. self.entries.append(entry)
  530. for dist in find_distributions(entry, True):
  531. self.add(dist, entry, False)
  532. def __contains__(self, dist):
  533. """True if `dist` is the active distribution for its project"""
  534. return self.by_key.get(dist.key) == dist
  535. def find(self, req):
  536. """Find a distribution matching requirement `req`
  537. If there is an active distribution for the requested project, this
  538. returns it as long as it meets the version requirement specified by
  539. `req`. But, if there is an active distribution for the project and it
  540. does *not* meet the `req` requirement, ``VersionConflict`` is raised.
  541. If there is no active distribution for the requested project, ``None``
  542. is returned.
  543. """
  544. dist = self.by_key.get(req.key)
  545. if dist is not None and dist not in req:
  546. # XXX add more info
  547. raise VersionConflict(dist, req)
  548. return dist
  549. def iter_entry_points(self, group, name=None):
  550. """Yield entry point objects from `group` matching `name`
  551. If `name` is None, yields all entry points in `group` from all
  552. distributions in the working set, otherwise only ones matching
  553. both `group` and `name` are yielded (in distribution order).
  554. """
  555. for dist in self:
  556. entries = dist.get_entry_map(group)
  557. if name is None:
  558. for ep in entries.values():
  559. yield ep
  560. elif name in entries:
  561. yield entries[name]
  562. def run_script(self, requires, script_name):
  563. """Locate distribution for `requires` and run `script_name` script"""
  564. ns = sys._getframe(1).f_globals
  565. name = ns['__name__']
  566. ns.clear()
  567. ns['__name__'] = name
  568. self.require(requires)[0].run_script(script_name, ns)
  569. def __iter__(self):
  570. """Yield distributions for non-duplicate projects in the working set
  571. The yield order is the order in which the items' path entries were
  572. added to the working set.
  573. """
  574. seen = {}
  575. for item in self.entries:
  576. if item not in self.entry_keys:
  577. # workaround a cache issue
  578. continue
  579. for key in self.entry_keys[item]:
  580. if key not in seen:
  581. seen[key]=1
  582. yield self.by_key[key]
  583. def add(self, dist, entry=None, insert=True, replace=False):
  584. """Add `dist` to working set, associated with `entry`
  585. If `entry` is unspecified, it defaults to the ``.location`` of `dist`.
  586. On exit from this routine, `entry` is added to the end of the working
  587. set's ``.entries`` (if it wasn't already present).
  588. `dist` is only added to the working set if it's for a project that
  589. doesn't already have a distribution in the set, unless `replace=True`.
  590. If it's added, any callbacks registered with the ``subscribe()`` method
  591. will be called.
  592. """
  593. if insert:
  594. dist.insert_on(self.entries, entry, replace=replace)
  595. if entry is None:
  596. entry = dist.location
  597. keys = self.entry_keys.setdefault(entry,[])
  598. keys2 = self.entry_keys.setdefault(dist.location,[])
  599. if not replace and dist.key in self.by_key:
  600. # ignore hidden distros
  601. return
  602. self.by_key[dist.key] = dist
  603. if dist.key not in keys:
  604. keys.append(dist.key)
  605. if dist.key not in keys2:
  606. keys2.append(dist.key)
  607. self._added_new(dist)
  608. def resolve(self, requirements, env=None, installer=None,
  609. replace_conflicting=False):
  610. """List all distributions needed to (recursively) meet `requirements`
  611. `requirements` must be a sequence of ``Requirement`` objects. `env`,
  612. if supplied, should be an ``Environment`` instance. If
  613. not supplied, it defaults to all distributions available within any
  614. entry or distribution in the working set. `installer`, if supplied,
  615. will be invoked with each requirement that cannot be met by an
  616. already-installed distribution; it should return a ``Distribution`` or
  617. ``None``.
  618. Unless `replace_conflicting=True`, raises a VersionConflict exception if
  619. any requirements are found on the path that have the correct name but
  620. the wrong version. Otherwise, if an `installer` is supplied it will be
  621. invoked to obtain the correct version of the requirement and activate
  622. it.
  623. """
  624. # set up the stack
  625. requirements = list(requirements)[::-1]
  626. # set of processed requirements
  627. processed = {}
  628. # key -> dist
  629. best = {}
  630. to_activate = []
  631. req_extras = _ReqExtras()
  632. # Mapping of requirement to set of distributions that required it;
  633. # useful for reporting info about conflicts.
  634. required_by = collections.defaultdict(set)
  635. while requirements:
  636. # process dependencies breadth-first
  637. req = requirements.pop(0)
  638. if req in processed:
  639. # Ignore cyclic or redundant dependencies
  640. continue
  641. if not req_extras.markers_pass(req):
  642. continue
  643. dist = best.get(req.key)
  644. if dist is None:
  645. # Find the best distribution and add it to the map
  646. dist = self.by_key.get(req.key)
  647. if dist is None or (dist not in req and replace_conflicting):
  648. ws = self
  649. if env is None:
  650. if dist is None:
  651. env = Environment(self.entries)
  652. else:
  653. # Use an empty environment and workingset to avoid
  654. # any further conflicts with the conflicting
  655. # distribution
  656. env = Environment([])
  657. ws = WorkingSet([])
  658. dist = best[req.key] = env.best_match(req, ws, installer)
  659. if dist is None:
  660. requirers = required_by.get(req, None)
  661. raise DistributionNotFound(req, requirers)
  662. to_activate.append(dist)
  663. if dist not in req:
  664. # Oops, the "best" so far conflicts with a dependency
  665. dependent_req = required_by[req]
  666. raise VersionConflict(dist, req).with_context(dependent_req)
  667. # push the new requirements onto the stack
  668. new_requirements = dist.requires(req.extras)[::-1]
  669. requirements.extend(new_requirements)
  670. # Register the new requirements needed by req
  671. for new_requirement in new_requirements:
  672. required_by[new_requirement].add(req.project_name)
  673. req_extras[new_requirement] = req.extras
  674. processed[req] = True
  675. # return list of distros to activate
  676. return to_activate
  677. def find_plugins(self, plugin_env, full_env=None, installer=None,
  678. fallback=True):
  679. """Find all activatable distributions in `plugin_env`
  680. Example usage::
  681. distributions, errors = working_set.find_plugins(
  682. Environment(plugin_dirlist)
  683. )
  684. # add plugins+libs to sys.path
  685. map(working_set.add, distributions)
  686. # display errors
  687. print('Could not load', errors)
  688. The `plugin_env` should be an ``Environment`` instance that contains
  689. only distributions that are in the project's "plugin directory" or
  690. directories. The `full_env`, if supplied, should be an ``Environment``
  691. contains all currently-available distributions. If `full_env` is not
  692. supplied, one is created automatically from the ``WorkingSet`` this
  693. method is called on, which will typically mean that every directory on
  694. ``sys.path`` will be scanned for distributions.
  695. `installer` is a standard installer callback as used by the
  696. ``resolve()`` method. The `fallback` flag indicates whether we should
  697. attempt to resolve older versions of a plugin if the newest version
  698. cannot be resolved.
  699. This method returns a 2-tuple: (`distributions`, `error_info`), where
  700. `distributions` is a list of the distributions found in `plugin_env`
  701. that were loadable, along with any other distributions that are needed
  702. to resolve their dependencies. `error_info` is a dictionary mapping
  703. unloadable plugin distributions to an exception instance describing the
  704. error that occurred. Usually this will be a ``DistributionNotFound`` or
  705. ``VersionConflict`` instance.
  706. """
  707. plugin_projects = list(plugin_env)
  708. # scan project names in alphabetic order
  709. plugin_projects.sort()
  710. error_info = {}
  711. distributions = {}
  712. if full_env is None:
  713. env = Environment(self.entries)
  714. env += plugin_env
  715. else:
  716. env = full_env + plugin_env
  717. shadow_set = self.__class__([])
  718. # put all our entries in shadow_set
  719. list(map(shadow_set.add, self))
  720. for project_name in plugin_projects:
  721. for dist in plugin_env[project_name]:
  722. req = [dist.as_requirement()]
  723. try:
  724. resolvees = shadow_set.resolve(req, env, installer)
  725. except ResolutionError as v:
  726. # save error info
  727. error_info[dist] = v
  728. if fallback:
  729. # try the next older version of project
  730. continue
  731. else:
  732. # give up on this project, keep going
  733. break
  734. else:
  735. list(map(shadow_set.add, resolvees))
  736. distributions.update(dict.fromkeys(resolvees))
  737. # success, no need to try any more versions of this project
  738. break
  739. distributions = list(distributions)
  740. distributions.sort()
  741. return distributions, error_info
  742. def require(self, *requirements):
  743. """Ensure that distributions matching `requirements` are activated
  744. `requirements` must be a string or a (possibly-nested) sequence
  745. thereof, specifying the distributions and versions required. The
  746. return value is a sequence of the distributions that needed to be
  747. activated to fulfill the requirements; all relevant distributions are
  748. included, even if they were already activated in this working set.
  749. """
  750. needed = self.resolve(parse_requirements(requirements))
  751. for dist in needed:
  752. self.add(dist)
  753. return needed
  754. def subscribe(self, callback):
  755. """Invoke `callback` for all distributions (including existing ones)"""
  756. if callback in self.callbacks:
  757. return
  758. self.callbacks.append(callback)
  759. for dist in self:
  760. callback(dist)
  761. def _added_new(self, dist):
  762. for callback in self.callbacks:
  763. callback(dist)
  764. def __getstate__(self):
  765. return (
  766. self.entries[:], self.entry_keys.copy(), self.by_key.copy(),
  767. self.callbacks[:]
  768. )
  769. def __setstate__(self, e_k_b_c):
  770. entries, keys, by_key, callbacks = e_k_b_c
  771. self.entries = entries[:]
  772. self.entry_keys = keys.copy()
  773. self.by_key = by_key.copy()
  774. self.callbacks = callbacks[:]
  775. class _ReqExtras(dict):
  776. """
  777. Map each requirement to the extras that demanded it.
  778. """
  779. def markers_pass(self, req):
  780. """
  781. Evaluate markers for req against each extra that
  782. demanded it.
  783. Return False if the req has a marker and fails
  784. evaluation. Otherwise, return True.
  785. """
  786. extra_evals = (
  787. req.marker.evaluate({'extra': extra})
  788. for extra in self.get(req, ()) + (None,)
  789. )
  790. return not req.marker or any(extra_evals)
  791. class Environment(object):
  792. """Searchable snapshot of distributions on a search path"""
  793. def __init__(self, search_path=None, platform=get_supported_platform(),
  794. python=PY_MAJOR):
  795. """Snapshot distributions available on a search path
  796. Any distributions found on `search_path` are added to the environment.
  797. `search_path` should be a sequence of ``sys.path`` items. If not
  798. supplied, ``sys.path`` is used.
  799. `platform` is an optional string specifying the name of the platform
  800. that platform-specific distributions must be compatible with. If
  801. unspecified, it defaults to the current platform. `python` is an
  802. optional string naming the desired version of Python (e.g. ``'3.3'``);
  803. it defaults to the current version.
  804. You may explicitly set `platform` (and/or `python`) to ``None`` if you
  805. wish to map *all* distributions, not just those compatible with the
  806. running platform or Python version.
  807. """
  808. self._distmap = {}
  809. self.platform = platform
  810. self.python = python
  811. self.scan(search_path)
  812. def can_add(self, dist):
  813. """Is distribution `dist` acceptable for this environment?
  814. The distribution must match the platform and python version
  815. requirements specified when this environment was created, or False
  816. is returned.
  817. """
  818. return (self.python is None or dist.py_version is None
  819. or dist.py_version==self.python) \
  820. and compatible_platforms(dist.platform, self.platform)
  821. def remove(self, dist):
  822. """Remove `dist` from the environment"""
  823. self._distmap[dist.key].remove(dist)
  824. def scan(self, search_path=None):
  825. """Scan `search_path` for distributions usable in this environment
  826. Any distributions found are added to the environment.
  827. `search_path` should be a sequence of ``sys.path`` items. If not
  828. supplied, ``sys.path`` is used. Only distributions conforming to
  829. the platform/python version defined at initialization are added.
  830. """
  831. if search_path is None:
  832. search_path = sys.path
  833. for item in search_path:
  834. for dist in find_distributions(item):
  835. self.add(dist)
  836. def __getitem__(self, project_name):
  837. """Return a newest-to-oldest list of distributions for `project_name`
  838. Uses case-insensitive `project_name` comparison, assuming all the
  839. project's distributions use their project's name converted to all
  840. lowercase as their key.
  841. """
  842. distribution_key = project_name.lower()
  843. return self._distmap.get(distribution_key, [])
  844. def add(self, dist):
  845. """Add `dist` if we ``can_add()`` it and it has not already been added
  846. """
  847. if self.can_add(dist) and dist.has_version():
  848. dists = self._distmap.setdefault(dist.key, [])
  849. if dist not in dists:
  850. dists.append(dist)
  851. dists.sort(key=operator.attrgetter('hashcmp'), reverse=True)
  852. def best_match(self, req, working_set, installer=None):
  853. """Find distribution best matching `req` and usable on `working_set`
  854. This calls the ``find(req)`` method of the `working_set` to see if a
  855. suitable distribution is already active. (This may raise
  856. ``VersionConflict`` if an unsuitable version of the project is already
  857. active in the specified `working_set`.) If a suitable distribution
  858. isn't active, this method returns the newest distribution in the
  859. environment that meets the ``Requirement`` in `req`. If no suitable
  860. distribution is found, and `installer` is supplied, then the result of
  861. calling the environment's ``obtain(req, installer)`` method will be
  862. returned.
  863. """
  864. dist = working_set.find(req)
  865. if dist is not None:
  866. return dist
  867. for dist in self[req.key]:
  868. if dist in req:
  869. return dist
  870. # try to download/install
  871. return self.obtain(req, installer)
  872. def obtain(self, requirement, installer=None):
  873. """Obtain a distribution matching `requirement` (e.g. via download)
  874. Obtain a distro that matches requirement (e.g. via download). In the
  875. base ``Environment`` class, this routine just returns
  876. ``installer(requirement)``, unless `installer` is None, in which case
  877. None is returned instead. This method is a hook that allows subclasses
  878. to attempt other ways of obtaining a distribution before falling back
  879. to the `installer` argument."""
  880. if installer is not None:
  881. return installer(requirement)
  882. def __iter__(self):
  883. """Yield the unique project names of the available distributions"""
  884. for key in self._distmap.keys():
  885. if self[key]:
  886. yield key
  887. def __iadd__(self, other):
  888. """In-place addition of a distribution or environment"""
  889. if isinstance(other, Distribution):
  890. self.add(other)
  891. elif isinstance(other, Environment):
  892. for project in other:
  893. for dist in other[project]:
  894. self.add(dist)
  895. else:
  896. raise TypeError("Can't add %r to environment" % (other,))
  897. return self
  898. def __add__(self, other):
  899. """Add an environment or distribution to an environment"""
  900. new = self.__class__([], platform=None, python=None)
  901. for env in self, other:
  902. new += env
  903. return new
  904. # XXX backward compatibility
  905. AvailableDistributions = Environment
  906. class ExtractionError(RuntimeError):
  907. """An error occurred extracting a resource
  908. The following attributes are available from instances of this exception:
  909. manager
  910. The resource manager that raised this exception
  911. cache_path
  912. The base directory for resource extraction
  913. original_error
  914. The exception instance that caused extraction to fail
  915. """
  916. class ResourceManager:
  917. """Manage resource extraction and packages"""
  918. extraction_path = None
  919. def __init__(self):
  920. self.cached_files = {}
  921. def resource_exists(self, package_or_requirement, resource_name):
  922. """Does the named resource exist?"""
  923. return get_provider(package_or_requirement).has_resource(resource_name)
  924. def resource_isdir(self, package_or_requirement, resource_name):
  925. """Is the named resource an existing directory?"""
  926. return get_provider(package_or_requirement).resource_isdir(
  927. resource_name
  928. )
  929. def resource_filename(self, package_or_requirement, resource_name):
  930. """Return a true filesystem path for specified resource"""
  931. return get_provider(package_or_requirement).get_resource_filename(
  932. self, resource_name
  933. )
  934. def resource_stream(self, package_or_requirement, resource_name):
  935. """Return a readable file-like object for specified resource"""
  936. return get_provider(package_or_requirement).get_resource_stream(
  937. self, resource_name
  938. )
  939. def resource_string(self, package_or_requirement, resource_name):
  940. """Return specified resource as a string"""
  941. return get_provider(package_or_requirement).get_resource_string(
  942. self, resource_name
  943. )
  944. def resource_listdir(self, package_or_requirement, resource_name):
  945. """List the contents of the named resource directory"""
  946. return get_provider(package_or_requirement).resource_listdir(
  947. resource_name
  948. )
  949. def extraction_error(self):
  950. """Give an error message for problems extracting file(s)"""
  951. old_exc = sys.exc_info()[1]
  952. cache_path = self.extraction_path or get_default_cache()
  953. tmpl = textwrap.dedent("""
  954. Can't extract file(s) to egg cache
  955. The following error occurred while trying to extract file(s) to the Python egg
  956. cache:
  957. {old_exc}
  958. The Python egg cache directory is currently set to:
  959. {cache_path}
  960. Perhaps your account does not have write access to this directory? You can
  961. change the cache directory by setting the PYTHON_EGG_CACHE environment
  962. variable to point to an accessible directory.
  963. """).lstrip()
  964. err = ExtractionError(tmpl.format(**locals()))
  965. err.manager = self
  966. err.cache_path = cache_path
  967. err.original_error = old_exc
  968. raise err
  969. def get_cache_path(self, archive_name, names=()):
  970. """Return absolute location in cache for `archive_name` and `names`
  971. The parent directory of the resulting path will be created if it does
  972. not already exist. `archive_name` should be the base filename of the
  973. enclosing egg (which may not be the name of the enclosing zipfile!),
  974. including its ".egg" extension. `names`, if provided, should be a
  975. sequence of path name parts "under" the egg's extraction location.
  976. This method should only be called by resource providers that need to
  977. obtain an extraction location, and only for names they intend to
  978. extract, as it tracks the generated names for possible cleanup later.
  979. """
  980. extract_path = self.extraction_path or get_default_cache()
  981. target_path = os.path.join(extract_path, archive_name+'-tmp', *names)
  982. try:
  983. _bypass_ensure_directory(target_path)
  984. except:
  985. self.extraction_error()
  986. self._warn_unsafe_extraction_path(extract_path)
  987. self.cached_files[target_path] = 1
  988. return target_path
  989. @staticmethod
  990. def _warn_unsafe_extraction_path(path):
  991. """
  992. If the default extraction path is overridden and set to an insecure
  993. location, such as /tmp, it opens up an opportunity for an attacker to
  994. replace an extracted file with an unauthorized payload. Warn the user
  995. if a known insecure location is used.
  996. See Distribute #375 for more details.
  997. """
  998. if os.name == 'nt' and not path.startswith(os.environ['windir']):
  999. # On Windows, permissions are generally restrictive by default
  1000. # and temp directories are not writable by other users, so
  1001. # bypass the warning.
  1002. return
  1003. mode = os.stat(path).st_mode
  1004. if mode & stat.S_IWOTH or mode & stat.S_IWGRP:
  1005. msg = ("%s is writable by group/others and vulnerable to attack "
  1006. "when "
  1007. "used with get_resource_filename. Consider a more secure "
  1008. "location (set with .set_extraction_path or the "
  1009. "PYTHON_EGG_CACHE environment variable)." % path)
  1010. warnings.warn(msg, UserWarning)
  1011. def postprocess(self, tempname, filename):
  1012. """Perform any platform-specific postprocessing of `tempname`
  1013. This is where Mac header rewrites should be done; other platforms don't
  1014. have anything special they should do.
  1015. Resource providers should call this method ONLY after successfully
  1016. extracting a compressed resource. They must NOT call it on resources
  1017. that are already in the filesystem.
  1018. `tempname` is the current (temporary) name of the file, and `filename`
  1019. is the name it will be renamed to by the caller after this routine
  1020. returns.
  1021. """
  1022. if os.name == 'posix':
  1023. # Make the resource executable
  1024. mode = ((os.stat(tempname).st_mode) | 0o555) & 0o7777
  1025. os.chmod(tempname, mode)
  1026. def set_extraction_path(self, path):
  1027. """Set the base path where resources will be extracted to, if needed.
  1028. If you do not call this routine before any extractions take place, the
  1029. path defaults to the return value of ``get_default_cache()``. (Which
  1030. is based on the ``PYTHON_EGG_CACHE`` environment variable, with various
  1031. platform-specific fallbacks. See that routine's documentation for more
  1032. details.)
  1033. Resources are extracted to subdirectories of this path based upon
  1034. information given by the ``IResourceProvider``. You may set this to a
  1035. temporary directory, but then you must call ``cleanup_resources()`` to
  1036. delete the extracted files when done. There is no guarantee that
  1037. ``cleanup_resources()`` will be able to remove all extracted files.
  1038. (Note: you may not change the extraction path for a given resource
  1039. manager once resources have been extracted, unless you first call
  1040. ``cleanup_resources()``.)
  1041. """
  1042. if self.cached_files:
  1043. raise ValueError(
  1044. "Can't change extraction path, files already extracted"
  1045. )
  1046. self.extraction_path = path
  1047. def cleanup_resources(self, force=False):
  1048. """
  1049. Delete all extracted resource files and directories, returning a list
  1050. of the file and directory names that could not be successfully removed.
  1051. This function does not have any concurrency protection, so it should
  1052. generally only be called when the extraction path is a temporary
  1053. directory exclusive to a single process. This method is not
  1054. automatically called; you must call it explicitly or register it as an
  1055. ``atexit`` function if you wish to ensure cleanup of a temporary
  1056. directory used for extractions.
  1057. """
  1058. # XXX
  1059. def get_default_cache():
  1060. """Determine the default cache location
  1061. This returns the ``PYTHON_EGG_CACHE`` environment variable, if set.
  1062. Otherwise, on Windows, it returns a "Python-Eggs" subdirectory of the
  1063. "Application Data" directory. On all other systems, it's "~/.python-eggs".
  1064. """
  1065. try:
  1066. return os.environ['PYTHON_EGG_CACHE']
  1067. except KeyError:
  1068. pass
  1069. if os.name!='nt':
  1070. return os.path.expanduser('~/.python-eggs')
  1071. # XXX this may be locale-specific!
  1072. app_data = 'Application Data'
  1073. app_homes = [
  1074. # best option, should be locale-safe
  1075. (('APPDATA',), None),
  1076. (('USERPROFILE',), app_data),
  1077. (('HOMEDRIVE','HOMEPATH'), app_data),
  1078. (('HOMEPATH',), app_data),
  1079. (('HOME',), None),
  1080. # 95/98/ME
  1081. (('WINDIR',), app_data),
  1082. ]
  1083. for keys, subdir in app_homes:
  1084. dirname = ''
  1085. for key in keys:
  1086. if key in os.environ:
  1087. dirname = os.path.join(dirname, os.environ[key])
  1088. else:
  1089. break
  1090. else:
  1091. if subdir:
  1092. dirname = os.path.join(dirname, subdir)
  1093. return os.path.join(dirname, 'Python-Eggs')
  1094. else:
  1095. raise RuntimeError(
  1096. "Please set the PYTHON_EGG_CACHE environment variable"
  1097. )
  1098. def safe_name(name):
  1099. """Convert an arbitrary string to a standard distribution name
  1100. Any runs of non-alphanumeric/. characters are replaced with a single '-'.
  1101. """
  1102. return re.sub('[^A-Za-z0-9.]+', '-', name)
  1103. def safe_version(version):
  1104. """
  1105. Convert an arbitrary string to a standard version string
  1106. """
  1107. try:
  1108. # normalize the version
  1109. return str(packaging.version.Version(version))
  1110. except packaging.version.InvalidVersion:
  1111. version = version.replace(' ','.')
  1112. return re.sub('[^A-Za-z0-9.]+', '-', version)
  1113. def safe_extra(extra):
  1114. """Convert an arbitrary string to a standard 'extra' name
  1115. Any runs of non-alphanumeric characters are replaced with a single '_',
  1116. and the result is always lowercased.
  1117. """
  1118. return re.sub('[^A-Za-z0-9.]+', '_', extra).lower()
  1119. def to_filename(name):
  1120. """Convert a project or version name to its filename-escaped form
  1121. Any '-' characters are currently replaced with '_'.
  1122. """
  1123. return name.replace('-','_')
  1124. def invalid_marker(text):
  1125. """
  1126. Validate text as a PEP 508 environment marker; return an exception
  1127. if invalid or False otherwise.
  1128. """
  1129. try:
  1130. evaluate_marker(text)
  1131. except SyntaxError as e:
  1132. e.filename = None
  1133. e.lineno = None
  1134. return e
  1135. return False
  1136. def evaluate_marker(text, extra=None):
  1137. """
  1138. Evaluate a PEP 508 environment marker.
  1139. Return a boolean indicating the marker result in this environment.
  1140. Raise SyntaxError if marker is invalid.
  1141. This implementation uses the 'pyparsing' module.
  1142. """
  1143. try:
  1144. marker = packaging.markers.Marker(text)
  1145. return marker.evaluate()
  1146. except packaging.markers.InvalidMarker as e:
  1147. raise SyntaxError(e)
  1148. class NullProvider:
  1149. """Try to implement resources and metadata for arbitrary PEP 302 loaders"""
  1150. egg_name = None
  1151. egg_info = None
  1152. loader = None
  1153. def __init__(self, module):
  1154. self.loader = getattr(module, '__loader__', None)
  1155. self.module_path = os.path.dirname(getattr(module, '__file__', ''))
  1156. def get_resource_filename(self, manager, resource_name):
  1157. return self._fn(self.module_path, resource_name)
  1158. def get_resource_stream(self, manager, resource_name):
  1159. return io.BytesIO(self.get_resource_string(manager, resource_name))
  1160. def get_resource_string(self, manager, resource_name):
  1161. return self._get(self._fn(self.module_path, resource_name))
  1162. def has_resource(self, resource_name):
  1163. return self._has(self._fn(self.module_path, resource_name))
  1164. def has_metadata(self, name):
  1165. return self.egg_info and self._has(self._fn(self.egg_info, name))
  1166. if sys.version_info <= (3,):
  1167. def get_metadata(self, name):
  1168. if not self.egg_info:
  1169. return ""
  1170. return self._get(self._fn(self.egg_info, name))
  1171. else:
  1172. def get_metadata(self, name):
  1173. if not self.egg_info:
  1174. return ""
  1175. return self._get(self._fn(self.egg_info, name)).decode("utf-8")
  1176. def get_metadata_lines(self, name):
  1177. return yield_lines(self.get_metadata(name))
  1178. def resource_isdir(self, resource_name):
  1179. return self._isdir(self._fn(self.module_path, resource_name))
  1180. def metadata_isdir(self, name):
  1181. return self.egg_info and self._isdir(self._fn(self.egg_info, name))
  1182. def resource_listdir(self, resource_name):
  1183. return self._listdir(self._fn(self.module_path, resource_name))
  1184. def metadata_listdir(self, name):
  1185. if self.egg_info:
  1186. return self._listdir(self._fn(self.egg_info, name))
  1187. return []
  1188. def run_script(self, script_name, namespace):
  1189. script = 'scripts/'+script_name
  1190. if not self.has_metadata(script):
  1191. raise ResolutionError("No script named %r" % script_name)
  1192. script_text = self.get_metadata(script).replace('\r\n', '\n')
  1193. script_text = script_text.replace('\r', '\n')
  1194. script_filename = self._fn(self.egg_info, script)
  1195. namespace['__file__'] = script_filename
  1196. if os.path.exists(script_filename):
  1197. source = open(script_filename).read()
  1198. code = compile(source, script_filename, 'exec')
  1199. exec(code, namespace, namespace)
  1200. else:
  1201. from linecache import cache
  1202. cache[script_filename] = (
  1203. len(script_text), 0, script_text.split('\n'), script_filename
  1204. )
  1205. script_code = compile(script_text, script_filename,'exec')
  1206. exec(script_code, namespace, namespace)
  1207. def _has(self, path):
  1208. raise NotImplementedError(
  1209. "Can't perform this operation for unregistered loader type"
  1210. )
  1211. def _isdir(self, path):
  1212. raise NotImplementedError(
  1213. "Can't perform this operation for unregistered loader type"
  1214. )
  1215. def _listdir(self, path):
  1216. raise NotImplementedError(
  1217. "Can't perform this operation for unregistered loader type"
  1218. )
  1219. def _fn(self, base, resource_name):
  1220. if resource_name:
  1221. return os.path.join(base, *resource_name.split('/'))
  1222. return base
  1223. def _get(self, path):
  1224. if hasattr(self.loader, 'get_data'):
  1225. return self.loader.get_data(path)
  1226. raise NotImplementedError(
  1227. "Can't perform this operation for loaders without 'get_data()'"
  1228. )
  1229. register_loader_type(object, NullProvider)
  1230. class EggProvider(NullProvider):
  1231. """Provider based on a virtual filesystem"""
  1232. def __init__(self, module):
  1233. NullProvider.__init__(self, module)
  1234. self._setup_prefix()
  1235. def _setup_prefix(self):
  1236. # we assume here that our metadata may be nested inside a "basket"
  1237. # of multiple eggs; that's why we use module_path instead of .archive
  1238. path = self.module_path
  1239. old = None
  1240. while path!=old:
  1241. if _is_unpacked_egg(path):
  1242. self.egg_name = os.path.basename(path)
  1243. self.egg_info = os.path.join(path, 'EGG-INFO')
  1244. self.egg_root = path
  1245. break
  1246. old = path
  1247. path, base = os.path.split(path)
  1248. class DefaultProvider(EggProvider):
  1249. """Provides access to package resources in the filesystem"""
  1250. def _has(self, path):
  1251. return os.path.exists(path)
  1252. def _isdir(self, path):
  1253. return os.path.isdir(path)
  1254. def _listdir(self, path):
  1255. return os.listdir(path)
  1256. def get_resource_stream(self, manager, resource_name):
  1257. return open(self._fn(self.module_path, resource_name), 'rb')
  1258. def _get(self, path):
  1259. with open(path, 'rb') as stream:
  1260. return stream.read()
  1261. @classmethod
  1262. def _register(cls):
  1263. loader_cls = getattr(importlib_machinery, 'SourceFileLoader',
  1264. type(None))
  1265. register_loader_type(loader_cls, cls)
  1266. DefaultProvider._register()
  1267. class EmptyProvider(NullProvider):
  1268. """Provider that returns nothing for all requests"""
  1269. _isdir = _has = lambda self, path: False
  1270. _get = lambda self, path: ''
  1271. _listdir = lambda self, path: []
  1272. module_path = None
  1273. def __init__(self):
  1274. pass
  1275. empty_provider = EmptyProvider()
  1276. class ZipManifests(dict):
  1277. """
  1278. zip manifest builder
  1279. """
  1280. @classmethod
  1281. def build(cls, path):
  1282. """
  1283. Build a dictionary similar to the zipimport directory
  1284. caches, except instead of tuples, store ZipInfo objects.
  1285. Use a platform-specific path separator (os.sep) for the path keys
  1286. for compatibility with pypy on Windows.
  1287. """
  1288. with ContextualZipFile(path) as zfile:
  1289. items = (
  1290. (
  1291. name.replace('/', os.sep),
  1292. zfile.getinfo(name),
  1293. )
  1294. for name in zfile.namelist()
  1295. )
  1296. return dict(items)
  1297. load = build
  1298. class MemoizedZipManifests(ZipManifests):
  1299. """
  1300. Memoized zipfile manifests.
  1301. """
  1302. manifest_mod = collections.namedtuple('manifest_mod', 'manifest mtime')
  1303. def load(self, path):
  1304. """
  1305. Load a manifest at path or return a suitable manifest already loaded.
  1306. """
  1307. path = os.path.normpath(path)
  1308. mtime = os.stat(path).st_mtime
  1309. if path not in self or self[path].mtime != mtime:
  1310. manifest = self.build(path)
  1311. self[path] = self.manifest_mod(manifest, mtime)
  1312. return self[path].manifest
  1313. class ContextualZipFile(zipfile.ZipFile):
  1314. """
  1315. Supplement ZipFile class to support context manager for Python 2.6
  1316. """
  1317. def __enter__(self):
  1318. return self
  1319. def __exit__(self, type, value, traceback):
  1320. self.close()
  1321. def __new__(cls, *args, **kwargs):
  1322. """
  1323. Construct a ZipFile or ContextualZipFile as appropriate
  1324. """
  1325. if hasattr(zipfile.ZipFile, '__exit__'):
  1326. return zipfile.ZipFile(*args, **kwargs)
  1327. return super(ContextualZipFile, cls).__new__(cls)
  1328. class ZipProvider(EggProvider):
  1329. """Resource support for zips and eggs"""
  1330. eagers = None
  1331. _zip_manifests = MemoizedZipManifests()
  1332. def __init__(self, module):
  1333. EggProvider.__init__(self, module)
  1334. self.zip_pre = self.loader.archive+os.sep
  1335. def _zipinfo_name(self, fspath):
  1336. # Convert a virtual filename (full path to file) into a zipfile subpath
  1337. # usable with the zipimport directory cache for our target archive
  1338. if fspath.startswith(self.zip_pre):
  1339. return fspath[len(self.zip_pre):]
  1340. raise AssertionError(
  1341. "%s is not a subpath of %s" % (fspath, self.zip_pre)
  1342. )
  1343. def _parts(self, zip_path):
  1344. # Convert a zipfile subpath into an egg-relative path part list.
  1345. # pseudo-fs path
  1346. fspath = self.zip_pre+zip_path
  1347. if fspath.startswith(self.egg_root+os.sep):
  1348. return fspath[len(self.egg_root)+1:].split(os.sep)
  1349. raise AssertionError(
  1350. "%s is not a subpath of %s" % (fspath, self.egg_root)
  1351. )
  1352. @property
  1353. def zipinfo(self):
  1354. return self._zip_manifests.load(self.loader.archive)
  1355. def get_resource_filename(self, manager, resource_name):
  1356. if not self.egg_name:
  1357. raise NotImplementedError(
  1358. "resource_filename() only supported for .egg, not .zip"
  1359. )
  1360. # no need to lock for extraction, since we use temp names
  1361. zip_path = self._resource_to_zip(resource_name)
  1362. eagers = self._get_eager_resources()
  1363. if '/'.join(self._parts(zip_path)) in eagers:
  1364. for name in eagers:
  1365. self._extract_resource(manager, self._eager_to_zip(name))
  1366. return self._extract_resource(manager, zip_path)
  1367. @staticmethod
  1368. def _get_date_and_size(zip_stat):
  1369. size = zip_stat.file_size
  1370. # ymdhms+wday, yday, dst
  1371. date_time = zip_stat.date_time + (0, 0, -1)
  1372. # 1980 offset already done
  1373. timestamp = time.mktime(date_time)
  1374. return timestamp, size
  1375. def _extract_resource(self, manager, zip_path):
  1376. if zip_path in self._index():
  1377. for name in self._index()[zip_path]:
  1378. last = self._extract_resource(
  1379. manager, os.path.join(zip_path, name)
  1380. )
  1381. # return the extracted directory name
  1382. return os.path.dirname(last)
  1383. timestamp, size = self._get_date_and_size(self.zipinfo[zip_path])
  1384. if not WRITE_SUPPORT:
  1385. raise IOError('"os.rename" and "os.unlink" are not supported '
  1386. 'on this platform')
  1387. try:
  1388. real_path = manager.get_cache_path(
  1389. self.egg_name, self._parts(zip_path)
  1390. )
  1391. if self._is_current(real_path, zip_path):
  1392. return real_path
  1393. outf, tmpnam = _mkstemp(".$extract", dir=os.path.dirname(real_path))
  1394. os.write(outf, self.loader.get_data(zip_path))
  1395. os.close(outf)
  1396. utime(tmpnam, (timestamp, timestamp))
  1397. manager.postprocess(tmpnam, real_path)
  1398. try:
  1399. rename(tmpnam, real_path)
  1400. except os.error:
  1401. if os.path.isfile(real_path):
  1402. if self._is_current(real_path, zip_path):
  1403. # the file became current since it was checked above,
  1404. # so proceed.
  1405. return real_path
  1406. # Windows, del old file and retry
  1407. elif os.name=='nt':
  1408. unlink(real_path)
  1409. rename(tmpnam, real_path)
  1410. return real_path
  1411. raise
  1412. except os.error:
  1413. # report a user-friendly error
  1414. manager.extraction_error()
  1415. return real_path
  1416. def _is_current(self, file_path, zip_path):
  1417. """
  1418. Return True if the file_path is current for this zip_path
  1419. """
  1420. timestamp, size = self._get_date_and_size(self.zipinfo[zip_path])
  1421. if not os.path.isfile(file_path):
  1422. return False
  1423. stat = os.stat(file_path)
  1424. if stat.st_size!=size or stat.st_mtime!=timestamp:
  1425. return False
  1426. # check that the contents match
  1427. zip_contents = self.loader.get_data(zip_path)
  1428. with open(file_path, 'rb') as f:
  1429. file_contents = f.read()
  1430. return zip_contents == file_contents
  1431. def _get_eager_resources(self):
  1432. if self.eagers is None:
  1433. eagers = []
  1434. for name in ('native_libs.txt', 'eager_resources.txt'):
  1435. if self.has_metadata(name):
  1436. eagers.extend(self.get_metadata_lines(name))
  1437. self.eagers = eagers
  1438. return self.eagers
  1439. def _index(self):
  1440. try:
  1441. return self._dirindex
  1442. except AttributeError:
  1443. ind = {}
  1444. for path in self.zipinfo:
  1445. parts = path.split(os.sep)
  1446. while parts:
  1447. parent = os.sep.join(parts[:-1])
  1448. if parent in ind:
  1449. ind[parent].append(parts[-1])
  1450. break
  1451. else:
  1452. ind[parent] = [parts.pop()]
  1453. self._dirindex = ind
  1454. return ind
  1455. def _has(self, fspath):
  1456. zip_path = self._zipinfo_name(fspath)
  1457. return zip_path in self.zipinfo or zip_path in self._index()
  1458. def _isdir(self, fspath):
  1459. return self._zipinfo_name(fspath) in self._index()
  1460. def _listdir(self, fspath):
  1461. return list(self._index().get(self._zipinfo_name(fspath), ()))
  1462. def _eager_to_zip(self, resource_name):
  1463. return self._zipinfo_name(self._fn(self.egg_root, resource_name))
  1464. def _resource_to_zip(self, resource_name):
  1465. return self._zipinfo_name(self._fn(self.module_path, resource_name))
  1466. register_loader_type(zipimport.zipimporter, ZipProvider)
  1467. class FileMetadata(EmptyProvider):
  1468. """Metadata handler for standalone PKG-INFO files
  1469. Usage::
  1470. metadata = FileMetadata("/path/to/PKG-INFO")
  1471. This provider rejects all data and metadata requests except for PKG-INFO,
  1472. which is treated as existing, and will be the contents of the file at
  1473. the provided location.
  1474. """
  1475. def __init__(self, path):
  1476. self.path = path
  1477. def has_metadata(self, name):
  1478. return name=='PKG-INFO' and os.path.isfile(self.path)
  1479. def get_metadata(self, name):
  1480. if name=='PKG-INFO':
  1481. with io.open(self.path, encoding='utf-8') as f:
  1482. try:
  1483. metadata = f.read()
  1484. except UnicodeDecodeError as exc:
  1485. # add path context to error message
  1486. tmpl = " in {self.path}"
  1487. exc.reason += tmpl.format(self=self)
  1488. raise
  1489. return metadata
  1490. raise KeyError("No metadata except PKG-INFO is available")
  1491. def get_metadata_lines(self, name):
  1492. return yield_lines(self.get_metadata(name))
  1493. class PathMetadata(DefaultProvider):
  1494. """Metadata provider for egg directories
  1495. Usage::
  1496. # Development eggs:
  1497. egg_info = "/path/to/PackageName.egg-info"
  1498. base_dir = os.path.dirname(egg_info)
  1499. metadata = PathMetadata(base_dir, egg_info)
  1500. dist_name = os.path.splitext(os.path.basename(egg_info))[0]
  1501. dist = Distribution(basedir, project_name=dist_name, metadata=metadata)
  1502. # Unpacked egg directories:
  1503. egg_path = "/path/to/PackageName-ver-pyver-etc.egg"
  1504. metadata = PathMetadata(egg_path, os.path.join(egg_path,'EGG-INFO'))
  1505. dist = Distribution.from_filename(egg_path, metadata=metadata)
  1506. """
  1507. def __init__(self, path, egg_info):
  1508. self.module_path = path
  1509. self.egg_info = egg_info
  1510. class EggMetadata(ZipProvider):
  1511. """Metadata provider for .egg files"""
  1512. def __init__(self, importer):
  1513. """Create a metadata provider from a zipimporter"""
  1514. self.zip_pre = importer.archive+os.sep
  1515. self.loader = importer
  1516. if importer.prefix:
  1517. self.module_path = os.path.join(importer.archive, importer.prefix)
  1518. else:
  1519. self.module_path = importer.archive
  1520. self._setup_prefix()
  1521. _declare_state('dict', _distribution_finders = {})
  1522. def register_finder(importer_type, distribution_finder):
  1523. """Register `distribution_finder` to find distributions in sys.path items
  1524. `importer_type` is the type or class of a PEP 302 "Importer" (sys.path item
  1525. handler), and `distribution_finder` is a callable that, passed a path
  1526. item and the importer instance, yields ``Distribution`` instances found on
  1527. that path item. See ``pkg_resources.find_on_path`` for an example."""
  1528. _distribution_finders[importer_type] = distribution_finder
  1529. def find_distributions(path_item, only=False):
  1530. """Yield distributions accessible via `path_item`"""
  1531. importer = get_importer(path_item)
  1532. finder = _find_adapter(_distribution_finders, importer)
  1533. return finder(importer, path_item, only)
  1534. def find_eggs_in_zip(importer, path_item, only=False):
  1535. """
  1536. Find eggs in zip files; possibly multiple nested eggs.
  1537. """
  1538. if importer.archive.endswith('.whl'):
  1539. # wheels are not supported with this finder
  1540. # they don't have PKG-INFO metadata, and won't ever contain eggs
  1541. return
  1542. metadata = EggMetadata(importer)
  1543. if metadata.has_metadata('PKG-INFO'):
  1544. yield Distribution.from_filename(path_item, metadata=metadata)
  1545. if only:
  1546. # don't yield nested distros
  1547. return
  1548. for subitem in metadata.resource_listdir('/'):
  1549. if _is_unpacked_egg(subitem):
  1550. subpath = os.path.join(path_item, subitem)
  1551. for dist in find_eggs_in_zip(zipimport.zipimporter(subpath), subpath):
  1552. yield dist
  1553. register_finder(zipimport.zipimporter, find_eggs_in_zip)
  1554. def find_nothing(importer, path_item, only=False):
  1555. return ()
  1556. register_finder(object, find_nothing)
  1557. def find_on_path(importer, path_item, only=False):
  1558. """Yield distributions accessible on a sys.path directory"""
  1559. path_item = _normalize_cached(path_item)
  1560. if os.path.isdir(path_item) and os.access(path_item, os.R_OK):
  1561. if _is_unpacked_egg(path_item):
  1562. yield Distribution.from_filename(
  1563. path_item, metadata=PathMetadata(
  1564. path_item, os.path.join(path_item,'EGG-INFO')
  1565. )
  1566. )
  1567. else:
  1568. # scan for .egg and .egg-info in directory
  1569. for entry in os.listdir(path_item):
  1570. lower = entry.lower()
  1571. if lower.endswith('.egg-info') or lower.endswith('.dist-info'):
  1572. fullpath = os.path.join(path_item, entry)
  1573. if os.path.isdir(fullpath):
  1574. # egg-info directory, allow getting metadata
  1575. metadata = PathMetadata(path_item, fullpath)
  1576. else:
  1577. metadata = FileMetadata(fullpath)
  1578. yield Distribution.from_location(
  1579. path_item, entry, metadata, precedence=DEVELOP_DIST
  1580. )
  1581. elif not only and _is_unpacked_egg(entry):
  1582. dists = find_distributions(os.path.join(path_item, entry))
  1583. for dist in dists:
  1584. yield dist
  1585. elif not only and lower.endswith('.egg-link'):
  1586. with open(os.path.join(path_item, entry)) as entry_file:
  1587. entry_lines = entry_file.readlines()
  1588. for line in entry_lines:
  1589. if not line.strip():
  1590. continue
  1591. path = os.path.join(path_item, line.rstrip())
  1592. dists = find_distributions(path)
  1593. for item in dists:
  1594. yield item
  1595. break
  1596. register_finder(pkgutil.ImpImporter, find_on_path)
  1597. if hasattr(importlib_machinery, 'FileFinder'):
  1598. register_finder(importlib_machinery.FileFinder, find_on_path)
  1599. _declare_state('dict', _namespace_handlers={})
  1600. _declare_state('dict', _namespace_packages={})
  1601. def register_namespace_handler(importer_type, namespace_handler):
  1602. """Register `namespace_handler` to declare namespace packages
  1603. `importer_type` is the type or class of a PEP 302 "Importer" (sys.path item
  1604. handler), and `namespace_handler` is a callable like this::
  1605. def namespace_handler(importer, path_entry, moduleName, module):
  1606. # return a path_entry to use for child packages
  1607. Namespace handlers are only called if the importer object has already
  1608. agreed that it can handle the relevant path item, and they should only
  1609. return a subpath if the module __path__ does not already contain an
  1610. equivalent subpath. For an example namespace handler, see
  1611. ``pkg_resources.file_ns_handler``.
  1612. """
  1613. _namespace_handlers[importer_type] = namespace_handler
  1614. def _handle_ns(packageName, path_item):
  1615. """Ensure that named package includes a subpath of path_item (if needed)"""
  1616. importer = get_importer(path_item)
  1617. if importer is None:
  1618. return None
  1619. loader = importer.find_module(packageName)
  1620. if loader is None:
  1621. return None
  1622. module = sys.modules.get(packageName)
  1623. if module is None:
  1624. module = sys.modules[packageName] = types.ModuleType(packageName)
  1625. module.__path__ = []
  1626. _set_parent_ns(packageName)
  1627. elif not hasattr(module,'__path__'):
  1628. raise TypeError("Not a package:", packageName)
  1629. handler = _find_adapter(_namespace_handlers, importer)
  1630. subpath = handler(importer, path_item, packageName, module)
  1631. if subpath is not None:
  1632. path = module.__path__
  1633. path.append(subpath)
  1634. loader.load_module(packageName)
  1635. _rebuild_mod_path(path, packageName, module)
  1636. return subpath
  1637. def _rebuild_mod_path(orig_path, package_name, module):
  1638. """
  1639. Rebuild module.__path__ ensuring that all entries are ordered
  1640. corresponding to their sys.path order
  1641. """
  1642. sys_path = [_normalize_cached(p) for p in sys.path]
  1643. def position_in_sys_path(path):
  1644. """
  1645. Return the ordinal of the path based on its position in sys.path
  1646. """
  1647. path_parts = path.split(os.sep)
  1648. module_parts = package_name.count('.') + 1
  1649. parts = path_parts[:-module_parts]
  1650. return sys_path.index(_normalize_cached(os.sep.join(parts)))
  1651. orig_path.sort(key=position_in_sys_path)
  1652. module.__path__[:] = [_normalize_cached(p) for p in orig_path]
  1653. def declare_namespace(packageName):
  1654. """Declare that package 'packageName' is a namespace package"""
  1655. _imp.acquire_lock()
  1656. try:
  1657. if packageName in _namespace_packages:
  1658. return
  1659. path, parent = sys.path, None
  1660. if '.' in packageName:
  1661. parent = '.'.join(packageName.split('.')[:-1])
  1662. declare_namespace(parent)
  1663. if parent not in _namespace_packages:
  1664. __import__(parent)
  1665. try:
  1666. path = sys.modules[parent].__path__
  1667. except AttributeError:
  1668. raise TypeError("Not a package:", parent)
  1669. # Track what packages are namespaces, so when new path items are added,
  1670. # they can be updated
  1671. _namespace_packages.setdefault(parent,[]).append(packageName)
  1672. _namespace_packages.setdefault(packageName,[])
  1673. for path_item in path:
  1674. # Ensure all the parent's path items are reflected in the child,
  1675. # if they apply
  1676. _handle_ns(packageName, path_item)
  1677. finally:
  1678. _imp.release_lock()
  1679. def fixup_namespace_packages(path_item, parent=None):
  1680. """Ensure that previously-declared namespace packages include path_item"""
  1681. _imp.acquire_lock()
  1682. try:
  1683. for package in _namespace_packages.get(parent,()):
  1684. subpath = _handle_ns(package, path_item)
  1685. if subpath:
  1686. fixup_namespace_packages(subpath, package)
  1687. finally:
  1688. _imp.release_lock()
  1689. def file_ns_handler(importer, path_item, packageName, module):
  1690. """Compute an ns-package subpath for a filesystem or zipfile importer"""
  1691. subpath = os.path.join(path_item, packageName.split('.')[-1])
  1692. normalized = _normalize_cached(subpath)
  1693. for item in module.__path__:
  1694. if _normalize_cached(item)==normalized:
  1695. break
  1696. else:
  1697. # Only return the path if it's not already there
  1698. return subpath
  1699. register_namespace_handler(pkgutil.ImpImporter, file_ns_handler)
  1700. register_namespace_handler(zipimport.zipimporter, file_ns_handler)
  1701. if hasattr(importlib_machinery, 'FileFinder'):
  1702. register_namespace_handler(importlib_machinery.FileFinder, file_ns_handler)
  1703. def null_ns_handler(importer, path_item, packageName, module):
  1704. return None
  1705. register_namespace_handler(object, null_ns_handler)
  1706. def normalize_path(filename):
  1707. """Normalize a file/dir name for comparison purposes"""
  1708. return os.path.normcase(os.path.realpath(filename))
  1709. def _normalize_cached(filename, _cache={}):
  1710. try:
  1711. return _cache[filename]
  1712. except KeyError:
  1713. _cache[filename] = result = normalize_path(filename)
  1714. return result
  1715. def _is_unpacked_egg(path):
  1716. """
  1717. Determine if given path appears to be an unpacked egg.
  1718. """
  1719. return (
  1720. path.lower().endswith('.egg')
  1721. )
  1722. def _set_parent_ns(packageName):
  1723. parts = packageName.split('.')
  1724. name = parts.pop()
  1725. if parts:
  1726. parent = '.'.join(parts)
  1727. setattr(sys.modules[parent], name, sys.modules[packageName])
  1728. def yield_lines(strs):
  1729. """Yield non-empty/non-comment lines of a string or sequence"""
  1730. if isinstance(strs, six.string_types):
  1731. for s in strs.splitlines():
  1732. s = s.strip()
  1733. # skip blank lines/comments
  1734. if s and not s.startswith('#'):
  1735. yield s
  1736. else:
  1737. for ss in strs:
  1738. for s in yield_lines(ss):
  1739. yield s
  1740. MODULE = re.compile(r"\w+(\.\w+)*$").match
  1741. EGG_NAME = re.compile(
  1742. r"""
  1743. (?P<name>[^-]+) (
  1744. -(?P<ver>[^-]+) (
  1745. -py(?P<pyver>[^-]+) (
  1746. -(?P<plat>.+)
  1747. )?
  1748. )?
  1749. )?
  1750. """,
  1751. re.VERBOSE | re.IGNORECASE,
  1752. ).match
  1753. class EntryPoint(object):
  1754. """Object representing an advertised importable object"""
  1755. def __init__(self, name, module_name, attrs=(), extras=(), dist=None):
  1756. if not MODULE(module_name):
  1757. raise ValueError("Invalid module name", module_name)
  1758. self.name = name
  1759. self.module_name = module_name
  1760. self.attrs = tuple(attrs)
  1761. self.extras = Requirement.parse(("x[%s]" % ','.join(extras))).extras
  1762. self.dist = dist
  1763. def __str__(self):
  1764. s = "%s = %s" % (self.name, self.module_name)
  1765. if self.attrs:
  1766. s += ':' + '.'.join(self.attrs)
  1767. if self.extras:
  1768. s += ' [%s]' % ','.join(self.extras)
  1769. return s
  1770. def __repr__(self):
  1771. return "EntryPoint.parse(%r)" % str(self)
  1772. def load(self, require=True, *args, **kwargs):
  1773. """
  1774. Require packages for this EntryPoint, then resolve it.
  1775. """
  1776. if not require or args or kwargs:
  1777. warnings.warn(
  1778. "Parameters to load are deprecated. Call .resolve and "
  1779. ".require separately.",
  1780. DeprecationWarning,
  1781. stacklevel=2,
  1782. )
  1783. if require:
  1784. self.require(*args, **kwargs)
  1785. return self.resolve()
  1786. def resolve(self):
  1787. """
  1788. Resolve the entry point from its module and attrs.
  1789. """
  1790. module = __import__(self.module_name, fromlist=['__name__'], level=0)
  1791. try:
  1792. return functools.reduce(getattr, self.attrs, module)
  1793. except AttributeError as exc:
  1794. raise ImportError(str(exc))
  1795. def require(self, env=None, installer=None):
  1796. if self.extras and not self.dist:
  1797. raise UnknownExtra("Can't require() without a distribution", self)
  1798. reqs = self.dist.requires(self.extras)
  1799. items = working_set.resolve(reqs, env, installer)
  1800. list(map(working_set.add, items))
  1801. pattern = re.compile(
  1802. r'\s*'
  1803. r'(?P<name>.+?)\s*'
  1804. r'=\s*'
  1805. r'(?P<module>[\w.]+)\s*'
  1806. r'(:\s*(?P<attr>[\w.]+))?\s*'
  1807. r'(?P<extras>\[.*\])?\s*$'
  1808. )
  1809. @classmethod
  1810. def parse(cls, src, dist=None):
  1811. """Parse a single entry point from string `src`
  1812. Entry point syntax follows the form::
  1813. name = some.module:some.attr [extra1, extra2]
  1814. The entry name and module name are required, but the ``:attrs`` and
  1815. ``[extras]`` parts are optional
  1816. """
  1817. m = cls.pattern.match(src)
  1818. if not m:
  1819. msg = "EntryPoint must be in 'name=module:attrs [extras]' format"
  1820. raise ValueError(msg, src)
  1821. res = m.groupdict()
  1822. extras = cls._parse_extras(res['extras'])
  1823. attrs = res['attr'].split('.') if res['attr'] else ()
  1824. return cls(res['name'], res['module'], attrs, extras, dist)
  1825. @classmethod
  1826. def _parse_extras(cls, extras_spec):
  1827. if not extras_spec:
  1828. return ()
  1829. req = Requirement.parse('x' + extras_spec)
  1830. if req.specs:
  1831. raise ValueError()
  1832. return req.extras
  1833. @classmethod
  1834. def parse_group(cls, group, lines, dist=None):
  1835. """Parse an entry point group"""
  1836. if not MODULE(group):
  1837. raise ValueError("Invalid group name", group)
  1838. this = {}
  1839. for line in yield_lines(lines):
  1840. ep = cls.parse(line, dist)
  1841. if ep.name in this:
  1842. raise ValueError("Duplicate entry point", group, ep.name)
  1843. this[ep.name]=ep
  1844. return this
  1845. @classmethod
  1846. def parse_map(cls, data, dist=None):
  1847. """Parse a map of entry point groups"""
  1848. if isinstance(data, dict):
  1849. data = data.items()
  1850. else:
  1851. data = split_sections(data)
  1852. maps = {}
  1853. for group, lines in data:
  1854. if group is None:
  1855. if not lines:
  1856. continue
  1857. raise ValueError("Entry points must be listed in groups")
  1858. group = group.strip()
  1859. if group in maps:
  1860. raise ValueError("Duplicate group name", group)
  1861. maps[group] = cls.parse_group(group, lines, dist)
  1862. return maps
  1863. def _remove_md5_fragment(location):
  1864. if not location:
  1865. return ''
  1866. parsed = urllib.parse.urlparse(location)
  1867. if parsed[-1].startswith('md5='):
  1868. return urllib.parse.urlunparse(parsed[:-1] + ('',))
  1869. return location
  1870. def _version_from_file(lines):
  1871. """
  1872. Given an iterable of lines from a Metadata file, return
  1873. the value of the Version field, if present, or None otherwise.
  1874. """
  1875. is_version_line = lambda line: line.lower().startswith('version:')
  1876. version_lines = filter(is_version_line, lines)
  1877. line = next(iter(version_lines), '')
  1878. _, _, value = line.partition(':')
  1879. return safe_version(value.strip()) or None
  1880. class Distribution(object):
  1881. """Wrap an actual or potential sys.path entry w/metadata"""
  1882. PKG_INFO = 'PKG-INFO'
  1883. def __init__(self, location=None, metadata=None, project_name=None,
  1884. version=None, py_version=PY_MAJOR, platform=None,
  1885. precedence=EGG_DIST):
  1886. self.project_name = safe_name(project_name or 'Unknown')
  1887. if version is not None:
  1888. self._version = safe_version(version)
  1889. self.py_version = py_version
  1890. self.platform = platform
  1891. self.location = location
  1892. self.precedence = precedence
  1893. self._provider = metadata or empty_provider
  1894. @classmethod
  1895. def from_location(cls, location, basename, metadata=None, **kw):
  1896. project_name, version, py_version, platform = [None]*4
  1897. basename, ext = os.path.splitext(basename)
  1898. if ext.lower() in _distributionImpl:
  1899. cls = _distributionImpl[ext.lower()]
  1900. match = EGG_NAME(basename)
  1901. if match:
  1902. project_name, version, py_version, platform = match.group(
  1903. 'name', 'ver', 'pyver', 'plat'
  1904. )
  1905. return cls(
  1906. location, metadata, project_name=project_name, version=version,
  1907. py_version=py_version, platform=platform, **kw
  1908. )._reload_version()
  1909. def _reload_version(self):
  1910. return self
  1911. @property
  1912. def hashcmp(self):
  1913. return (
  1914. self.parsed_version,
  1915. self.precedence,
  1916. self.key,
  1917. _remove_md5_fragment(self.location),
  1918. self.py_version or '',
  1919. self.platform or '',
  1920. )
  1921. def __hash__(self):
  1922. return hash(self.hashcmp)
  1923. def __lt__(self, other):
  1924. return self.hashcmp < other.hashcmp
  1925. def __le__(self, other):
  1926. return self.hashcmp <= other.hashcmp
  1927. def __gt__(self, other):
  1928. return self.hashcmp > other.hashcmp
  1929. def __ge__(self, other):
  1930. return self.hashcmp >= other.hashcmp
  1931. def __eq__(self, other):
  1932. if not isinstance(other, self.__class__):
  1933. # It's not a Distribution, so they are not equal
  1934. return False
  1935. return self.hashcmp == other.hashcmp
  1936. def __ne__(self, other):
  1937. return not self == other
  1938. # These properties have to be lazy so that we don't have to load any
  1939. # metadata until/unless it's actually needed. (i.e., some distributions
  1940. # may not know their name or version without loading PKG-INFO)
  1941. @property
  1942. def key(self):
  1943. try:
  1944. return self._key
  1945. except AttributeError:
  1946. self._key = key = self.project_name.lower()
  1947. return key
  1948. @property
  1949. def parsed_version(self):
  1950. if not hasattr(self, "_parsed_version"):
  1951. self._parsed_version = parse_version(self.version)
  1952. return self._parsed_version
  1953. def _warn_legacy_version(self):
  1954. LV = packaging.version.LegacyVersion
  1955. is_legacy = isinstance(self._parsed_version, LV)
  1956. if not is_legacy:
  1957. return
  1958. # While an empty version is technically a legacy version and
  1959. # is not a valid PEP 440 version, it's also unlikely to
  1960. # actually come from someone and instead it is more likely that
  1961. # it comes from setuptools attempting to parse a filename and
  1962. # including it in the list. So for that we'll gate this warning
  1963. # on if the version is anything at all or not.
  1964. if not self.version:
  1965. return
  1966. tmpl = textwrap.dedent("""
  1967. '{project_name} ({version})' is being parsed as a legacy,
  1968. non PEP 440,
  1969. version. You may find odd behavior and sort order.
  1970. In particular it will be sorted as less than 0.0. It
  1971. is recommended to migrate to PEP 440 compatible
  1972. versions.
  1973. """).strip().replace('\n', ' ')
  1974. warnings.warn(tmpl.format(**vars(self)), PEP440Warning)
  1975. @property
  1976. def version(self):
  1977. try:
  1978. return self._version
  1979. except AttributeError:
  1980. version = _version_from_file(self._get_metadata(self.PKG_INFO))
  1981. if version is None:
  1982. tmpl = "Missing 'Version:' header and/or %s file"
  1983. raise ValueError(tmpl % self.PKG_INFO, self)
  1984. return version
  1985. @property
  1986. def _dep_map(self):
  1987. try:
  1988. return self.__dep_map
  1989. except AttributeError:
  1990. dm = self.__dep_map = {None: []}
  1991. for name in 'requires.txt', 'depends.txt':
  1992. for extra, reqs in split_sections(self._get_metadata(name)):
  1993. if extra:
  1994. if ':' in extra:
  1995. extra, marker = extra.split(':', 1)
  1996. if invalid_marker(marker):
  1997. # XXX warn
  1998. reqs=[]
  1999. elif not evaluate_marker(marker):
  2000. reqs=[]
  2001. extra = safe_extra(extra) or None
  2002. dm.setdefault(extra,[]).extend(parse_requirements(reqs))
  2003. return dm
  2004. def requires(self, extras=()):
  2005. """List of Requirements needed for this distro if `extras` are used"""
  2006. dm = self._dep_map
  2007. deps = []
  2008. deps.extend(dm.get(None, ()))
  2009. for ext in extras:
  2010. try:
  2011. deps.extend(dm[safe_extra(ext)])
  2012. except KeyError:
  2013. raise UnknownExtra(
  2014. "%s has no such extra feature %r" % (self, ext)
  2015. )
  2016. return deps
  2017. def _get_metadata(self, name):
  2018. if self.has_metadata(name):
  2019. for line in self.get_metadata_lines(name):
  2020. yield line
  2021. def activate(self, path=None):
  2022. """Ensure distribution is importable on `path` (default=sys.path)"""
  2023. if path is None:
  2024. path = sys.path
  2025. self.insert_on(path, replace=True)
  2026. if path is sys.path:
  2027. fixup_namespace_packages(self.location)
  2028. for pkg in self._get_metadata('namespace_packages.txt'):
  2029. if pkg in sys.modules:
  2030. declare_namespace(pkg)
  2031. def egg_name(self):
  2032. """Return what this distribution's standard .egg filename should be"""
  2033. filename = "%s-%s-py%s" % (
  2034. to_filename(self.project_name), to_filename(self.version),
  2035. self.py_version or PY_MAJOR
  2036. )
  2037. if self.platform:
  2038. filename += '-' + self.platform
  2039. return filename
  2040. def __repr__(self):
  2041. if self.location:
  2042. return "%s (%s)" % (self, self.location)
  2043. else:
  2044. return str(self)
  2045. def __str__(self):
  2046. try:
  2047. version = getattr(self, 'version', None)
  2048. except ValueError:
  2049. version = None
  2050. version = version or "[unknown version]"
  2051. return "%s %s" % (self.project_name, version)
  2052. def __getattr__(self, attr):
  2053. """Delegate all unrecognized public attributes to .metadata provider"""
  2054. if attr.startswith('_'):
  2055. raise AttributeError(attr)
  2056. return getattr(self._provider, attr)
  2057. @classmethod
  2058. def from_filename(cls, filename, metadata=None, **kw):
  2059. return cls.from_location(
  2060. _normalize_cached(filename), os.path.basename(filename), metadata,
  2061. **kw
  2062. )
  2063. def as_requirement(self):
  2064. """Return a ``Requirement`` that matches this distribution exactly"""
  2065. if isinstance(self.parsed_version, packaging.version.Version):
  2066. spec = "%s==%s" % (self.project_name, self.parsed_version)
  2067. else:
  2068. spec = "%s===%s" % (self.project_name, self.parsed_version)
  2069. return Requirement.parse(spec)
  2070. def load_entry_point(self, group, name):
  2071. """Return the `name` entry point of `group` or raise ImportError"""
  2072. ep = self.get_entry_info(group, name)
  2073. if ep is None:
  2074. raise ImportError("Entry point %r not found" % ((group, name),))
  2075. return ep.load()
  2076. def get_entry_map(self, group=None):
  2077. """Return the entry point map for `group`, or the full entry map"""
  2078. try:
  2079. ep_map = self._ep_map
  2080. except AttributeError:
  2081. ep_map = self._ep_map = EntryPoint.parse_map(
  2082. self._get_metadata('entry_points.txt'), self
  2083. )
  2084. if group is not None:
  2085. return ep_map.get(group,{})
  2086. return ep_map
  2087. def get_entry_info(self, group, name):
  2088. """Return the EntryPoint object for `group`+`name`, or ``None``"""
  2089. return self.get_entry_map(group).get(name)
  2090. def insert_on(self, path, loc=None, replace=False):
  2091. """Insert self.location in path before its nearest parent directory"""
  2092. loc = loc or self.location
  2093. if not loc:
  2094. return
  2095. nloc = _normalize_cached(loc)
  2096. bdir = os.path.dirname(nloc)
  2097. npath= [(p and _normalize_cached(p) or p) for p in path]
  2098. for p, item in enumerate(npath):
  2099. if item == nloc:
  2100. break
  2101. elif item == bdir and self.precedence == EGG_DIST:
  2102. # if it's an .egg, give it precedence over its directory
  2103. if path is sys.path:
  2104. self.check_version_conflict()
  2105. path.insert(p, loc)
  2106. npath.insert(p, nloc)
  2107. break
  2108. else:
  2109. if path is sys.path:
  2110. self.check_version_conflict()
  2111. if replace:
  2112. path.insert(0, loc)
  2113. else:
  2114. path.append(loc)
  2115. return
  2116. # p is the spot where we found or inserted loc; now remove duplicates
  2117. while True:
  2118. try:
  2119. np = npath.index(nloc, p+1)
  2120. except ValueError:
  2121. break
  2122. else:
  2123. del npath[np], path[np]
  2124. # ha!
  2125. p = np
  2126. return
  2127. def check_version_conflict(self):
  2128. if self.key == 'setuptools':
  2129. # ignore the inevitable setuptools self-conflicts :(
  2130. return
  2131. nsp = dict.fromkeys(self._get_metadata('namespace_packages.txt'))
  2132. loc = normalize_path(self.location)
  2133. for modname in self._get_metadata('top_level.txt'):
  2134. if (modname not in sys.modules or modname in nsp
  2135. or modname in _namespace_packages):
  2136. continue
  2137. if modname in ('pkg_resources', 'setuptools', 'site'):
  2138. continue
  2139. fn = getattr(sys.modules[modname], '__file__', None)
  2140. if fn and (normalize_path(fn).startswith(loc) or
  2141. fn.startswith(self.location)):
  2142. continue
  2143. issue_warning(
  2144. "Module %s was already imported from %s, but %s is being added"
  2145. " to sys.path" % (modname, fn, self.location),
  2146. )
  2147. def has_version(self):
  2148. try:
  2149. self.version
  2150. except ValueError:
  2151. issue_warning("Unbuilt egg for " + repr(self))
  2152. return False
  2153. return True
  2154. def clone(self,**kw):
  2155. """Copy this distribution, substituting in any changed keyword args"""
  2156. names = 'project_name version py_version platform location precedence'
  2157. for attr in names.split():
  2158. kw.setdefault(attr, getattr(self, attr, None))
  2159. kw.setdefault('metadata', self._provider)
  2160. return self.__class__(**kw)
  2161. @property
  2162. def extras(self):
  2163. return [dep for dep in self._dep_map if dep]
  2164. class EggInfoDistribution(Distribution):
  2165. def _reload_version(self):
  2166. """
  2167. Packages installed by distutils (e.g. numpy or scipy),
  2168. which uses an old safe_version, and so
  2169. their version numbers can get mangled when
  2170. converted to filenames (e.g., 1.11.0.dev0+2329eae to
  2171. 1.11.0.dev0_2329eae). These distributions will not be
  2172. parsed properly
  2173. downstream by Distribution and safe_version, so
  2174. take an extra step and try to get the version number from
  2175. the metadata file itself instead of the filename.
  2176. """
  2177. md_version = _version_from_file(self._get_metadata(self.PKG_INFO))
  2178. if md_version:
  2179. self._version = md_version
  2180. return self
  2181. class DistInfoDistribution(Distribution):
  2182. """Wrap an actual or potential sys.path entry w/metadata, .dist-info style"""
  2183. PKG_INFO = 'METADATA'
  2184. EQEQ = re.compile(r"([\(,])\s*(\d.*?)\s*([,\)])")
  2185. @property
  2186. def _parsed_pkg_info(self):
  2187. """Parse and cache metadata"""
  2188. try:
  2189. return self._pkg_info
  2190. except AttributeError:
  2191. metadata = self.get_metadata(self.PKG_INFO)
  2192. self._pkg_info = email.parser.Parser().parsestr(metadata)
  2193. return self._pkg_info
  2194. @property
  2195. def _dep_map(self):
  2196. try:
  2197. return self.__dep_map
  2198. except AttributeError:
  2199. self.__dep_map = self._compute_dependencies()
  2200. return self.__dep_map
  2201. def _compute_dependencies(self):
  2202. """Recompute this distribution's dependencies."""
  2203. dm = self.__dep_map = {None: []}
  2204. reqs = []
  2205. # Including any condition expressions
  2206. for req in self._parsed_pkg_info.get_all('Requires-Dist') or []:
  2207. reqs.extend(parse_requirements(req))
  2208. def reqs_for_extra(extra):
  2209. for req in reqs:
  2210. if not req.marker or req.marker.evaluate({'extra': extra}):
  2211. yield req
  2212. common = frozenset(reqs_for_extra(None))
  2213. dm[None].extend(common)
  2214. for extra in self._parsed_pkg_info.get_all('Provides-Extra') or []:
  2215. extra = safe_extra(extra.strip())
  2216. dm[extra] = list(frozenset(reqs_for_extra(extra)) - common)
  2217. return dm
  2218. _distributionImpl = {
  2219. '.egg': Distribution,
  2220. '.egg-info': EggInfoDistribution,
  2221. '.dist-info': DistInfoDistribution,
  2222. }
  2223. def issue_warning(*args,**kw):
  2224. level = 1
  2225. g = globals()
  2226. try:
  2227. # find the first stack frame that is *not* code in
  2228. # the pkg_resources module, to use for the warning
  2229. while sys._getframe(level).f_globals is g:
  2230. level += 1
  2231. except ValueError:
  2232. pass
  2233. warnings.warn(stacklevel=level + 1, *args, **kw)
  2234. class RequirementParseError(ValueError):
  2235. def __str__(self):
  2236. return ' '.join(self.args)
  2237. def parse_requirements(strs):
  2238. """Yield ``Requirement`` objects for each specification in `strs`
  2239. `strs` must be a string, or a (possibly-nested) iterable thereof.
  2240. """
  2241. # create a steppable iterator, so we can handle \-continuations
  2242. lines = iter(yield_lines(strs))
  2243. for line in lines:
  2244. # Drop comments -- a hash without a space may be in a URL.
  2245. if ' #' in line:
  2246. line = line[:line.find(' #')]
  2247. # If there is a line continuation, drop it, and append the next line.
  2248. if line.endswith('\\'):
  2249. line = line[:-2].strip()
  2250. line += next(lines)
  2251. yield Requirement(line)
  2252. class Requirement(packaging.requirements.Requirement):
  2253. def __init__(self, requirement_string):
  2254. """DO NOT CALL THIS UNDOCUMENTED METHOD; use Requirement.parse()!"""
  2255. try:
  2256. super(Requirement, self).__init__(requirement_string)
  2257. except packaging.requirements.InvalidRequirement as e:
  2258. raise RequirementParseError(str(e))
  2259. self.unsafe_name = self.name
  2260. project_name = safe_name(self.name)
  2261. self.project_name, self.key = project_name, project_name.lower()
  2262. self.specs = [
  2263. (spec.operator, spec.version) for spec in self.specifier]
  2264. self.extras = tuple(map(safe_extra, self.extras))
  2265. self.hashCmp = (
  2266. self.key,
  2267. self.specifier,
  2268. frozenset(self.extras),
  2269. str(self.marker) if self.marker else None,
  2270. )
  2271. self.__hash = hash(self.hashCmp)
  2272. def __eq__(self, other):
  2273. return (
  2274. isinstance(other, Requirement) and
  2275. self.hashCmp == other.hashCmp
  2276. )
  2277. def __ne__(self, other):
  2278. return not self == other
  2279. def __contains__(self, item):
  2280. if isinstance(item, Distribution):
  2281. if item.key != self.key:
  2282. return False
  2283. item = item.version
  2284. # Allow prereleases always in order to match the previous behavior of
  2285. # this method. In the future this should be smarter and follow PEP 440
  2286. # more accurately.
  2287. return self.specifier.contains(item, prereleases=True)
  2288. def __hash__(self):
  2289. return self.__hash
  2290. def __repr__(self): return "Requirement.parse(%r)" % str(self)
  2291. @staticmethod
  2292. def parse(s):
  2293. req, = parse_requirements(s)
  2294. return req
  2295. def _get_mro(cls):
  2296. """Get an mro for a type or classic class"""
  2297. if not isinstance(cls, type):
  2298. class cls(cls, object): pass
  2299. return cls.__mro__[1:]
  2300. return cls.__mro__
  2301. def _find_adapter(registry, ob):
  2302. """Return an adapter factory for `ob` from `registry`"""
  2303. for t in _get_mro(getattr(ob, '__class__', type(ob))):
  2304. if t in registry:
  2305. return registry[t]
  2306. def ensure_directory(path):
  2307. """Ensure that the parent directory of `path` exists"""
  2308. dirname = os.path.dirname(path)
  2309. if not os.path.isdir(dirname):
  2310. os.makedirs(dirname)
  2311. def _bypass_ensure_directory(path):
  2312. """Sandbox-bypassing version of ensure_directory()"""
  2313. if not WRITE_SUPPORT:
  2314. raise IOError('"os.mkdir" not supported on this platform.')
  2315. dirname, filename = split(path)
  2316. if dirname and filename and not isdir(dirname):
  2317. _bypass_ensure_directory(dirname)
  2318. mkdir(dirname, 0o755)
  2319. def split_sections(s):
  2320. """Split a string or iterable thereof into (section, content) pairs
  2321. Each ``section`` is a stripped version of the section header ("[section]")
  2322. and each ``content`` is a list of stripped lines excluding blank lines and
  2323. comment-only lines. If there are any such lines before the first section
  2324. header, they're returned in a first ``section`` of ``None``.
  2325. """
  2326. section = None
  2327. content = []
  2328. for line in yield_lines(s):
  2329. if line.startswith("["):
  2330. if line.endswith("]"):
  2331. if section or content:
  2332. yield section, content
  2333. section = line[1:-1].strip()
  2334. content = []
  2335. else:
  2336. raise ValueError("Invalid section heading", line)
  2337. else:
  2338. content.append(line)
  2339. # wrap up last segment
  2340. yield section, content
  2341. def _mkstemp(*args,**kw):
  2342. old_open = os.open
  2343. try:
  2344. # temporarily bypass sandboxing
  2345. os.open = os_open
  2346. return tempfile.mkstemp(*args,**kw)
  2347. finally:
  2348. # and then put it back
  2349. os.open = old_open
  2350. # Silence the PEP440Warning by default, so that end users don't get hit by it
  2351. # randomly just because they use pkg_resources. We want to append the rule
  2352. # because we want earlier uses of filterwarnings to take precedence over this
  2353. # one.
  2354. warnings.filterwarnings("ignore", category=PEP440Warning, append=True)
  2355. # from jaraco.functools 1.3
  2356. def _call_aside(f, *args, **kwargs):
  2357. f(*args, **kwargs)
  2358. return f
  2359. @_call_aside
  2360. def _initialize(g=globals()):
  2361. "Set up global resource manager (deliberately not state-saved)"
  2362. manager = ResourceManager()
  2363. g['_manager'] = manager
  2364. for name in dir(manager):
  2365. if not name.startswith('_'):
  2366. g[name] = getattr(manager, name)
  2367. @_call_aside
  2368. def _initialize_master_working_set():
  2369. """
  2370. Prepare the master working set and make the ``require()``
  2371. API available.
  2372. This function has explicit effects on the global state
  2373. of pkg_resources. It is intended to be invoked once at
  2374. the initialization of this module.
  2375. Invocation by other packages is unsupported and done
  2376. at their own risk.
  2377. """
  2378. working_set = WorkingSet._build_master()
  2379. _declare_state('object', working_set=working_set)
  2380. require = working_set.require
  2381. iter_entry_points = working_set.iter_entry_points
  2382. add_activation_listener = working_set.subscribe
  2383. run_script = working_set.run_script
  2384. # backward compatibility
  2385. run_main = run_script
  2386. # Activate all distributions already on sys.path, and ensure that
  2387. # all distributions added to the working set in the future (e.g. by
  2388. # calling ``require()``) will get activated as well.
  2389. add_activation_listener(lambda dist: dist.activate())
  2390. working_set.entries=[]
  2391. # match order
  2392. list(map(working_set.add_entry, sys.path))
  2393. globals().update(locals())