def check_libraries_requirements(): """Check if the required dependencies are met. Calling this function at the program start will allow the program to terminate gracefully in case of an unmet dependency instead of crashing while performing important tasks.""" file_path = paths.get_base_path('requirements.txt') try: # Skip the check if we are running in a |PyInstaller| bundle. Assume everything is all right. if not paths.is_pyinstaller_bundle(): with file(file_path) as req_file: requirements = req_file.readlines() require(requirements) # Libtorrent requirements try: # Workaround for libtorrent version (not available on pip so it cannot # be written inside requirements.txt). import libtorrent if LooseVersion(libtorrent.version) < LooseVersion( libtorrent_least_required_version): raise VersionConflict( 'libtorrent {}'.format(libtorrent.version), 'libtorrent >= {}'.format( libtorrent_least_required_version)) except ImportError: raise DistributionNotFound('libtorrent') # Kivy requirements try: import multiprocessing multiprocessing.freeze_support() import kivy kivy.require(kivy_least_required_version) except ImportError: raise DistributionNotFound('kivy') except Exception: # Kivy raises an Exception with a not-so-nicely formatted message # Just print it and exit msg = "".join(_format_exc_info(*sys.exc_info())) + \ "\nAvast DeepScreen is known to fail here." MessageBox(msg, 'Kivy error') sys.exit(1) except VersionConflict as ex: message = 'Wrong library version. Installed: {}. Required: {}'.format( ex.args[0], ex.args[1]) MessageBox(message, 'Error') sys.exit(1) except DistributionNotFound as ex: message = 'Missing python library. Required: {}'.format(ex.args[0]) MessageBox(message, 'Error') sys.exit(1)
def _prepare_bootstrap(self): """ Write enough of distribute and pip into the .pex .bootstrap directory so that we can be fully self-contained. """ bare_env = pkg_resources.Environment() pip_req = pkg_resources.Requirement.parse('pip>=1.1') distribute_req = pkg_resources.Requirement.parse('distribute>=0.6.24') pip_dist = distribute_dist = None for dist in DistributionHelper.all_distributions(sys.path): if dist in pip_req and bare_env.can_add(dist): pip_dist = dist if dist in distribute_req and bare_env.can_add(dist): distribute_dist = dist if pip_dist and distribute_dist: break if not pip_dist: raise DistributionNotFound('Could not find pip!') if not distribute_dist: raise DistributionNotFound('Could not find distribute!') PEX.debug('Writing .bootstrap library.') for fn, content in DistributionHelper.walk_data(pip_dist): if fn.startswith('pip/'): # PEX.debug('BOOTSTRAP: Writing %s' % fn) self._chroot.write(content, os.path.join(self.BOOTSTRAP_DIR, fn), 'resource') for fn, content in DistributionHelper.walk_data(distribute_dist): if fn.startswith('pkg_resources.py') or fn.startswith('setuptools'): # PEX.debug('BOOTSTRAP: Writing %s' % fn) self._chroot.write(content, os.path.join(self.BOOTSTRAP_DIR, fn), 'resource') libraries = ( 'twitter.common.dirutil', 'twitter.common.collections', 'twitter.common.contextutil', 'twitter.common.lang', 'twitter.common.python' ) for name in libraries: dirname = name.replace('.', '/') provider = pkg_resources.get_provider(name) if not isinstance(provider, pkg_resources.DefaultProvider): mod = __import__(name, fromlist=['wutttt']) provider = pkg_resources.ZipProvider(mod) for fn in provider.resource_listdir(''): if fn.endswith('.py'): # PEX.debug('BOOTSTRAP: Writing %s' % os.path.join(dirname, fn)) self._chroot.write(provider.get_resource_string(name, fn), os.path.join(self.BOOTSTRAP_DIR, dirname, fn), 'resource') for initdir in ('twitter', 'twitter/common'): self._chroot.write( b"__import__('pkg_resources').declare_namespace(__name__)", os.path.join(self.BOOTSTRAP_DIR, initdir, '__init__.py'), 'resource')
def check(self, raise_=True): """Check that all requirements are available (importable) and their versions match (using pkg.__version__). :param raise_: Raise DistributionNotFound if ImportError or VersionConflict if version doesn't match? If false just return None. """ for req in self: try: mod = __import__(req.modname) except ImportError: if raise_: raise DistributionNotFound(str(req)) return False if not req.specs: # No version constraints continue try: version = mod.__version__ except AttributeError as e: raise AttributeError("%s: %s" % (e, mod)) if version not in req: if raise_: raise VersionConflict( "Need %s. Found %s" % (str(req), version)) return False return True
def test_get_version_from_package_error(self): with patch('apsconnectcli.apsconnect.pkg_resources') as pkg_mock, \ patch('apsconnectcli.apsconnect.bin_version') as bin_mock: bin_mock.return_value = 'v100500' pkg_mock.DistributionNotFound = DistributionNotFound pkg_mock.get_distribution.side_effect = DistributionNotFound() result = get_version() self.assertEqual(result, 'v100500')
def resolve_requirement(requirement: Requirement): dist = best.get(requirement.key) if dist is None: # Find the best distribution and add it to the map dist = self.by_key.get(requirement.key) if dist is None or (dist not in requirement and replace_conflicting): ws = self with lock: if env[0] is None: if dist is None: env[0] = Environment(self.entries) else: # Use an empty environment and workingset to avoid # any further conflicts with the conflicting # distribution env[0] = Environment([]) ws = WorkingSet([]) dist = best[requirement.key] = env[0].best_match( requirement, ws, installer, replace_conflicting=replace_conflicting, ) if dist is None: requirers = required_by.get(requirement, None) raise DistributionNotFound(requirement, requirers) resolved.append(dist) if dist not in requirement: # Oops, the "best" so far conflicts with a dependency dependent_requirement = required_by[requirement] raise VersionConflict( dist, requirement).with_context(dependent_requirement) with lock: # push the new requirements onto the stack new_requirements = [ requirement for requirement in dist.requires(requirement.extras)[::-1] if requirement.key not in self.excludes ] req_stack.extend(new_requirements) # Register the new requirements needed by requirement for new_requirement in new_requirements: required_by[new_requirement].add(requirement.project_name) requirement_extras[new_requirement] = requirement.extras processed[requirement] = True
def test_verify_py_pkgs(self, require_mock, find_dist_mock): find_dist_mock.return_value = [ 'gevent==1.1.0', 'lockfile==0.11.0', 'netaddr==0.7.18', 'netifaces==0.10.4', 'psutil==4.0.0', 'requests==2.12.4', 'ujson==1.33', 'python-daemon==2.0.6', 'setproctitle==1.1.10', 'rstr==2.2.3', 'flup==1.0.2', 'scandir==1.5', 'crossplane==0.3.1', 'PyMySQL==0.7.11' ] fail_count = self.healthcheck.verify_py_pkgs() assert fail_count == 0 find_dist_mock.return_value = [ 'gevent==1.1.0', 'lockfile==0.11.0', 'netaddr==0.7.18', 'netifaces==0.10.4', 'psutil==4.0.0', 'requests==2.12.4', 'ujson==1.33' ] require_mock.side_effect = DistributionNotFound( Exception, 'distribution not found') fail_count = self.healthcheck.verify_py_pkgs() assert fail_count > 0
def test_version_ko(mocker): mocker.patch('connect.client.version.get_distribution', side_effect=DistributionNotFound()) assert get_version() == '0.0.0'
def raise_exeception(name): raise DistributionNotFound("No get_distribution mock")
def raise_distribution_not_found(name): raise DistributionNotFound("pkg_resources cannot get distribution")
def _raise(name): raise DistributionNotFound("No get_distribution mock")
def test_get_version_get_distribution_fails(self): with mock.patch('pkg_resources.get_distribution') as get_distribution_mock: get_distribution_mock.side_effect = DistributionNotFound() pkg_version = get_version(__package_name__) assert pkg_version == '<none!?>'
def _get_dist(req, ws, always_unzip): # @UnusedVariable match = [d for d in dists if d in req] if match: return match raise DistributionNotFound(req)
def raise_distribution_not_found(*args, **kwargs): raise DistributionNotFound()
def resolve( self, requirements: Sequence[Requirement], env: Optional[Environment] = None, installer: Optional[Callable[[str], Distribution]] = None, replace_conflicting: Optional[bool] = False, extras: List[str] = None, ) -> List[Distribution]: """List all distributions needed to (recursively) meet `requirements` `requirements` must be a sequence of ``Requirement`` objects. `env`, if supplied, should be an ``Environment`` instance. If not supplied, it defaults to all distributions available within any entry or distribution in the working set. `installer`, if supplied, will be invoked with each requirement that cannot be met by an already-installed distribution; it should return a ``Distribution`` or ``None``. Unless `replace_conflicting=True`, raises a VersionConflict exception if any requirements are found on the path that have the correct name but the wrong version. Otherwise, if an `installer` is supplied it will be invoked to obtain the correct version of the requirement and activate it. `extras` is a list of the extras to be used with these requirements. This is important because extra requirements may look like `my_req; extra = "my_extra"`, which would otherwise be interpreted as a purely optional requirement. Instead, we want to be able to assert that these requirements are truly required. """ # set up the stack requirements = list(requirements)[::-1] # set of processed requirements processed = {} # key -> dist best = {} resolved = [] requirement_extras = _ReqExtras() # Mapping of requirement to set of distributions that required it; # useful for reporting info about conflicts. required_by = defaultdict(set) while requirements: # process dependencies breadth-first requirement = requirements.pop(0) if requirement in processed: # Ignore cyclic or redundant dependencies continue if not requirement_extras.markers_pass(requirement, extras): continue dist = best.get(requirement.key) if dist is None: # Find the best distribution and add it to the map dist = self.by_key.get(requirement.key) if dist is None or (dist not in requirement and replace_conflicting): ws = self if env is None: if dist is None: env = Environment(self.entries) else: # Use an empty environment and workingset to avoid # any further conflicts with the conflicting # distribution env = Environment([]) ws = WorkingSet([]) dist = best[requirement.key] = env.best_match( requirement, ws, installer, replace_conflicting=replace_conflicting, ) if dist is None: requirers = required_by.get(requirement, None) raise DistributionNotFound(requirement, requirers) resolved.append(dist) if dist not in requirement: # Oops, the "best" so far conflicts with a dependency dependent_requirement = required_by[requirement] raise VersionConflict(dist, requirement).with_context( dependent_requirement ) # push the new requirements onto the stack new_requirements = [ requirement for requirement in dist.requires(requirement.extras)[::-1] if requirement.key not in self.excludes ] requirements.extend(new_requirements) # Register the new requirements needed by requirement for new_requirement in new_requirements: required_by[new_requirement].add(requirement.project_name) requirement_extras[new_requirement] = requirement.extras processed[requirement] = True # return list of distros to activate return resolved
def version_test_side_effect(args): raise DistributionNotFound()
# -*- coding: utf-8 -*- from __future__ import unicode_literals from pkg_resources import get_distribution, DistributionNotFound import os from nibble.information import Information from nibble.duration import Duration from nibble.speed import Speed from nibble.expression.lexer import Lexer, LexingError from nibble.expression.parser import Parser, ParsingError __title__ = 'nibble' __author__ = 'George Brighton' __license__ = 'MIT' __copyright__ = 'Copyright 2017 George Brighton' # adapted from http://stackoverflow.com/a/17638236 try: dist = get_distribution(__title__) path = os.path.normcase(dist.location) pwd = os.path.normcase(__file__) if not pwd.startswith(os.path.join(path, __title__)): raise DistributionNotFound() __version__ = dist.version except DistributionNotFound: __version__ = 'unknown'
def mock_distributionerror(*args, **kwargs): raise DistributionNotFound(args, kwargs)
parse_connection_uri from trac.db.util import ConnectionWrapper, IterableCursor from trac.util import get_pkginfo, lazy from trac.util.compat import close_fds from trac.util.html import Markup from trac.util.text import empty, exception_to_unicode, to_unicode from trac.util.translation import _ try: import psycopg2 as psycopg import psycopg2.extensions from psycopg2 import DataError, ProgrammingError from psycopg2.extensions import register_type, UNICODE, \ register_adapter, AsIs, QuotedString except ImportError: raise DistributionNotFound('psycopg2>=2.0 or psycopg2-binary', ['Trac']) else: register_type(UNICODE) register_adapter(Markup, lambda markup: QuotedString(unicode(markup))) register_adapter(type(empty), lambda empty: AsIs("''")) psycopg2_version = get_pkginfo(psycopg).get('version', psycopg.__version__) _libpq_pathname = None if not hasattr(psycopg, 'libpq_version'): # search path of libpq only if it is dynamically linked _f = _match = None try: with open(psycopg._psycopg.__file__, 'rb') as _f: if os.name != 'nt': _match = re.search( r''' \0(