Exemple #1
0
def env_activate(args):
    env = args.activate_env
    if not args.shell:
        spack.cmd.common.shell_init_instructions(
            "spack env activate",
            "    eval `spack env activate {sh_arg} %s`" % env,
        )
        return 1

    if ev.exists(env) and not args.dir:
        spack_env = ev.root(env)
        short_name = env
        env_prompt = '[%s]' % env

    elif ev.is_env_dir(env):
        spack_env = os.path.abspath(env)
        short_name = os.path.basename(os.path.abspath(env))
        env_prompt = '[%s]' % short_name

    else:
        tty.die("No such environment: '%s'" % env)

    if spack_env == os.environ.get('SPACK_ENV'):
        tty.die("Environment %s is already active" % args.activate_env)

    active_env = ev.get_env(namedtuple('args', ['env'])(env), 'activate')
    cmds = ev.activate(active_env,
                       add_view=args.with_view,
                       shell=args.shell,
                       prompt=env_prompt if args.prompt else None)
    sys.stdout.write(cmds)
Exemple #2
0
def env_activate(args):
    if not args.activate_env and not args.dir and not args.temp:
        tty.die('spack env activate requires an environment name, directory, or --temp')

    if not args.shell:
        spack.cmd.common.shell_init_instructions(
            "spack env activate",
            "    eval `spack env activate {sh_arg} [...]`",
        )
        return 1

    # Error out when -e, -E, -D flags are given, cause they are ambiguous.
    if args.env or args.no_env or args.env_dir:
        tty.die('Calling spack env activate with --env, --env-dir and --no-env '
                'is ambiguous')

    env_name_or_dir = args.activate_env or args.dir

    # Temporary environment
    if args.temp:
        env = create_temp_env_directory()
        spack_env = os.path.abspath(env)
        short_name = os.path.basename(spack_env)
        ev.Environment(env).write(regenerate=False)

    # Named environment
    elif ev.exists(env_name_or_dir) and not args.dir:
        spack_env = ev.root(env_name_or_dir)
        short_name = env_name_or_dir

    # Environment directory
    elif ev.is_env_dir(env_name_or_dir):
        spack_env = os.path.abspath(env_name_or_dir)
        short_name = os.path.basename(spack_env)

    else:
        tty.die("No such environment: '%s'" % env_name_or_dir)

    env_prompt = '[%s]' % short_name

    if spack_env == os.environ.get('SPACK_ENV'):
        tty.debug("Environment is already active")
        return

    # Activate new environment
    active_env = ev.Environment(spack_env)
    cmds = spack.environment.shell.activate_header(
        env=active_env,
        shell=args.shell,
        prompt=env_prompt if args.prompt else None
    )
    env_mods = spack.environment.shell.activate(
        env=active_env,
        add_view=args.with_view
    )
    cmds += env_mods.shell_modifications(args.shell)

    sys.stdout.write(cmds)
Exemple #3
0
def env_activate(args):
    env = args.activate_env
    if not args.shell:
        msg = [
            "This command works best with Spack's shell support",
            ""
        ] + spack.cmd.common.shell_init_instructions + [
            'Or, if you want to use `spack env activate` without initializing',
            'shell support, you can run one of these:',
            '',
            '    eval `spack env activate --sh %s`   # for bash/sh' % env,
            '    eval `spack env activate --csh %s`  # for csh/tcsh' % env,
        ]
        tty.msg(*msg)
        return 1

    if ev.exists(env) and not args.dir:
        spack_env = ev.root(env)
        short_name = env
        env_prompt = '[%s]' % env

    elif ev.is_env_dir(env):
        spack_env = os.path.abspath(env)
        short_name = os.path.basename(os.path.abspath(env))
        env_prompt = '[%s]' % short_name

    else:
        tty.die("No such environment: '%s'" % env)

    if spack_env == os.environ.get('SPACK_ENV'):
        tty.die("Environment %s is already active" % args.activate_env)

    if args.shell == 'csh':
        # TODO: figure out how to make color work for csh
        sys.stdout.write('setenv SPACK_ENV %s;\n' % spack_env)
        sys.stdout.write('alias despacktivate "spack env deactivate";\n')
        if args.prompt:
            sys.stdout.write('if (! $?SPACK_OLD_PROMPT ) '
                             'setenv SPACK_OLD_PROMPT "${prompt}";\n')
            sys.stdout.write('set prompt="%s ${prompt}";\n' % env_prompt)

    else:
        if 'color' in os.environ['TERM']:
            env_prompt = colorize('@G{%s} ' % env_prompt, color=True)

        sys.stdout.write('export SPACK_ENV=%s;\n' % spack_env)
        sys.stdout.write("alias despacktivate='spack env deactivate';\n")
        if args.prompt:
            sys.stdout.write('if [ -z "${SPACK_OLD_PS1}" ]; then\n')
            sys.stdout.write('export SPACK_OLD_PS1="${PS1}"; fi;\n')
            sys.stdout.write('export PS1="%s ${PS1}";\n' % env_prompt)
