# Buggy code
def install(parser, args, **kwargs):
    if not args.package:
        tty.die("install requires at least one package argument")

    if args.jobs is not None:
        if args.jobs <= 0:
            tty.die("The -j option must be a positive integer!")

    if args.no_checksum:
        spack.do_checksum = False  # TODO: remove this global.

    # Parse cli arguments and construct a dictionary
    # that will be passed to Package.do_install API
    kwargs.update(
        {
            "keep_prefix": args.keep_prefix,
            "keep_stage": args.keep_stage,
            "install_deps": "dependencies" in args.things_to_install,
            "make_jobs": args.jobs,
            "run_tests": args.run_tests,
            "verbose": args.verbose,
            "fake": args.fake,
            "dirty": args.dirty,
        }
    )

    # Spec from cli
    specs = spack.cmd.parse_specs(args.package, concretize=True)
    if len(specs) == 0:
        tty.error("The `spack install` command requires a spec to install.")

    for spec in specs:
        # Check if we were asked to produce some log for dashboards
        if args.log_format is not None:
            # Compute the filename for logging
            log_filename = args.log_file
            if not log_filename:
                log_filename = default_log_file(spec)
            # Create the test suite in which to log results
            test_suite = TestSuite(spec)
            # Decorate PackageBase.do_install to get installation status
            PackageBase.do_install = junit_output(spec, test_suite)(
                PackageBase.do_install
            )

        # Do the actual installation
        if args.things_to_install == "dependencies":
            # Install dependencies as-if they were installed
            # for root (explicit=False in the DB)
            kwargs["explicit"] = False
            for s in spec.dependencies():
                p = spack.repo.get(s)
                p.do_install(**kwargs)
        else:
            package = spack.repo.get(spec)
            kwargs["explicit"] = True
            package.do_install(**kwargs)

        # Dump log file if asked to
        if args.log_format is not None:
            test_suite.dump(log_filename)


# Traceback (KeyError)
amklinv@edison10:~/edison/spack/bin> ./spack -vd install xsdk arch=cray-CNL-ivybridge
==> Reading config file /global/u1/a/amklinv/edison/spack/etc/spack/defaults/packages.yaml
==> Reading config file /global/homes/a/amklinv/.spack/cray/packages.yaml
==> READ LOCK: /global/homes/a/amklinv/.spack/cache/providers/.builtin-index.yaml.lock[0:0] [Acquiring]
==> READ LOCK: /global/homes/a/amklinv/.spack/cache/providers/.builtin-index.yaml.lock[0:0] [Released]
==> '/usr/bin/ibv_devices'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-mpich/7.4.1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-mpich/7.4.1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-mpich/7.4.1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-hdf5-parallel/1.8.12'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-mpich/7.4.1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-hdf5-parallel/1.8.12'
==> Reading config file /global/homes/a/amklinv/.spack/cray/compilers.yaml
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-mpich/7.4.1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-hdf5-parallel/1.8.12'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-mpich/7.4.1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-hdf5-parallel/1.8.12'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'PrgEnv-gnu'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-x1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-x2'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-cray'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-intel'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-pgi'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-pathscale'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'load' 'PrgEnv-gnu'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'gcc/6.1.0'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'gcc'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'gcc-cross-aarch64'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'load' 'gcc/6.1.0'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-mpich/7.4.1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'PrgEnv-gnu'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-x1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-x2'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-cray'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-intel'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-pgi'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-pathscale'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'load' 'PrgEnv-gnu'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'gcc/6.1.0'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'gcc'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'gcc-cross-aarch64'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'load' 'gcc/6.1.0'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-hdf5-parallel/1.8.12'
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[6653323931864697468:1] [Acquiring]
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[6653323931864697468:1] [Released]
==> Installing xsdk
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[2794471412757580533:1] [Acquiring]
==> hypre is already installed in /global/u1/a/amklinv/edison/spack/opt/spack/cray-CNL-ivybridge/gcc-6.1.0/hypre-develop-jwh6ty3izsk6ukytgs2i23qxgo3i4lrq
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/lock[0:0] [Acquiring]
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/lock[0:0] [Released]
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[2794471412757580533:1] [Released]
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[840206737987988608:1] [Acquiring]
==> superlu-dist is already installed in /global/u1/a/amklinv/edison/spack/opt/spack/cray-CNL-ivybridge/gcc-6.1.0/superlu-dist-develop-c5jaojzxkpmqdyxz7xts4crqshkhaxd4
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[840206737987988608:1] [Released]
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[4617400482960019937:1] [Acquiring]
==> trilinos is already installed in /global/u1/a/amklinv/edison/spack/opt/spack/cray-CNL-ivybridge/gcc-6.1.0/trilinos-develop-qaujvdhjder4h77x3td7z7jkfi27rcop
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/lock[0:0] [Acquiring]
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/lock[0:0] [Released]
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[4617400482960019937:1] [Released]
Traceback (most recent call last):
File "./spack", line 212, in <module>
main(sys.argv)
File "./spack", line 208, in main
_main(args, unknown)
File "./spack", line 174, in _main
return_val = command(parser, args)
File "/global/u1/a/amklinv/edison/spack/lib/spack/spack/cmd/install.py", line 346, in install
package.do_install(**kwargs)
File "/global/u1/a/amklinv/edison/spack/lib/spack/spack/package.py", line 1185, in do_install
**kwargs
File "/global/u1/a/amklinv/edison/spack/lib/spack/spack/package.py", line 1157, in do_install
rec = spack.store.db.get_record(self.spec)
File "/global/u1/a/amklinv/edison/spack/lib/spack/spack/database.py", line 81, in converter
return function(self, spec_like, *args, **kwargs)
File "/global/u1/a/amklinv/edison/spack/lib/spack/spack/database.py", line 554, in get_record
key = self._get_matching_spec_key(spec, **kwargs)
File "/global/u1/a/amklinv/edison/spack/lib/spack/spack/database.py", line 549, in _get_matching_spec_key
raise KeyError("No such spec in database! %s" % spec)
KeyError: 'No such spec in database! trilinos@develop%gcc@6.1.0+boost~debug+hdf5+hypre+metis~mumps~python+shared~suite-sparse~superlu+superlu-dist+xsdkflags arch=cray-CNL-ivybridge^boost@1.63.0%gcc@6.1.0+atomic+chrono+date_time~debug+filesystem~graph~icu+iostreams+locale+log+math~mpi+multithreaded+program_options~python+random+regex+serialization+shared+signals~singlethreaded+system~taggedlayout+test+thread+timer+wave arch=cray-CNL-ivybridge^bzip2@1.0.6%gcc@6.1.0 arch=cray-CNL-ivybridge^cmake@3.7.1%gcc@6.1.0~doc+ncurses+openssl+ownlibs~qt arch=cray-CNL-ivybridge^glm@0.9.7.1%gcc@6.1.0 arch=cray-CNL-ivybridge^hdf5@1.8.12%gcc@6.1.0+cxx~debug+fortran+mpi+pic+shared~szip~threadsafe arch=cray-CNL-ivybridge^hypre@develop%gcc@6.1.0~internal-superlu+shared arch=cray-CNL-ivybridge^libsigsegv@2.10%gcc@6.1.0 arch=cray-CNL-ivybridge^m4@1.4.17%gcc@6.1.0+sigsegv arch=cray-CNL-ivybridge^matio@1.5.9%gcc@6.1.0+hdf5+zlib arch=cray-CNL-ivybridge^metis@5.1.0%gcc@6.1.0~debug~gdb~idx64~real64+shared arch=cray-CNL-ivybridge^mpich@7.4.1%gcc@6.1.0+hydra+pmi+romio~verbs arch=cray-CNL-ivybridge^ncurses@6.0%gcc@6.1.0 arch=cray-CNL-ivybridge^netcdf@4.4.1%gcc@6.1.0~cdmremote~dap~hdf4 maxdims=1024 maxvars=8192+mpi~parallel-netcdf+shared arch=cray-CNL-ivybridge^openblas@0.2.19%gcc@6.1.0~openmp+pic+shared arch=cray-CNL-ivybridge^openssl@1.0.2j%gcc@6.1.0 arch=cray-CNL-ivybridge^parmetis@4.0.3%gcc@6.1.0~debug~gdb+shared arch=cray-CNL-ivybridge^superlu-dist@develop%gcc@6.1.0 arch=cray-CNL-ivybridge^zlib@1.2.10%gcc@6.1.0+pic arch=cray-CNL-ivybridge'

