Repository: pypa/virtualenv

Issue:
regression in virtualenv 15.0.2: --clear no longer works on Windows
The tcl/tk "fix" (which there appears to be no way to disable?) has caused `--clear` to start raising an exception; sample traceback [here](https://buildbot.twistedmatrix.com/builders/windows7-64-py2.7/builds/117/steps/shell/logs/stdio)

```
Deleting tree C:\Users\BUILDS~1\BOT-GL~1\WINDOW~1.7-S\venv\Lib
Not deleting C:\Users\BUILDS~1\BOT-GL~1\WINDOW~1.7-S\venv\Scripts
Traceback (most recent call last):
  File "C:\Python27\lib\runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "C:\Python27\lib\runpy.py", line 72, in _run_code
    exec code in run_globals
  File "C:\Python27\lib\site-packages\virtualenv.py", line 2332, in <module>
    main()
  File "C:\Python27\lib\site-packages\virtualenv.py", line 711, in main
    symlink=options.symlink)
  File "C:\Python27\lib\site-packages\virtualenv.py", line 924, in create_environment
    site_packages=site_packages, clear=clear, symlink=symlink))
  File "C:\Python27\lib\site-packages\virtualenv.py", line 1136, in install_python
    copy_tcltk(prefix, home_dir, symlink)
  File "C:\Python27\lib\site-packages\virtualenv.py", line 1077, in copy_tcltk
    copyfileordir(srcdir, dstdir, symlink)
  File "C:\Python27\lib\site-packages\virtualenv.py", line 327, in copyfileordir
    shutil.copytree(src, dest, symlink)
  File "C:\Python27\lib\shutil.py", line 177, in copytree
    os.makedirs(dst)
  File "C:\Python27\lib\os.py", line 157, in makedirs
    mkdir(name, mode)
WindowsError: [Error 183] Cannot create a file when that file already exists: 'C:\\Users\\BUILDS~1\\BOT-GL~1\\WINDOW~1.7-S\\venv/tcl/tcl8.5'
```

This broke all of Twisted's buildbots when we upgraded.



Discussion:
A similar regression happens on creating a virtualenv. But I get something along the lines of `Directory does not exist '.../python\\tcl/tcl8.5'`, and indeed this directory doesn't even exist in my Windows python installation.

Similar regression when trying to reinstall a virtualenv in a folder already containing a virtualenv.

```
$ virtualenv --version
15.0.1

$ virtualenv venv/test
Overwriting C:\dev\git\venv\test\lib\orig-prefix.txt with new content
New python executable in C:\dev\git\venv\test\Scripts\python.exe
Installing setuptools, pip, wheel...done.

$ virtualenv venv/test
New python executable in C:\dev\git\venv\test\Scripts\python.exe
Installing setuptools, pip, wheel...done.

$ pip install -U virtualenv===15.0.2
Collecting virtualenv===15.0.2
  Downloading https://root/pypi/+f/832/cc0a303e61fa7/virtualenv-15.0.2-py2.py3-none-any.whl (1.8MB)
    100% |################################| 1.8MB 2.3MB/s
Installing collected packages: virtualenv
  Found existing installation: virtualenv 15.0.1
    Uninstalling virtualenv-15.0.1:
      Successfully uninstalled virtualenv-15.0.1
Successfully installed virtualenv-15.0.2

$ virtualenv venv/test
Traceback (most recent call last):
  File "f:\bin\python\278\lib\runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "f:\bin\python\278\lib\runpy.py", line 72, in _run_code
    exec code in run_globals
  File "F:\bin\Python\278\scripts\virtualenv.exe\__main__.py", line 9, in <module>
  File "f:\bin\python\278\lib\site-packages\virtualenv.py", line 711, in main
    symlink=options.symlink)
  File "f:\bin\python\278\lib\site-packages\virtualenv.py", line 924, in create_environment
    site_packages=site_packages, clear=clear, symlink=symlink))
  File "f:\bin\python\278\lib\site-packages\virtualenv.py", line 1136, in install_python
    copy_tcltk(prefix, home_dir, symlink)
  File "f:\bin\python\278\lib\site-packages\virtualenv.py", line 1077, in copy_tcltk
    copyfileordir(srcdir, dstdir, symlink)
  File "f:\bin\python\278\lib\site-packages\virtualenv.py", line 327, in copyfileordir
    shutil.copytree(src, dest, symlink)
  File "f:\bin\python\278\lib\shutil.py", line 177, in copytree
    os.makedirs(dst)
  File "f:\bin\python\278\lib\os.py", line 157, in makedirs
    mkdir(name, mode)
WindowsError: [Error 183] Cannot create a file when that file already exists: 'C:\\dev\\git\\venv\\test/tcl/tcl8.5'
```


Resolving patch:
diff --git a/virtualenv.py b/virtualenv.py
--- a/virtualenv.py
+++ b/virtualenv.py
@@ -1074,7 +1074,8 @@ def copy_tcltk(src, dest, symlink):
     for name in ['tcl', 'tk']:
         srcdir = src + '/tcl/' + name + libver
         dstdir = dest + '/tcl/' + name + libver
-        copyfileordir(srcdir, dstdir, symlink)
+        if os.path.exists(srcdir) and not os.path.exists(dstdir):
+            copyfileordir(srcdir, dstdir, symlink)
 
 def subst_path(prefix_path, prefix, home_dir):
     prefix_path = os.path.normpath(prefix_path)


Test patch: