# Buggy code
def __init__(
    self, url_or_fetch_strategy, name=None, mirror_path=None, keep=False, path=None
):
    """Create a stage object.
    Parameters:
      url_or_fetch_strategy
          URL of the archive to be downloaded into this stage, OR
          a valid FetchStrategy.

      name
          If a name is provided, then this stage is a named stage
          and will persist between runs (or if you construct another
          stage object later).  If name is not provided, then this
          stage will be given a unique name automatically.

      mirror_path
          If provided, Stage will search Spack's mirrors for
          this archive at the mirror_path, before using the
          default fetch strategy.

      keep
          By default, when used as a context manager, the Stage
          is deleted on exit when no exceptions are raised.
          Pass True to keep the stage intact even if no
          exceptions are raised.
    """
    # TODO: fetch/stage coupling needs to be reworked -- the logic
    # TODO: here is convoluted and not modular enough.
    if isinstance(url_or_fetch_strategy, basestring):
        self.fetcher = fs.from_url(url_or_fetch_strategy)
    elif isinstance(url_or_fetch_strategy, fs.FetchStrategy):
        self.fetcher = url_or_fetch_strategy
    else:
        raise ValueError("Can't construct Stage without url or fetch strategy")
    self.fetcher.set_stage(self)
    # self.fetcher can change with mirrors.
    self.default_fetcher = self.fetcher
    # used for mirrored archives of repositories.
    self.skip_checksum_for_mirror = True

    # TODO : this uses a protected member of tempfile, but seemed the only
    # TODO : way to get a temporary name besides, the temporary link name
    # TODO : won't be the same as the temporary stage area in tmp_root
    self.name = name
    if name is None:
        self.name = STAGE_PREFIX + next(tempfile._get_candidate_names())
    self.mirror_path = mirror_path
    self.tmp_root = find_tmp_root()

    # Try to construct here a temporary name for the stage directory
    # If this is a named stage, then construct a named path.
    if path is not None:
        self.path = path
    else:
        self.path = join_path(spack.stage_path, self.name)

    # Flag to decide whether to delete the stage folder on exit or not
    self.keep = keep


# Traceback (KeyError)
==> Installing cactusext
==> openmpi is already installed in /home/ux452368/comet/src/spack/opt/spack/linux-centos6-x86_64/gcc-6.1.0-spack/openmpi-2.0.0-5l3pomaqonrx3phvbm4kblzsms56ljtj
==> hwloc is already installed in /home/ux452368/comet/src/spack/opt/spack/linux-centos6-x86_64/gcc-6.1.0-spack/hwloc-1.11.3-xgdwohjb2o26uftzaqtcmwf2ejxlhidw
==> git is already installed in /home/ux452368/comet/src/spack/opt/spack/linux-centos6-x86_64/gcc-6.1.0-spack/git-2.8.1-jh72meyfuritl2wexqmckrshmufe7az4
==> papi is already installed in /home/ux452368/comet/src/spack/opt/spack/linux-centos6-x86_64/gcc-6.1.0-spack/papi-5.4.3-tnovpyennp4cerqns4xnfscfvl6d3zlz
==> hdf5-blosc is already installed in /home/ux452368/comet/src/spack/opt/spack/linux-centos6-x86_64/gcc-6.1.0-spack/hdf5-blosc-master-6m7xtyqubxsb2merc36i2oyhtdvno3zp
==> lmod is already installed in /home/ux452368/comet/src/spack/opt/spack/linux-centos6-x86_64/gcc-6.1.0-spack/lmod-6.4.1-i2xu3l63aipam7bdlu3ig4eriqtjnbbd
==> hdf5 is already installed in /home/ux452368/comet/src/spack/opt/spack/linux-centos6-x86_64/gcc-6.1.0-spack/hdf5-1.10.0-patch1-jfa4zar7ysebuadia7zlhoebrlaeo5lv
==> python is already installed in /home/ux452368/comet/src/spack/opt/spack/linux-centos6-x86_64/gcc-6.1.0-spack/python-2.7.11-y6bfneoxof25qt6wgdsbfsra62grfdnz
==> lua is already installed in /home/ux452368/comet/src/spack/opt/spack/linux-centos6-x86_64/gcc-6.1.0-spack/lua-5.3.2-xe6niqb4cwgkdpq33qjoi3rby3vcdnew
==> fftw is already installed in /home/ux452368/comet/src/spack/opt/spack/linux-centos6-x86_64/gcc-6.1.0-spack/fftw-3.3.4-ojokm7aahpro46e4tvoqgsc57lzbfdum
==> openssl is already installed in /home/ux452368/comet/src/spack/opt/spack/linux-centos6-x86_64/gcc-6.1.0-spack/openssl-1.0.2h-ookfh4fsbv5ijqjsewerc5vvf4hzfme6
==> julia is already installed in /home/ux452368/comet/src/spack/opt/spack/linux-centos6-x86_64/gcc-6.1.0-spack/julia-master-bzqdzwekzl3geaeyuexssnelbnwfiwsf
Traceback (most recent call last):
File "/home/ux452368/comet/src/spack/bin/spack", line 179, in <module>
main()
File "/home/ux452368/comet/src/spack/bin/spack", line 157, in main
return_val = command(parser, args)
File "/home/ux452368/comet/src/spack/lib/spack/spack/cmd/install.py", line 91, in install
explicit=True)
File "/home/ux452368/comet/src/spack/lib/spack/spack/package.py", line 943, in do_install
run_tests=run_tests)
File "/home/ux452368/comet/src/spack/lib/spack/spack/package.py", line 1096, in do_install_dependencies
dep.package.do_install(**kwargs)
File "/home/ux452368/comet/src/spack/lib/spack/spack/package.py", line 925, in do_install
rec = spack.installed_db.get_record(self.spec)
File "/home/ux452368/comet/src/spack/lib/spack/spack/database.py", line 76, in converter
return function(self, spec_like, *args, **kwargs)
File "/home/ux452368/comet/src/spack/lib/spack/spack/database.py", line 461, in get_record
key = self._get_matching_spec_key(spec, **kwargs)
File "/home/ux452368/comet/src/spack/lib/spack/spack/database.py", line 456, in _get_matching_spec_key
raise KeyError("No such spec in database! %s" % spec)
KeyError: 'No such spec in database! julia@master%gcc@6.1.0-spack+binutils~cxx+hdf5+mpi~plot~python arch=linux-centos6-x86_64^binutils@2.25%gcc@6.1.0-spack+gold~krellpatch~libiberty arch=linux-centos6-x86_64^bzip2@1.0.6%gcc@6.1.0-spack arch=linux-centos6-x86_64^cmake@3.5.2%gcc@6.1.0-spack~doc+ncurses+openssl~qt arch=linux-centos6-x86_64^curl@7.49.1%gcc@6.1.0-spack arch=linux-centos6-x86_64^expat@2.1.0%gcc@6.1.0-spack arch=linux-centos6-x86_64^git@2.8.1%gcc@6.1.0-spack arch=linux-centos6-x86_64^hdf5@1.10.0-patch1%gcc@6.1.0-spack+cxx~debug+fortran+mpi+shared~szip~threadsafe arch=linux-centos6-x86_64^hwloc@1.11.3%gcc@6.1.0-spack arch=linux-centos6-x86_64^libpciaccess@0.13.4%gcc@6.1.0-spack arch=linux-centos6-x86_64^libsigsegv@2.10%gcc@6.1.0-spack arch=linux-centos6-x86_64^m4@1.4.17%gcc@6.1.0-spack+sigsegv arch=linux-centos6-x86_64^ncurses@6.0%gcc@6.1.0-spack arch=linux-centos6-x86_64^openmpi@2.0.0%gcc@6.1.0-spack~mxm~pmi~psm~psm2~slurm~sqlite3~thread_multiple~tm+verbs+vt arch=linux-centos6-x86_64^openssl@1.0.2h%gcc@6.1.0-spack arch=linux-centos6-x86_64^python@2.7.11%gcc@6.1.0-spack~ucs4 arch=linux-centos6-x86_64^readline@6.3%gcc@6.1.0-spack arch=linux-centos6-x86_64^sqlite@3.8.5%gcc@6.1.0-spack arch=linux-centos6-x86_64^zlib@1.2.8%gcc@6.1.0-spack arch=linux-centos6-x86_64'

