def make_module_available(module, spec=None, install=False): """Ensure module is importable""" # If we already can import it, that's great try: __import__(module) return except ImportError: pass # If it's already installed, use it # Search by spec spec = spack.spec.Spec(spec or module) # We have to run as part of this python # We can constrain by a shortened version in place of a version range # because this spec is only used for querying or as a placeholder to be # replaced by an external that already has a concrete version. This syntax # is not sufficient when concretizing without an external, as it will # concretize to [email protected] instead of [email protected] python_requirement = '^' + spec_for_current_python() spec.constrain(python_requirement) installed_specs = spack.store.db.query(spec, installed=True) for ispec in installed_specs: # TODO: make sure run-environment is appropriate module_path = ispec['python'].package.get_python_lib(prefix=ispec.prefix) try: sys.path.append(module_path) __import__(module) return except ImportError: tty.warn("Spec %s did not provide module %s" % (ispec, module)) sys.path = sys.path[:-1] def _raise_error(module_name, module_spec): error_msg = 'cannot import module "{0}"'.format(module_name) if module_spec: error_msg += ' from spec "{0}'.format(module_spec) raise ImportError(error_msg) if not install: _raise_error(module, spec) with spack_python_interpreter(): # We will install for ourselves, using this python if needed # Concretize the spec spec.concretize() spec.package.do_install() module_path = spec['python'].package.get_python_lib(prefix=spec.prefix) try: sys.path.append(module_path) __import__(module) return except ImportError: sys.path = sys.path[:-1] _raise_error(module, spec)
def concretize_develop(self, spec): """ Add ``dev_path=*`` variant to packages built from local source. """ env = spack.environment.get_env(None, None) dev_info = env.dev_specs.get(spec.name, {}) if env else {} if not dev_info: return False path = os.path.normpath(os.path.join(env.path, dev_info['path'])) if 'dev_path' in spec.variants: assert spec.variants['dev_path'].value == path changed = False else: spec.variants.setdefault( 'dev_path', vt.SingleValuedVariant('dev_path', path)) changed = True changed |= spec.constrain(dev_info['spec']) return changed