From d87a6525823e0d75b9f6b71fb265ebffb9c060a4 Mon Sep 17 00:00:00 2001
From: Todd Gamblin <tgamblin@llnl.gov>
Date: Fri, 22 Aug 2014 11:00:19 -0700
Subject: [PATCH] Add spack cd and spack location commands.

- Better shell support for cd'ing into directories
- Fix some csh weirdness with nested aliases.
---
 lib/spack/spack/cmd/cd.py                     | 38 ++++++++
 lib/spack/spack/cmd/location.py               | 93 +++++++++++++++++++
 lib/spack/spack/cmd/mirror.py                 |  2 +-
 lib/spack/spack/cmd/stage.py                  | 30 +-----
 lib/spack/spack/modules.py                    |  6 +-
 .../csh/{spack_pathadd.csh => pathadd.csh}    |  0
 share/spack/csh/spack.csh                     | 50 +++++-----
 share/spack/setup-env.csh                     |  2 +-
 share/spack/setup-env.sh                      | 38 ++++----
 9 files changed, 186 insertions(+), 73 deletions(-)
 create mode 100644 lib/spack/spack/cmd/cd.py
 create mode 100644 lib/spack/spack/cmd/location.py
 rename share/spack/csh/{spack_pathadd.csh => pathadd.csh} (100%)

diff --git a/lib/spack/spack/cmd/cd.py b/lib/spack/spack/cmd/cd.py
new file mode 100644
index 0000000000..24d56db7d0
--- /dev/null
+++ b/lib/spack/spack/cmd/cd.py
@@ -0,0 +1,38 @@
+##############################################################################
+# Copyright (c) 2013, Lawrence Livermore National Security, LLC.
+# Produced at the Lawrence Livermore National Laboratory.
+#
+# This file is part of Spack.
+# Written by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
+# LLNL-CODE-647188
+#
+# For details, see https://scalability-llnl.github.io/spack
+# Please also see the LICENSE file for our notice and the LGPL.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License (as published by
+# the Free Software Foundation) version 2.1 dated February 1999.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
+# conditions of the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+##############################################################################
+import spack.cmd.location
+import spack.modules
+
+description="cd to spack directories in the shell."
+
+def setup_parser(subparser):
+    """This is for decoration -- spack cd is used through spack's
+       shell support.  This allows spack cd to print a descriptive
+       help message when called with -h."""
+    spack.cmd.location.setup_parser(subparser)
+
+
+def cd(parser, args):
+    spack.modules.print_help()
diff --git a/lib/spack/spack/cmd/location.py b/lib/spack/spack/cmd/location.py
new file mode 100644
index 0000000000..074d984ee6
--- /dev/null
+++ b/lib/spack/spack/cmd/location.py
@@ -0,0 +1,93 @@
+##############################################################################
+# Copyright (c) 2013, Lawrence Livermore National Security, LLC.
+# Produced at the Lawrence Livermore National Laboratory.
+#
+# This file is part of Spack.
+# Written by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
+# LLNL-CODE-647188
+#
+# For details, see https://scalability-llnl.github.io/spack
+# Please also see the LICENSE file for our notice and the LGPL.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License (as published by
+# the Free Software Foundation) version 2.1 dated February 1999.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
+# conditions of the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+##############################################################################
+import os
+from external import argparse
+
+import llnl.util.tty as tty
+from llnl.util.filesystem import join_path
+
+import spack
+import spack.cmd
+
+description="Print out locations of various diectories used by Spack"
+
+def setup_parser(subparser):
+    global directories
+    directories = subparser.add_mutually_exclusive_group()
+
+    directories.add_argument(
+        '-m', '--module-dir', action='store_true', help="Spack python module directory.")
+    directories.add_argument(
+        '-r', '--spack-root', action='store_true', help="Spack installation root.")
+
+    directories.add_argument(
+        '-i', '--install-dir', action='store_true',
+        help="Install prefix for spec (spec need not be installed).")
+    directories.add_argument(
+        '-p', '--package-dir', action='store_true',
+        help="Directory enclosing a spec's package.py file.")
+    directories.add_argument(
+        '-s', '--stage-dir', action='store_true', help="Stage directory for a spec.")
+    directories.add_argument(
+        '-b', '--build-dir', action='store_true',
+        help="Expanded archive directory for a spec (requires it to be staged first).")
+
+    subparser.add_argument(
+        'spec', nargs=argparse.REMAINDER, help="spec of package to fetch directory for.")
+
+
+def location(parser, args):
+    if args.module_dir:
+        print spack.module_path
+
+    elif args.spack_root:
+        print spack.prefix
+
+    else:
+        specs = spack.cmd.parse_specs(args.spec, concretize=True)
+        if not specs:
+            tty.die("You must supply a spec.")
+        if len(specs) != 1:
+            tty.die("Too many specs.  Need only one.")
+        spec = specs[0]
+
+        if args.install_dir:
+            print spec.prefix
+
+        elif args.package_dir:
+            print join_path(spack.db.root, spec.name)
+
+        else:
+            pkg = spack.db.get(spec)
+
+            if args.stage_dir:
+                print pkg.stage.path
+
+            else:  #  args.build_dir is the default.
+                if not os.listdir(pkg.stage.path):
+                    tty.die("Build directory does not exist yet. Run this to create it:",
+                            "spack stage " + " ".join(args.spec))
+                print pkg.stage.expanded_archive_path
+
diff --git a/lib/spack/spack/cmd/mirror.py b/lib/spack/spack/cmd/mirror.py
index a039e3383a..b42b329085 100644
--- a/lib/spack/spack/cmd/mirror.py
+++ b/lib/spack/spack/cmd/mirror.py
@@ -41,7 +41,7 @@
 from spack.util.compression import extension
 
 
