Source code for spack.cmd.develop

# 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
import shutil

import llnl.util.tty as tty

import spack.cmd
import spack.cmd.common.arguments as arguments
import spack.spec
import spack.util.path
import spack.version
from spack.error import SpackError

description = "add a spec to an environment's dev-build information"
section = "environments"
level = "long"


[docs]def setup_parser(subparser): subparser.add_argument("-p", "--path", help="Source location of package") clone_group = subparser.add_mutually_exclusive_group() clone_group.add_argument( "--no-clone", action="store_false", dest="clone", default=None, help="Do not clone. The package already exists at the source path", ) clone_group.add_argument( "--clone", action="store_true", dest="clone", default=None, help="Clone the package even if the path already exists", ) subparser.add_argument( "-f", "--force", help="Remove any files or directories that block cloning source code" ) arguments.add_common_arguments(subparser, ["spec"])
[docs]def develop(parser, args): env = spack.cmd.require_active_env(cmd_name="develop") if not args.spec: if args.clone is False: raise SpackError("No spec provided to spack develop command") # download all dev specs for name, entry in env.dev_specs.items(): path = entry.get("path", name) abspath = spack.util.path.canonicalize_path(path, default_wd=env.path) if os.path.exists(abspath): msg = "Skipping developer download of %s" % entry["spec"] msg += " because its path already exists." tty.msg(msg) continue # Both old syntax `spack develop pkg@x` and new syntax `spack develop pkg@=x` # are currently supported. spec = spack.spec.parse_with_version_concrete(entry["spec"]) pkg_cls = spack.repo.path.get_pkg_class(spec.name) pkg_cls(spec).stage.steal_source(abspath) if not env.dev_specs: tty.warn("No develop specs to download") return specs = spack.cmd.parse_specs(args.spec) if len(specs) > 1: raise SpackError("spack develop requires at most one named spec") spec = specs[0] version = spec.versions.concrete_range_as_version if not version: raise SpackError("Packages to develop must have a concrete version") spec.versions = spack.version.VersionList([version]) # default path is relative path to spec.name path = args.path or spec.name abspath = spack.util.path.canonicalize_path(path, default_wd=env.path) # clone default: only if the path doesn't exist clone = args.clone if clone is None: clone = not os.path.exists(abspath) if not clone and not os.path.exists(abspath): raise SpackError("Provided path %s does not exist" % abspath) if clone and os.path.exists(abspath): if args.force: shutil.rmtree(abspath) else: msg = "Path %s already exists and cannot be cloned to." % abspath msg += " Use `spack develop -f` to overwrite." raise SpackError(msg) with env.write_transaction(): changed = env.develop(spec, path, clone) if changed: env.write()