summaryrefslogtreecommitdiff
path: root/lib/spack/spack/bootstrap.py
blob: 60f8153ae27512d1fbe7fa2cc72727d69e941023 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
# Copyright 2013-2022 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
from __future__ import print_function

import contextlib
import copy
import fnmatch
import functools
import json
import os
import os.path
import platform
import re
import sys
import sysconfig
import uuid

import six

import archspec.cpu

import llnl.util.filesystem as fs
import llnl.util.tty as tty
from llnl.util.lang import GroupedExceptionHandler

import spack.binary_distribution
import spack.config
import spack.detection
import spack.environment
import spack.modules
import spack.paths
import spack.platforms
import spack.repo
import spack.spec
import spack.store
import spack.user_environment
import spack.util.environment
import spack.util.executable
import spack.util.path
import spack.util.spack_yaml
import spack.util.url
import spack.version

#: Name of the file containing metadata about the bootstrapping source
METADATA_YAML_FILENAME = "metadata.yaml"

is_windows = sys.platform == "win32"

#: Map a bootstrapper type to the corresponding class
_bootstrap_methods = {}


def _bootstrapper(type):
    """Decorator to register classes implementing bootstrapping
    methods.

    Args:
        type (str): string identifying the class
    """

    def _register(cls):
        _bootstrap_methods[type] = cls
        return cls

    return _register


def _try_import_from_store(module, query_spec, query_info=None):
    """Return True if the module can be imported from an already
    installed spec, False otherwise.

    Args:
        module: Python module to be imported
        query_spec: spec that may provide the module
        query_info (dict or None): if a dict is passed it is populated with the
            command found and the concrete spec providing it
    """
    # If it is a string assume it's one of the root specs by this module
    if isinstance(query_spec, six.string_types):
        # We have to run as part of this python interpreter
        query_spec += " ^" + spec_for_current_python()

    installed_specs = spack.store.db.query(query_spec, installed=True)

    for candidate_spec in installed_specs:
        pkg = candidate_spec["python"].package
        module_paths = [
            os.path.join(candidate_spec.prefix, pkg.purelib),
            os.path.join(candidate_spec.prefix, pkg.platlib),
        ]  # type: list[str]
        path_before = list(sys.path)

        # Python 3.8+ on Windows does not search dependent DLLs in PATH,
        # so we need to manually add it using os.add_dll_directory
        # https://docs.python.org/3/whatsnew/3.8.html#bpo-36085-whatsnew
        if sys.version_info[:2] >= (3, 8) and sys.platform == "win32":
            if os.path.isdir(candidate_spec.prefix.bin):
                os.add_dll_directory(candidate_spec.prefix.bin)  # novermin

        # NOTE: try module_paths first and last, last allows an existing version in path
        # to be picked up and used, possibly depending on something in the store, first
        # allows the bootstrap version to work when an incompatible version is in
        # sys.path
        orders = [
            module_paths + sys.path,
            sys.path + module_paths,
        ]
        for path in orders:
            sys.path = path
            try:
                _fix_ext_suffix(candidate_spec)
                if _python_import(module):
                    msg = (
                        '[BOOTSTRAP MODULE {0}] The installed spec "{1}/{2}" '
                        'provides the "{0}" Python module'
                    ).format(module, query_spec, candidate_spec.dag_hash())
                    tty.debug(msg)
                    if query_info is not None:
                        query_info["spec"] = candidate_spec
                    return True
            except Exception as e:
                msg = (
                    "unexpected error while trying to import module "
                    '"{0}" from spec "{1}" [error="{2}"]'
                )
                tty.warn(msg.format(module, candidate_spec, str(e)))
            else:
                msg = "Spec {0} did not provide module {1}"
                tty.warn(msg.format(candidate_spec, module))

        sys.path = path_before

    return False


