def _find_pip_info(self): """ Try to find information about the installed package from pip. This should be wrapped in a try/except. :returns: information from pip about ``self.package_name``. :rtype: dict """ res = {} dist = None dist_name = self.package_name.replace('_', '-') logger.debug('Checking for pip distribution named: %s', dist_name) for d in get_installed_distributions(): if d.project_name == dist_name: dist = d if dist is None: logger.debug('could not find dist matching package_name') return res logger.debug('found dist: %s', dist) self._pip_locations = [dist.location] ver, url = self._dist_version_url(dist) res['version'] = ver res['url'] = url # this is a bit of an ugly, lazy hack... try: req = FrozenRequirement.from_dist(dist, []) except TypeError: # nocoverage req = FrozenRequirement.from_dist(dist) logger.debug('pip FrozenRequirement: %s', req) res['requirement'] = str(req.req) return res
def get_benchopt_requirement(): """Specification for pip requirement to install benchopt in conda env. Find out how benchopt was installed so we can install the same version even if it was installed in develop mode. This requires pip version >= 20.1 Returns ------- pip_requirement : str String to pass to pip to instal benchopt in another environment. is_editable : bool Whether the current installation is in development mode or not. """ # Ignore distutils replacement warning when importing pip package. It is # not clear why this started to trigger such warning in #265 # XXX - Investigate this warning and fix it. import warnings warnings.filterwarnings("ignore", message="Setuptools is replacing distutils.", category=UserWarning) from pip._internal.metadata import get_default_environment from pip._internal.operations.freeze import FrozenRequirement dist = get_default_environment().get_distribution('benchopt') # If benchopt is installed in editable mode, get the module path to install # it directly from the folder. Else, install it correctly even if it is # installed with an url. assert dist is not None, ( 'benchopt is not installed in the current environment?') req = FrozenRequirement.from_dist(dist) if req.editable: return f'-e {dist.location}', True return str(req), False
def get_benchopt_requirement(): """Specification for pip requirement to install benchopt in conda env. Find out how benchopt where installed so we can install the same version even if it was installed in develop mode. This requires pip version >= 20.1 Returns ------- pip_requirement : str String to pass to pip to instal benchopt in another environment. is_editable : bool Whether the current installation is in development mode or not. """ from pip._internal.metadata import get_default_environment from pip._internal.operations.freeze import FrozenRequirement dist = get_default_environment().get_distribution('benchopt') # If benchopt is installed in editable mode, get the module path to install # it directly from the folder. Else, install it correctly even if it is # installed with an url. assert dist is not None, ( 'benchopt is not installed in the current environment?') req = FrozenRequirement.from_dist(dist) if req.editable: return f'-e {dist.location}', True return str(req), False
def get_frozen_reqs(): installations = {} for _, dist in get_distributions().items(): try: req = FrozenRequirement.from_dist(dist) except pkg_resources.RequirementParseError: logger.warning("Could not parse requirement: %s", dist.project_name) continue installations[req.name] = req return installations
def test_editable(): distributions = {v.key: v for v in pkg_resources.working_set} for key, value in distributions.items(): print(key, value) distribution = distributions['despasito'] frozen_requirement = FrozenRequirement.from_dist(distribution) if not frozen_requirement.editable: pytest.exit( "Tests should only be run when installed as editable: `pip install -e .`, consider cloning the repository on GitHub" ) else: assert True
def dist_to_req(dist): """Make a pip.FrozenRequirement from a pkg_resources distribution object""" try: # :pragma:nocover: (pip>=10) from pip._internal.operations.freeze import FrozenRequirement except ImportError: # :pragma:nocover: (pip<10) from pip import FrozenRequirement # normalize the casing, dashes in the req name orig_name, dist.project_name = dist.project_name, dist.key result = FrozenRequirement.from_dist(dist, []) # put things back the way we found it. dist.project_name = orig_name return result
def get_benchopt_requirement_line(): """Specification for pip requirement to install benchopt in conda env. Find out how benchopt where installed so we can install the same version even if it was installed in develop mode. This requires pip version >= 20.1 """ # If benchopt is installed in editable mode, get the module path to install # it directly from the folder. Else, install it correctly even if it is # installed with an url. dist = [ t for t in get_installed_distributions() if t.project_name == 'benchopt' ][0] req = FrozenRequirement.from_dist(dist) if req.editable: return f'-e {dist.module_path}' return str(req)
def frozen_req_from_dist(dist): try: return FrozenRequirement.from_dist(dist) except TypeError: return FrozenRequirement.from_dist(dist, [])
def frozen_repr(obj): fr = FrozenRequirement.from_dist(obj, []) return str(fr).strip()