|
| 1 | +# Copyright 2013-2020 Lawrence Livermore National Security, LLC and other |
| 2 | +# Spack Project Developers. See the top-level COPYRIGHT file for details. |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: (Apache-2.0 OR MIT) |
| 5 | +import contextlib |
| 6 | +import os |
| 7 | +import sys |
| 8 | + |
| 9 | +import llnl.util.filesystem as fs |
| 10 | +import llnl.util.tty as tty |
| 11 | + |
| 12 | +import spack.architecture |
| 13 | +import spack.config |
| 14 | +import spack.paths |
| 15 | +import spack.repo |
| 16 | +import spack.spec |
| 17 | +import spack.store |
| 18 | +import spack.user_environment as uenv |
| 19 | +import spack.util.executable |
| 20 | +from spack.util.environment import EnvironmentModifications |
| 21 | + |
| 22 | + |
| 23 | +@contextlib.contextmanager |
| 24 | +def spack_python_interpreter(): |
| 25 | + """Override the current configuration to set the interpreter under |
| 26 | + which Spack is currently running as the only Python external spec |
| 27 | + available. |
| 28 | + """ |
| 29 | + python_cls = type(spack.spec.Spec('python').package) |
| 30 | + python_prefix = os.path.dirname(os.path.dirname(sys.executable)) |
| 31 | + externals = python_cls.determine_spec_details( |
| 32 | + python_prefix, [os.path.basename(sys.executable)]) |
| 33 | + external_python = externals[0] |
| 34 | + |
| 35 | + entry = { |
| 36 | + 'buildable': False, |
| 37 | + 'externals': [ |
| 38 | + {'prefix': python_prefix, 'spec': str(external_python)} |
| 39 | + ] |
| 40 | + } |
| 41 | + |
| 42 | + with spack.config.override('packages:python::', entry): |
| 43 | + yield |
| 44 | + |
| 45 | + |
| 46 | +def make_module_available(module, spec=None, install=False): |
| 47 | + """Ensure module is importable""" |
| 48 | + # If we already can import it, that's great |
| 49 | + try: |
| 50 | + __import__(module) |
| 51 | + return |
| 52 | + except ImportError: |
| 53 | + pass |
| 54 | + |
| 55 | + # If it's already installed, use it |
| 56 | + # Search by spec |
| 57 | + spec = spack.spec.Spec(spec or module) |
| 58 | + |
| 59 | + # We have to run as part of this python |
| 60 | + # We can constrain by a shortened version in place of a version range |
| 61 | + # because this spec is only used for querying or as a placeholder to be |
| 62 | + # replaced by an external that already has a concrete version. This syntax |
| 63 | + # is not suffucient when concretizing without an external, as it will |
| 64 | + # concretize to python@X.Y instead of python@X.Y.Z |
| 65 | + spec.constrain('^python@%d.%d' % sys.version_info[:2]) |
| 66 | + installed_specs = spack.store.db.query(spec, installed=True) |
| 67 | + |
| 68 | + for ispec in installed_specs: |
| 69 | + # TODO: make sure run-environment is appropriate |
| 70 | + module_path = os.path.join(ispec.prefix, |
| 71 | + ispec['python'].package.site_packages_dir) |
| 72 | + module_path_64 = module_path.replace('/lib/', '/lib64/') |
| 73 | + try: |
| 74 | + sys.path.append(module_path) |
| 75 | + sys.path.append(module_path_64) |
| 76 | + __import__(module) |
| 77 | + return |
| 78 | + except ImportError: |
| 79 | + tty.warn("Spec %s did not provide module %s" % (ispec, module)) |
| 80 | + sys.path = sys.path[:-2] |
| 81 | + |
| 82 | + def _raise_error(module_name, module_spec): |
| 83 | + error_msg = 'cannot import module "{0}"'.format(module_name) |
| 84 | + if module_spec: |
| 85 | + error_msg += ' from spec "{0}'.format(module_spec) |
| 86 | + raise ImportError(error_msg) |
| 87 | + |
| 88 | + if not install: |
| 89 | + _raise_error(module, spec) |
| 90 | + |
| 91 | + with spack_python_interpreter(): |
| 92 | + # We will install for ourselves, using this python if needed |
| 93 | + # Concretize the spec |
| 94 | + spec.concretize() |
| 95 | + spec.package.do_install() |
| 96 | + |
| 97 | + module_path = os.path.join(spec.prefix, |
| 98 | + spec['python'].package.site_packages_dir) |
| 99 | + module_path_64 = module_path.replace('/lib/', '/lib64/') |
| 100 | + try: |
| 101 | + sys.path.append(module_path) |
| 102 | + sys.path.append(module_path_64) |
| 103 | + __import__(module) |
| 104 | + return |
| 105 | + except ImportError: |
| 106 | + sys.path = sys.path[:-2] |
| 107 | + _raise_error(module, spec) |
| 108 | + |
| 109 | + |
| 110 | +def get_executable(exe, spec=None, install=False): |
| 111 | + """Find an executable named exe, either in PATH or in Spack |
| 112 | +
|
| 113 | + Args: |
| 114 | + exe (str): needed executable name |
| 115 | + spec (Spec or str): spec to search for exe in (default exe) |
| 116 | + install (bool): install spec if not available |
| 117 | +
|
| 118 | + When ``install`` is True, Spack will use the python used to run Spack as an |
| 119 | + external. The ``install`` option should only be used with packages that |
| 120 | + install quickly (when using external python) or are guaranteed by Spack |
| 121 | + organization to be in a binary mirror (clingo).""" |
| 122 | + # Search the system first |
| 123 | + runner = spack.util.executable.which(exe) |
| 124 | + if runner: |
| 125 | + return runner |
| 126 | + |
| 127 | + # Check whether it's already installed |
| 128 | + spec = spack.spec.Spec(spec or exe) |
| 129 | + installed_specs = spack.store.db.query(spec, installed=True) |
| 130 | + for ispec in installed_specs: |
| 131 | + # filter out directories of the same name as the executable |
| 132 | + exe_path = [exe_p for exe_p in fs.find(ispec.prefix, exe) |
| 133 | + if fs.is_exe(exe_p)] |
| 134 | + if exe_path: |
| 135 | + ret = spack.util.executable.Executable(exe_path[0]) |
| 136 | + envmod = EnvironmentModifications() |
| 137 | + for dep in ispec.traverse(root=True, order='post'): |
| 138 | + envmod.extend(uenv.environment_modifications_for_spec(dep)) |
| 139 | + ret.add_default_envmod(envmod) |
| 140 | + return ret |
| 141 | + else: |
| 142 | + tty.warn('Exe %s not found in prefix %s' % (exe, ispec.prefix)) |
| 143 | + |
| 144 | + def _raise_error(executable, exe_spec): |
| 145 | + error_msg = 'cannot find the executable "{0}"'.format(executable) |
| 146 | + if exe_spec: |
| 147 | + error_msg += ' from spec "{0}'.format(exe_spec) |
| 148 | + raise RuntimeError(error_msg) |
| 149 | + |
| 150 | + # If we're not allowed to install this for ourselves, we can't find it |
| 151 | + if not install: |
| 152 | + _raise_error(exe, spec) |
| 153 | + |
| 154 | + with spack_python_interpreter(): |
| 155 | + # We will install for ourselves, using this python if needed |
| 156 | + # Concretize the spec |
| 157 | + spec.concretize() |
| 158 | + |
| 159 | + spec.package.do_install() |
| 160 | + # filter out directories of the same name as the executable |
| 161 | + exe_path = [exe_p for exe_p in fs.find(spec.prefix, exe) |
| 162 | + if fs.is_exe(exe_p)] |
| 163 | + if exe_path: |
| 164 | + ret = spack.util.executable.Executable(exe_path[0]) |
| 165 | + envmod = EnvironmentModifications() |
| 166 | + for dep in spec.traverse(root=True, order='post'): |
| 167 | + envmod.extend(uenv.environment_modifications_for_spec(dep)) |
| 168 | + ret.add_default_envmod(envmod) |
| 169 | + return ret |
| 170 | + |
| 171 | + _raise_error(exe, spec) |
| 172 | + |
| 173 | + |
| 174 | +@contextlib.contextmanager |
| 175 | +def ensure_bootstrap_configuration(): |
| 176 | + # Default configuration scopes excluding command |
| 177 | + # line and builtin |
| 178 | + config_scopes = [ |
| 179 | + spack.config.ConfigScope(name, path) |
| 180 | + for name, path in spack.config.configuration_paths |
| 181 | + ] |
| 182 | + |
| 183 | + with spack.architecture.use_platform(spack.architecture.real_platform()): |
| 184 | + with spack.config.use_configuration(*config_scopes): |
| 185 | + with spack.repo.use_repositories(spack.paths.packages_path): |
| 186 | + with spack.store.use_store(spack.paths.user_bootstrap_store): |
| 187 | + with spack_python_interpreter(): |
| 188 | + yield |
0 commit comments