def _fix_ext_suffix(candidate_spec):
    """Fix the external suffixes of Python extensions on the fly for
    platforms that may need it

    Args:
        candidate_spec (Spec): installed spec with a Python module
            to be checked.
    """
    # Here we map target families to the patterns expected
    # by pristine CPython. Only architectures with known issues
    # are included. Known issues:
    #
    # [RHEL + ppc64le]: https://github.com/spack/spack/issues/25734
    #
    _suffix_to_be_checked = {
        "ppc64le": {
            "glob": "*.cpython-*-powerpc64le-linux-gnu.so",
            "re": r".cpython-[\w]*-powerpc64le-linux-gnu.so",
            "fmt": r"{module}.cpython-{major}{minor}m-powerpc64le-linux-gnu.so",
        }
    }

    # If the current architecture is not problematic return
    generic_target = archspec.cpu.host().family
    if str(generic_target) not in _suffix_to_be_checked:
        return

    # If there's no EXT_SUFFIX (Python < 3.5) or the suffix matches
    # the expectations, return since the package is surely good
    ext_suffix = sysconfig.get_config_var("EXT_SUFFIX")
    if ext_suffix is None:
        return

    expected = _suffix_to_be_checked[str(generic_target)]
    if fnmatch.fnmatch(ext_suffix, expected["glob"]):
        return

    # If we are here it means the current interpreter expects different names
    # than pristine CPython. So:
    # 1. Find what we have installed
    # 2. Create symbolic links for the other names, it they're not there already

    # Check if standard names are installed and if we have to create
    # link for this interpreter
    standard_extensions = fs.find(candidate_spec.prefix, expected["glob"])
    link_names = [re.sub(expected["re"], ext_suffix, s) for s in standard_extensions]
    for file_name, link_name in zip(standard_extensions, link_names):
        if os.path.exists(link_name):
            continue
        os.symlink(file_name, link_name)

    # Check if this interpreter installed something and we have to create
    # links for a standard CPython interpreter
    non_standard_extensions = fs.find(candidate_spec.prefix, "*" + ext_suffix)
    for abs_path in non_standard_extensions:
        directory, filename = os.path.split(abs_path)
        module = filename.split(".")[0]
        link_name = os.path.join(
            directory,
            expected["fmt"].format(
                module=module, major=sys.version_info[0], minor=sys.version_info[1]
            ),
        )
        if os.path.exists(link_name):
            continue
        os.symlink(abs_path, link_name)


def _executables_in_store(executables, query_spec, query_info=None):
    """Return True if at least one of the executables can be retrieved from
    a spec in store, False otherwise.

    The different executables must provide the same functionality and are
    "alternate" to each other, i.e. the function will exit True on the first
    executable found.

    Args:
        executables: list of executables to be searched
        query_spec: spec that may provide the executable
        query_info (dict or None): if a dict is passed it is populated with the
            command found and the concrete spec providing it
    """
    executables_str = ", ".join(executables)
    msg = "[BOOTSTRAP EXECUTABLES {0}] Try installed specs with query '{1}'"
    tty.debug(msg.format(executables_str, query_spec))
    installed_specs = spack.store.db.query(query_spec, installed=True)
    if installed_specs:
        for concrete_spec in installed_specs:
            bin_dir = concrete_spec.prefix.bin
            # IF we have a "bin" directory and it contains
            # the executables we are looking for
            if (
                os.path.exists(bin_dir)
                and os.path.isdir(bin_dir)
                and spack.util.executable.which_string(*executables, path=bin_dir)
            ):
                spack.util.environment.path_put_first("PATH", [bin_dir])
                if query_info is not None:
                    query_info["command"] = spack.util.executable.which(*executables, path=bin_dir)
                    query_info["spec"] = concrete_spec
                return True
    return False


class _BootstrapperBase(object):
    """Base class to derive types that can bootstrap software for Spack"""

    config_scope_name = ""

    def __init__(self, conf):
        self.name = conf["name"]
        self.url = conf["info"]["url"]

    @property
    def mirror_url(self):
        # Absolute paths
        if os.path.isabs(self.url):
            return spack.util.url.format(self.url)

        # Check for :// and assume it's an url if we find it
        if "://" in self.url:
            return self.url

        # Otherwise, it's a relative path
        return spack.util.url.format(os.path.join(self.metadata_dir, self.url))

    @property
    def mirror_scope(self):
        return spack.config.InternalConfigScope(
            self.config_scope_name, {"mirrors:": {self.name: self.mirror_url}}
        )


