# Buggy code
def _write(self):
    """Write the in-memory database index to its file path.

    Does no locking.

    """
    temp_name = "%s.%s.temp" % (socket.getfqdn(), os.getpid())
    temp_file = join_path(self._db_dir, temp_name)

    # Write a temporary database file them move it into place
    try:
        with open(temp_file, "w") as f:
            self._write_to_yaml(f)
        os.rename(temp_file, self._index_path)

    except:
        # Clean up temp file if something goes wrong.
        if os.path.exists(temp_file):
            os.remove(temp_file)
        raise


# Traceback (KeyError)
paul ~/C/s/bin > ./spack install memaxes
==> Installing memaxes
==> cmake is already installed in /home/paul/Clinic/spack/opt/spack/x86_64/gcc-5.2.1/cmake-3.0.2-bv634arumx3m47mpynkw5ygmiqdlcykp.
==> Installing qt
==> glib is already installed in /home/paul/Clinic/spack/opt/spack/x86_64/gcc-5.2.1/glib-2.42.1-dwjk6vntaa4dextqivrup3odqfjfg6mh.
==> libtiff is already installed in /home/paul/Clinic/spack/opt/spack/x86_64/gcc-5.2.1/libtiff-4.0.3-n6k5fnztfshg3krijefks5qmnaptvigk.
==> libxml2 is already installed in /home/paul/Clinic/spack/opt/spack/x86_64/gcc-5.2.1/libxml2-2.9.2-lt2vnkj75wp2cvi2q75azleq3kjr2alr.
==> dbus is already installed in /home/paul/Clinic/spack/opt/spack/x86_64/gcc-5.2.1/dbus-1.9.0-vk3dr7ps5csbudicoqgpk3hvl27wlz4z.
==> zlib is already installed in /home/paul/Clinic/spack/opt/spack/x86_64/gcc-5.2.1/zlib-1.2.8-5cowb3si2gbcqchtdewwxwhmypem4hdu.
==> jpeg is already installed in /home/paul/Clinic/spack/opt/spack/x86_64/gcc-5.2.1/jpeg-9a-v6w3vn6ytc4owszkccwmolwhytnbvasw.
==> Installing gtkplus
==> gdk-pixbuf is already installed in /home/paul/Clinic/spack/opt/spack/x86_64/gcc-5.2.1/gdk-pixbuf-2.31.2-n5bcojzjasynb3pn55lbyw5jebko3ptu.
==> pango is already installed in /home/paul/Clinic/spack/opt/spack/x86_64/gcc-5.2.1/pango-1.36.8-boik7co2rayuuosdnv6qnzpkiklkl4ug.
==> atk is already installed in /home/paul/Clinic/spack/opt/spack/x86_64/gcc-5.2.1/atk-2.14.0-6xy47vuatz52e3luykhccrcpm6bkptpw.
==> cairo is already installed in /home/paul/Clinic/spack/opt/spack/x86_64/gcc-5.2.1/cairo-1.14.0-rqplcapyohdgxjolv26ktt7esmqptstj.
==> Trying to fetch from http://ftp.gnome.org/pub/gnome/sources/gtk+/2.24/gtk+-2.24.25.tar.xz
######################################################################## 100.0%
==> Staging archive: /home/paul/Clinic/spack/var/spack/stage/gtkplus-2.24.25-dqc66tudnnl3qte7r67vinptia3dnu7y/gtk+-2.24.25.tar.xz
==> Created stage in /home/paul/Clinic/spack/var/spack/stage/gtkplus-2.24.25-dqc66tudnnl3qte7r67vinptia3dnu7y.
==> Patched gtkplus
==> Building gtkplus.
Traceback (most recent call last):
File "/home/paul/Clinic/spack/lib/spack/spack/build_environment.py", line 278, in fork
function()
File "/home/paul/Clinic/spack/lib/spack/spack/package.py", line 839, in real_work
cleanup()
File "/home/paul/Clinic/spack/lib/spack/spack/package.py", line 786, in cleanup
self.remove_prefix()
File "/home/paul/Clinic/spack/lib/spack/spack/package.py", line 614, in remove_prefix
spack.installed_db.remove(self.spec)
File "/home/paul/Clinic/spack/lib/spack/spack/database.py", line 74, in converter
return function(self, spec_like, *args, **kwargs)
File "/home/paul/Clinic/spack/lib/spack/spack/database.py", line 502, in remove
return self._remove(spec)
File "/home/paul/Clinic/spack/lib/spack/spack/database.py", line 472, in _remove
key = self._get_matching_spec_key(spec)
File "/home/paul/Clinic/spack/lib/spack/spack/database.py", line 442, in _get_matching_spec_key
raise KeyError("No such spec in database! %s" % spec)
KeyError: 'No such spec in database! gtkplus@2.24.25%gcc@5.2.1=x86_64^atk@2.14.0%gcc@5.2.1=x86_64^cairo@1.14.0%gcc@5.2.1=x86_64^fontconfig@2.11.1%gcc@5.2.1=x86_64^freetype@2.5.3%gcc@5.2.1=x86_64^gdk-pixbuf@2.31.2%gcc@5.2.1=x86_64^glib@2.42.1%gcc@5.2.1=x86_64^harfbuzz@0.9.37%gcc@5.2.1=x86_64^icu@54.1%gcc@5.2.1=x86_64^jpeg@9a%gcc@5.2.1=x86_64^libffi@3.2.1%gcc@5.2.1=x86_64^libpng@1.6.16%gcc@5.2.1=x86_64^libtiff@4.0.3%gcc@5.2.1=x86_64^pango@1.36.8%gcc@5.2.1=x86_64^pixman@0.32.6%gcc@5.2.1=x86_64^xz@5.2.2%gcc@5.2.1=x86_64^zlib@1.2.8%gcc@5.2.1=x86_64'
==> Error: Installation process had nonzero exit code.

# Fixed code
def _write(self):
    """Write the in-memory database index to its file path.

    Does no locking.

    """
    temp_file = self._index_path + (".%s.%s.temp" % (socket.getfqdn(), os.getpid()))

    # Write a temporary database file them move it into place
    try:
        with open(temp_file, "w") as f:
            self._write_to_yaml(f)
        os.rename(temp_file, self._index_path)
    except:
        # Clean up temp file if something goes wrong.
        if os.path.exists(temp_file):
            os.remove(temp_file)
        raise