Exemple #4
0
def env_activate(args):
    env = args.activate_env
    if not args.shell:
        msg = [
            "This command works best with Spack's shell support",
            ""
        ] + spack.cmd.common.shell_init_instructions + [
            'Or, if you want to use `spack env activate` without initializing',
            'shell support, you can run one of these:',
            '',
            '    eval `spack env activate --sh %s`   # for bash/sh' % env,
            '    eval `spack env activate --csh %s`  # for csh/tcsh' % env,
        ]
        tty.msg(*msg)
        return 1

    if ev.exists(env) and not args.dir:
        spack_env = ev.root(env)
        short_name = env
        env_prompt = '[%s]' % env

    elif ev.is_env_dir(env):
        spack_env = os.path.abspath(env)
        short_name = os.path.basename(os.path.abspath(env))
        env_prompt = '[%s]' % short_name

    else:
        tty.die("No such environment: '%s'" % env)

    if spack_env == os.environ.get('SPACK_ENV'):
        tty.die("Environment %s is already active" % args.activate_env)

    if args.shell == 'csh':
        # TODO: figure out how to make color work for csh
        sys.stdout.write('setenv SPACK_ENV %s;\n' % spack_env)
        sys.stdout.write('alias despacktivate "spack env deactivate";\n')
        if args.prompt:
            sys.stdout.write('if (! $?SPACK_OLD_PROMPT ) '
                             'setenv SPACK_OLD_PROMPT "${prompt}";\n')
            sys.stdout.write('set prompt="%s ${prompt}";\n' % env_prompt)

    else:
        if 'color' in os.environ['TERM']:
            env_prompt = colorize('@G{%s} ' % env_prompt, color=True)

        sys.stdout.write('export SPACK_ENV=%s;\n' % spack_env)
        sys.stdout.write("alias despacktivate='spack env deactivate';\n")
        if args.prompt:
            sys.stdout.write('if [ -z "${SPACK_OLD_PS1}" ]; then\n')
            sys.stdout.write('export SPACK_OLD_PS1="${PS1}"; fi;\n')
            sys.stdout.write('export PS1="%s ${PS1}";\n' % env_prompt)
Exemple #5
0
def find_environment(args):
    """Find active environment from args or environment variable.

    Check for an environment in this order:
        1. via ``spack -e ENV`` or ``spack -D DIR`` (arguments)
        2. via a path in the spack.environment.spack_env_var environment variable.

    If an environment is found, read it in.  If not, return None.

    Arguments:
        args (argparse.Namespace): argparse namespace with command arguments

    Returns:
        (spack.environment.Environment): a found environment, or ``None``
    """

    # treat env as a name
    env = args.env
    if env:
        if ev.exists(env):
            return ev.read(env)

    else:
        # if env was specified, see if it is a directory otherwise, look
        # at env_dir (env and env_dir are mutually exclusive)
        env = args.env_dir

        # if no argument, look for the environment variable
        if not env:
            env = os.environ.get(ev.spack_env_var)

            # nothing was set; there's no active environment
            if not env:
                return None

    # if we get here, env isn't the name of a spack environment; it has
    # to be a path to an environment, or there is something wrong.
    if ev.is_env_dir(env):
        return ev.Environment(env)

    raise ev.SpackEnvironmentError('no environment in %s' % env)
Exemple #6
0
def env_activate(args):
    env = args.activate_env
    if not args.shell:
        msg = [
            "This command works best with Spack's shell support",
            ""
        ] + spack.cmd.common.shell_init_instructions + [
            'Or, if you want to use `spack env activate` without initializing',
            'shell support, you can run one of these:',
            '',
            '    eval `spack env activate --sh %s`   # for bash/sh' % env,
            '    eval `spack env activate --csh %s`  # for csh/tcsh' % env,
        ]
        tty.msg(*msg)
        return 1

    if ev.exists(env) and not args.dir:
        spack_env = ev.root(env)
        short_name = env
        env_prompt = '[%s]' % env

    elif ev.is_env_dir(env):
        spack_env = os.path.abspath(env)
        short_name = os.path.basename(os.path.abspath(env))
        env_prompt = '[%s]' % short_name

    else:
        tty.die("No such environment: '%s'" % env)

    if spack_env == os.environ.get('SPACK_ENV'):
        tty.die("Environment %s is already active" % args.activate_env)

    active_env = ev.get_env(namedtuple('args', ['env'])(env),
                            'activate')
    cmds = ev.activate(
        active_env, add_view=args.with_view, shell=args.shell,
        prompt=env_prompt if args.prompt else None
    )
    sys.stdout.write(cmds)