@_bootstrapper(type="buildcache")
class _BuildcacheBootstrapper(_BootstrapperBase):
    """Install the software needed during bootstrapping from a buildcache."""

    def __init__(self, conf):
        super(_BuildcacheBootstrapper, self).__init__(conf)
        self.metadata_dir = spack.util.path.canonicalize_path(conf["metadata"])
        self.last_search = None
        self.config_scope_name = "bootstrap_buildcache-{}".format(uuid.uuid4())

    @staticmethod
    def _spec_and_platform(abstract_spec_str):
        """Return the spec object and platform we need to use when
        querying the buildcache.

        Args:
            abstract_spec_str: abstract spec string we are looking for
        """
        # This import is local since it is needed only on Cray
        import spack.platforms.linux

        # Try to install from an unsigned binary cache
        abstract_spec = spack.spec.Spec(abstract_spec_str)
        # On Cray we want to use Linux binaries if available from mirrors
        bincache_platform = spack.platforms.real_host()
        return abstract_spec, bincache_platform

    def _read_metadata(self, package_name):
        """Return metadata about the given package."""
        json_filename = "{0}.json".format(package_name)
        json_dir = self.metadata_dir
        json_path = os.path.join(json_dir, json_filename)
        with open(json_path) as f:
            data = json.load(f)
        return data

    def _install_by_hash(self, pkg_hash, pkg_sha256, index, bincache_platform):
        index_spec = next(x for x in index if x.dag_hash() == pkg_hash)
        # Reconstruct the compiler that we need to use for bootstrapping
        compiler_entry = {
            "modules": [],
            "operating_system": str(index_spec.os),
            "paths": {
                "cc": "/dev/null",
                "cxx": "/dev/null",
                "f77": "/dev/null",
                "fc": "/dev/null",
            },
            "spec": str(index_spec.compiler),
            "target": str(index_spec.target.family),
        }
        with spack.platforms.use_platform(bincache_platform):
            with spack.config.override("compilers", [{"compiler": compiler_entry}]):
                spec_str = "/" + pkg_hash
                query = spack.binary_distribution.BinaryCacheQuery(all_architectures=True)
                matches = spack.store.find([spec_str], multiple=False, query_fn=query)
                for match in matches:
                    spack.binary_distribution.install_root_node(
                        match, allow_root=True, unsigned=True, force=True, sha256=pkg_sha256
                    )

    def _install_and_test(self, abstract_spec, bincache_platform, bincache_data, test_fn):
        # Ensure we see only the buildcache being used to bootstrap
        with spack.config.override(self.mirror_scope):
            # This index is currently needed to get the compiler used to build some
            # specs that we know by dag hash.
            spack.binary_distribution.binary_index.regenerate_spec_cache()
            index = spack.binary_distribution.update_cache_and_get_specs()

            if not index:
                raise RuntimeError("The binary index is empty")

            for item in bincache_data["verified"]:
                candidate_spec = item["spec"]
                # This will be None for things that don't depend on python
                python_spec = item.get("python", None)
                # Skip specs which are not compatible
                if not abstract_spec.satisfies(candidate_spec):
                    continue

                if python_spec is not None and python_spec not in abstract_spec:
                    continue

                for pkg_name, pkg_hash, pkg_sha256 in item["binaries"]:
                    # TODO: undo installations that didn't complete?
                    self._install_by_hash(pkg_hash, pkg_sha256, index, bincache_platform)

                info = {}
                if test_fn(query_spec=abstract_spec, query_info=info):
                    self.last_search = info
                    return True
        return False

    def try_import(self, module, abstract_spec_str):
        test_fn, info = functools.partial(_try_import_from_store, module), {}
        if test_fn(query_spec=abstract_spec_str, query_info=info):
            return True

        tty.info("Bootstrapping {0} from pre-built binaries".format(module))
        abstract_spec, bincache_platform = self._spec_and_platform(
            abstract_spec_str + " ^" + spec_for_current_python()
        )
        data = self._read_metadata(module)
        return self._install_and_test(abstract_spec, bincache_platform, data, test_fn)

    def try_search_path(self, executables, abstract_spec_str):
        test_fn, info = functools.partial(_executables_in_store, executables), {}
        if test_fn(query_spec=abstract_spec_str, query_info=info):
            self.last_search = info
            return True

        abstract_spec, bincache_platform = self._spec_and_platform(abstract_spec_str)
        tty.info("Bootstrapping {0} from pre-built binaries".format(abstract_spec.name))
        data = self._read_metadata(abstract_spec.name)
        return self._install_and_test(abstract_spec, bincache_platform, data, test_fn)


