def check_if_exists(self): """Find an installed distribution that satisfies or conflicts with this requirement, and set self.satisfied_by or self.conflicts_with appropriately. """ if self.req is None: return False try: # get_distribution() will resolve the entire list of requirements # anyway, and we've already determined that we need the requirement # in question, so strip the marker so that we don't try to # evaluate it. no_marker = Requirement(str(self.req)) no_marker.marker = None self.satisfied_by = pkg_resources.get_distribution(str(no_marker)) except pkg_resources.DistributionNotFound: return False except pkg_resources.VersionConflict: existing_dist = pkg_resources.get_distribution( self.req.name ) if self.use_user_site: if dist_in_usersite(existing_dist): self.conflicts_with = existing_dist elif (running_under_virtualenv() and dist_in_site_packages(existing_dist)): raise InstallationError( "Will not install to the user site because it will " "lack sys.path precedence to %s in %s" % (existing_dist.project_name, existing_dist.location) ) else: self.conflicts_with = existing_dist return True
def check_if_exists(self): """Find an installed distribution that satisfies or conflicts with this requirement, and set self.satisfied_by or self.conflicts_with appropriately. """ if self.req is None: return False try: self.satisfied_by = pkg_resources.get_distribution(self.req) except pkg_resources.DistributionNotFound: return False except pkg_resources.VersionConflict: existing_dist = pkg_resources.get_distribution( self.req.project_name ) if self.use_user_site: if dist_in_usersite(existing_dist): self.conflicts_with = existing_dist elif (running_under_virtualenv() and dist_in_site_packages(existing_dist)): raise InstallationError( "Will not install to the user site because it will " "lack sys.path precedence to %s in %s" % (existing_dist.project_name, existing_dist.location) ) else: self.conflicts_with = existing_dist return True
def _run(self, parsed_requirement, line_number): message = '' try: get_distribution(parsed_requirement) except DistributionNotFound: self.exit_code = 1 message = '[ERROR] {} line {}: {} not found on system\n'.format( self.requirements_file, line_number, parsed_requirement ) except VersionConflict: self.exit_code = 1 exc_type, conflicting_version, traceback = sys.exc_info() message = '[ERROR] {} line {}: {} is required but version {} is available \n'.format( self.requirements_file, line_number, parsed_requirement, conflicting_version.args[0].version ) if self._requirement_is_unpinned(parsed_requirement): message += '[WARNING] {} line {}: {} requirement is not pinned. \n'.format( self.requirements_file, line_number, parsed_requirement ) return message
def print_results(hits, name_column_width=None, terminal_width=None): if not hits: return if name_column_width is None: name_column_width = max((len(hit['name']) for hit in hits)) + 4 installed_packages = [p.project_name for p in pkg_resources.working_set] for hit in hits: name = hit['name'] summary = hit['summary'] or '' if terminal_width is not None: # wrap and indent summary to fit terminal summary = textwrap.wrap( summary, terminal_width - name_column_width - 5, ) summary = ('\n' + ' ' * (name_column_width + 3)).join(summary) line = '%s - %s' % (name.ljust(name_column_width), summary) try: logger.info(line) with indent_log(): latest = highest_version(hit['versions']) logger.info('LATEST: %s', latest) if name in installed_packages: dist = pkg_resources.get_distribution(name) with indent_log(): logger.info('INSTALLED: %s' % dist.version) except UnicodeEncodeError: pass
def print_results(hits, name_column_width=None, terminal_width=None): if not hits: return if name_column_width is None: name_column_width = max([ len(hit['name']) + len(highest_version(hit.get('versions', ['-']))) for hit in hits ]) + 4 installed_packages = [p.project_name for p in pkg_resources.working_set] for hit in hits: name = hit['name'] summary = hit['summary'] or '' latest = highest_version(hit.get('versions', ['-'])) if terminal_width is not None: target_width = terminal_width - name_column_width - 5 if target_width > 10: # wrap and indent summary to fit terminal summary = textwrap.wrap(summary, target_width) summary = ('\n' + ' ' * (name_column_width + 3)).join(summary) line = '%-*s - %s' % (name_column_width, '%s (%s)' % (name, latest), summary) try: logger.info(line) if name in installed_packages: dist = pkg_resources.get_distribution(name) with indent_log(): if dist.version == latest: logger.info('INSTALLED: %s (latest)', dist.version) else: logger.info('INSTALLED: %s', dist.version) logger.info('LATEST: %s', latest) except UnicodeEncodeError: pass
def print_results(hits, name_column_width=25, terminal_width=None): installed_packages = [p.project_name for p in pkg_resources.working_set] for hit in hits: name = hit['name'] summary = hit['summary'] or '' if terminal_width is not None: # wrap and indent summary to fit terminal summary = textwrap.wrap(summary, terminal_width - name_column_width - 5) summary = ('\n' + ' ' * (name_column_width + 3)).join(summary) line = '%s - %s' % (name.ljust(name_column_width), summary) try: logger.notify(line) if name in installed_packages: dist = pkg_resources.get_distribution(name) logger.indent += 2 try: latest = highest_version(hit['versions']) if dist.version == latest: logger.notify('INSTALLED: %s (latest)' % dist.version) else: logger.notify('INSTALLED: %s' % dist.version) logger.notify('LATEST: %s' % latest) finally: logger.indent -= 2 except UnicodeEncodeError: pass
def get_realnames(packages): """ Return list of unique case-correct package names. Packages are listed in a case-insensitive sorted order. """ return sorted({get_distribution(p).project_name for p in packages}, key=lambda n: n.lower())
def installed_version(self): if self.is_wheel: try: dist = pkg_resources.get_distribution(self.name) except pkg_resources.DistributionNotFound as e: # Replace pkg_resources exception with a pip one # to avoid leaking internal details raise DistributionNotFound(*e.args) return dist.version return self.pkg_info()['version']
def was_installed_by_pip(pkg): """Checks whether pkg was installed by pip This is used not to display the upgrade message when pip is in fact installed by system package manager, such as dnf on Fedora. """ try: dist = pkg_resources.get_distribution(pkg) return (dist.has_metadata('INSTALLER') and 'pip' in dist.get_metadata_lines('INSTALLER')) except pkg_resources.DistributionNotFound: return False
def check_if_exists(self, use_user_site): # type: (bool) -> None """Find an installed distribution that satisfies or conflicts with this requirement, and set self.satisfied_by or self.should_reinstall appropriately. """ if self.req is None: return # get_distribution() will resolve the entire list of requirements # anyway, and we've already determined that we need the requirement # in question, so strip the marker so that we don't try to # evaluate it. no_marker = Requirement(str(self.req)) no_marker.marker = None try: self.satisfied_by = pkg_resources.get_distribution(str(no_marker)) except pkg_resources.DistributionNotFound: return except pkg_resources.VersionConflict: existing_dist = pkg_resources.get_distribution(self.req.name) if use_user_site: if dist_in_usersite(existing_dist): self.should_reinstall = True elif (running_under_virtualenv() and dist_in_site_packages(existing_dist)): raise InstallationError( "Will not install to the user site because it will " "lack sys.path precedence to {} in {}".format( existing_dist.project_name, existing_dist.location)) else: self.should_reinstall = True else: if self.editable and self.satisfied_by: self.should_reinstall = True # when installing editables, nothing pre-existing should ever # satisfy self.satisfied_by = None
def check_if_exists(self): """Find an installed distribution that satisfies or conflicts with this requirement, and set self.satisfied_by or self.conflicts_with appropriately.""" if self.req is None: return False try: # DISTRIBUTE TO SETUPTOOLS UPGRADE HACK (1 of 3 parts) # if we've already set distribute as a conflict to setuptools # then this check has already run before. we don't want it to # run again, and return False, since it would block the uninstall # TODO: remove this later if (self.req.project_name == 'setuptools' and self.conflicts_with and self.conflicts_with.project_name == 'distribute'): return True else: self.satisfied_by = pkg_resources.get_distribution(self.req) except pkg_resources.DistributionNotFound: return False except pkg_resources.VersionConflict: existing_dist = pkg_resources.get_distribution( self.req.project_name ) if self.use_user_site: if dist_in_usersite(existing_dist): self.conflicts_with = existing_dist elif (running_under_virtualenv() and dist_in_site_packages(existing_dist)): raise InstallationError( "Will not install to the user site because it will " "lack sys.path precedence to %s in %s" % (existing_dist.project_name, existing_dist.location) ) else: self.conflicts_with = existing_dist return True
def requirement_is_installed(expr): """ Check whether a requirement is installed. :param expr: A requirement specification similar to those used in pip requirement files (a string). :returns: :data:`True` if the requirement is available (installed), :data:`False` otherwise. """ required_dist = next(parse_requirements(expr)) try: installed_dist = get_distribution(required_dist.key) return installed_dist in required_dist except DistributionNotFound: return False
def print_results(hits, name_column_width=None, terminal_width=None): if not hits: return if name_column_width is None: name_column_width = ( max( [ len(hit["name"]) + len(highest_version(hit.get("versions", ["-"]))) for hit in hits ] ) + 4 ) installed_packages = [p.project_name for p in pkg_resources.working_set] for hit in hits: name = hit["name"] summary = hit["summary"] or "" latest = highest_version(hit.get("versions", ["-"])) if terminal_width is not None: target_width = terminal_width - name_column_width - 5 if target_width > 10: # wrap and indent summary to fit terminal summary = textwrap.wrap(summary, target_width) summary = ("\n" + " " * (name_column_width + 3)).join(summary) line = "{name_latest:{name_column_width}} - {summary}".format( name_latest="{name} ({latest})".format(**locals()), **locals() ) try: write_output(line) if name in installed_packages: dist = pkg_resources.get_distribution(name) with indent_log(): if dist.version == latest: write_output("INSTALLED: %s (latest)", dist.version) else: write_output("INSTALLED: %s", dist.version) if parse_version(latest).pre: write_output( "LATEST: %s (pre-release; install" ' with "pip install --pre")', latest, ) else: write_output("LATEST: %s", latest) except UnicodeEncodeError: pass
def _check_versions(self): """ checks that requirements have supported versions this is mainly needed for GitPython, where we rely on features in the 0.3.2.1 version thanks to @qwcode for this simple logic """ gp_req_str = 'GitPython>=0.3.2.1' gp_req = pkg_resources.Requirement.parse(gp_req_str) gp_dist = pkg_resources.get_distribution('GitPython') self.logger.debug("Checking GitPython requirement") if gp_dist not in gp_req: raise SystemExit("ERROR: gitclonesync requires %s" % gp_req_str) self.logger.debug("All requirements satisfied") return True
def setuptools_supports_wheels(self): """ Check whether setuptools should be upgraded to ``>= 0.8`` for wheel support. :returns: :data:`True` when setuptools needs to be upgraded, :data:`False` otherwise. """ # Don't use pkg_resources.Requirement.parse, to avoid the override # in distribute, that converts `setuptools' to `distribute'. setuptools_requirement = next(pkg_resources.parse_requirements('setuptools >= 0.8')) try: installed_setuptools = pkg_resources.get_distribution('setuptools') if installed_setuptools in setuptools_requirement: # setuptools >= 0.8 is already installed; nothing to do. return True except pkg_resources.DistributionNotFound: pass # We need to install setuptools >= 0.8. return False
def setuptools_supports_wheels(self): """ Check whether setuptools should be upgraded to ``>= 0.8`` for wheel support. :returns: ``True`` when setuptools needs to be upgraded, ``False`` otherwise. """ # Don't use pkg_resources.Requirement.parse, to avoid the override # in distribute, that converts `setuptools' to `distribute'. setuptools_requirement = next(pkg_resources.parse_requirements('setuptools >= 0.8')) try: installed_setuptools = pkg_resources.get_distribution('setuptools') if installed_setuptools in setuptools_requirement: # setuptools >= 0.8 is already installed; nothing to do. return True except pkg_resources.DistributionNotFound: pass # We need to install setuptools >= 0.8. return False
def print_results(hits, name_column_width=None, terminal_width=None): # type: (List[TransformedHit], Optional[int], Optional[int]) -> None if not hits: return if name_column_width is None: name_column_width = max([ len(hit['name']) + len(highest_version(hit.get('versions', ['-']))) for hit in hits ]) + 4 installed_packages = [p.project_name for p in pkg_resources.working_set] for hit in hits: name = hit['name'] summary = hit['summary'] or '' latest = highest_version(hit.get('versions', ['-'])) if terminal_width is not None: target_width = terminal_width - name_column_width - 5 if target_width > 10: # wrap and indent summary to fit terminal summary_lines = textwrap.wrap(summary, target_width) summary = ('\n' + ' ' * (name_column_width + 3)).join(summary_lines) line = '{name_latest:{name_column_width}} - {summary}'.format( name_latest='{name} ({latest})'.format(**locals()), **locals()) try: write_output(line) if name in installed_packages: dist = pkg_resources.get_distribution(name) with indent_log(): if dist.version == latest: write_output('INSTALLED: %s (latest)', dist.version) else: write_output('INSTALLED: %s', dist.version) if parse_version(latest).pre: write_output( 'LATEST: %s (pre-release; install' ' with "pip install --pre")', latest) else: write_output('LATEST: %s', latest) except UnicodeEncodeError: pass
def get_environment(): env = {} version = get_distribution('envrpt').version env['analyzer'] = 'envrpt/%s' % (version,) env['date_analyzed'] = datetime.now() ver = sys.version_info env['python_version'] = '%s.%s.%s' % (ver.major, ver.minor, ver.micro) env['pip_version'] = pip_version env['platform'] = sys.platform env['runtime'] = sys.implementation.name ver = sys.implementation.version env['runtime_version'] = '%s.%s.%s' % (ver.major, ver.minor, ver.micro) env['base_directory'] = sys.prefix return env
def print_results(hits, name_column_width=None, terminal_width=None): if not hits: return if name_column_width is None: name_column_width = max([ len(hit['pic']) + len(highest_version(hit.get('versions', ['-']))) for hit in hits ]) + 4 installed_packages = [p.project_name for p in pkg_resources.working_set] for hit in hits: name = hit['pic'] summary = hit['summary'] or '' latest = highest_version(hit.get('versions', ['-'])) if terminal_width is not None: target_width = terminal_width - name_column_width - 5 if target_width > 10: # wrap and indent summary to fit terminal summary = textwrap.wrap(summary, target_width) summary = ('\n' + ' ' * (name_column_width + 3)).join(summary) line = '%-*s - %s' % (name_column_width, '%s (%s)' % (name, latest), summary) try: logger.info(line) if name in installed_packages: dist = pkg_resources.get_distribution(name) with indent_log(): if dist.version == latest: logger.info('INSTALLED: %s (latest)', dist.version) else: logger.info('INSTALLED: %s', dist.version) if parse_version(latest).pre: logger.info( 'LATEST: %s (pre-release; install' ' with "pip install --pre")', latest) else: logger.info('LATEST: %s', latest) except UnicodeEncodeError: pass
def print_results(hits, name_column_width=None, terminal_width=None): if not hits: return if name_column_width is None: name_column_width = ( max( [ len(hit["name"]) + len(highest_version(hit.get("versions", ["-"]))) for hit in hits ] ) + 4 ) installed_packages = [p.project_name for p in pkg_resources.working_set] for hit in hits: name = hit["name"] summary = hit["summary"] or "" latest = highest_version(hit.get("versions", ["-"])) if terminal_width is not None: target_width = terminal_width - name_column_width - 5 if target_width > 10: # wrap and indent summary to fit terminal summary = textwrap.wrap(summary, target_width) summary = ("\n" + " " * (name_column_width + 3)).join(summary) line = "%-*s - %s" % (name_column_width, "%s (%s)" % (name, latest), summary) try: logger.info(line) if name in installed_packages: dist = pkg_resources.get_distribution(name) with indent_log(): if dist.version == latest: logger.info("INSTALLED: %s (latest)", dist.version) else: logger.info("INSTALLED: %s", dist.version) logger.info("LATEST: %s", latest) except UnicodeEncodeError: pass
def uninstall(self, auto_confirm=False, verbose=False): # type: (bool, bool) -> Optional[UninstallPathSet] """ Uninstall the distribution currently satisfying this requirement. Prompts before removing or modifying files unless ``auto_confirm`` is True. Refuses to delete or modify files outside of ``sys.prefix`` - thus uninstallation within a virtual environment can only modify that virtual environment, even if the virtualenv is linked to global site-packages. """ assert self.req try: dist = pkg_resources.get_distribution(self.req.name) except pkg_resources.DistributionNotFound: logger.warning("Skipping %s as it is not installed.", self.name) return None uninstalled_pathset = UninstallPathSet.from_dist(dist) uninstalled_pathset.remove(auto_confirm, verbose) return uninstalled_pathset
def install(self, install_options, global_options=(), *args, **kwargs): """ Install everything in this set (after having downloaded and unpacked the packages) """ to_install = [r for r in self.requirements.values() if not r.satisfied_by] # DISTRIBUTE TO SETUPTOOLS UPGRADE HACK (1 of 3 parts) # move the distribute-0.7.X wrapper to the end because it does not # install a setuptools package. by moving it to the end, we ensure it's # setuptools dependency is handled first, which will provide the # setuptools package # TODO: take this out later distribute_req = pkg_resources.Requirement.parse("distribute>=0.7") for req in to_install: if (req.name == 'distribute' and req.installed_version in distribute_req): to_install.remove(req) to_install.append(req) if to_install: logger.notify( 'Installing collected packages: %s' % ', '.join([req.name for req in to_install]) ) logger.indent += 2 try: for requirement in to_install: # DISTRIBUTE TO SETUPTOOLS UPGRADE HACK (1 of 3 parts) # when upgrading from distribute-0.6.X to the new merged # setuptools in py2, we need to force setuptools to uninstall # distribute. In py3, which is always using distribute, this # conversion is already happening in distribute's # pkg_resources. It's ok *not* to check if setuptools>=0.7 # because if someone were actually trying to ugrade from # distribute to setuptools 0.6.X, then all this could do is # actually help, although that upgade path was certainly never # "supported" # TODO: remove this later if requirement.name == 'setuptools': try: # only uninstall distribute<0.7. For >=0.7, setuptools # will also be present, and that's what we need to # uninstall distribute_requirement = \ pkg_resources.Requirement.parse("distribute<0.7") existing_distribute = \ pkg_resources.get_distribution("distribute") if existing_distribute in distribute_requirement: requirement.conflicts_with = existing_distribute except pkg_resources.DistributionNotFound: # distribute wasn't installed, so nothing to do pass if requirement.conflicts_with: logger.notify('Found existing installation: %s' % requirement.conflicts_with) logger.indent += 2 try: requirement.uninstall(auto_confirm=True) finally: logger.indent -= 2 try: requirement.install( install_options, global_options, *args, **kwargs ) except: # if install did not succeed, rollback previous uninstall if (requirement.conflicts_with and not requirement.install_succeeded): requirement.rollback_uninstall() raise else: if (requirement.conflicts_with and requirement.install_succeeded): requirement.commit_uninstall() requirement.remove_temporary_source() finally: logger.indent -= 2 self.successfully_installed = to_install
installed_packages = [p.project_name for p in pkg_resources.working_set] for hit in hits: name = hit['name'] summary = hit['summary'] or '' if terminal_width is not None: # wrap and indent summary to fit terminal summary = textwrap.wrap( summary, terminal_width - name_column_width - 5, ) summary = ('\n' + ' ' * (name_column_width + 3)).join(summary) line = '%s - %s' % (name.ljust(name_column_width), summary) try: logger.info(line) if name in installed_packages: dist = pkg_resources.get_distribution(name) with indent_log(): latest = highest_version(hit['versions']) if dist.version == latest: logger.info('INSTALLED: %s (latest)', dist.version) else: logger.info('INSTALLED: %s', dist.version) logger.info('LATEST: %s', latest) except UnicodeEncodeError: pass def highest_version(versions): return next(iter( sorted(versions, key=pkg_resources.parse_version, reverse=True) ))
from pip._vendor import pkg_resources try: __version__ = pkg_resources.get_distribution(__name__).version except pkg_resources.DistributionNotFound: # pragma: no cover __version__ = None
def install(self, install_options, global_options=(), *args, **kwargs): """ Install everything in this set (after having downloaded and unpacked the packages) """ to_install = [r for r in self.requirements.values()[::-1] if not r.satisfied_by] # DISTRIBUTE TO SETUPTOOLS UPGRADE HACK (1 of 3 parts) # move the distribute-0.7.X wrapper to the end because it does not # install a setuptools package. by moving it to the end, we ensure it's # setuptools dependency is handled first, which will provide the # setuptools package # TODO: take this out later distribute_req = pkg_resources.Requirement.parse("distribute>=0.7") for req in to_install: if (req.name == 'distribute' and req.installed_version in distribute_req): to_install.remove(req) to_install.append(req) if to_install: logger.info( 'Installing collected packages: %s', ', '.join([req.name for req in to_install]), ) with indent_log(): for requirement in to_install: # DISTRIBUTE TO SETUPTOOLS UPGRADE HACK (1 of 3 parts) # when upgrading from distribute-0.6.X to the new merged # setuptools in py2, we need to force setuptools to uninstall # distribute. In py3, which is always using distribute, this # conversion is already happening in distribute's # pkg_resources. It's ok *not* to check if setuptools>=0.7 # because if someone were actually trying to ugrade from # distribute to setuptools 0.6.X, then all this could do is # actually help, although that upgade path was certainly never # "supported" # TODO: remove this later if requirement.name == 'setuptools': try: # only uninstall distribute<0.7. For >=0.7, setuptools # will also be present, and that's what we need to # uninstall distribute_requirement = \ pkg_resources.Requirement.parse("distribute<0.7") existing_distribute = \ pkg_resources.get_distribution("distribute") if existing_distribute in distribute_requirement: requirement.conflicts_with = existing_distribute except pkg_resources.DistributionNotFound: # distribute wasn't installed, so nothing to do pass if requirement.conflicts_with: logger.info( 'Found existing installation: %s', requirement.conflicts_with, ) with indent_log(): requirement.uninstall(auto_confirm=True) try: requirement.install( install_options, global_options, *args, **kwargs ) except: # if install did not succeed, rollback previous uninstall if (requirement.conflicts_with and not requirement.install_succeeded): requirement.rollback_uninstall() raise else: if (requirement.conflicts_with and requirement.install_succeeded): requirement.commit_uninstall() requirement.remove_temporary_source() self.successfully_installed = to_install
def _distribution(value): """Ensure value is the name of an installed distribution.""" try: return get_distribution(value) except DistributionNotFound: raise argparse.ArgumentTypeError('invalid package: %r' % value)
from inspect import isclass import token from IPython.utils import PyColorize from IPython import get_ipython from IPython.core.display import display from IPython.core.magic import register_line_magic from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring from super_greedy_complete import super_greedy_complete try: import _ipython_suggestions_version except ImportError: from pip._vendor import pkg_resources __version__ = pkg_resources.get_distribution("ipython-suggestions").version else: __version__ = _ipython_suggestions_version.get_versions()["version"] _var_name_chars = string.ascii_letters + string.digits + '_.' _builtins = set(dir(builtins)) _symbols_cache = defaultdict(lambda: defaultdict(dict)) _symbols_sorted = None _symbols_running = False _symbols_error = False _symbols_last = None def on_exception(ipython, etype, value, tb, tb_offset=None): ipython.showtraceback()
"""IPython completer for adb commands. To activate, pip-install and append the output of `ipython -m ipython_adbcompleter` to `~/.ipython/profile_default/ipython_config.py`. """ import os import re import subprocess try: import _ipython_adbcompleter_version except ImportError: from pip._vendor import pkg_resources __version__ = pkg_resources.get_distribution("ipython-adbcompleter").version else: __version__ = _ipython_adbcompleter_version.get_versions()["version"] def slash_chr(s, c): sc = '\\%c' % c return sc.join(ss.replace(c, sc) for ss in s.split(sc)) def unslash_chr(s, c): sc = '\\%c' % c while True: s2 = s.replace(sc, c) if s != s2: return s2 s = s2