Repository: spack/spack

Issue:
spack find : always prompt 0 installed packages
On a clean `develop` checkout : 

```
$ git clone https://github.com/LLNL/spack.git
Cloning into 'spack'...
remote: Counting objects: 25613, done.
remote: Compressing objects: 100% (42/42), done.
remote: Total 25613 (delta 12), reused 3 (delta 3), pack-reused 25557
Receiving objects: 100% (25613/25613), 6.65 MiB | 6.46 MiB/s, done.
Resolving deltas: 100% (13031/13031), done.
Checking connectivity... done.

$ cd spack
$ . share/spack/setup-env.sh 
$ spack compilers
==> Available compilers
-- gcc ----------------------------------------------------------
gcc@4.8

$ spack install zlib
==> Installing zlib
==> Trying to fetch from file:///home/mculpo/production/spack-mirror/zlib/zlib-1.2.8.tar.gz
######################################################################## 100,0%
==> Staging archive: /home/mculpo/tmp/spack/var/spack/stage/zlib-1.2.8-d6pdl6xvnvap6ihrqcqtgvweghbszmix/zlib-1.2.8.tar.gz
==> Created stage in /home/mculpo/tmp/spack/var/spack/stage/zlib-1.2.8-d6pdl6xvnvap6ihrqcqtgvweghbszmix
==> No patches needed for zlib
==> Building zlib
==> Successfully installed zlib
  Fetch: 0.01s.  Build: 3.69s.  Total: 3.70s.
[+] /home/mculpo/tmp/spack/opt/spack/linux-x86_64/gcc-4.8/zlib-1.2.8-d6pdl6xvnvap6ihrqcqtgvweghbszmix

$ spack find
==> 0 installed packages.

$ spack install szip
==> Installing szip
==> Trying to fetch from file:///home/mculpo/production/spack-mirror/szip/szip-2.1.tar.gz
######################################################################## 100,0%
==> Staging archive: /home/mculpo/tmp/spack/var/spack/stage/szip-2.1-esfmhl54wbdb7nnnip6y6jbxlbmxs2jq/szip-2.1.tar.gz
==> Created stage in /home/mculpo/tmp/spack/var/spack/stage/szip-2.1-esfmhl54wbdb7nnnip6y6jbxlbmxs2jq
==> No patches needed for szip
==> Building szip
==> Successfully installed szip
  Fetch: 0.01s.  Build: 8.09s.  Total: 8.10s.
[+] /home/mculpo/tmp/spack/opt/spack/linux-x86_64/gcc-4.8/szip-2.1-esfmhl54wbdb7nnnip6y6jbxlbmxs2jq

$ spack find 
==> 0 installed packages.
```

The db seems to be written correctly : 

```
database:
  installs:
    d6pdl6xvnvap6ihrqcqtgvweghbszmix:
      explicit: true
      installed: true
      path: /home/mculpo/tmp/spack/opt/spack/linux-x86_64/gcc-4.8/zlib-1.2.8-d6pdl6xvnvap6ihrqcqtgvweghbszmix
      ref_count: 0
      spec:
        zlib:
          arch: linux-x86_64
          compiler:
            name: gcc
            version: '4.8'
          dependencies: {}
          namespace: builtin
          parameters:
            cflags: []
            cppflags: []
            cxxflags: []
            fflags: []
            ldflags: []
            ldlibs: []
          version: 1.2.8
    esfmhl54wbdb7nnnip6y6jbxlbmxs2jq:
      explicit: true
      installed: true
      path: /home/mculpo/tmp/spack/opt/spack/linux-x86_64/gcc-4.8/szip-2.1-esfmhl54wbdb7nnnip6y6jbxlbmxs2jq
      ref_count: 0
      spec:
        szip:
          arch: linux-x86_64
          compiler:
            name: gcc
            version: '4.8'
          dependencies: {}
          namespace: builtin
          parameters:
            cflags: []
            cppflags: []
            cxxflags: []
            fflags: []
            ldflags: []
            ldlibs: []
          version: '2.1'
  version: 0.9.1
```