@_bootstrapper(type="install")
class _SourceBootstrapper(_BootstrapperBase):
    """Install the software needed during bootstrapping from sources."""

    def __init__(self, conf):
        super(_SourceBootstrapper, self).__init__(conf)
        self.metadata_dir = spack.util.path.canonicalize_path(conf["metadata"])
        self.conf = conf
        self.last_search = None
        self.config_scope_name = "bootstrap_source-{}".format(uuid.uuid4())

    def try_import(self, module, abstract_spec_str):
        info = {}
        if _try_import_from_store(module, abstract_spec_str, query_info=info):
            self.last_search = info
            return True

        tty.info("Bootstrapping {0} from sources".format(module))

        # If we compile code from sources detecting a few build tools
        # might reduce compilation time by a fair amount
        _add_externals_if_missing()

        # Try to build and install from sources
        with spack_python_interpreter():
            # Add hint to use frontend operating system on Cray
            concrete_spec = spack.spec.Spec(abstract_spec_str + " ^" + spec_for_current_python())

            if module == "clingo":
                # TODO: remove when the old concretizer is deprecated
                concrete_spec._old_concretize(deprecation_warning=False)
            else:
                concrete_spec.concretize()

        msg = "[BOOTSTRAP MODULE {0}] Try installing '{1}' from sources"
        tty.debug(msg.format(module, abstract_spec_str))

        # Install the spec that should make the module importable
        with spack.config.override(self.mirror_scope):
            concrete_spec.package.do_install(fail_fast=True)

        if _try_import_from_store(module, query_spec=concrete_spec, query_info=info):
            self.last_search = info
            return True
        return False

    def try_search_path(self, executables, abstract_spec_str):
        info = {}
        if _executables_in_store(executables, abstract_spec_str, query_info=info):
            self.last_search = info
            return True

        tty.info("Bootstrapping {0} from sources".format(abstract_spec_str))

        # If we compile code from sources detecting a few build tools
        # might reduce compilation time by a fair amount
        _add_externals_if_missing()

        concrete_spec = spack.spec.Spec(abstract_spec_str)
        if concrete_spec.name == "patchelf":
            concrete_spec._old_concretize(deprecation_warning=False)
        else:
            concrete_spec.concretize()

        msg = "[BOOTSTRAP] Try installing '{0}' from sources"
        tty.debug(msg.format(abstract_spec_str))
        with spack.config.override(self.mirror_scope):
            concrete_spec.package.do_install()
        if _executables_in_store(executables, concrete_spec, query_info=info):
            self.last_search = info
            return True
        return False


def _make_bootstrapper(conf):
    """Return a bootstrap object built according to the
    configuration argument
    """
    btype = conf["type"]
    return _bootstrap_methods[btype](conf)


def source_is_enabled_or_raise(conf):
    """Raise ValueError if the source is not enabled for bootstrapping"""
    trusted, name = spack.config.get("bootstrap:trusted"), conf["name"]
    if not trusted.get(name, False):
        raise ValueError("source is not trusted")


def spec_for_current_python():
    """For bootstrapping purposes we are just interested in the Python
    minor version (all patches are ABI compatible with the same minor)
    and on whether ucs4 support has been enabled for Python 2.7

    See:
      https://www.python.org/dev/peps/pep-0513/
      https://stackoverflow.com/a/35801395/771663
    """
    version_str = ".".join(str(x) for x in sys.version_info[:2])
    variant_str = ""
    if sys.version_info[0] == 2 and sys.version_info[1] == 7:
        unicode_size = sysconfig.get_config_var("Py_UNICODE_SIZE")
        variant_str = "+ucs4" if unicode_size == 4 else "~ucs4"

    spec_fmt = "python@{0} {1}"
    return spec_fmt.format(version_str, variant_str)


@contextlib.contextmanager
def spack_python_interpreter():
    """Override the current configuration to set the interpreter under
    which Spack is currently running as the only Python external spec
    available.
    """
    python_prefix = sys.exec_prefix
    external_python = spec_for_current_python()

    entry = {
        "buildable": False,
        "externals": [{"prefix": python_prefix, "spec": str(external_python)}],
    }

    with spack.config.override("packages:python::", entry):
        yield


def ensure_module_importable_or_raise(module, abstract_spec=None):
    """Make the requested module available for import, or raise.

    This function tries to import a Python module in the current interpreter
    using, in order, the methods configured in bootstrap.yaml.

    If none of the methods succeed, an exception is raised. The function exits
    on first success.

    Args:
        module (str): module to be imported in the current interpreter
        abstract_spec (str): abstract spec that might provide the module. If not
            given it defaults to "module"

    Raises:
        ImportError: if the module couldn't be imported
    """
    # If we can import it already, that's great
    tty.debug("[BOOTSTRAP MODULE {0}] Try importing from Python".format(module))
    if _python_import(module):
        return

    abstract_spec = abstract_spec or module

    h = GroupedExceptionHandler()

    for current_config in bootstrapping_sources():
        with h.forward(current_config["name"]):
            source_is_enabled_or_raise(current_config)

            b = _make_bootstrapper(current_config)
            if b.try_import(module, abstract_spec):
                return

    assert h, (
        "expected at least one exception to have been raised at this point: "
        "while bootstrapping {0}".format(module)
    )
    msg = 'cannot bootstrap the "{0}" Python module '.format(module)
    if abstract_spec:
        msg += 'from spec "{0}" '.format(abstract_spec)
    if tty.is_debug():
        msg += h.grouped_message(with_tracebacks=True)
    else:
        msg += h.grouped_message(with_tracebacks=False)
        msg += "\nRun `spack --debug ...` for more detailed errors"
    raise ImportError(msg)


