当前位置: 首页>>代码示例>>Python>>正文


Python Environment.best_match方法代码示例

本文整理汇总了Python中pkg_resources.Environment.best_match方法的典型用法代码示例。如果您正苦于以下问题:Python Environment.best_match方法的具体用法?Python Environment.best_match怎么用?Python Environment.best_match使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pkg_resources.Environment的用法示例。


在下文中一共展示了Environment.best_match方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Bootstrapper

# 需要导入模块: from pkg_resources import Environment [as 别名]
# 或者: from pkg_resources.Environment import best_match [as 别名]

#.........这里部分代码省略.........
            logger.critical("Got force_setuptools=%r AND force_distribute=%r",
                            force_setuptools, force_distribute)
            raise ValueError("Can't force both on setuptools and distribute !")
        if force_setuptools is not None:
            setuptools, setuptools_rhs = 'setuptools', force_setuptools
        if force_distribute is not None:
            setuptools, setuptools_rhs = 'distribute', force_distribute

        # actually, installing distribute with any version of setuptools or
        # itself happens to work... provided that the magic buildout marker
        # that tells is setup no to touch the global site-packages is there
        # otherwise, it'd try the rename an .egg_info, and would fail for non-
        # privileged users, even for a local egg production.
        # Usually, the shell would set it, thankfully it's not libc's concern
        if setuptools == 'distribute':
            os.environ['_'] = 'buildout_unibootstrap'

        if DISTRIBUTE and setuptools == 'setuptools':
            self.setuptools_req = self._setuptools_req(setuptools_rhs)
            self.init_ez_install_distribute_to_setuptools()
            return

        self.setuptools_req = Requirement.parse(setuptools + setuptools_rhs)
        self._ez_install_pypath = None

    def ensure_req(self, req):
        """Make sure that requirement is satisfied and return location.

        Either we have it already, or we install it.
        """
        # we don't use obtain() because it's not clear how it would
        # not use the working set with the full sys.path instead of our empty
        # one (fearing also slight behaviour changes across versions)
        dist = self.env.best_match(req, self.ws)
        if dist is None:
            dist = self.grab_req(req)
            self.init_env()
            dist = self.env.best_match(req, self.ws)
            if dist is None:
                raise LookupError(req)
        logger.info("Requirement %s fulfilled at %s", req, dist.location)
        return dist.location

    def grab_req(self, req):
        """Install a requirement to self.eggs_dir.

        We don't use the internal API of setuptools and spawn of subprocess
        because:
        - we might use a different version of setuptools than the one we import
          from here
        - the command-line (or surface) API is obviously very stable
        """
        if self.offline:
            # actually, we would have a small ability to find setuptools
            # eggs that are present locally, but that's too complicated for
            # now and has few chances of success
            self.error("%s is not available in specified dists "
                       "directory %s, and can't be downloaded "
                       "(offline mode is requested)" % (req, self.eggs_dir))

        logger.info("%s not available locally, attempting to download", req)
        os_env = dict(os.environ)
        pypath = self._ez_install_pypath
        if pypath is not None:
            os_env['PYTHONPATH'] = pypath
        subprocess.check_call(self._ez_install +
开发者ID:anybox,项目名称:anybox.buildbot.odoo,代码行数:70,代码来源:unibootstrap.py

示例2: DistributionsManager

# 需要导入模块: from pkg_resources import Environment [as 别名]
# 或者: from pkg_resources.Environment import best_match [as 别名]
class DistributionsManager(object):
	def __init__(self):
		self.mod_folder = os.path.join(os.getcwd(), "raduga_modules")
		if not os.path.exists(self.mod_folder):
			os.mkdir(self.mod_folder)
		self._init_environment()

	def _init_environment(self):
		dist_folders = map(lambda d: os.path.join(os.getcwd(), self.mod_folder, d), os.listdir(self.mod_folder))
		dist_folders = filter(lambda f: os.path.exists(os.path.join(f, "EGG-INFO")), dist_folders)
		dists = map(lambda f: Distribution.from_filename(f) , dist_folders)
		#
		self.pkg_env = Environment()
		for dist in dists: self.pkg_env.add(dist)

	def _add_to_environment(self, egg_folder):
		dist = Distribution.from_filename(egg_folder)
		self.pkg_env.add(dist)

	def _match_req(self, req):
		return self.pkg_env.best_match(req, working_set)

	def _flatten_reqs(self, *req_sets):
		# req_sets further in the list take precedence
		reqs = {}
		for rset in req_sets:
			for sreq in rset:
				req = Requirement.parse(sreq)
				reqs[req.key] = req
		return reqs.values()

	@contextmanager
	def requirement_loader(self, *req_sets):
		# Save sys.path and sys.modules, to be restored later
		import sys, copy
		old_path = copy.copy(sys.path)
		old_sys_modules = sys.modules.keys()
		# Find distributions for all the requirements
		req_dists = []
		reqs = self._flatten_reqs(req_sets)
		for req in reqs:
			match = self._match_req(req)
			if match is None:
				raise RuntimeError("Unable to find distribution matching %s" % str(req))
			req_dists.append(match)
		# Activate the distributions, return control
		for req in req_dists: req.activate()
		yield
		# Restore sys path and modules
		sys.path = old_path
		for modname in sys.modules.keys():
			if not modname in old_sys_modules:
				del sys.modules[modname]

	def install_dist(self, path):
		setup_py = os.path.join(os.getcwd(), path, "setup.py")
		if not os.path.isfile(setup_py):
			raise RuntimeError("Folder %s doesn't have a setup file" % path)
		with self._build_egg_env(path) as tempdir:
			import subprocess, zipfile
			subprocess.check_call(["python", setup_py, "bdist_egg", "--dist-dir=%s" % tempdir])
			egg = os.listdir(tempdir)[0]    # egg will be the single entry in the temp folder
			# TODO: check if exactly that same egg is installed
			eggf = os.path.join(self.mod_folder, egg)   # target egg folder
			os.mkdir(eggf)
			eggz = zipfile.ZipFile(os.path.join(tempdir, egg))
			eggz.extractall(eggf)

	@contextmanager
	def _build_egg_env(self, path):
		import tempfile, shutil
		old_cwd = os.getcwd()
		os.chdir(path)
		tempdir = tempfile.mkdtemp()
		yield tempdir
		shutil.rmtree(tempdir)
		os.chdir(old_cwd)
开发者ID:tuxpiper,项目名称:raduga,代码行数:79,代码来源:distmgr.py


注:本文中的pkg_resources.Environment.best_match方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。