Discussion:

Resolving patch:
diff --git a/lib/spack/spack/cmd/find.py b/lib/spack/spack/cmd/find.py
--- a/lib/spack/spack/cmd/find.py
+++ b/lib/spack/spack/cmd/find.py
@@ -22,66 +22,92 @@
 # 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 sys
-import collections
-import itertools
 import argparse
-from StringIO import StringIO
+import sys
 
 import llnl.util.tty as tty
+import spack
+import spack.spec
+from llnl.util.lang import *
 from llnl.util.tty.colify import *
 from llnl.util.tty.color import *
-from llnl.util.lang import *
 
-import spack
-import spack.spec
+description = "Find installed spack packages"
 
-description ="Find installed spack packages"
 
 def setup_parser(subparser):
     format_group = subparser.add_mutually_exclusive_group()
+    format_group.add_argument('-s',
+                              '--short',
+                              action='store_const',
+                              dest='mode',
+                              const='short',
+                              help='Show only specs (default)')
+    format_group.add_argument('-p',
+                              '--paths',
+                              action='store_const',
+                              dest='mode',
+                              const='paths',
+                              help='Show paths to package install directories')
     format_group.add_argument(
-        '-s', '--short', action='store_const', dest='mode', const='short',
-        help='Show only specs (default)')
-    format_group.add_argument(
-        '-p', '--paths', action='store_const', dest='mode', const='paths',
-        help='Show paths to package install directories')
-    format_group.add_argument(
-        '-d', '--deps', action='store_const', dest='mode', const='deps',
+        '-d',
+        '--deps',
+        action='store_const',
+        dest='mode',
+        const='deps',
         help='Show full dependency DAG of installed packages')
 
-    subparser.add_argument(
-        '-l', '--long', action='store_true', dest='long',
-        help='Show dependency hashes as well as versions.')
-    subparser.add_argument(
-        '-L', '--very-long', action='store_true', dest='very_long',
-        help='Show dependency hashes as well as versions.')
-    subparser.add_argument(
-        '-f', '--show-flags', action='store_true', dest='show_flags',
-        help='Show spec compiler flags.')
+    subparser.add_argument('-l',
+                           '--long',
+                           action='store_true',
+                           dest='long',
+                           help='Show dependency hashes as well as versions.')
+    subparser.add_argument('-L',
+                           '--very-long',
+                           action='store_true',
+                           dest='very_long',
+                           help='Show dependency hashes as well as versions.')
+    subparser.add_argument('-f',
+                           '--show-flags',
+                           action='store_true',
+                           dest='show_flags',
+                           help='Show spec compiler flags.')
 
     subparser.add_argument(
-        '-e', '--explicit', action='store_true',
+        '-e',
+        '--explicit',
+        action='store_true',
         help='Show only specs that were installed explicitly')
     subparser.add_argument(
-        '-E', '--implicit', action='store_true',
+        '-E',
+        '--implicit',
+        action='store_true',
         help='Show only specs that were installed as dependencies')
     subparser.add_argument(
-        '-u', '--unknown', action='store_true', dest='unknown',
+        '-u',
+        '--unknown',
+        action='store_true',
+        dest='unknown',
         help='Show only specs Spack does not have a package for.')
     subparser.add_argument(
-        '-m', '--missing', action='store_true', dest='missing',
+        '-m',
+        '--missing',
+        action='store_true',
+        dest='missing',
         help='Show missing dependencies as well as installed specs.')
-    subparser.add_argument(
-        '-M', '--only-missing', action='store_true', dest='only_missing',
-        help='Show only missing dependencies.')
-    subparser.add_argument(
-        '-N', '--namespace', action='store_true',
-        help='Show fully qualified package names.')
+    subparser.add_argument('-M',
+                           '--only-missing',
+                           action='store_true',
+                           dest='only_missing',
+                           help='Show only missing dependencies.')
+    subparser.add_argument('-N',
+                           '--namespace',
+                           action='store_true',
+                           help='Show fully qualified package names.')
 