def ensure_executables_in_path_or_raise(executables, abstract_spec, cmd_check=None):
    """Ensure that some executables are in path or raise.

    Args:
        executables (list): list of executables to be searched in the PATH,
            in order. The function exits on the first one found.
        abstract_spec (str): abstract spec that provides the executables
        cmd_check (object): callable predicate that takes a
            ``spack.util.executable.Executable`` command and validate it. Should return
            ``True`` if the executable is acceptable, ``False`` otherwise.
            Can be used to, e.g., ensure a suitable version of the command before
            accepting for bootstrapping.

    Raises:
        RuntimeError: if the executables cannot be ensured to be in PATH

    Return:
        Executable object

    """
    cmd = spack.util.executable.which(*executables)
    if cmd:
        if not cmd_check or cmd_check(cmd):
            return cmd

    executables_str = ", ".join(executables)

    h = GroupedExceptionHandler()

    for current_config in bootstrapping_sources():
        with h.forward(current_config["name"]):
            source_is_enabled_or_raise(current_config)

            b = _make_bootstrapper(current_config)
            if b.try_search_path(executables, abstract_spec):
                # Additional environment variables needed
                concrete_spec, cmd = b.last_search["spec"], b.last_search["command"]
                env_mods = spack.util.environment.EnvironmentModifications()
                for dep in concrete_spec.traverse(
                    root=True, order="post", deptype=("link", "run")
                ):
                    env_mods.extend(
                        spack.user_environment.environment_modifications_for_spec(
                            dep, set_package_py_globals=False
                        )
                    )
                cmd.add_default_envmod(env_mods)
                return cmd

    assert h, (
        "expected at least one exception to have been raised at this point: "
        "while bootstrapping {0}".format(executables_str)
    )
    msg = "cannot bootstrap any of the {0} executables ".format(executables_str)
    if abstract_spec:
        msg += 'from spec "{0}" '.format(abstract_spec)
    if tty.is_debug():
        msg += h.grouped_message(with_tracebacks=True)
    else:
        msg += h.grouped_message(with_tracebacks=False)
        msg += "\nRun `spack --debug ...` for more detailed errors"
    raise RuntimeError(msg)


def _python_import(module):
    try:
        __import__(module)
    except ImportError:
        return False
    return True


def _bootstrap_config_scopes():
    tty.debug("[BOOTSTRAP CONFIG SCOPE] name=_builtin")
    config_scopes = [spack.config.InternalConfigScope("_builtin", spack.config.config_defaults)]
    configuration_paths = (spack.config.configuration_defaults_path, ("bootstrap", _config_path()))
    for name, path in configuration_paths:
        platform = spack.platforms.host().name
        platform_scope = spack.config.ConfigScope(
            "/".join([name, platform]), os.path.join(path, platform)
        )
        generic_scope = spack.config.ConfigScope(name, path)
        config_scopes.extend([generic_scope, platform_scope])
        msg = "[BOOTSTRAP CONFIG SCOPE] name={0}, path={1}"
        tty.debug(msg.format(generic_scope.name, generic_scope.path))
        tty.debug(msg.format(platform_scope.name, platform_scope.path))
    return config_scopes


def _add_compilers_if_missing():
    arch = spack.spec.ArchSpec.frontend_arch()
    if not spack.compilers.compilers_for_arch(arch):
        new_compilers = spack.compilers.find_new_compilers()
        if new_compilers:
            spack.compilers.add_compilers_to_config(new_compilers, init_config=False)


def _add_externals_if_missing():
    search_list = [
        # clingo
        spack.repo.path.get_pkg_class("cmake"),
        spack.repo.path.get_pkg_class("bison"),
        # GnuPG
        spack.repo.path.get_pkg_class("gawk"),
    ]
    if is_windows:
        search_list.append(spack.repo.path.get_pkg_class("winbison"))
    detected_packages = spack.detection.by_executable(search_list)
    spack.detection.update_configuration(detected_packages, scope="bootstrap")


#: Reference counter for the bootstrapping configuration context manager
_REF_COUNT = 0


def is_bootstrapping():
    global _REF_COUNT
    return _REF_COUNT > 0


@contextlib.contextmanager
def ensure_bootstrap_configuration():
    # The context manager is reference counted to ensure we don't swap multiple
    # times if there's nested use of it in the stack. One compelling use case
    # is bootstrapping patchelf during the bootstrap of clingo.
    global _REF_COUNT
    already_swapped = bool(_REF_COUNT)
    _REF_COUNT += 1
    try:
        if already_swapped:
            yield
        else:
            with _ensure_bootstrap_configuration():
                yield
    finally:
        _REF_COUNT -= 1


@contextlib.contextmanager
def _ensure_bootstrap_configuration():
    bootstrap_store_path = store_path()
    user_configuration = _read_and_sanitize_configuration()
    with spack.environment.no_active_environment():
        with spack.platforms.prevent_cray_detection():
            with spack.platforms.use_platform(spack.platforms.real_host()):
                with spack.repo.use_repositories(spack.paths.packages_path):
                    with spack.store.use_store(bootstrap_store_path):
                        # Default configuration scopes excluding command line
                        # and builtin but accounting for platform specific scopes
                        config_scopes = _bootstrap_config_scopes()
                        with spack.config.use_configuration(*config_scopes):
                            # We may need to compile code from sources, so ensure we
                            # have compilers for the current platform
                            _add_compilers_if_missing()
                            spack.config.set("bootstrap", user_configuration["bootstrap"])
                            spack.config.set("config", user_configuration["config"])
                            with spack.modules.disable_modules():
                                with spack_python_interpreter():
                                    yield


def _read_and_sanitize_configuration():
    """Read the user configuration that needs to be reused for bootstrapping
    and remove the entries that should not be copied over.
    """
    # Read the "config" section but pop the install tree (the entry will not be
    # considered due to the use_store context manager, so it will be confusing
    # to have it in the configuration).
    config_yaml = spack.config.get("config")
    config_yaml.pop("install_tree", None)
    user_configuration = {"bootstrap": spack.config.get("bootstrap"), "config": config_yaml}
    return user_configuration


def store_path():
    """Path to the store used for bootstrapped software"""
    enabled = spack.config.get("bootstrap:enable", True)
    if not enabled:
        msg = "bootstrapping is currently disabled. " 'Use "spack bootstrap enable" to enable it'
        raise RuntimeError(msg)

    return _store_path()


def _root_path():
    """Root of all the bootstrap related folders"""
    return spack.config.get("bootstrap:root", spack.paths.default_user_bootstrap_path)


def _store_path():
    bootstrap_root_path = _root_path()
    return spack.util.path.canonicalize_path(os.path.join(bootstrap_root_path, "store"))


def _config_path():
    bootstrap_root_path = _root_path()
    return spack.util.path.canonicalize_path(os.path.join(bootstrap_root_path, "config"))


def _root_spec(spec_str):
    """Add a proper compiler and target to a spec used during bootstrapping.

    Args:
        spec_str (str): spec to be bootstrapped. Must be without compiler and target.
    """
    # Add a proper compiler hint to the root spec. We use GCC for
    # everything but MacOS and Windows.
    if str(spack.platforms.host()) == "darwin":
        spec_str += " %apple-clang"
    elif str(spack.platforms.host()) == "windows":
        spec_str += " %msvc"
    else:
        spec_str += " %gcc"

    target = archspec.cpu.host().family
    spec_str += " target={0}".format(target)

    tty.debug("[BOOTSTRAP ROOT SPEC] {0}".format(spec_str))
    return spec_str


def clingo_root_spec():
    """Return the root spec used to bootstrap clingo"""
    return _root_spec("clingo-bootstrap@spack+python")


def ensure_clingo_importable_or_raise():
    """Ensure that the clingo module is available for import."""
    ensure_module_importable_or_raise(module="clingo", abstract_spec=clingo_root_spec())


def gnupg_root_spec():
    """Return the root spec used to bootstrap GnuPG"""
    return _root_spec("gnupg@2.3:")


def ensure_gpg_in_path_or_raise():
    """Ensure gpg or gpg2 are in the PATH or raise."""
    return ensure_executables_in_path_or_raise(
        executables=["gpg2", "gpg"], abstract_spec=gnupg_root_spec()
    )


def patchelf_root_spec():
    """Return the root spec used to bootstrap patchelf"""
    # 0.13.1 is the last version not to require C++17.
    return _root_spec("patchelf@0.13.1:")


def verify_patchelf(patchelf):
    """Older patchelf versions can produce broken binaries, so we
    verify the version here.

    Arguments:

        patchelf (spack.util.executable.Executable): patchelf executable
    """
    out = patchelf("--version", output=str, error=os.devnull, fail_on_error=False).strip()
    if patchelf.returncode != 0:
        return False
    parts = out.split(" ")
    if len(parts) < 2:
        return False
    try:
        version = spack.version.Version(parts[1])
    except ValueError:
        return False
    return version >= spack.version.Version("0.13.1")


def ensure_patchelf_in_path_or_raise():
    """Ensure patchelf is in the PATH or raise."""
    # The old concretizer is not smart and we're doing its job: if the latest patchelf
    # does not concretize because the compiler doesn't support C++17, we try to
    # concretize again with an upperbound @:13.
    try:
        return ensure_executables_in_path_or_raise(
            executables=["patchelf"], abstract_spec=patchelf_root_spec(), cmd_check=verify_patchelf
        )
    except RuntimeError:
        return ensure_executables_in_path_or_raise(
            executables=["patchelf"],
            abstract_spec=_root_spec("patchelf@0.13.1:0.13"),
            cmd_check=verify_patchelf,
        )


###
# Development dependencies
###


def isort_root_spec():
    return _root_spec("py-isort@4.3.5:")


def ensure_isort_in_path_or_raise():
    """Ensure that isort is in the PATH or raise."""
    executable, root_spec = "isort", isort_root_spec()
    return ensure_executables_in_path_or_raise([executable], abstract_spec=root_spec)


def mypy_root_spec():
    return _root_spec("py-mypy@0.900:")


def ensure_mypy_in_path_or_raise():
    """Ensure that mypy is in the PATH or raise."""
    executable, root_spec = "mypy", mypy_root_spec()
    return ensure_executables_in_path_or_raise([executable], abstract_spec=root_spec)


def black_root_spec():
    # black v21 is the last version to support Python 2.7.
    # Upgrade when we no longer support Python 2.7
    return _root_spec("py-black@:21")


def ensure_black_in_path_or_raise():
    """Ensure that black is in the PATH or raise."""
    root_spec = black_root_spec()

    def check_black(black_cmd):
        """Ensure sutable black version."""
        try:
            output = black_cmd("--version", output=str)
        except Exception as e:
            tty.debug("Error getting version of %s: %s" % (black_cmd, e))
            return False

        match = re.match("black, ([^ ]+)", output)
        if not match:
            return False

        black_version = spack.version.Version(match.group(1))
        return black_version.satisfies(spack.spec.Spec(root_spec).versions)

    return ensure_executables_in_path_or_raise(["black"], root_spec, check_black)


def flake8_root_spec():
    return _root_spec("py-flake8")


def ensure_flake8_in_path_or_raise():
    """Ensure that flake8 is in the PATH or raise."""
    executable, root_spec = "flake8", flake8_root_spec()
    return ensure_executables_in_path_or_raise([executable], abstract_spec=root_spec)


def all_root_specs(development=False):
    """Return a list of all the root specs that may be used to bootstrap Spack.

    Args:
        development (bool): if True include dev dependencies
    """
    specs = [clingo_root_spec(), gnupg_root_spec(), patchelf_root_spec()]
    if development:
        specs += [isort_root_spec(), mypy_root_spec(), black_root_spec(), flake8_root_spec()]
    return specs


def _missing(name, purpose, system_only=True):
    """Message to be printed if an executable is not found"""
    msg = '[{2}] MISSING "{0}": {1}'
    if not system_only:
        return msg.format(name, purpose, "@*y{{B}}")
    return msg.format(name, purpose, "@*y{{-}}")


def _required_system_executable(exes, msg):
    """Search for an executable is the system path only."""
    if isinstance(exes, six.string_types):
        exes = (exes,)
    if spack.util.executable.which_string(*exes):
        return True, None
    return False, msg


def _required_python_module(module, query_spec, msg):
    """Check if a Python module is available in the current interpreter or
    if it can be loaded from the bootstrap store
    """
    if _python_import(module) or _try_import_from_store(module, query_spec):
        return True, None
    return False, msg


def _required_executable(exes, query_spec, msg):
    """Search for an executable in the system path or in the bootstrap store."""
    if isinstance(exes, six.string_types):
        exes = (exes,)
    if spack.util.executable.which_string(*exes) or _executables_in_store(exes, query_spec):
        return True, None
    return False, msg