# Fixed code
def install(parser, args, **kwargs):
    if not args.package:
        tty.die("install requires at least one package argument")

    if args.jobs is not None:
        if args.jobs <= 0:
            tty.die("The -j option must be a positive integer!")

    if args.no_checksum:
        spack.do_checksum = False  # TODO: remove this global.

    # Parse cli arguments and construct a dictionary
    # that will be passed to Package.do_install API
    kwargs.update(
        {
            "keep_prefix": args.keep_prefix,
            "keep_stage": args.keep_stage,
            "restage": args.restage,
            "install_deps": "dependencies" in args.things_to_install,
            "make_jobs": args.jobs,
            "run_tests": args.run_tests,
            "verbose": args.verbose,
            "fake": args.fake,
            "dirty": args.dirty,
        }
    )

    # Spec from cli
    specs = spack.cmd.parse_specs(args.package, concretize=True)
    if len(specs) == 0:
        tty.error("The `spack install` command requires a spec to install.")

    for spec in specs:
        # Check if we were asked to produce some log for dashboards
        if args.log_format is not None:
            # Compute the filename for logging
            log_filename = args.log_file
            if not log_filename:
                log_filename = default_log_file(spec)
            # Create the test suite in which to log results
            test_suite = TestSuite(spec)
            # Decorate PackageBase.do_install to get installation status
            PackageBase.do_install = junit_output(spec, test_suite)(
                PackageBase.do_install
            )

        # Do the actual installation
        if args.things_to_install == "dependencies":
            # Install dependencies as-if they were installed
            # for root (explicit=False in the DB)
            kwargs["explicit"] = False
            for s in spec.dependencies():
                p = spack.repo.get(s)
                p.do_install(**kwargs)
        else:
            package = spack.repo.get(spec)
            kwargs["explicit"] = True
            package.do_install(**kwargs)

        # Dump log file if asked to
        if args.log_format is not None:
            test_suite.dump(log_filename)