-    subparser.add_argument(
-        'query_specs', nargs=argparse.REMAINDER,
-        help='optional specs to filter results')
+    subparser.add_argument('query_specs',
+                           nargs=argparse.REMAINDER,
+                           help='optional specs to filter results')
 
 
 def gray_hash(spec, length):
@@ -109,14 +135,15 @@ def display_specs(specs, **kwargs):
 
     # Traverse the index and print out each package
     for i, (architecture, compiler) in enumerate(sorted(index)):
-        if i > 0: print
+        if i > 0:
+            print
 
-        header = "%s{%s} / %s{%s}" % (
-            spack.spec.architecture_color, architecture,
-            spack.spec.compiler_color, compiler)
+        header = "%s{%s} / %s{%s}" % (spack.spec.architecture_color,
+                                      architecture, spack.spec.compiler_color,
+                                      compiler)
         tty.hline(colorize(header), char='-')
 
-        specs = index[(architecture,compiler)]
+        specs = index[(architecture, compiler)]
         specs.sort()
 
         abbreviated = [s.format(format_string, color=True) for s in specs]
@@ -128,20 +155,21 @@ def display_specs(specs, **kwargs):
 
             for abbrv, spec in zip(abbreviated, specs):
                 if hashes:
-                    print gray_hash(spec, hlen),
-                print format % (abbrv, spec.prefix)
+                    print(gray_hash(spec, hlen), )
+                print(format % (abbrv, spec.prefix))
 
         elif mode == 'deps':
             for spec in specs:
-                print spec.tree(
+                print(spec.tree(
                     format=format_string,
                     color=True,
                     indent=4,
-                    prefix=(lambda s: gray_hash(s, hlen)) if hashes else None)
+                    prefix=(lambda s: gray_hash(s, hlen)) if hashes else None))
 
         elif mode == 'short':
             # Print columns of output if not printing flags
             if not flags:
+
                 def fmt(s):
                     string = ""
                     if hashes:
@@ -149,18 +177,17 @@ def fmt(s):
                     string += s.format('$-%s$@$+' % nfmt, color=True)
 
                     return string
+
                 colify(fmt(s) for s in specs)
             # Print one entry per line if including flags
             else:
                 for spec in specs:
                     # Print the hash if necessary
                     hsh = gray_hash(spec, hlen) + ' ' if hashes else ''
-                    print hsh + spec.format(format_string, color=True) + '\n'
+                    print(hsh + spec.format(format_string, color=True) + '\n')
 
         else:
-            raise ValueError(
-                "Invalid mode for display_specs: %s. Must be one of (paths, deps, short)." % mode)
-
+            raise ValueError("Invalid mode for display_specs: %s. Must be one of (paths, deps, short)." % mode)  # NOQA: ignore=E501
 
 
 def find(parser, args):
@@ -186,19 +213,20 @@ def find(parser, args):
     if args.unknown:
         known = False
 
-    explicit = None
+    explicit = any
     if args.explicit:
         explicit = False
     if args.implicit:
         explicit = True
 
-    q_args = { 'installed' : installed, 'known' : known, "explicit" : explicit }
+    q_args = {'installed': installed, 'known': known, "explicit": explicit}
 
     # Get all the specs the user asked for
     if not query_specs:
         specs = set(spack.installed_db.query(**q_args))
     else:
-        results = [set(spack.installed_db.query(qs, **q_args)) for qs in query_specs]
+        results = [set(spack.installed_db.query(qs, **q_args))
+                   for qs in query_specs]
         specs = set.union(*results)
 
     if not args.mode:
@@ -206,7 +234,8 @@ def find(parser, args):
 
     if sys.stdout.isatty():
         tty.msg("%d installed packages." % len(specs))
-    display_specs(specs, mode=args.mode,
+    display_specs(specs,
+                  mode=args.mode,
                   long=args.long,
                   very_long=args.very_long,
                   show_flags=args.show_flags)


Test patch: