summaryrefslogtreecommitdiff
path: root/lib/spack/spack/cmd/tutorial.py
blob: fcfab00513755ee7dfce3ac7df1145c2657b7427 (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
# Copyright 2013-2023 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)

import os.path
import shutil

import llnl.util.tty as tty
from llnl.util.filesystem import working_dir

import spack
import spack.config
import spack.paths
import spack.util.git
import spack.util.gpg
from spack.cmd.common import arguments
from spack.util.spack_yaml import syaml_dict

description = "set up spack for our tutorial (WARNING: modifies config!)"
section = "config"
level = "long"


# tutorial configuration parameters
tutorial_branch = "releases/v0.21"
tutorial_mirror = "file:///mirror"
tutorial_key = os.path.join(spack.paths.share_path, "keys", "tutorial.pub")

# configs to remove
rm_configs = [
    "~/.spack/linux/compilers.yaml",
    "~/.spack/packages.yaml",
    "~/.spack/mirrors.yaml",
    "~/.spack/modules.yaml",
    "~/.spack/config.yaml",
]


def setup_parser(subparser):
    arguments.add_common_arguments(subparser, ["yes_to_all"])


def tutorial(parser, args):
    if not spack.cmd.spack_is_git_repo():
        tty.die("This command requires a git installation of Spack!")

    if not args.yes_to_all:
        tty.msg(
            "This command will set up Spack for the tutorial at "
            "https://spack-tutorial.readthedocs.io.",
            "",
        )
        tty.warn(
            "This will modify your Spack configuration by:",
            "  - deleting some configuration in ~/.spack",
            "  - adding a mirror and trusting its public key",
            "  - checking out a particular branch of Spack",
            "",
        )
        if not tty.get_yes_or_no("Are you sure you want to proceed?"):
            tty.die("Aborted")

    rm_cmds = [f"rm -f {f}" for f in rm_configs]
    tty.msg("Reverting compiler and repository configuration", *rm_cmds)
    for path in rm_configs:
        if os.path.exists(path):
            shutil.rmtree(path, ignore_errors=True)

    tty.msg(
        "Ensuring that the tutorial binary mirror is configured:",
        f"spack mirror add tutorial {tutorial_mirror}",
    )
    mirror_config = syaml_dict()
    mirror_config["tutorial"] = tutorial_mirror
    spack.config.set("mirrors", mirror_config, scope="user")

    tty.msg("Ensuring that we trust tutorial binaries", f"spack gpg trust {tutorial_key}")
    spack.util.gpg.trust(tutorial_key)

    # Note that checkout MUST be last. It changes Spack under our feet.
    # If you don't put this last, you'll get import errors for the code
    # that follows (exacerbated by the various lazy singletons we use)
    tty.msg(f"Ensuring we're on the {tutorial_branch} branch")
    git = spack.util.git.git(required=True)
    with working_dir(spack.paths.prefix):
        git("checkout", tutorial_branch)
    # NO CODE BEYOND HERE