# Fixed code
def __init__(
    self,
    url_or_fetch_strategy,
    name=None,
    mirror_path=None,
    keep=False,
    path=None,
    lock=True,
):
    """Create a stage object.
    Parameters:
      url_or_fetch_strategy
          URL of the archive to be downloaded into this stage, OR
          a valid FetchStrategy.

      name
          If a name is provided, then this stage is a named stage
          and will persist between runs (or if you construct another
          stage object later).  If name is not provided, then this
          stage will be given a unique name automatically.

      mirror_path
          If provided, Stage will search Spack's mirrors for
          this archive at the mirror_path, before using the
          default fetch strategy.

      keep
          By default, when used as a context manager, the Stage
          is deleted on exit when no exceptions are raised.
          Pass True to keep the stage intact even if no
          exceptions are raised.
    """
    # TODO: fetch/stage coupling needs to be reworked -- the logic
    # TODO: here is convoluted and not modular enough.
    if isinstance(url_or_fetch_strategy, basestring):
        self.fetcher = fs.from_url(url_or_fetch_strategy)
    elif isinstance(url_or_fetch_strategy, fs.FetchStrategy):
        self.fetcher = url_or_fetch_strategy
    else:
        raise ValueError("Can't construct Stage without url or fetch strategy")
    self.fetcher.set_stage(self)
    # self.fetcher can change with mirrors.
    self.default_fetcher = self.fetcher
    # used for mirrored archives of repositories.
    self.skip_checksum_for_mirror = True

    # TODO : this uses a protected member of tempfile, but seemed the only
    # TODO : way to get a temporary name besides, the temporary link name
    # TODO : won't be the same as the temporary stage area in tmp_root
    self.name = name
    if name is None:
        self.name = STAGE_PREFIX + next(tempfile._get_candidate_names())
    self.mirror_path = mirror_path
    self.tmp_root = find_tmp_root()

    # Try to construct here a temporary name for the stage directory
    # If this is a named stage, then construct a named path.
    if path is not None:
        self.path = path
    else:
        self.path = join_path(spack.stage_path, self.name)

    # Flag to decide whether to delete the stage folder on exit or not
    self.keep = keep

    # File lock for the stage directory.  We use one file for all
    # stage locks. See Spec.prefix_lock for details on this approach.
    self._lock = None
    if lock:
        if self.name not in Stage.stage_locks:
            sha1 = hashlib.sha1(self.name).digest()
            lock_id = prefix_bits(sha1, bit_length(sys.maxsize))
            stage_lock_path = join_path(spack.stage_path, ".lock")

            Stage.stage_locks[self.name] = llnl.util.lock.Lock(
                stage_lock_path, lock_id, 1
            )

        self._lock = Stage.stage_locks[self.name]