Exemple #1
0
 def setUp(self):
     self.backup = []
     # paranoid check
     self.cfgstr = str(cfg)
     # clean up externals cfg for proper testing
     if cfg.has_section('externals'):
         self.backup = copy.deepcopy(cfg.items('externals'))
     cfg.remove_section('externals')
Exemple #2
0
 def setUp(self):
     self.backup = []
     # paranoid check
     self.cfgstr = str(cfg)
     # clean up externals cfg for proper testing
     if cfg.has_section('externals'):
         self.backup = copy.deepcopy(cfg.items('externals'))
     cfg.remove_section('externals')
Exemple #3
0
 def tearDown(self):
     if len(self.backup):
         # wipe existing one completely
         if cfg.has_section('externals'):
             cfg.remove_section('externals')
         cfg.add_section('externals')
         for o,v in self.backup:
             cfg.set('externals', o,v)
     # paranoid check
     # since order can't be guaranteed, lets check
     # each item after sorting
     self.assertEqual(sorted(self.cfgstr.split('\n')),
                          sorted(str(cfg).split('\n')))
Exemple #4
0
 def tearDown(self):
     if len(self.backup):
         # wipe existing one completely
         if cfg.has_section('externals'):
             cfg.remove_section('externals')
         cfg.add_section('externals')
         for o, v in self.backup:
             cfg.set('externals', o, v)
     # paranoid check
     # since order can't be guaranteed, lets check
     # each item after sorting
     self.assertEqual(sorted(self.cfgstr.split('\n')),
                      sorted(str(cfg).split('\n')))
Exemple #5
0
        if __debug__:
            debug('EXT', "Presence of %s is%s verified%s" %
                  (dep, {True:'', False:' NOT'}[result], error_str))

    if not result:
        if raise_:
            raise exception("Required external '%s' was not found" % dep)
        if issueWarning is not None \
               and cfg.getboolean('externals', 'issue warning', True):
            if issueWarning is True:
                warning("Required external '%s' was not found" % dep)
            else:
                warning(issueWarning)

    # store result in config manager
    if not cfg.has_section('externals'):
        cfg.add_section('externals')
    if result:
        cfg.set('externals', 'have ' + dep, 'yes')
    else:
        cfg.set('externals', 'have ' + dep, 'no')

    return result

# Bind functions for some versions checks
versions._KNOWN.update({
    'shogun': __assign_shogun_version,
    'shogun:rev': __assign_shogun_version,
    'shogun:full': __assign_shogun_version,
    })
Exemple #6
0
def exists(dep, force=False, raise_=False, issueWarning=None,
           exception=RuntimeError):
    """
    Test whether a known dependency is installed on the system.

    This method allows us to test for individual dependencies without
    testing all known dependencies. It also ensures that we only test
    for a dependency once.

    Parameters
    ----------
    dep : string or list of string
      The dependency key(s) to test.
    force : boolean
      Whether to force the test even if it has already been
      performed.
    raise_ : boolean, str
      Whether to raise an exception if dependency is missing.
      If True, it is still conditioned on the global setting
      MVPA_EXTERNALS_RAISE_EXCEPTION, while would raise exception
      if missing despite the configuration if 'always'.
    issueWarning : string or None or True
      If string, warning with given message would be thrown.
      If True, standard message would be used for the warning
      text.
    exception : exception, optional
      What exception to raise.  Defaults to RuntimeError
    """
    # if we are provided with a list of deps - go through all of them
    if isinstance(dep, (list, tuple)):
        results = [ exists(dep_, force, raise_) for dep_ in dep ]
        return bool(reduce(lambda x, y: x and y, results, True))

    # where to look in cfg
    cfgid = 'have ' + dep

    # pre-handle raise_ according to the global settings and local argument
    if isinstance(raise_, str):
        if raise_.lower() == 'always':
            raise_ = True
        else:
            raise ValueError("Unknown value of raise_=%s. "
                             "Must be bool or 'always'" % raise_)
    else: # must be bool conditioned on the global settings
        raise_ = (raise_
                  and cfg.getboolean('externals', 'raise exception', True))

    # prevent unnecessary testing
    if cfg.has_option('externals', cfgid) \
       and not cfg.getboolean('externals', 'retest', default='no') \
       and not force:
        if __debug__:
            debug('EXT', "Skip retesting for '%s'." % dep)

        # check whether an exception should be raised, even though the external
        # was already tested previously
        if not cfg.getboolean('externals', cfgid) and raise_:
            raise exception("Required external '%s' was not found" % dep)
        return cfg.getboolean('externals', cfgid)

    # determine availability of external (non-cached)

    # default to 'not found'
    result = False

    if dep not in _KNOWN:
        raise ValueError("%r is not a known dependency key." % (dep,))
    else:
        # try and load the specific dependency
        if __debug__:
            debug('EXT', "Checking for the presence of %s" % dep)

        # Exceptions which are silently caught while running tests for externals
        _caught_exceptions = [ImportError, AttributeError, RuntimeError]

        try:
            # Suppress NumPy warnings while testing for externals
            old_handling = np.seterr(all="ignore")

            error_str = ''
            try:
                exec(_KNOWN[dep])
                result = True
            except tuple(_caught_exceptions) as e:
                error_str = ". Caught exception was: " + str(e)
            except Exception as e:
                # Add known ones by their names so we don't need to
                # actually import anything manually to get those classes
                if e.__class__.__name__ in ['RPy_Exception', 'RRuntimeError',
                                            'RPy_RException']:
                    _caught_exceptions += [e.__class__]
                    error_str = ". Caught exception was: " + str(e)
                else:
                    raise
        finally:
            # And restore warnings
            np.seterr(**old_handling)

        if __debug__:
            vstr = ' (%s)' % versions[dep] if dep in versions else ''
            debug('EXT', "Presence of %s%s is%s verified%s" %
                  (dep, vstr, {True: '', False: ' NOT'}[result], error_str))

    if not result:
        if raise_:
            raise exception("Required external '%s' was not found" % dep)
        if issueWarning is not None \
               and cfg.getboolean('externals', 'issue warning', True):
            if issueWarning is True:
                warning("Required external '%s' was not found" % dep)
            else:
                warning(issueWarning)

    # store result in config manager
    if not cfg.has_section('externals'):
        cfg.add_section('externals')
    if result:
        cfg.set('externals', 'have ' + dep, 'yes')
    else:
        cfg.set('externals', 'have ' + dep, 'no')

    return result