Exemple #7
0
def env_activate(args):
    env = args.activate_env
    if not args.shell:
        spack.cmd.common.shell_init_instructions(
            "spack env activate",
            "    eval `spack env activate {sh_arg} %s`" % env,
        )
        return 1

    # Error out when -e, -E, -D flags are given, cause they are ambiguous.
    if args.env or args.no_env or args.env_dir:
        tty.die(
            'Calling spack env activate with --env, --env-dir and --no-env '
            'is ambiguous')

    if ev.exists(env) and not args.dir:
        spack_env = ev.root(env)
        short_name = env
        env_prompt = '[%s]' % env

    elif ev.is_env_dir(env):
        spack_env = os.path.abspath(env)
        short_name = os.path.basename(os.path.abspath(env))
        env_prompt = '[%s]' % short_name

    else:
        tty.die("No such environment: '%s'" % env)

    if spack_env == os.environ.get('SPACK_ENV'):
        tty.debug("Environment %s is already active" % args.activate_env)
        return

    cmds = ev.activate(ev.Environment(spack_env),
                       add_view=args.with_view,
                       shell=args.shell,
                       prompt=env_prompt if args.prompt else None)
    sys.stdout.write(cmds)
Exemple #8
0
def external(parser, args):
    extern_dir = get_external_dir()
    if args.list:
        snaps = get_all_snapshots()
        dated = get_ordered_dated_snapshots()
        if snaps and dated:
            non_dated = list(set(snaps) - set(dated))

        def print_snapshots(snaps):
            for s in snaps:
                env_dir = os.path.join(extern_dir, s)
                print(' - {path}'.format(path=env_dir))

        print('-' * 54)
        print('Available snapshot directories are:')
        print('-' * 54)
        if dated:
            print('\nDated Snapshots (ordered)')
            print('-' * 54)
            print_snapshots(dated)
        if non_dated:
            print('\nAdditional Snapshots (unordered)')
            print('-' * 54)
            print_snapshots(non_dated)
        return
    env = ev.active_environment()
    if not env:
        tty.die('spack manager external requires an active environment')
    if args.latest:
        snaps = get_ordered_dated_snapshots()
        if not snaps:
            print('WARNING: No \'externals.yaml\' created because no valid '
                  'snapshots were found. \n'
                  '  If you are trying to use a system level snapshot make '
                  'sure you have SPACK_MANAGER_EXTERNAL pointing to '
                  'spack-manager directory for the system.\n')
            return
        else:
            snap_path = os.path.join(extern_dir, snaps[0])
    else:
        snap_path = args.path

    # check that directory of ext view exists
    if not snap_path or not ev.is_env_dir(snap_path):
        tty.die('External path must point to a spack environment with a view. '
                'Auto detection of the latest dated snapshot can be achived'
                ' with the \'--latest\' flag.')

    snap_env = ev.Environment(snap_path)
    snap_env.check_views()

    if not snap_env.views:
        tty.die('Environments used to create externals must have at least 1'
                ' associated view')
    # copy the file and overwrite any that may exist (or merge?)
    inc_name_abs = os.path.abspath(os.path.join(env.path, args.name))

    try:
        detected = assemble_dict_of_detected_externals(snap_env,
                                                       args.blacklist,
                                                       args.whitelist)
        src = create_yaml_from_detected_externals(detected)
    except ev.SpackEnvironmentError as e:
        tty.die(e.long_message)

    if include_entry_exists(env, args.name):
        if args.merge:
            # merge the existing includes with the new one
            # giving precedent to the new data coming in
            dest = spack.config.read_config_file(
                inc_name_abs, spack.config.section_schemas['packages'])
            combined = spack.config.merge_yaml(src, dest)
            final = combined
        else:
            final = src
    else:
        add_include_entry(env, args.name)
        final = src

    with open(inc_name_abs, 'w') as fout:
        syaml.dump_config(final, stream=fout, default_flow_style=False)

    env.write()