def _core_requirements():
    _core_system_exes = {
        "make": _missing("make", "required to build software from sources"),
        "patch": _missing("patch", "required to patch source code before building"),
        "bash": _missing("bash", "required for Spack compiler wrapper"),
        "tar": _missing("tar", "required to manage code archives"),
        "gzip": _missing("gzip", "required to compress/decompress code archives"),
        "unzip": _missing("unzip", "required to compress/decompress code archives"),
        "bzip2": _missing("bzip2", "required to compress/decompress code archives"),
        "git": _missing("git", "required to fetch/manage git repositories"),
    }
    if platform.system().lower() == "linux":
        _core_system_exes["xz"] = _missing("xz", "required to compress/decompress code archives")

    # Executables that are not bootstrapped yet
    result = [_required_system_executable(exe, msg) for exe, msg in _core_system_exes.items()]
    # Python modules
    result.append(
        _required_python_module(
            "clingo", clingo_root_spec(), _missing("clingo", "required to concretize specs", False)
        )
    )
    return result


def _buildcache_requirements():
    _buildcache_exes = {
        "file": _missing("file", "required to analyze files for buildcaches"),
        ("gpg2", "gpg"): _missing("gpg2", "required to sign/verify buildcaches", False),
    }
    if platform.system().lower() == "darwin":
        _buildcache_exes["otool"] = _missing("otool", "required to relocate binaries")

    # Executables that are not bootstrapped yet
    result = [_required_system_executable(exe, msg) for exe, msg in _buildcache_exes.items()]

    if platform.system().lower() == "linux":
        result.append(
            _required_executable(
                "patchelf",
                patchelf_root_spec(),
                _missing("patchelf", "required to relocate binaries", False),
            )
        )

    return result


def _optional_requirements():
    _optional_exes = {
        "zstd": _missing("zstd", "required to compress/decompress code archives"),
        "svn": _missing("svn", "required to manage subversion repositories"),
        "hg": _missing("hg", "required to manage mercurial repositories"),
    }
    # Executables that are not bootstrapped yet
    result = [_required_system_executable(exe, msg) for exe, msg in _optional_exes.items()]
    return result


def _development_requirements():
    return [
        _required_executable(
            "isort", isort_root_spec(), _missing("isort", "required for style checks", False)
        ),
        _required_executable(
            "mypy", mypy_root_spec(), _missing("mypy", "required for style checks", False)
        ),
        _required_executable(
            "flake8", flake8_root_spec(), _missing("flake8", "required for style checks", False)
        ),
        _required_executable(
            "black", black_root_spec(), _missing("black", "required for code formatting", False)
        ),
    ]


def status_message(section):
    """Return a status message to be printed to screen that refers to the
    section passed as argument and a bool which is True if there are missing
    dependencies.

    Args:
        section (str): either 'core' or 'buildcache' or 'optional' or 'develop'
    """
    pass_token, fail_token = "@*g{[PASS]}", "@*r{[FAIL]}"

    # Contain the header of the section and a list of requirements
    spack_sections = {
        "core": ("{0} @*{{Core Functionalities}}", _core_requirements),
        "buildcache": ("{0} @*{{Binary packages}}", _buildcache_requirements),
        "optional": ("{0} @*{{Optional Features}}", _optional_requirements),
        "develop": ("{0} @*{{Development Dependencies}}", _development_requirements),
    }
    msg, required_software = spack_sections[section]

    with ensure_bootstrap_configuration():
        missing_software = False
        for found, err_msg in required_software():
            if not found:
                missing_software = True
                msg += "\n  " + err_msg
        msg += "\n"
        msg = msg.format(pass_token if not missing_software else fail_token)
    return msg, missing_software


def bootstrapping_sources(scope=None):
    """Return the list of configured sources of software for bootstrapping Spack

    Args:
        scope (str or None): if a valid configuration scope is given, return the
            list only from that scope
    """
    source_configs = spack.config.get("bootstrap:sources", default=None, scope=scope)
    source_configs = source_configs or []
    list_of_sources = []
    for entry in source_configs:
        current = copy.copy(entry)
        metadata_dir = spack.util.path.canonicalize_path(entry["metadata"])
        metadata_yaml = os.path.join(metadata_dir, METADATA_YAML_FILENAME)
        with open(metadata_yaml) as f:
            current.update(spack.util.spack_yaml.load(f))
        list_of_sources.append(current)
    return list_of_sources