PageRenderTime 63ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/pkg_resources.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 2625 lines | 2424 code | 96 blank | 105 comment | 59 complexity | 42537d2e5dd1311cff8b1e28342369c5 MD5 | raw file
  1. """Package resource API
  2. --------------------
  3. A resource is a logical file contained within a package, or a logical
  4. subdirectory thereof. The package resource API expects resource names
  5. to have their path parts separated with ``/``, *not* whatever the local
  6. path separator is. Do not use os.path operations to manipulate resource
  7. names being passed into the API.
  8. The package resource API is designed to work with normal filesystem packages,
  9. .egg files, and unpacked .egg files. It can also work in a limited way with
  10. .zip files and with custom PEP 302 loaders that support the ``get_data()``
  11. method.
  12. """
  13. import sys, os, zipimport, time, re, imp
  14. try:
  15. frozenset
  16. except NameError:
  17. from sets import ImmutableSet as frozenset
  18. # capture these to bypass sandboxing
  19. from os import utime, rename, unlink, mkdir
  20. from os import open as os_open
  21. from os.path import isdir, split
  22. def _bypass_ensure_directory(name, mode=0777):
  23. # Sandbox-bypassing version of ensure_directory()
  24. dirname, filename = split(name)
  25. if dirname and filename and not isdir(dirname):
  26. _bypass_ensure_directory(dirname)
  27. mkdir(dirname, mode)
  28. _state_vars = {}
  29. def _declare_state(vartype, **kw):
  30. g = globals()
  31. for name, val in kw.iteritems():
  32. g[name] = val
  33. _state_vars[name] = vartype
  34. def __getstate__():
  35. state = {}
  36. g = globals()
  37. for k, v in _state_vars.iteritems():
  38. state[k] = g['_sget_'+v](g[k])
  39. return state
  40. def __setstate__(state):
  41. g = globals()
  42. for k, v in state.iteritems():
  43. g['_sset_'+_state_vars[k]](k, g[k], v)
  44. return state
  45. def _sget_dict(val):
  46. return val.copy()
  47. def _sset_dict(key, ob, state):
  48. ob.clear()
  49. ob.update(state)
  50. def _sget_object(val):
  51. return val.__getstate__()
  52. def _sset_object(key, ob, state):
  53. ob.__setstate__(state)
  54. _sget_none = _sset_none = lambda *args: None
  55. def get_supported_platform():
  56. """Return this platform's maximum compatible version.
  57. distutils.util.get_platform() normally reports the minimum version
  58. of Mac OS X that would be required to *use* extensions produced by
  59. distutils. But what we want when checking compatibility is to know the
  60. version of Mac OS X that we are *running*. To allow usage of packages that
  61. explicitly require a newer version of Mac OS X, we must also know the
  62. current version of the OS.
  63. If this condition occurs for any other platform with a version in its
  64. platform strings, this function should be extended accordingly.
  65. """
  66. plat = get_build_platform(); m = macosVersionString.match(plat)
  67. if m is not None and sys.platform == "darwin":
  68. try:
  69. plat = 'macosx-%s-%s' % ('.'.join(_macosx_vers()[:2]), m.group(3))
  70. except ValueError:
  71. pass # not Mac OS X
  72. return plat
  73. __all__ = [
  74. # Basic resource access and distribution/entry point discovery
  75. 'require', 'run_script', 'get_provider', 'get_distribution',
  76. 'load_entry_point', 'get_entry_map', 'get_entry_info', 'iter_entry_points',
  77. 'resource_string', 'resource_stream', 'resource_filename',
  78. 'resource_listdir', 'resource_exists', 'resource_isdir',
  79. # Environmental control
  80. 'declare_namespace', 'working_set', 'add_activation_listener',
  81. 'find_distributions', 'set_extraction_path', 'cleanup_resources',
  82. 'get_default_cache',
  83. # Primary implementation classes
  84. 'Environment', 'WorkingSet', 'ResourceManager',
  85. 'Distribution', 'Requirement', 'EntryPoint',
  86. # Exceptions
  87. 'ResolutionError','VersionConflict','DistributionNotFound','UnknownExtra',
  88. 'ExtractionError',
  89. # Parsing functions and string utilities
  90. 'parse_requirements', 'parse_version', 'safe_name', 'safe_version',
  91. 'get_platform', 'compatible_platforms', 'yield_lines', 'split_sections',
  92. 'safe_extra', 'to_filename',
  93. # filesystem utilities
  94. 'ensure_directory', 'normalize_path',
  95. # Distribution "precedence" constants
  96. 'EGG_DIST', 'BINARY_DIST', 'SOURCE_DIST', 'CHECKOUT_DIST', 'DEVELOP_DIST',
  97. # "Provider" interfaces, implementations, and registration/lookup APIs
  98. 'IMetadataProvider', 'IResourceProvider', 'FileMetadata',
  99. 'PathMetadata', 'EggMetadata', 'EmptyProvider', 'empty_provider',
  100. 'NullProvider', 'EggProvider', 'DefaultProvider', 'ZipProvider',
  101. 'register_finder', 'register_namespace_handler', 'register_loader_type',
  102. 'fixup_namespace_packages', 'get_importer',
  103. # Deprecated/backward compatibility only
  104. 'run_main', 'AvailableDistributions',
  105. ]
  106. class ResolutionError(Exception):
  107. """Abstract base for dependency resolution errors"""
  108. def __repr__(self): return self.__class__.__name__+repr(self.args)
  109. class VersionConflict(ResolutionError):
  110. """An already-installed version conflicts with the requested version"""
  111. class DistributionNotFound(ResolutionError):
  112. """A requested distribution was not found"""
  113. class UnknownExtra(ResolutionError):
  114. """Distribution doesn't have an "extra feature" of the given name"""
  115. _provider_factories = {}
  116. PY_MAJOR = sys.version[:3]
  117. EGG_DIST = 3
  118. BINARY_DIST = 2
  119. SOURCE_DIST = 1
  120. CHECKOUT_DIST = 0
  121. DEVELOP_DIST = -1
  122. def register_loader_type(loader_type, provider_factory):
  123. """Register `provider_factory` to make providers for `loader_type`
  124. `loader_type` is the type or class of a PEP 302 ``module.__loader__``,
  125. and `provider_factory` is a function that, passed a *module* object,
  126. returns an ``IResourceProvider`` for that module.
  127. """
  128. _provider_factories[loader_type] = provider_factory
  129. def get_provider(moduleOrReq):
  130. """Return an IResourceProvider for the named module or requirement"""
  131. if isinstance(moduleOrReq,Requirement):
  132. return working_set.find(moduleOrReq) or require(str(moduleOrReq))[0]
  133. try:
  134. module = sys.modules[moduleOrReq]
  135. except KeyError:
  136. __import__(moduleOrReq)
  137. module = sys.modules[moduleOrReq]
  138. loader = getattr(module, '__loader__', None)
  139. return _find_adapter(_provider_factories, loader)(module)
  140. def _macosx_vers(_cache=[]):
  141. if not _cache:
  142. from platform import mac_ver
  143. _cache.append(mac_ver()[0].split('.'))
  144. return _cache[0]
  145. def _macosx_arch(machine):
  146. return {'PowerPC':'ppc', 'Power_Macintosh':'ppc'}.get(machine,machine)
  147. def get_build_platform():
  148. """Return this platform's string for platform-specific distributions
  149. XXX Currently this is the same as ``distutils.util.get_platform()``, but it
  150. needs some hacks for Linux and Mac OS X.
  151. """
  152. from distutils.util import get_platform
  153. plat = get_platform()
  154. if sys.platform == "darwin" and not plat.startswith('macosx-'):
  155. try:
  156. version = _macosx_vers()
  157. machine = os.uname()[4].replace(" ", "_")
  158. return "macosx-%d.%d-%s" % (int(version[0]), int(version[1]),
  159. _macosx_arch(machine))
  160. except ValueError:
  161. # if someone is running a non-Mac darwin system, this will fall
  162. # through to the default implementation
  163. pass
  164. return plat
  165. macosVersionString = re.compile(r"macosx-(\d+)\.(\d+)-(.*)")
  166. darwinVersionString = re.compile(r"darwin-(\d+)\.(\d+)\.(\d+)-(.*)")
  167. get_platform = get_build_platform # XXX backward compat
  168. def compatible_platforms(provided,required):
  169. """Can code for the `provided` platform run on the `required` platform?
  170. Returns true if either platform is ``None``, or the platforms are equal.
  171. XXX Needs compatibility checks for Linux and other unixy OSes.
  172. """
  173. if provided is None or required is None or provided==required:
  174. return True # easy case
  175. # Mac OS X special cases
  176. reqMac = macosVersionString.match(required)
  177. if reqMac:
  178. provMac = macosVersionString.match(provided)
  179. # is this a Mac package?
  180. if not provMac:
  181. # this is backwards compatibility for packages built before
  182. # setuptools 0.6. All packages built after this point will
  183. # use the new macosx designation.
  184. provDarwin = darwinVersionString.match(provided)
  185. if provDarwin:
  186. dversion = int(provDarwin.group(1))
  187. macosversion = "%s.%s" % (reqMac.group(1), reqMac.group(2))
  188. if dversion == 7 and macosversion >= "10.3" or \
  189. dversion == 8 and macosversion >= "10.4":
  190. #import warnings
  191. #warnings.warn("Mac eggs should be rebuilt to "
  192. # "use the macosx designation instead of darwin.",
  193. # category=DeprecationWarning)
  194. return True
  195. return False # egg isn't macosx or legacy darwin
  196. # are they the same major version and machine type?
  197. if provMac.group(1) != reqMac.group(1) or \
  198. provMac.group(3) != reqMac.group(3):
  199. return False
  200. # is the required OS major update >= the provided one?
  201. if int(provMac.group(2)) > int(reqMac.group(2)):
  202. return False
  203. return True
  204. # XXX Linux and other platforms' special cases should go here
  205. return False
  206. def run_script(dist_spec, script_name):
  207. """Locate distribution `dist_spec` and run its `script_name` script"""
  208. ns = sys._getframe(1).f_globals
  209. name = ns['__name__']
  210. ns.clear()
  211. ns['__name__'] = name
  212. require(dist_spec)[0].run_script(script_name, ns)
  213. run_main = run_script # backward compatibility
  214. def get_distribution(dist):
  215. """Return a current distribution object for a Requirement or string"""
  216. if isinstance(dist,basestring): dist = Requirement.parse(dist)
  217. if isinstance(dist,Requirement): dist = get_provider(dist)
  218. if not isinstance(dist,Distribution):
  219. raise TypeError("Expected string, Requirement, or Distribution", dist)
  220. return dist
  221. def load_entry_point(dist, group, name):
  222. """Return `name` entry point of `group` for `dist` or raise ImportError"""
  223. return get_distribution(dist).load_entry_point(group, name)
  224. def get_entry_map(dist, group=None):
  225. """Return the entry point map for `group`, or the full entry map"""
  226. return get_distribution(dist).get_entry_map(group)
  227. def get_entry_info(dist, group, name):
  228. """Return the EntryPoint object for `group`+`name`, or ``None``"""
  229. return get_distribution(dist).get_entry_info(group, name)
  230. class IMetadataProvider:
  231. def has_metadata(name):
  232. """Does the package's distribution contain the named metadata?"""
  233. def get_metadata(name):
  234. """The named metadata resource as a string"""
  235. def get_metadata_lines(name):
  236. """Yield named metadata resource as list of non-blank non-comment lines
  237. Leading and trailing whitespace is stripped from each line, and lines
  238. with ``#`` as the first non-blank character are omitted."""
  239. def metadata_isdir(name):
  240. """Is the named metadata a directory? (like ``os.path.isdir()``)"""
  241. def metadata_listdir(name):
  242. """List of metadata names in the directory (like ``os.listdir()``)"""
  243. def run_script(script_name, namespace):
  244. """Execute the named script in the supplied namespace dictionary"""
  245. class IResourceProvider(IMetadataProvider):
  246. """An object that provides access to package resources"""
  247. def get_resource_filename(manager, resource_name):
  248. """Return a true filesystem path for `resource_name`
  249. `manager` must be an ``IResourceManager``"""
  250. def get_resource_stream(manager, resource_name):
  251. """Return a readable file-like object for `resource_name`
  252. `manager` must be an ``IResourceManager``"""
  253. def get_resource_string(manager, resource_name):
  254. """Return a string containing the contents of `resource_name`
  255. `manager` must be an ``IResourceManager``"""
  256. def has_resource(resource_name):
  257. """Does the package contain the named resource?"""
  258. def resource_isdir(resource_name):
  259. """Is the named resource a directory? (like ``os.path.isdir()``)"""
  260. def resource_listdir(resource_name):
  261. """List of resource names in the directory (like ``os.listdir()``)"""
  262. class WorkingSet(object):
  263. """A collection of active distributions on sys.path (or a similar list)"""
  264. def __init__(self, entries=None):
  265. """Create working set from list of path entries (default=sys.path)"""
  266. self.entries = []
  267. self.entry_keys = {}
  268. self.by_key = {}
  269. self.callbacks = []
  270. if entries is None:
  271. entries = sys.path
  272. for entry in entries:
  273. self.add_entry(entry)
  274. def add_entry(self, entry):
  275. """Add a path item to ``.entries``, finding any distributions on it
  276. ``find_distributions(entry, True)`` is used to find distributions
  277. corresponding to the path entry, and they are added. `entry` is
  278. always appended to ``.entries``, even if it is already present.
  279. (This is because ``sys.path`` can contain the same value more than
  280. once, and the ``.entries`` of the ``sys.path`` WorkingSet should always
  281. equal ``sys.path``.)
  282. """
  283. self.entry_keys.setdefault(entry, [])
  284. self.entries.append(entry)
  285. for dist in find_distributions(entry, True):
  286. self.add(dist, entry, False)
  287. def __contains__(self,dist):
  288. """True if `dist` is the active distribution for its project"""
  289. return self.by_key.get(dist.key) == dist
  290. def find(self, req):
  291. """Find a distribution matching requirement `req`
  292. If there is an active distribution for the requested project, this
  293. returns it as long as it meets the version requirement specified by
  294. `req`. But, if there is an active distribution for the project and it
  295. does *not* meet the `req` requirement, ``VersionConflict`` is raised.
  296. If there is no active distribution for the requested project, ``None``
  297. is returned.
  298. """
  299. dist = self.by_key.get(req.key)
  300. if dist is not None and dist not in req:
  301. raise VersionConflict(dist,req) # XXX add more info
  302. else:
  303. return dist
  304. def iter_entry_points(self, group, name=None):
  305. """Yield entry point objects from `group` matching `name`
  306. If `name` is None, yields all entry points in `group` from all
  307. distributions in the working set, otherwise only ones matching
  308. both `group` and `name` are yielded (in distribution order).
  309. """
  310. for dist in self:
  311. entries = dist.get_entry_map(group)
  312. if name is None:
  313. for ep in entries.values():
  314. yield ep
  315. elif name in entries:
  316. yield entries[name]
  317. def run_script(self, requires, script_name):
  318. """Locate distribution for `requires` and run `script_name` script"""
  319. ns = sys._getframe(1).f_globals
  320. name = ns['__name__']
  321. ns.clear()
  322. ns['__name__'] = name
  323. self.require(requires)[0].run_script(script_name, ns)
  324. def __iter__(self):
  325. """Yield distributions for non-duplicate projects in the working set
  326. The yield order is the order in which the items' path entries were
  327. added to the working set.
  328. """
  329. seen = {}
  330. for item in self.entries:
  331. for key in self.entry_keys[item]:
  332. if key not in seen:
  333. seen[key]=1
  334. yield self.by_key[key]
  335. def add(self, dist, entry=None, insert=True):
  336. """Add `dist` to working set, associated with `entry`
  337. If `entry` is unspecified, it defaults to the ``.location`` of `dist`.
  338. On exit from this routine, `entry` is added to the end of the working
  339. set's ``.entries`` (if it wasn't already present).
  340. `dist` is only added to the working set if it's for a project that
  341. doesn't already have a distribution in the set. If it's added, any
  342. callbacks registered with the ``subscribe()`` method will be called.
  343. """
  344. if insert:
  345. dist.insert_on(self.entries, entry)
  346. if entry is None:
  347. entry = dist.location
  348. keys = self.entry_keys.setdefault(entry,[])
  349. keys2 = self.entry_keys.setdefault(dist.location,[])
  350. if dist.key in self.by_key:
  351. return # ignore hidden distros
  352. self.by_key[dist.key] = dist
  353. if dist.key not in keys:
  354. keys.append(dist.key)
  355. if dist.key not in keys2:
  356. keys2.append(dist.key)
  357. self._added_new(dist)
  358. def resolve(self, requirements, env=None, installer=None):
  359. """List all distributions needed to (recursively) meet `requirements`
  360. `requirements` must be a sequence of ``Requirement`` objects. `env`,
  361. if supplied, should be an ``Environment`` instance. If
  362. not supplied, it defaults to all distributions available within any
  363. entry or distribution in the working set. `installer`, if supplied,
  364. will be invoked with each requirement that cannot be met by an
  365. already-installed distribution; it should return a ``Distribution`` or
  366. ``None``.
  367. """
  368. requirements = list(requirements)[::-1] # set up the stack
  369. processed = {} # set of processed requirements
  370. best = {} # key -> dist
  371. to_activate = []
  372. while requirements:
  373. req = requirements.pop(0) # process dependencies breadth-first
  374. if req in processed:
  375. # Ignore cyclic or redundant dependencies
  376. continue
  377. dist = best.get(req.key)
  378. if dist is None:
  379. # Find the best distribution and add it to the map
  380. dist = self.by_key.get(req.key)
  381. if dist is None:
  382. if env is None:
  383. env = Environment(self.entries)
  384. dist = best[req.key] = env.best_match(req, self, installer)
  385. if dist is None:
  386. raise DistributionNotFound(req) # XXX put more info here
  387. to_activate.append(dist)
  388. if dist not in req:
  389. # Oops, the "best" so far conflicts with a dependency
  390. raise VersionConflict(dist,req) # XXX put more info here
  391. requirements.extend(dist.requires(req.extras)[::-1])
  392. processed[req] = True
  393. return to_activate # return list of distros to activate
  394. def find_plugins(self,
  395. plugin_env, full_env=None, installer=None, fallback=True
  396. ):
  397. """Find all activatable distributions in `plugin_env`
  398. Example usage::
  399. distributions, errors = working_set.find_plugins(
  400. Environment(plugin_dirlist)
  401. )
  402. map(working_set.add, distributions) # add plugins+libs to sys.path
  403. print "Couldn't load", errors # display errors
  404. The `plugin_env` should be an ``Environment`` instance that contains
  405. only distributions that are in the project's "plugin directory" or
  406. directories. The `full_env`, if supplied, should be an ``Environment``
  407. contains all currently-available distributions. If `full_env` is not
  408. supplied, one is created automatically from the ``WorkingSet`` this
  409. method is called on, which will typically mean that every directory on
  410. ``sys.path`` will be scanned for distributions.
  411. `installer` is a standard installer callback as used by the
  412. ``resolve()`` method. The `fallback` flag indicates whether we should
  413. attempt to resolve older versions of a plugin if the newest version
  414. cannot be resolved.
  415. This method returns a 2-tuple: (`distributions`, `error_info`), where
  416. `distributions` is a list of the distributions found in `plugin_env`
  417. that were loadable, along with any other distributions that are needed
  418. to resolve their dependencies. `error_info` is a dictionary mapping
  419. unloadable plugin distributions to an exception instance describing the
  420. error that occurred. Usually this will be a ``DistributionNotFound`` or
  421. ``VersionConflict`` instance.
  422. """
  423. plugin_projects = list(plugin_env)
  424. plugin_projects.sort() # scan project names in alphabetic order
  425. error_info = {}
  426. distributions = {}
  427. if full_env is None:
  428. env = Environment(self.entries)
  429. env += plugin_env
  430. else:
  431. env = full_env + plugin_env
  432. shadow_set = self.__class__([])
  433. map(shadow_set.add, self) # put all our entries in shadow_set
  434. for project_name in plugin_projects:
  435. for dist in plugin_env[project_name]:
  436. req = [dist.as_requirement()]
  437. try:
  438. resolvees = shadow_set.resolve(req, env, installer)
  439. except ResolutionError,v:
  440. error_info[dist] = v # save error info
  441. if fallback:
  442. continue # try the next older version of project
  443. else:
  444. break # give up on this project, keep going
  445. else:
  446. map(shadow_set.add, resolvees)
  447. distributions.update(dict.fromkeys(resolvees))
  448. # success, no need to try any more versions of this project
  449. break
  450. distributions = list(distributions)
  451. distributions.sort()
  452. return distributions, error_info
  453. def require(self, *requirements):
  454. """Ensure that distributions matching `requirements` are activated
  455. `requirements` must be a string or a (possibly-nested) sequence
  456. thereof, specifying the distributions and versions required. The
  457. return value is a sequence of the distributions that needed to be
  458. activated to fulfill the requirements; all relevant distributions are
  459. included, even if they were already activated in this working set.
  460. """
  461. needed = self.resolve(parse_requirements(requirements))
  462. for dist in needed:
  463. self.add(dist)
  464. return needed
  465. def subscribe(self, callback):
  466. """Invoke `callback` for all distributions (including existing ones)"""
  467. if callback in self.callbacks:
  468. return
  469. self.callbacks.append(callback)
  470. for dist in self:
  471. callback(dist)
  472. def _added_new(self, dist):
  473. for callback in self.callbacks:
  474. callback(dist)
  475. def __getstate__(self):
  476. return (
  477. self.entries[:], self.entry_keys.copy(), self.by_key.copy(),
  478. self.callbacks[:]
  479. )
  480. def __setstate__(self, (entries, keys, by_key, callbacks)):
  481. self.entries = entries[:]
  482. self.entry_keys = keys.copy()
  483. self.by_key = by_key.copy()
  484. self.callbacks = callbacks[:]
  485. class Environment(object):
  486. """Searchable snapshot of distributions on a search path"""
  487. def __init__(self, search_path=None, platform=get_supported_platform(), python=PY_MAJOR):
  488. """Snapshot distributions available on a search path
  489. Any distributions found on `search_path` are added to the environment.
  490. `search_path` should be a sequence of ``sys.path`` items. If not
  491. supplied, ``sys.path`` is used.
  492. `platform` is an optional string specifying the name of the platform
  493. that platform-specific distributions must be compatible with. If
  494. unspecified, it defaults to the current platform. `python` is an
  495. optional string naming the desired version of Python (e.g. ``'2.4'``);
  496. it defaults to the current version.
  497. You may explicitly set `platform` (and/or `python`) to ``None`` if you
  498. wish to map *all* distributions, not just those compatible with the
  499. running platform or Python version.
  500. """
  501. self._distmap = {}
  502. self._cache = {}
  503. self.platform = platform
  504. self.python = python
  505. self.scan(search_path)
  506. def can_add(self, dist):
  507. """Is distribution `dist` acceptable for this environment?
  508. The distribution must match the platform and python version
  509. requirements specified when this environment was created, or False
  510. is returned.
  511. """
  512. return (self.python is None or dist.py_version is None
  513. or dist.py_version==self.python) \
  514. and compatible_platforms(dist.platform,self.platform)
  515. def remove(self, dist):
  516. """Remove `dist` from the environment"""
  517. self._distmap[dist.key].remove(dist)
  518. def scan(self, search_path=None):
  519. """Scan `search_path` for distributions usable in this environment
  520. Any distributions found are added to the environment.
  521. `search_path` should be a sequence of ``sys.path`` items. If not
  522. supplied, ``sys.path`` is used. Only distributions conforming to
  523. the platform/python version defined at initialization are added.
  524. """
  525. if search_path is None:
  526. search_path = sys.path
  527. for item in search_path:
  528. for dist in find_distributions(item):
  529. self.add(dist)
  530. def __getitem__(self,project_name):
  531. """Return a newest-to-oldest list of distributions for `project_name`
  532. """
  533. try:
  534. return self._cache[project_name]
  535. except KeyError:
  536. project_name = project_name.lower()
  537. if project_name not in self._distmap:
  538. return []
  539. if project_name not in self._cache:
  540. dists = self._cache[project_name] = self._distmap[project_name]
  541. _sort_dists(dists)
  542. return self._cache[project_name]
  543. def add(self,dist):
  544. """Add `dist` if we ``can_add()`` it and it isn't already added"""
  545. if self.can_add(dist) and dist.has_version():
  546. dists = self._distmap.setdefault(dist.key,[])
  547. if dist not in dists:
  548. dists.append(dist)
  549. if dist.key in self._cache:
  550. _sort_dists(self._cache[dist.key])
  551. def best_match(self, req, working_set, installer=None):
  552. """Find distribution best matching `req` and usable on `working_set`
  553. This calls the ``find(req)`` method of the `working_set` to see if a
  554. suitable distribution is already active. (This may raise
  555. ``VersionConflict`` if an unsuitable version of the project is already
  556. active in the specified `working_set`.) If a suitable distribution
  557. isn't active, this method returns the newest distribution in the
  558. environment that meets the ``Requirement`` in `req`. If no suitable
  559. distribution is found, and `installer` is supplied, then the result of
  560. calling the environment's ``obtain(req, installer)`` method will be
  561. returned.
  562. """
  563. dist = working_set.find(req)
  564. if dist is not None:
  565. return dist
  566. for dist in self[req.key]:
  567. if dist in req:
  568. return dist
  569. return self.obtain(req, installer) # try and download/install
  570. def obtain(self, requirement, installer=None):
  571. """Obtain a distribution matching `requirement` (e.g. via download)
  572. Obtain a distro that matches requirement (e.g. via download). In the
  573. base ``Environment`` class, this routine just returns
  574. ``installer(requirement)``, unless `installer` is None, in which case
  575. None is returned instead. This method is a hook that allows subclasses
  576. to attempt other ways of obtaining a distribution before falling back
  577. to the `installer` argument."""
  578. if installer is not None:
  579. return installer(requirement)
  580. def __iter__(self):
  581. """Yield the unique project names of the available distributions"""
  582. for key in self._distmap.keys():
  583. if self[key]: yield key
  584. def __iadd__(self, other):
  585. """In-place addition of a distribution or environment"""
  586. if isinstance(other,Distribution):
  587. self.add(other)
  588. elif isinstance(other,Environment):
  589. for project in other:
  590. for dist in other[project]:
  591. self.add(dist)
  592. else:
  593. raise TypeError("Can't add %r to environment" % (other,))
  594. return self
  595. def __add__(self, other):
  596. """Add an environment or distribution to an environment"""
  597. new = self.__class__([], platform=None, python=None)
  598. for env in self, other:
  599. new += env
  600. return new
  601. AvailableDistributions = Environment # XXX backward compatibility
  602. class ExtractionError(RuntimeError):
  603. """An error occurred extracting a resource
  604. The following attributes are available from instances of this exception:
  605. manager
  606. The resource manager that raised this exception
  607. cache_path
  608. The base directory for resource extraction
  609. original_error
  610. The exception instance that caused extraction to fail
  611. """
  612. class ResourceManager:
  613. """Manage resource extraction and packages"""
  614. extraction_path = None
  615. def __init__(self):
  616. self.cached_files = {}
  617. def resource_exists(self, package_or_requirement, resource_name):
  618. """Does the named resource exist?"""
  619. return get_provider(package_or_requirement).has_resource(resource_name)
  620. def resource_isdir(self, package_or_requirement, resource_name):
  621. """Is the named resource an existing directory?"""
  622. return get_provider(package_or_requirement).resource_isdir(
  623. resource_name
  624. )
  625. def resource_filename(self, package_or_requirement, resource_name):
  626. """Return a true filesystem path for specified resource"""
  627. return get_provider(package_or_requirement).get_resource_filename(
  628. self, resource_name
  629. )
  630. def resource_stream(self, package_or_requirement, resource_name):
  631. """Return a readable file-like object for specified resource"""
  632. return get_provider(package_or_requirement).get_resource_stream(
  633. self, resource_name
  634. )
  635. def resource_string(self, package_or_requirement, resource_name):
  636. """Return specified resource as a string"""
  637. return get_provider(package_or_requirement).get_resource_string(
  638. self, resource_name
  639. )
  640. def resource_listdir(self, package_or_requirement, resource_name):
  641. """List the contents of the named resource directory"""
  642. return get_provider(package_or_requirement).resource_listdir(
  643. resource_name
  644. )
  645. def extraction_error(self):
  646. """Give an error message for problems extracting file(s)"""
  647. old_exc = sys.exc_info()[1]
  648. cache_path = self.extraction_path or get_default_cache()
  649. err = ExtractionError("""Can't extract file(s) to egg cache
  650. The following error occurred while trying to extract file(s) to the Python egg
  651. cache:
  652. %s
  653. The Python egg cache directory is currently set to:
  654. %s
  655. Perhaps your account does not have write access to this directory? You can
  656. change the cache directory by setting the PYTHON_EGG_CACHE environment
  657. variable to point to an accessible directory.
  658. """ % (old_exc, cache_path)
  659. )
  660. err.manager = self
  661. err.cache_path = cache_path
  662. err.original_error = old_exc
  663. raise err
  664. def get_cache_path(self, archive_name, names=()):
  665. """Return absolute location in cache for `archive_name` and `names`
  666. The parent directory of the resulting path will be created if it does
  667. not already exist. `archive_name` should be the base filename of the
  668. enclosing egg (which may not be the name of the enclosing zipfile!),
  669. including its ".egg" extension. `names`, if provided, should be a
  670. sequence of path name parts "under" the egg's extraction location.
  671. This method should only be called by resource providers that need to
  672. obtain an extraction location, and only for names they intend to
  673. extract, as it tracks the generated names for possible cleanup later.
  674. """
  675. extract_path = self.extraction_path or get_default_cache()
  676. target_path = os.path.join(extract_path, archive_name+'-tmp', *names)
  677. try:
  678. _bypass_ensure_directory(target_path)
  679. except:
  680. self.extraction_error()
  681. self.cached_files[target_path] = 1
  682. return target_path
  683. def postprocess(self, tempname, filename):
  684. """Perform any platform-specific postprocessing of `tempname`
  685. This is where Mac header rewrites should be done; other platforms don't
  686. have anything special they should do.
  687. Resource providers should call this method ONLY after successfully
  688. extracting a compressed resource. They must NOT call it on resources
  689. that are already in the filesystem.
  690. `tempname` is the current (temporary) name of the file, and `filename`
  691. is the name it will be renamed to by the caller after this routine
  692. returns.
  693. """
  694. if os.name == 'posix':
  695. # Make the resource executable
  696. mode = ((os.stat(tempname).st_mode) | 0555) & 07777
  697. os.chmod(tempname, mode)
  698. def set_extraction_path(self, path):
  699. """Set the base path where resources will be extracted to, if needed.
  700. If you do not call this routine before any extractions take place, the
  701. path defaults to the return value of ``get_default_cache()``. (Which
  702. is based on the ``PYTHON_EGG_CACHE`` environment variable, with various
  703. platform-specific fallbacks. See that routine's documentation for more
  704. details.)
  705. Resources are extracted to subdirectories of this path based upon
  706. information given by the ``IResourceProvider``. You may set this to a
  707. temporary directory, but then you must call ``cleanup_resources()`` to
  708. delete the extracted files when done. There is no guarantee that
  709. ``cleanup_resources()`` will be able to remove all extracted files.
  710. (Note: you may not change the extraction path for a given resource
  711. manager once resources have been extracted, unless you first call
  712. ``cleanup_resources()``.)
  713. """
  714. if self.cached_files:
  715. raise ValueError(
  716. "Can't change extraction path, files already extracted"
  717. )
  718. self.extraction_path = path
  719. def cleanup_resources(self, force=False):
  720. """
  721. Delete all extracted resource files and directories, returning a list
  722. of the file and directory names that could not be successfully removed.
  723. This function does not have any concurrency protection, so it should
  724. generally only be called when the extraction path is a temporary
  725. directory exclusive to a single process. This method is not
  726. automatically called; you must call it explicitly or register it as an
  727. ``atexit`` function if you wish to ensure cleanup of a temporary
  728. directory used for extractions.
  729. """
  730. # XXX
  731. def get_default_cache():
  732. """Determine the default cache location
  733. This returns the ``PYTHON_EGG_CACHE`` environment variable, if set.
  734. Otherwise, on Windows, it returns a "Python-Eggs" subdirectory of the
  735. "Application Data" directory. On all other systems, it's "~/.python-eggs".
  736. """
  737. try:
  738. return os.environ['PYTHON_EGG_CACHE']
  739. except KeyError:
  740. pass
  741. if os.name!='nt':
  742. return os.path.expanduser('~/.python-eggs')
  743. app_data = 'Application Data' # XXX this may be locale-specific!
  744. app_homes = [
  745. (('APPDATA',), None), # best option, should be locale-safe
  746. (('USERPROFILE',), app_data),
  747. (('HOMEDRIVE','HOMEPATH'), app_data),
  748. (('HOMEPATH',), app_data),
  749. (('HOME',), None),
  750. (('WINDIR',), app_data), # 95/98/ME
  751. ]
  752. for keys, subdir in app_homes:
  753. dirname = ''
  754. for key in keys:
  755. if key in os.environ:
  756. dirname = os.path.join(dirname, os.environ[key])
  757. else:
  758. break
  759. else:
  760. if subdir:
  761. dirname = os.path.join(dirname,subdir)
  762. return os.path.join(dirname, 'Python-Eggs')
  763. else:
  764. raise RuntimeError(
  765. "Please set the PYTHON_EGG_CACHE enviroment variable"
  766. )
  767. def safe_name(name):
  768. """Convert an arbitrary string to a standard distribution name
  769. Any runs of non-alphanumeric/. characters are replaced with a single '-'.
  770. """
  771. return re.sub('[^A-Za-z0-9.]+', '-', name)
  772. def safe_version(version):
  773. """Convert an arbitrary string to a standard version string
  774. Spaces become dots, and all other non-alphanumeric characters become
  775. dashes, with runs of multiple dashes condensed to a single dash.
  776. """
  777. version = version.replace(' ','.')
  778. return re.sub('[^A-Za-z0-9.]+', '-', version)
  779. def safe_extra(extra):
  780. """Convert an arbitrary string to a standard 'extra' name
  781. Any runs of non-alphanumeric characters are replaced with a single '_',
  782. and the result is always lowercased.
  783. """
  784. return re.sub('[^A-Za-z0-9.]+', '_', extra).lower()
  785. def to_filename(name):
  786. """Convert a project or version name to its filename-escaped form
  787. Any '-' characters are currently replaced with '_'.
  788. """
  789. return name.replace('-','_')
  790. class NullProvider:
  791. """Try to implement resources and metadata for arbitrary PEP 302 loaders"""
  792. egg_name = None
  793. egg_info = None
  794. loader = None
  795. def __init__(self, module):
  796. self.loader = getattr(module, '__loader__', None)
  797. self.module_path = os.path.dirname(getattr(module, '__file__', ''))
  798. def get_resource_filename(self, manager, resource_name):
  799. return self._fn(self.module_path, resource_name)
  800. def get_resource_stream(self, manager, resource_name):
  801. return StringIO(self.get_resource_string(manager, resource_name))
  802. def get_resource_string(self, manager, resource_name):
  803. return self._get(self._fn(self.module_path, resource_name))
  804. def has_resource(self, resource_name):
  805. return self._has(self._fn(self.module_path, resource_name))
  806. def has_metadata(self, name):
  807. return self.egg_info and self._has(self._fn(self.egg_info,name))
  808. def get_metadata(self, name):
  809. if not self.egg_info:
  810. return ""
  811. return self._get(self._fn(self.egg_info,name))
  812. def get_metadata_lines(self, name):
  813. return yield_lines(self.get_metadata(name))
  814. def resource_isdir(self,resource_name):
  815. return self._isdir(self._fn(self.module_path, resource_name))
  816. def metadata_isdir(self,name):
  817. return self.egg_info and self._isdir(self._fn(self.egg_info,name))
  818. def resource_listdir(self,resource_name):
  819. return self._listdir(self._fn(self.module_path,resource_name))
  820. def metadata_listdir(self,name):
  821. if self.egg_info:
  822. return self._listdir(self._fn(self.egg_info,name))
  823. return []
  824. def run_script(self,script_name,namespace):
  825. script = 'scripts/'+script_name
  826. if not self.has_metadata(script):
  827. raise ResolutionError("No script named %r" % script_name)
  828. script_text = self.get_metadata(script).replace('\r\n','\n')
  829. script_text = script_text.replace('\r','\n')
  830. script_filename = self._fn(self.egg_info,script)
  831. namespace['__file__'] = script_filename
  832. if os.path.exists(script_filename):
  833. execfile(script_filename, namespace, namespace)
  834. else:
  835. from linecache import cache
  836. cache[script_filename] = (
  837. len(script_text), 0, script_text.split('\n'), script_filename
  838. )
  839. script_code = compile(script_text,script_filename,'exec')
  840. exec script_code in namespace, namespace
  841. def _has(self, path):
  842. raise NotImplementedError(
  843. "Can't perform this operation for unregistered loader type"
  844. )
  845. def _isdir(self, path):
  846. raise NotImplementedError(
  847. "Can't perform this operation for unregistered loader type"
  848. )
  849. def _listdir(self, path):
  850. raise NotImplementedError(
  851. "Can't perform this operation for unregistered loader type"
  852. )
  853. def _fn(self, base, resource_name):
  854. if resource_name:
  855. return os.path.join(base, *resource_name.split('/'))
  856. return base
  857. def _get(self, path):
  858. if hasattr(self.loader, 'get_data'):
  859. return self.loader.get_data(path)
  860. raise NotImplementedError(
  861. "Can't perform this operation for loaders without 'get_data()'"
  862. )
  863. register_loader_type(object, NullProvider)
  864. class EggProvider(NullProvider):
  865. """Provider based on a virtual filesystem"""
  866. def __init__(self,module):
  867. NullProvider.__init__(self,module)
  868. self._setup_prefix()
  869. def _setup_prefix(self):
  870. # we assume here that our metadata may be nested inside a "basket"
  871. # of multiple eggs; that's why we use module_path instead of .archive
  872. path = self.module_path
  873. old = None
  874. while path!=old:
  875. if path.lower().endswith('.egg'):
  876. self.egg_name = os.path.basename(path)
  877. self.egg_info = os.path.join(path, 'EGG-INFO')
  878. self.egg_root = path
  879. break
  880. old = path
  881. path, base = os.path.split(path)
  882. class DefaultProvider(EggProvider):
  883. """Provides access to package resources in the filesystem"""
  884. def _has(self, path):
  885. return os.path.exists(path)
  886. def _isdir(self,path):
  887. return os.path.isdir(path)
  888. def _listdir(self,path):
  889. return os.listdir(path)
  890. def get_resource_stream(self, manager, resource_name):
  891. return open(self._fn(self.module_path, resource_name), 'rb')
  892. def _get(self, path):
  893. stream = open(path, 'rb')
  894. try:
  895. return stream.read()
  896. finally:
  897. stream.close()
  898. register_loader_type(type(None), DefaultProvider)
  899. class EmptyProvider(NullProvider):
  900. """Provider that returns nothing for all requests"""
  901. _isdir = _has = lambda self,path: False
  902. _get = lambda self,path: ''
  903. _listdir = lambda self,path: []
  904. module_path = None
  905. def __init__(self):
  906. pass
  907. empty_provider = EmptyProvider()
  908. class ZipProvider(EggProvider):
  909. """Resource support for zips and eggs"""
  910. eagers = None
  911. def __init__(self, module):
  912. EggProvider.__init__(self,module)
  913. self.zipinfo = zipimport._zip_directory_cache[self.loader.archive]
  914. self.zip_pre = self.loader.archive+os.sep
  915. def _zipinfo_name(self, fspath):
  916. # Convert a virtual filename (full path to file) into a zipfile subpath
  917. # usable with the zipimport directory cache for our target archive
  918. if fspath.startswith(self.zip_pre):
  919. return fspath[len(self.zip_pre):]
  920. raise AssertionError(
  921. "%s is not a subpath of %s" % (fspath,self.zip_pre)
  922. )
  923. def _parts(self,zip_path):
  924. # Convert a zipfile subpath into an egg-relative path part list
  925. fspath = self.zip_pre+zip_path # pseudo-fs path
  926. if fspath.startswith(self.egg_root+os.sep):
  927. return fspath[len(self.egg_root)+1:].split(os.sep)
  928. raise AssertionError(
  929. "%s is not a subpath of %s" % (fspath,self.egg_root)
  930. )
  931. def get_resource_filename(self, manager, resource_name):
  932. if not self.egg_name:
  933. raise NotImplementedError(
  934. "resource_filename() only supported for .egg, not .zip"
  935. )
  936. # no need to lock for extraction, since we use temp names
  937. zip_path = self._resource_to_zip(resource_name)
  938. eagers = self._get_eager_resources()
  939. if '/'.join(self._parts(zip_path)) in eagers:
  940. for name in eagers:
  941. self._extract_resource(manager, self._eager_to_zip(name))
  942. return self._extract_resource(manager, zip_path)
  943. def _extract_resource(self, manager, zip_path):
  944. if zip_path in self._index():
  945. for name in self._index()[zip_path]:
  946. last = self._extract_resource(
  947. manager, os.path.join(zip_path, name)
  948. )
  949. return os.path.dirname(last) # return the extracted directory name
  950. zip_stat = self.zipinfo[zip_path]
  951. t,d,size = zip_stat[5], zip_stat[6], zip_stat[3]
  952. date_time = (
  953. (d>>9)+1980, (d>>5)&0xF, d&0x1F, # ymd
  954. (t&0xFFFF)>>11, (t>>5)&0x3F, (t&0x1F) * 2, 0, 0, -1 # hms, etc.
  955. )
  956. timestamp = time.mktime(date_time)
  957. try:
  958. real_path = manager.get_cache_path(
  959. self.egg_name, self._parts(zip_path)
  960. )
  961. if os.path.isfile(real_path):
  962. stat = os.stat(real_path)
  963. if stat.st_size==size and stat.st_mtime==timestamp:
  964. # size and stamp match, don't bother extracting
  965. return real_path
  966. outf, tmpnam = _mkstemp(".$extract", dir=os.path.dirname(real_path))
  967. os.write(outf, self.loader.get_data(zip_path))
  968. os.close(outf)
  969. utime(tmpnam, (timestamp,timestamp))
  970. manager.postprocess(tmpnam, real_path)
  971. try:
  972. rename(tmpnam, real_path)
  973. except os.error:
  974. if os.path.isfile(real_path):
  975. stat = os.stat(real_path)
  976. if stat.st_size==size and stat.st_mtime==timestamp:
  977. # size and stamp match, somebody did it just ahead of
  978. # us, so we're done
  979. return real_path
  980. elif os.name=='nt': # Windows, del old file and retry
  981. unlink(real_path)
  982. rename(tmpnam, real_path)
  983. return real_path
  984. raise
  985. except os.error:
  986. manager.extraction_error() # report a user-friendly error
  987. return real_path
  988. def _get_eager_resources(self):
  989. if self.eagers is None:
  990. eagers = []
  991. for name in ('native_libs.txt', 'eager_resources.txt'):
  992. if self.has_metadata(name):
  993. eagers.extend(self.get_metadata_lines(name))
  994. self.eagers = eagers
  995. return self.eagers
  996. def _index(self):
  997. try:
  998. return self._dirindex
  999. except AttributeError:
  1000. ind = {}
  1001. for path in self.zipinfo:
  1002. parts = path.split(os.sep)
  1003. while parts:
  1004. parent = os.sep.join(parts[:-1])
  1005. if parent in ind:
  1006. ind[parent].append(parts[-1])
  1007. break
  1008. else:
  1009. ind[parent] = [parts.pop()]
  1010. self._dirindex = ind
  1011. return ind
  1012. def _has(self, fspath):
  1013. zip_path = self._zipinfo_name(fspath)
  1014. return zip_path in self.zipinfo or zip_path in self._index()
  1015. def _isdir(self,fspath):
  1016. return self._zipinfo_name(fspath) in self._index()
  1017. def _listdir(self,fspath):
  1018. return list(self._index().get(self._zipinfo_name(fspath), ()))
  1019. def _eager_to_zip(self,resource_name):
  1020. return self._zipinfo_name(self._fn(self.egg_root,resource_name))
  1021. def _resource_to_zip(self,resource_name):
  1022. return self._zipinfo_name(self._fn(self.module_path,resource_name))
  1023. register_loader_type(zipimport.zipimporter, ZipProvider)
  1024. class FileMetadata(EmptyProvider):
  1025. """Metadata handler for standalone PKG-INFO files
  1026. Usage::
  1027. metadata = FileMetadata("/path/to/PKG-INFO")
  1028. This provider rejects all data and metadata requests except for PKG-INFO,
  1029. which is treated as existing, and will be the contents of the file at
  1030. the provided location.
  1031. """
  1032. def __init__(self,path):
  1033. self.path = path
  1034. def has_metadata(self,name):
  1035. return name=='PKG-INFO'
  1036. def get_metadata(self,name):
  1037. if name=='PKG-INFO':
  1038. return open(self.path,'rU').read()
  1039. raise KeyError("No metadata except PKG-INFO is available")
  1040. def get_metadata_lines(self,name):
  1041. return yield_lines(self.get_metadata(name))
  1042. class PathMetadata(DefaultProvider):
  1043. """Metadata provider for egg directories
  1044. Usage::
  1045. # Development eggs:
  1046. egg_info = "/path/to/PackageName.egg-info"
  1047. base_dir = os.path.dirname(egg_info)
  1048. metadata = PathMetadata(base_dir, egg_info)
  1049. dist_name = os.path.splitext(os.path.basename(egg_info))[0]
  1050. dist = Distribution(basedir,project_name=dist_name,metadata=metadata)
  1051. # Unpacked egg directories:
  1052. egg_path = "/path/to/PackageName-ver-pyver-etc.egg"
  1053. metadata = PathMetadata(egg_path, os.path.join(egg_path,'EGG-INFO'))
  1054. dist = Distribution.from_filename(egg_path, metadata=metadata)
  1055. """
  1056. def __init__(self, path, egg_info):
  1057. self.module_path = path
  1058. self.egg_info = egg_info
  1059. class EggMetadata(ZipProvider):
  1060. """Metadata provider for .egg files"""
  1061. def __init__(self, importer):
  1062. """Create a metadata provider from a zipimporter"""
  1063. self.zipinfo = zipimport._zip_directory_cache[importer.archive]
  1064. self.zip_pre = importer.archive+os.sep
  1065. self.loader = importer
  1066. if importer.prefix:
  1067. self.module_path = os.path.join(importer.archive, importer.prefix)
  1068. else:
  1069. self.module_path = importer.archive
  1070. self._setup_prefix()
  1071. class ImpWrapper:
  1072. """PEP 302 Importer that wraps Python's "normal" import algorithm"""
  1073. def __init__(self, path=None):
  1074. self.path = path
  1075. def find_module(self, fullname, path=None):
  1076. subname = fullname.split(".")[-1]
  1077. if subname != fullname and self.path is None:
  1078. return None
  1079. if self.path is None:
  1080. path = None
  1081. else:
  1082. path = [self.path]
  1083. try:
  1084. file, filename, etc = imp.find_module(subname, path)
  1085. except ImportError:
  1086. return None
  1087. return ImpLoader(file, filename, etc)
  1088. class ImpLoader:
  1089. """PEP 302 Loader that wraps Python's "normal" import algorithm"""
  1090. def __init__(self, file, filename, etc):
  1091. self.file = file
  1092. self.filename = filename
  1093. self.etc = etc
  1094. def load_module(self, fullname):
  1095. try:
  1096. mod = imp.load_module(fullname, self.file, self.filename, self.etc)
  1097. finally:
  1098. if self.file: self.file.close()
  1099. # Note: we don't set __loader__ because we want the module to look
  1100. # normal; i.e. this is just a wrapper for standard import machinery
  1101. return mod
  1102. def get_importer(path_item):
  1103. """Retrieve a PEP 302 "importer" for the given path item
  1104. If there is no importer, this returns a wrapper around the builtin import
  1105. machinery. The returned importer is only cached if it was created by a
  1106. path hook.
  1107. """
  1108. try:
  1109. importer = sys.path_importer_cache[path_item]
  1110. except KeyError:
  1111. for hook in sys.path_hooks:
  1112. try:
  1113. importer = hook(path_item)
  1114. except ImportError:
  1115. pass
  1116. else:
  1117. break
  1118. else:
  1119. importer = None
  1120. sys.path_importer_cache.setdefault(path_item,importer)
  1121. if importer is None:
  1122. try:
  1123. importer = ImpWrapper(path_item)
  1124. except ImportError:
  1125. pass
  1126. return importer
  1127. try:
  1128. from pkgutil import get_importer, ImpImporter
  1129. except ImportError:
  1130. pass # Python 2.3 or 2.4, use our own implementation
  1131. else:
  1132. ImpWrapper = ImpImporter # Python 2.5, use pkgutil's implementation
  1133. del ImpLoader, ImpImporter
  1134. _declare_state('dict', _distribution_finders = {})
  1135. def register_finder(importer_type, distribution_finder):
  1136. """Register `distribution_finder` to find distributions in sys.path items
  1137. `importer_type` is the type or class of a PEP 302 "Importer" (sys.path item
  1138. handler), and `distribution_finder` is a callable that, passed a path
  1139. item and the importer instance, yields ``Distribution`` instances found on
  1140. that path item. See ``pkg_resources.find_on_path`` for an example."""
  1141. _distribution_finders[importer_type] = distribution_finder
  1142. def find_distributions(path_item, only=False):
  1143. """Yield distributions accessible via `path_item`"""
  1144. importer = get_importer(path_item)
  1145. finder = _find_adapter(_distribution_finders, importer)
  1146. return finder(importer, path_item, only)
  1147. def find_in_zip(importer, path_item, only=False):
  1148. metadata = EggMetadata(importer)
  1149. if metadata.has_metadata('PKG-INFO'):
  1150. yield Distribution.from_filename(path_item, metadata=metadata)
  1151. if only:
  1152. return # don't yield nested distros
  1153. for subitem in metadata.resource_listdir('/'):
  1154. if subitem.endswith('.egg'):
  1155. subpath = os.path.join(path_item, subitem)
  1156. for dist in find_in_zip(zipimport.zipimporter(subpath), subpath):
  1157. yield dist
  1158. register_finder(zipimport.zipimporter, find_in_zip)
  1159. def StringIO(*args, **kw):
  1160. """Thunk to load the real StringIO on demand"""
  1161. global StringIO
  1162. try:
  1163. from cStringIO import StringIO
  1164. except ImportError:
  1165. from StringIO import StringIO
  1166. return StringIO(*args,**kw)
  1167. def find_nothing(importer, path_item, only=False):
  1168. return ()
  1169. register_finder(object,find_nothing)
  1170. def find_on_path(importer, path_item, only=False):
  1171. """Yield distributions accessible on a sys.path directory"""
  1172. path_item = _normalize_cached(path_item)
  1173. if os.path.isdir(path_item) and os.access(path_item, os.R_OK):
  1174. if path_item.lower().endswith('.egg'):
  1175. # unpacked egg
  1176. yield Distribution.from_filename(
  1177. path_item, metadata=PathMetadata(
  1178. path_item, os.path.join(path_item,'EGG-INFO')
  1179. )
  1180. )
  1181. else:
  1182. # scan for .egg and .egg-info in directory
  1183. for entry in os.listdir(path_item):
  1184. lower = entry.lower()
  1185. if lower.endswith('.egg-info'):
  1186. fullpath = os.path.join(path_item, entry)
  1187. if os.path.isdir(fullpath):
  1188. # egg-info directory, allow getting metadata
  1189. metadata = PathMetadata(path_item, fullpath)
  1190. else:
  1191. metadata = FileMetadata(fullpath)
  1192. yield Distribution.from_location(
  1193. path_item,entry,metadata,precedence=DEVELOP_DIST
  1194. )
  1195. elif not only and lower.endswith('.egg'):
  1196. for dist in find_distributions(os.path.join(path_item, entry)):
  1197. yield dist
  1198. elif not only and lower.endswith('.egg-link'):
  1199. for line in file(os.path.join(path_item, entry)):
  1200. if not line.strip(): continue
  1201. for item in find_distributions(os.path.join(path_item,line.rstrip())):
  1202. yield item
  1203. break
  1204. register_finder(ImpWrapper,find_on_path)
  1205. _declare_state('dict', _namespace_handlers = {})
  1206. _declare_state('dict', _namespace_packages = {})
  1207. def register_namespace_handler(importer_type, namespace_handler):
  1208. """Register `namespace_handler` to declare namespace packages
  1209. `importer_type` is the type or class of a PEP 302 "Importer" (sys.path item
  1210. handler), and `namespace_handler` is a callable like this::
  1211. def namespace_handler(importer,path_entry,moduleName,module):
  1212. # return a path_entry to use for child packages
  1213. Namespace handlers are only called if the importer object has already
  1214. agreed that it can handle the relevant path item, and they should only
  1215. return a subpath if the module __path__ does not already contain an
  1216. equivalent subpath. For an example namespace handler, see
  1217. ``pkg_resources.file_ns_handler``.
  1218. """
  1219. _namespace_handlers[importer_type] = namespace_handler
  1220. def _handle_ns(packageName, path_item):
  1221. """Ensure that named package includes a subpath of path_item (if needed)"""
  1222. importer = get_importer(path_item)
  1223. if importer is None:
  1224. return None
  1225. loader = importer.find_module(packageName)
  1226. if loader is None:
  1227. return None
  1228. module = sys.modules.get(packageName)
  1229. if module is None:
  1230. module = sys.modules[packageName] = imp.new_module(packageName)
  1231. module.__path__ = []; _set_parent_ns(packageName)
  1232. elif not hasattr(module,'__path__'):
  1233. raise TypeError("Not a package:", packageName)
  1234. handler = _find_adapter(_namespace_handlers, importer)
  1235. subpath = handler(importer,path_item,packageName,module)
  1236. if subpath is not None:
  1237. path = module.__path__; path.append(subpath)
  1238. loader.load_module(packageName); module.__path__ = path
  1239. return subpath
  1240. def declare_namespace(packageName):
  1241. """Declare that package 'packageName' is a namespace package"""
  1242. imp.acquire_lock()
  1243. try:
  1244. if packageName in _namespace_packages:
  1245. return
  1246. path, parent = sys.path, None
  1247. if '.' in packageName:
  1248. parent = '.'.join(packageName.split('.')[:-1])
  1249. declare_namespace(parent)
  1250. __import__(parent)
  1251. try:
  1252. path = sys.modules[parent].__path__
  1253. except AttributeError:
  1254. raise TypeError("Not a package:", parent)
  1255. # Track what packages are namespaces, so when new path items are added,
  1256. # they can be updated
  1257. _namespace_packages.setdefault(parent,[]).append(packageName)
  1258. _namespace_packages.setdefault(packageName,[])
  1259. for path_item in path:
  1260. # Ensure all the parent's path items are reflected in the child,
  1261. # if they apply
  1262. _handle_ns(packageName, path_item)
  1263. finally:
  1264. imp.release_lock()
  1265. def fixup_namespace_packages(path_item, parent=None):
  1266. """Ensure that previously-declared namespace packages include path_item"""
  1267. imp.acquire_lock()
  1268. try:
  1269. for package in _namespace_packages.get(parent,()):
  1270. subpath = _handle_ns(package, path_item)
  1271. if subpath: fixup_namespace_packages(subpath,package)
  1272. finally:
  1273. imp.release_lock()
  1274. def file_ns_handler(importer, path_item, packageName, module):
  1275. """Compute an ns-package subpath for a filesystem or zipfile importer"""
  1276. subpath = os.path.join(path_item, packageName.split('.')[-1])
  1277. normalized = _normalize_cached(subpath)
  1278. for item in module.__path__:
  1279. if _normalize_cached(item)==normalized:
  1280. break
  1281. else:
  1282. # Only return the path if it's not already there
  1283. return subpath
  1284. register_namespace_handler(ImpWrapper,file_ns_handler)
  1285. register_namespace_handler(zipimport.zipimporter,file_ns_handler)
  1286. def null_ns_handler(importer, path_item, packageName, module):
  1287. return None
  1288. register_namespace_handler(object,null_ns_handler)
  1289. def normalize_path(filename):
  1290. """Normalize a file/dir name for comparison purposes"""
  1291. return os.path.normcase(os.path.realpath(filename))
  1292. def _normalize_cached(filename,_cache={}):
  1293. try:
  1294. return _cache[filename]
  1295. except KeyError:
  1296. _cache[filename] = result = normalize_path(filename)
  1297. return result
  1298. def _set_parent_ns(packageName):
  1299. parts = packageName.split('.')
  1300. name = parts.pop()
  1301. if parts:
  1302. parent = '.'.join(parts)
  1303. setattr(sys.modules[parent], name, sys.modules[packageName])
  1304. def yield_lines(strs):
  1305. """Yield non-empty/non-comment lines of a ``basestring`` or sequence"""
  1306. if isinstance(strs,basestring):
  1307. for s in strs.splitlines():
  1308. s = s.strip()
  1309. if s and not s.startswith('#'): # skip blank lines/comments
  1310. yield s
  1311. else:
  1312. for ss in strs:
  1313. for s in yield_lines(ss):
  1314. yield s
  1315. LINE_END = re.compile(r"\s*(#.*)?$").match # whitespace and comment
  1316. CONTINUE = re.compile(r"\s*\\\s*(#.*)?$").match # line continuation
  1317. DISTRO = re.compile(r"\s*((\w|[-.])+)").match # Distribution or extra
  1318. VERSION = re.compile(r"\s*(<=?|>=?|==|!=)\s*((\w|[-.])+)").match # ver. info
  1319. COMMA = re.compile(r"\s*,").match # comma between items
  1320. OBRACKET = re.compile(r"\s*\[").match
  1321. CBRACKET = re.compile(r"\s*\]").match
  1322. MODULE = re.compile(r"\w+(\.\w+)*$").match
  1323. EGG_NAME = re.compile(
  1324. r"(?P<name>[^-]+)"
  1325. r"( -(?P<ver>[^-]+) (-py(?P<pyver>[^-]+) (-(?P<plat>.+))? )? )?",
  1326. re.VERBOSE | re.IGNORECASE
  1327. ).match
  1328. component_re = re.compile(r'(\d+ | [a-z]+ | \.| -)', re.VERBOSE)
  1329. replace = {'pre':'c', 'preview':'c','-':'final-','rc':'c','dev':'@'}.get
  1330. def _parse_version_parts(s):
  1331. for part in component_re.split(s):
  1332. part = replace(part,part)
  1333. if not part or part=='.':
  1334. continue
  1335. if part[:1] in '0123456789':
  1336. yield part.zfill(8) # pad for numeric comparison
  1337. else:
  1338. yield '*'+part
  1339. yield '*final' # ensure that alpha/beta/candidate are before final
  1340. def parse_version(s):
  1341. """Convert a version string to a chronologically-sortable key
  1342. This is a rough cross between distutils' StrictVersion and LooseVersion;
  1343. if you give it versions that would work with StrictVersion, then it behaves
  1344. the same; otherwise it acts like a slightly-smarter LooseVersion. It is
  1345. *possible* to create pathological version coding schemes that will fool
  1346. this parser, but they should be very rare in practice.
  1347. The returned value will be a tuple of strings. Numeric portions of the
  1348. version are padded to 8 digits so they will compare numerically, but
  1349. without relying on how numbers compare relative to strings. Dots are
  1350. dropped, but dashes are retained. Trailing zeros between alpha segments
  1351. or dashes are suppressed, so that e.g. "2.4.0" is considered the same as
  1352. "2.4". Alphanumeric parts are lower-cased.
  1353. The algorithm assumes that strings like "-" and any alpha string that
  1354. alphabetically follows "final" represents a "patch level". So, "2.4-1"
  1355. is assumed to be a branch or patch of "2.4", and therefore "2.4.1" is
  1356. considered newer than "2.4-1", which in turn is newer than "2.4".
  1357. Strings like "a", "b", "c", "alpha", "beta", "candidate" and so on (that
  1358. come before "final" alphabetically) are assumed to be pre-release versions,
  1359. so that the version "2.4" is considered newer than "2.4a1".
  1360. Finally, to handle miscellaneous cases, the strings "pre", "preview", and
  1361. "rc" are treated as if they were "c", i.e. as though they were release
  1362. candidates, and therefore are not as new as a version string that does not
  1363. contain them, and "dev" is replaced with an '@' so that it sorts lower than
  1364. than any other pre-release tag.
  1365. """
  1366. parts = []
  1367. for part in _parse_version_parts(s.lower()):
  1368. if part.startswith('*'):
  1369. if part<'*final': # remove '-' before a prerelease tag
  1370. while parts and parts[-1]=='*final-': parts.pop()
  1371. # remove trailing zeros from each series of numeric parts
  1372. while parts and parts[-1]=='00000000':
  1373. parts.pop()
  1374. parts.append(part)
  1375. return tuple(parts)
  1376. class EntryPoint(object):
  1377. """Object representing an advertised importable object"""
  1378. def __init__(self, name, module_name, attrs=(), extras=(), dist=None):
  1379. if not MODULE(module_name):
  1380. raise ValueError("Invalid module name", module_name)
  1381. self.name = name
  1382. self.module_name = module_name
  1383. self.attrs = tuple(attrs)
  1384. self.extras = Requirement.parse(("x[%s]" % ','.join(extras))).extras
  1385. self.dist = dist
  1386. def __str__(self):
  1387. s = "%s = %s" % (self.name, self.module_name)
  1388. if self.attrs:
  1389. s += ':' + '.'.join(self.attrs)
  1390. if self.extras:
  1391. s += ' [%s]' % ','.join(self.extras)
  1392. return s
  1393. def __repr__(self):
  1394. return "EntryPoint.parse(%r)" % str(self)
  1395. def load(self, require=True, env=None, installer=None):
  1396. if require: self.require(env, installer)
  1397. entry = __import__(self.module_name, globals(),globals(), ['__name__'])
  1398. for attr in self.attrs:
  1399. try:
  1400. entry = getattr(entry,attr)
  1401. except AttributeError:
  1402. raise ImportError("%r has no %r attribute" % (entry,attr))
  1403. return entry
  1404. def require(self, env=None, installer=None):
  1405. if self.extras and not self.dist:
  1406. raise UnknownExtra("Can't require() without a distribution", self)
  1407. map(working_set.add,
  1408. working_set.resolve(self.dist.requires(self.extras),env,installer))
  1409. #@classmethod
  1410. def parse(cls, src, dist=None):
  1411. """Parse a single entry point from string `src`
  1412. Entry point syntax follows the form::
  1413. name = some.module:some.attr [extra1,extra2]
  1414. The entry name and module name are required, but the ``:attrs`` and
  1415. ``[extras]`` parts are optional
  1416. """
  1417. try:
  1418. attrs = extras = ()
  1419. name,value = src.split('=',1)
  1420. if '[' in value:
  1421. value,extras = value.split('[',1)
  1422. req = Requirement.parse("x["+extras)
  1423. if req.specs: raise ValueError
  1424. extras = req.extras
  1425. if ':' in value:
  1426. value,attrs = value.split(':',1)
  1427. if not MODULE(attrs.rstrip()):
  1428. raise ValueError
  1429. attrs = attrs.rstrip().split('.')
  1430. except ValueError:
  1431. raise ValueError(
  1432. "EntryPoint must be in 'name=module:attrs [extras]' format",
  1433. src
  1434. )
  1435. else:
  1436. return cls(name.strip(), value.strip(), attrs, extras, dist)
  1437. parse = classmethod(parse)
  1438. #@classmethod
  1439. def parse_group(cls, group, lines, dist=None):
  1440. """Parse an entry point group"""
  1441. if not MODULE(group):
  1442. raise ValueError("Invalid group name", group)
  1443. this = {}
  1444. for line in yield_lines(lines):
  1445. ep = cls.parse(line, dist)
  1446. if ep.name in this:
  1447. raise ValueError("Duplicate entry point", group, ep.name)
  1448. this[ep.name]=ep
  1449. return this
  1450. parse_group = classmethod(parse_group)
  1451. #@classmethod
  1452. def parse_map(cls, data, dist=None):
  1453. """Parse a map of entry point groups"""
  1454. if isinstance(data,dict):
  1455. data = data.items()
  1456. else:
  1457. data = split_sections(data)
  1458. maps = {}
  1459. for group, lines in data:
  1460. if group is None:
  1461. if not lines:
  1462. continue
  1463. raise ValueError("Entry points must be listed in groups")
  1464. group = group.strip()
  1465. if group in maps:
  1466. raise ValueError("Duplicate group name", group)
  1467. maps[group] = cls.parse_group(group, lines, dist)
  1468. return maps
  1469. parse_map = classmethod(parse_map)
  1470. class Distribution(object):
  1471. """Wrap an actual or potential sys.path entry w/metadata"""
  1472. def __init__(self,
  1473. location=None, metadata=None, project_name=None, version=None,
  1474. py_version=PY_MAJOR, platform=None, precedence = EGG_DIST
  1475. ):
  1476. self.project_name = safe_name(project_name or 'Unknown')
  1477. if version is not None:
  1478. self._version = safe_version(version)
  1479. self.py_version = py_version
  1480. self.platform = platform
  1481. self.location = location
  1482. self.precedence = precedence
  1483. self._provider = metadata or empty_provider
  1484. #@classmethod
  1485. def from_location(cls,location,basename,metadata=None,**kw):
  1486. project_name, version, py_version, platform = [None]*4
  1487. basename, ext = os.path.splitext(basename)
  1488. if ext.lower() in (".egg",".egg-info"):
  1489. match = EGG_NAME(basename)
  1490. if match:
  1491. project_name, version, py_version, platform = match.group(
  1492. 'name','ver','pyver','plat'
  1493. )
  1494. return cls(
  1495. location, metadata, project_name=project_name, version=version,
  1496. py_version=py_version, platform=platform, **kw
  1497. )
  1498. from_location = classmethod(from_location)
  1499. hashcmp = property(
  1500. lambda self: (
  1501. getattr(self,'parsed_version',()), self.precedence, self.key,
  1502. -len(self.location or ''), self.location, self.py_version,
  1503. self.platform
  1504. )
  1505. )
  1506. def __cmp__(self, other): return cmp(self.hashcmp, other)
  1507. def __hash__(self): return hash(self.hashcmp)
  1508. # These properties have to be lazy so that we don't have to load any
  1509. # metadata until/unless it's actually needed. (i.e., some distributions
  1510. # may not know their name or version without loading PKG-INFO)
  1511. #@property
  1512. def key(self):
  1513. try:
  1514. return self._key
  1515. except AttributeError:
  1516. self._key = key = self.project_name.lower()
  1517. return key
  1518. key = property(key)
  1519. #@property
  1520. def parsed_version(self):
  1521. try:
  1522. return self._parsed_version
  1523. except AttributeError:
  1524. self._parsed_version = pv = parse_version(self.version)
  1525. return pv
  1526. parsed_version = property(parsed_version)
  1527. #@property
  1528. def version(self):
  1529. try:
  1530. return self._version
  1531. except AttributeError:
  1532. for line in self._get_metadata('PKG-INFO'):
  1533. if line.lower().startswith('version:'):
  1534. self._version = safe_version(line.split(':',1)[1].strip())
  1535. return self._version
  1536. else:
  1537. raise ValueError(
  1538. "Missing 'Version:' header and/or PKG-INFO file", self
  1539. )
  1540. version = property(version)
  1541. #@property
  1542. def _dep_map(self):
  1543. try:
  1544. return self.__dep_map
  1545. except AttributeError:
  1546. dm = self.__dep_map = {None: []}
  1547. for name in 'requires.txt', 'depends.txt':
  1548. for extra,reqs in split_sections(self._get_metadata(name)):
  1549. if extra: extra = safe_extra(extra)
  1550. dm.setdefault(extra,[]).extend(parse_requirements(reqs))
  1551. return dm
  1552. _dep_map = property(_dep_map)
  1553. def requires(self,extras=()):
  1554. """List of Requirements needed for this distro if `extras` are used"""
  1555. dm = self._dep_map
  1556. deps = []
  1557. deps.extend(dm.get(None,()))
  1558. for ext in extras:
  1559. try:
  1560. deps.extend(dm[safe_extra(ext)])
  1561. except KeyError:
  1562. raise UnknownExtra(
  1563. "%s has no such extra feature %r" % (self, ext)
  1564. )
  1565. return deps
  1566. def _get_metadata(self,name):
  1567. if self.has_metadata(name):
  1568. for line in self.get_metadata_lines(name):
  1569. yield line
  1570. def activate(self,path=None):
  1571. """Ensure distribution is importable on `path` (default=sys.path)"""
  1572. if path is None: path = sys.path
  1573. self.insert_on(path)
  1574. if path is sys.path:
  1575. fixup_namespace_packages(self.location)
  1576. map(declare_namespace, self._get_metadata('namespace_packages.txt'))
  1577. def egg_name(self):
  1578. """Return what this distribution's standard .egg filename should be"""
  1579. filename = "%s-%s-py%s" % (
  1580. to_filename(self.project_name), to_filename(self.version),
  1581. self.py_version or PY_MAJOR
  1582. )
  1583. if self.platform:
  1584. filename += '-'+self.platform
  1585. return filename
  1586. def __repr__(self):
  1587. if self.location:
  1588. return "%s (%s)" % (self,self.location)
  1589. else:
  1590. return str(self)
  1591. def __str__(self):
  1592. try: version = getattr(self,'version',None)
  1593. except ValueError: version = None
  1594. version = version or "[unknown version]"
  1595. return "%s %s" % (self.project_name,version)
  1596. def __getattr__(self,attr):
  1597. """Delegate all unrecognized public attributes to .metadata provider"""
  1598. if attr.startswith('_'):
  1599. raise AttributeError,attr
  1600. return getattr(self._provider, attr)
  1601. #@classmethod
  1602. def from_filename(cls,filename,metadata=None, **kw):
  1603. return cls.from_location(
  1604. _normalize_cached(filename), os.path.basename(filename), metadata,
  1605. **kw
  1606. )
  1607. from_filename = classmethod(from_filename)
  1608. def as_requirement(self):
  1609. """Return a ``Requirement`` that matches this distribution exactly"""
  1610. return Requirement.parse('%s==%s' % (self.project_name, self.version))
  1611. def load_entry_point(self, group, name):
  1612. """Return the `name` entry point of `group` or raise ImportError"""
  1613. ep = self.get_entry_info(group,name)
  1614. if ep is None:
  1615. raise ImportError("Entry point %r not found" % ((group,name),))
  1616. return ep.load()
  1617. def get_entry_map(self, group=None):
  1618. """Return the entry point map for `group`, or the full entry map"""
  1619. try:
  1620. ep_map = self._ep_map
  1621. except AttributeError:
  1622. ep_map = self._ep_map = EntryPoint.parse_map(
  1623. self._get_metadata('entry_points.txt'), self
  1624. )
  1625. if group is not None:
  1626. return ep_map.get(group,{})
  1627. return ep_map
  1628. def get_entry_info(self, group, name):
  1629. """Return the EntryPoint object for `group`+`name`, or ``None``"""
  1630. return self.get_entry_map(group).get(name)
  1631. def insert_on(self, path, loc = None):
  1632. """Insert self.location in path before its nearest parent directory"""
  1633. loc = loc or self.location
  1634. if not loc:
  1635. return
  1636. nloc = _normalize_cached(loc)
  1637. bdir = os.path.dirname(nloc)
  1638. npath= [(p and _normalize_cached(p) or p) for p in path]
  1639. bp = None
  1640. for p, item in enumerate(npath):
  1641. if item==nloc:
  1642. break
  1643. elif item==bdir and self.precedence==EGG_DIST:
  1644. # if it's an .egg, give it precedence over its directory
  1645. if path is sys.path:
  1646. self.check_version_conflict()
  1647. path.insert(p, loc)
  1648. npath.insert(p, nloc)
  1649. break
  1650. else:
  1651. if path is sys.path:
  1652. self.check_version_conflict()
  1653. path.append(loc)
  1654. return
  1655. # p is the spot where we found or inserted loc; now remove duplicates
  1656. while 1:
  1657. try:
  1658. np = npath.index(nloc, p+1)
  1659. except ValueError:
  1660. break
  1661. else:
  1662. del npath[np], path[np]
  1663. p = np # ha!
  1664. return
  1665. def check_version_conflict(self):
  1666. if self.key=='setuptools':
  1667. return # ignore the inevitable setuptools self-conflicts :(
  1668. nsp = dict.fromkeys(self._get_metadata('namespace_packages.txt'))
  1669. loc = normalize_path(self.location)
  1670. for modname in self._get_metadata('top_level.txt'):
  1671. if (modname not in sys.modules or modname in nsp
  1672. or modname in _namespace_packages
  1673. ):
  1674. continue
  1675. fn = getattr(sys.modules[modname], '__file__', None)
  1676. if fn and (normalize_path(fn).startswith(loc) or fn.startswith(loc)):
  1677. continue
  1678. issue_warning(
  1679. "Module %s was already imported from %s, but %s is being added"
  1680. " to sys.path" % (modname, fn, self.location),
  1681. )
  1682. def has_version(self):
  1683. try:
  1684. self.version
  1685. except ValueError:
  1686. issue_warning("Unbuilt egg for "+repr(self))
  1687. return False
  1688. return True
  1689. def clone(self,**kw):
  1690. """Copy this distribution, substituting in any changed keyword args"""
  1691. for attr in (
  1692. 'project_name', 'version', 'py_version', 'platform', 'location',
  1693. 'precedence'
  1694. ):
  1695. kw.setdefault(attr, getattr(self,attr,None))
  1696. kw.setdefault('metadata', self._provider)
  1697. return self.__class__(**kw)
  1698. #@property
  1699. def extras(self):
  1700. return [dep for dep in self._dep_map if dep]
  1701. extras = property(extras)
  1702. def issue_warning(*args,**kw):
  1703. level = 1
  1704. g = globals()
  1705. try:
  1706. # find the first stack frame that is *not* code in
  1707. # the pkg_resources module, to use for the warning
  1708. while sys._getframe(level).f_globals is g:
  1709. level += 1
  1710. except ValueError:
  1711. pass
  1712. from warnings import warn
  1713. warn(stacklevel = level+1, *args, **kw)
  1714. def parse_requirements(strs):
  1715. """Yield ``Requirement`` objects for each specification in `strs`
  1716. `strs` must be an instance of ``basestring``, or a (possibly-nested)
  1717. iterable thereof.
  1718. """
  1719. # create a steppable iterator, so we can handle \-continuations
  1720. lines = iter(yield_lines(strs))
  1721. def scan_list(ITEM,TERMINATOR,line,p,groups,item_name):
  1722. items = []
  1723. while not TERMINATOR(line,p):
  1724. if CONTINUE(line,p):
  1725. try:
  1726. line = lines.next(); p = 0
  1727. except StopIteration:
  1728. raise ValueError(
  1729. "\\ must not appear on the last nonblank line"
  1730. )
  1731. match = ITEM(line,p)
  1732. if not match:
  1733. raise ValueError("Expected "+item_name+" in",line,"at",line[p:])
  1734. items.append(match.group(*groups))
  1735. p = match.end()
  1736. match = COMMA(line,p)
  1737. if match:
  1738. p = match.end() # skip the comma
  1739. elif not TERMINATOR(line,p):
  1740. raise ValueError(
  1741. "Expected ',' or end-of-list in",line,"at",line[p:]
  1742. )
  1743. match = TERMINATOR(line,p)
  1744. if match: p = match.end() # skip the terminator, if any
  1745. return line, p, items
  1746. for line in lines:
  1747. match = DISTRO(line)
  1748. if not match:
  1749. raise ValueError("Missing distribution spec", line)
  1750. project_name = match.group(1)
  1751. p = match.end()
  1752. extras = []
  1753. match = OBRACKET(line,p)
  1754. if match:
  1755. p = match.end()
  1756. line, p, extras = scan_list(
  1757. DISTRO, CBRACKET, line, p, (1,), "'extra' name"
  1758. )
  1759. line, p, specs = scan_list(VERSION,LINE_END,line,p,(1,2),"version spec")
  1760. specs = [(op,safe_version(val)) for op,val in specs]
  1761. yield Requirement(project_name, specs, extras)
  1762. def _sort_dists(dists):
  1763. tmp = [(dist.hashcmp,dist) for dist in dists]
  1764. tmp.sort()
  1765. dists[::-1] = [d for hc,d in tmp]
  1766. class Requirement:
  1767. def __init__(self, project_name, specs, extras):
  1768. """DO NOT CALL THIS UNDOCUMENTED METHOD; use Requirement.parse()!"""
  1769. self.unsafe_name, project_name = project_name, safe_name(project_name)
  1770. self.project_name, self.key = project_name, project_name.lower()
  1771. index = [(parse_version(v),state_machine[op],op,v) for op,v in specs]
  1772. index.sort()
  1773. self.specs = [(op,ver) for parsed,trans,op,ver in index]
  1774. self.index, self.extras = index, tuple(map(safe_extra,extras))
  1775. self.hashCmp = (
  1776. self.key, tuple([(op,parsed) for parsed,trans,op,ver in index]),
  1777. frozenset(self.extras)
  1778. )
  1779. self.__hash = hash(self.hashCmp)
  1780. def __str__(self):
  1781. specs = ','.join([''.join(s) for s in self.specs])
  1782. extras = ','.join(self.extras)
  1783. if extras: extras = '[%s]' % extras
  1784. return '%s%s%s' % (self.project_name, extras, specs)
  1785. def __eq__(self,other):
  1786. return isinstance(other,Requirement) and self.hashCmp==other.hashCmp
  1787. def __contains__(self,item):
  1788. if isinstance(item,Distribution):
  1789. if item.key != self.key: return False
  1790. if self.index: item = item.parsed_version # only get if we need it
  1791. elif isinstance(item,basestring):
  1792. item = parse_version(item)
  1793. last = None
  1794. for parsed,trans,op,ver in self.index:
  1795. action = trans[cmp(item,parsed)]
  1796. if action=='F': return False
  1797. elif action=='T': return True
  1798. elif action=='+': last = True
  1799. elif action=='-' or last is None: last = False
  1800. if last is None: last = True # no rules encountered
  1801. return last
  1802. def __hash__(self):
  1803. return self.__hash
  1804. def __repr__(self): return "Requirement.parse(%r)" % str(self)
  1805. #@staticmethod
  1806. def parse(s):
  1807. reqs = list(parse_requirements(s))
  1808. if reqs:
  1809. if len(reqs)==1:
  1810. return reqs[0]
  1811. raise ValueError("Expected only one requirement", s)
  1812. raise ValueError("No requirements found", s)
  1813. parse = staticmethod(parse)
  1814. state_machine = {
  1815. # =><
  1816. '<' : '--T',
  1817. '<=': 'T-T',
  1818. '>' : 'F+F',
  1819. '>=': 'T+F',
  1820. '==': 'T..',
  1821. '!=': 'F++',
  1822. }
  1823. def _get_mro(cls):
  1824. """Get an mro for a type or classic class"""
  1825. if not isinstance(cls,type):
  1826. class cls(cls,object): pass
  1827. return cls.__mro__[1:]
  1828. return cls.__mro__
  1829. def _find_adapter(registry, ob):
  1830. """Return an adapter factory for `ob` from `registry`"""
  1831. for t in _get_mro(getattr(ob, '__class__', type(ob))):
  1832. if t in registry:
  1833. return registry[t]
  1834. def ensure_directory(path):
  1835. """Ensure that the parent directory of `path` exists"""
  1836. dirname = os.path.dirname(path)
  1837. if not os.path.isdir(dirname):
  1838. os.makedirs(dirname)
  1839. def split_sections(s):
  1840. """Split a string or iterable thereof into (section,content) pairs
  1841. Each ``section`` is a stripped version of the section header ("[section]")
  1842. and each ``content`` is a list of stripped lines excluding blank lines and
  1843. comment-only lines. If there are any such lines before the first section
  1844. header, they're returned in a first ``section`` of ``None``.
  1845. """
  1846. section = None
  1847. content = []
  1848. for line in yield_lines(s):
  1849. if line.startswith("["):
  1850. if line.endswith("]"):
  1851. if section or content:
  1852. yield section, content
  1853. section = line[1:-1].strip()
  1854. content = []
  1855. else:
  1856. raise ValueError("Invalid section heading", line)
  1857. else:
  1858. content.append(line)
  1859. # wrap up last segment
  1860. yield section, content
  1861. def _mkstemp(*args,**kw):
  1862. from tempfile import mkstemp
  1863. old_open = os.open
  1864. try:
  1865. os.open = os_open # temporarily bypass sandboxing
  1866. return mkstemp(*args,**kw)
  1867. finally:
  1868. os.open = old_open # and then put it back
  1869. # Set up global resource manager (deliberately not state-saved)
  1870. _manager = ResourceManager()
  1871. def _initialize(g):
  1872. for name in dir(_manager):
  1873. if not name.startswith('_'):
  1874. g[name] = getattr(_manager, name)
  1875. _initialize(globals())
  1876. # Prepare the master working set and make the ``require()`` API available
  1877. _declare_state('object', working_set = WorkingSet())
  1878. try:
  1879. # Does the main program list any requirements?
  1880. from __main__ import __requires__
  1881. except ImportError:
  1882. pass # No: just use the default working set based on sys.path
  1883. else:
  1884. # Yes: ensure the requirements are met, by prefixing sys.path if necessary
  1885. try:
  1886. working_set.require(__requires__)
  1887. except VersionConflict: # try it without defaults already on sys.path
  1888. working_set = WorkingSet([]) # by starting with an empty path
  1889. for dist in working_set.resolve(
  1890. parse_requirements(__requires__), Environment()
  1891. ):
  1892. working_set.add(dist)
  1893. for entry in sys.path: # add any missing entries from sys.path
  1894. if entry not in working_set.entries:
  1895. working_set.add_entry(entry)
  1896. sys.path[:] = working_set.entries # then copy back to sys.path
  1897. require = working_set.require
  1898. iter_entry_points = working_set.iter_entry_points
  1899. add_activation_listener = working_set.subscribe
  1900. run_script = working_set.run_script
  1901. run_main = run_script # backward compatibility
  1902. # Activate all distributions already on sys.path, and ensure that
  1903. # all distributions added to the working set in the future (e.g. by
  1904. # calling ``require()``) will get activated as well.
  1905. add_activation_listener(lambda dist: dist.activate())
  1906. working_set.entries=[]; map(working_set.add_entry,sys.path) # match order