# Buggy code
def copyfile(src, dest, symlink=True):
    if not os.path.exists(src):
        # Some bad symlink in the src
        logger.warn("Cannot find file %s (bad symlink)", src)
        return
    if os.path.exists(dest):
        logger.debug("File %s already exists", dest)
        return
    if not os.path.exists(os.path.dirname(dest)):
        logger.info("Creating parent directories for %s", os.path.dirname(dest))
        os.makedirs(os.path.dirname(dest))
    if symlink and hasattr(os, "symlink") and not IS_WIN:
        logger.info("Symlinking %s", dest)
        try:
            os.symlink(os.path.realpath(src), dest)
        except (OSError, NotImplementedError):
            logger.info("Symlinking failed, copying to %s", dest)
            copy_file_or_folder(src, dest, symlink)
    else:
        logger.info("Copying to %s", dest)
        copy_file_or_folder(src, dest, symlink)


# Traceback (IOError)
Symlinking Python bootstrap modules
Symlinking /lib/python2.7/config -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/config
Symlinking /lib/python2.7/lib-dynload -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/lib-dynload
Symlinking /lib/python2.7/os.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/os.py
Ignoring built-in bootstrap module: posix
Symlinking /lib/python2.7/posixpath.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/posixpath.py
Cannot import bootstrap module: nt
Symlinking /lib/python2.7/ntpath.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/ntpath.py
Symlinking /lib/python2.7/genericpath.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/genericpath.py
Symlinking /lib/python2.7/fnmatch.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/fnmatch.py
Symlinking /lib/python2.7/locale.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/locale.py
Symlinking /lib/python2.7/encodings -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/encodings
Symlinking /lib/python2.7/codecs.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/codecs.py
Symlinking /lib/python2.7/stat.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/stat.py
Symlinking /lib/python2.7/UserDict.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/UserDict.py
Symlinking /lib/python2.7/copy_reg.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/copy_reg.py
Symlinking /lib/python2.7/types.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/types.py
Symlinking /lib/python2.7/re.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/re.py
Symlinking /lib/python2.7/sre.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/sre.py
Symlinking /lib/python2.7/sre_parse.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/sre_parse.py
Symlinking /lib/python2.7/sre_constants.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/sre_constants.py
Symlinking /lib/python2.7/sre_compile.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/sre_compile.py
Symlinking /lib/python2.7/warnings.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/warnings.py
Symlinking /lib/python2.7/linecache.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/linecache.py
Symlinking /lib/python2.7/_abcoll.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/_abcoll.py
Symlinking /lib/python2.7/abc.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/abc.py
Symlinking /lib/python2.7/_weakrefset.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/_weakrefset.py
Creating /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/site-packages
Writing /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/site.py
Writing /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/orig-prefix.txt
Creating parent directories for /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/include
Symlinking /include/python2.7 -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/include/python2.7
Symlinking /bin/..//opt/python/lib/python2.7/mailbox.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/mailbox.py
Symlinking /bin/..//opt/python/lib/python2.7/repr.pyo -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/repr.pyo
Symlinking /bin/..//opt/python/lib/python2.7/symbol.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/symbol.py
Symlinking /bin/..//opt/python/lib/python2.7/mimify.pyo -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/mimify.pyo
Symlinking /bin/..//opt/python/lib/python2.7/ihooks.pyo -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/ihooks.pyo
Symlinking /bin/..//opt/python/lib/python2.7/macpath.pyo -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/macpath.pyo
Symlinking /bin/..//opt/python/lib/python2.7/mimetools.pyo -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/mimetools.pyo
Symlinking /bin/..//opt/python/lib/python2.7/smtplib.pyo -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/smtplib.pyo
Symlinking /bin/..//opt/python/lib/python2.7/sched.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/sched.py
Symlinking /bin/..//opt/python/lib/python2.7/zipfile.pyo -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/zipfile.pyo
Symlinking /bin/..//opt/python/lib/python2.7/user.pyo -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/user.pyo
Symlinking /bin/..//opt/python/lib/python2.7/_threading_local.pyo -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/_threading_local.pyo
Symlinking /bin/..//opt/python/lib/python2.7/xml -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/xml
Symlinking /bin/..//opt/python/lib/python2.7/SocketServer.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/SocketServer.py
Symlinking /bin/..//opt/python/lib/python2.7/robotparser.pyo -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/robotparser.pyo
Symlinking /bin/..//opt/python/lib/python2.7/re.py -> /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/re.py
Symlinking failed, copying to /src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/re.py
copy: src:/bin/..//opt/python/lib/python2.7/re.py dest:/src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/re.py

Traceback (most recent call last):
File "/src/build/firefox/mozilla-release/python/virtualenv/virtualenv.py", line 2573, in <module>
main()
File "/src/build/firefox/mozilla-release/python/virtualenv/virtualenv.py", line 974, in main
never_download=options.never_download)
File "/src/build/firefox/mozilla-release/python/virtualenv/virtualenv.py", line 1075, in create_environment
site_packages=site_packages, clear=clear))
File "/src/build/firefox/mozilla-release/python/virtualenv/virtualenv.py", line 1319, in install_python
copyfile(join(exec_dir, fn), join(lib_dir, fn))
File "/src/build/firefox/mozilla-release/python/virtualenv/virtualenv.py", line 447, in copyfile
copyfileordir(src, dest)
File "/src/build/firefox/mozilla-release/python/virtualenv/virtualenv.py", line 424, in copyfileordir
shutil.copy2(src, dest)
File "/lib/python2.7/shutil.py", line 128, in copy2
copyfile(src, dst)
File "/lib/python2.7/shutil.py", line 83, in copyfile
with open(dst, 'wb') as fdst:
IOError: [Errno 2] No such file or directory: '/src/build/firefox/mozilla-release/firefox-build-dir/_virtualenv/lib/python2.7/re.py'
Traceback (most recent call last):
File "/src/build/firefox/mozilla-release/build/virtualenv/populate_virtualenv.py", line 384, in <module>
manager.ensure()
File "/src/build/firefox/mozilla-release/build/virtualenv/populate_virtualenv.py", line 103, in ensure
return self.build()
File "/src/build/firefox/mozilla-release/build/virtualenv/populate_virtualenv.py", line 315, in build
self.create()
File "/src/build/firefox/mozilla-release/build/virtualenv/populate_virtualenv.py", line 122, in create
raise Exception('Error creating virtualenv.')
Exception: Error creating virtualenv.

# Fixed code
def copyfile(src, dest, symlink=True):
    if symlink and hasattr(os, "symlink") and not IS_WIN:
        return symlink_file_or_folder(src, dest)
    if not os.path.exists(src):
        # Some bad symlink in the src
        logger.warn("Cannot find file %s (bad symlink)", src)
        return
    if os.path.exists(dest):
        logger.debug("File %s already exists", dest)
        return
    if not os.path.exists(os.path.dirname(dest)):
        logger.info("Creating parent directories for %s", os.path.dirname(dest))
        os.makedirs(os.path.dirname(dest))
    logger.info("Copying to %s", dest)
    copy_file_or_folder(src, dest, symlink)