-description = "Manage spack mirrors."
+description = "Manage mirrors."
 
 def setup_parser(subparser):
     subparser.add_argument(
diff --git a/lib/spack/spack/cmd/stage.py b/lib/spack/spack/cmd/stage.py
index 5df0ffc2a5..c8bc473c55 100644
--- a/lib/spack/spack/cmd/stage.py
+++ b/lib/spack/spack/cmd/stage.py
@@ -37,13 +37,6 @@ def setup_parser(subparser):
         help="Do not check downloaded packages against checksum")
 
     dir_parser = subparser.add_mutually_exclusive_group()
-    dir_parser.add_argument(
-        '-d', '--print-stage-dir', action='store_const', dest='print_dir',
-        const='print_stage', help="Prints out the stage directory for a spec.")
-    dir_parser.add_argument(
-        '-b', '--print-build-dir', action='store_const', dest='print_dir',
-        const='print_build', help="Prints out the expanded archive path for a spec.")
-
     subparser.add_argument(
         'specs', nargs=argparse.REMAINDER, help="specs of packages to stage")
 
@@ -56,24 +49,7 @@ def stage(parser, args):
         spack.do_checksum = False
 
     specs = spack.cmd.parse_specs(args.specs, concretize=True)
-
-    if args.print_dir:
-        if len(specs) != 1:
-            tty.die("--print-stage-dir and --print-build-dir options only take one spec.")
-
-        spec = specs[0]
-        pkg = spack.db.get(spec)
-
-        if args.print_dir == 'print_stage':
-            print pkg.stage.path
-        elif args.print_dir == 'print_build':
-            if not os.listdir(pkg.stage.path):
-                tty.die("Stage directory is empty.  Run this first:",
-                        "spack stage " + " ".join(args.specs))
-            print pkg.stage.expanded_archive_path
-
-    else:
-        for spec in specs:
-            package = spack.db.get(spec)
-            package.do_stage()
+    for spec in specs:
+        package = spack.db.get(spec)
+        package.do_stage()
 
diff --git a/lib/spack/spack/modules.py b/lib/spack/spack/modules.py
index 5d2105e37c..755e9ea900 100644
--- a/lib/spack/spack/modules.py
+++ b/lib/spack/spack/modules.py
@@ -64,10 +64,10 @@
 def print_help():
     """For use by commands to tell user how to activate shell support."""
 
-    tty.msg("Spack module/dotkit support is not initialized.",
+    tty.msg("This command requires spack's shell integration.",
             "",
-            "To use dotkit or modules with Spack, you must first run",
-            "one of the commands below.  You can copy/paste them.",
+            "To initialize spack's shell commands, you must run one of",
+            "the commands below.  Choose the right command for your shell.",
             "",
             "For bash and zsh:",
             "    . %s/setup-env.sh" % spack.share_path,
diff --git a/share/spack/csh/spack_pathadd.csh b/share/spack/csh/pathadd.csh
similarity index 100%
rename from share/spack/csh/spack_pathadd.csh
rename to share/spack/csh/pathadd.csh
diff --git a/share/spack/csh/spack.csh b/share/spack/csh/spack.csh
index 6073673333..30c4ec1361 100644
--- a/share/spack/csh/spack.csh
+++ b/share/spack/csh/spack.csh
@@ -45,9 +45,9 @@ set _sp_spec=""
 # Figure out what type of module we're running here.
 set _sp_modtype = ""
 switch ($_sp_subcommand)
-case "cd":
+case cd:
     shift _sp_args
-    cd `spack stage --print-build-dir $_sp_args`
+    cd `spack location $_sp_args`
     breaksw
 case use:
 case unuse:
@@ -59,30 +59,36 @@ case unload:
         shift _sp_spec
         set _sp_spec = ($_sp_spec)
     endif
-    # Translate the parameter into pieces of a command.
-    # _sp_modtype is an arg to spack module find, and
-    # _sp_sh_cmd is the equivalent shell command.
-    switch ($_sp_subcommand)
-        case use:
-        case unuse:
-            set _sp_modtype = dotkit
-            set _sp_sh_cmd = ( "`alias $_sp_subcommand'" )
-            breaksw
-        case load:
-        case unload:
-            set _sp_modtype = tcl
-            set _sp_sh_cmd = ( "`alias module`" $_sp_subcommand )
-            breaksw
-    endsw
 
     # Here the user has run use or unuse with a spec.  Find a matching
     # spec using 'spack module find', then use the appropriate module
     # tool's commands to add/remove the result from the environment.
-    # If spack module command comes back with an error, do nothing.
-    set _sp_full_spec = ""
-    if { set _sp_full_spec = `\spack module find $_sp_modtype $_sp_spec` } then
-         $_sp_sh_cmd $_sp_module_args $_sp_full_spec
-    endif
+    switch ($_sp_subcommand)
+        case "use":
+            set _sp_full_spec = ( "`\spack $_sp_flags module find dotkit $_sp_spec`" )
+            if ( $? == 0 ) then
+                use $_sp_module_args $_sp_full_spec
+            endif
+            breaksw
+        case "unuse":
+            set _sp_full_spec = ( "`\spack $_sp_flags module find dotkit $_sp_spec`" )
+            if ( $? == 0 ) then
+                unuse $_sp_module_args $_sp_full_spec
+            endif
+            breaksw
+        case "load":
+            set _sp_full_spec = ( "`\spack $_sp_flags module find tcl $_sp_spec`" )
+            if ( $? == 0 ) then
+                module load $_sp_module_args $_sp_full_spec
+            endif
+            breaksw
+        case "unload":
+            set _sp_full_spec = ( "`\spack $_sp_flags module find tcl $_sp_spec`" )
+            if ( $? == 0 ) then
+                module unload $_sp_module_args $_sp_full_spec
+            endif
+            breaksw
+    endsw
     breaksw
 
 default:
diff --git a/share/spack/setup-env.csh b/share/spack/setup-env.csh
index cc12eae82f..5f91670a60 100755
--- a/share/spack/setup-env.csh
+++ b/share/spack/setup-env.csh
@@ -37,7 +37,7 @@ if ($?SPACK_ROOT) then
 
     # Command aliases point at separate source files
     alias spack          'set _sp_args = (\!*); source $_spack_share_dir/csh/spack.csh'
-    alias _spack_pathadd 'set _pa_args = (\!*) && source $_spack_share_dir/csh/spack_pathadd.csh'
+    alias _spack_pathadd 'set _pa_args = (\!*) && source $_spack_share_dir/csh/pathadd.csh'
 
     # Set up modules and dotkit search paths in the user environment
     # TODO: fix SYS_TYPE to something non-LLNL-specific
diff --git a/share/spack/setup-env.sh b/share/spack/setup-env.sh
index 9a6090a93b..6f56d4739b 100755
--- a/share/spack/setup-env.sh
+++ b/share/spack/setup-env.sh
@@ -76,7 +76,7 @@ function spack {
     # command.
     case $_sp_subcommand in
         "cd")
-            cd $(spack stage --print-build-dir "$@")
+            cd $(spack location "$@")
             return
             ;;
         "use"|"unuse"|"load"|"unload")
@@ -87,28 +87,28 @@ function spack {
                 _sp_spec="$@"
             fi
 
-            # Translate the parameter into pieces of a command.
-            # _sp_modtype is an arg to spack module find, and
-            # _sp_sh_cmd is the equivalent shell command.
-            case $_sp_subcommand in
-                "use"|"unuse")
-                    _sp_modtype=dotkit
-                    _sp_sh_cmd=$_sp_subcommand
-                    ;;
-                "load"|"unload")
-                    _sp_modtype=tcl
-                    _sp_sh_cmd="module $_sp_subcommand"
-                    ;;
-            esac
-
             # Here the user has run use or unuse with a spec.  Find a matching
             # spec using 'spack module find', then use the appropriate module
             # tool's commands to add/remove the result from the environment.
             # If spack module command comes back with an error, do nothing.
-            if _sp_full_spec=$(command spack $_sp_flags module find $_sp_modtype $_sp_spec); then
-                $_sp_sh_cmd $_sp_module_args $_sp_full_spec
-            fi
-            return
+            case $_sp_subcommand in
+                "use")
+                    if _sp_full_spec=$(command spack $_sp_flags module find dotkit $_sp_spec); then
+                        use $_sp_module_args $_sp_full_spec
+                    fi ;;
+                "unuse")
+                    if _sp_full_spec=$(command spack $_sp_flags module find dotkit $_sp_spec); then
+                        unuse $_sp_module_args $_sp_full_spec
+                    fi ;;
+                "load")
+                    if _sp_full_spec=$(command spack $_sp_flags module find dotkit $_sp_spec); then
+                        module load $_sp_module_args $_sp_full_spec
+                    fi ;;
+                "unload")
+                    if _sp_full_spec=$(command spack $_sp_flags module find dotkit $_sp_spec); then
+                        module unload $_sp_module_args $_sp_full_spec
+                    fi ;;
+            esac
             ;;
         *)
             command spack $_sp_flags $_sp_subcommand $_sp_spec
-- 
GitLab