Repository: conda/conda-build

Issue:
conda-build does not handle absence of MSVC on windows well
I was trying to build the **math3d** package inside conda, and I am failing and getting the following error

C:>conda skeleton pypi --recursive math3d --version 3.1.2 --output-dir C:\Anaconda2\skeleton
Using url https://files.pythonhosted.org/packages/1e/93/d0a462fc3acb5d9a71912129f9c72d448cd995ed98c632c729c6aa63d564/math3d-3.1.2.tar.gz (24 KB) for math3d.
Downloading math3d
Using cached download
Unpacking math3d...
done
working in c:\users\debang~1\appdata\local\temp\tmpsbez9sconda_skeleton_math3d-3.1.2.tar.gz
Fetching package metadata .........
Solving package specifications: ..........

The following NEW packages will be INSTALLED:

```
mkl:    11.3.3-1
numpy:  1.11.0-py27_1
pyyaml: 3.11-py27_4
yaml:   0.1.6-0
```

Linking packages ...
[      COMPLETE      ]|##################################################| 100%
Applying patch: 'c:\users\debang~1\appdata\local\temp\tmpsbez9sconda_skeleton_math3d-3.1.2.tar.gz\pypi-distutils.patch'
patching file core.py
Hunk #1 succeeded at 169 with fuzz 2 (offset 2 lines).
Writing recipe for math3d
Done

C:>conda build C:\Anaconda2\skeleton math3d
Removing old build environment
BUILD START: math3d-3.1.2-py27_0
Fetching package metadata .........
Solving package specifications: ..........

The following NEW packages will be INSTALLED:

```
pip:            8.1.2-py27_0
python:         2.7.11-4
setuptools:     23.0.0-py27_0
vs2008_runtime: 9.00.30729.1-1
wheel:          0.29.0-py27_0
```

Linking packages ...
[      COMPLETE      ]|##################################################| 100%
Source cache directory is: C:\Anaconda2\conda-bld\src_cache
Found source in cache: math3d-3.1.2.tar.gz
Extracting download
Package: math3d-3.1.2-py27_0
source tree in: C:\Anaconda2\conda-bld\work\math3d-3.1.2
An unexpected error has occurred, please consider sending the
following traceback to the conda GitHub issue tracker at:

```
https://github.com/conda/conda-build/issues
```

Include the output of the command 'conda info' in your report.

Traceback (most recent call last):
  File "C:\Anaconda2\Scripts\conda-build-script.py", line 4, in <module>
    sys.exit(main())
  File "C:\Anaconda2\lib\site-packages\conda_build\main_build.py", line 144, in main
    args_func(args, p)
  File "C:\Anaconda2\lib\site-packages\conda_build\main_build.py", line 389, in args_func
    args.func(args, p)
  File "C:\Anaconda2\lib\site-packages\conda_build\main_build.py", line 332, in execute
    dirty=args.dirty, activate=args.activate)
  File "C:\Anaconda2\lib\site-packages\conda_build\build.py", line 563, in build
    import conda_build.windows as windows
  File "C:\Anaconda2\lib\site-packages\conda_build\windows.py", line 30, in <module>
    'installationfolder')
  File "C:\Anaconda2\lib\distutils\msvc9compiler.py", line 73, in get_value
    raise KeyError(key)
KeyError: 'installationfolder'

C:>conda info
Current conda install:

```
         platform : win-64
    conda version : 4.1.3
conda-env version : 2.5.1
```

  conda-build version : 1.21.2
       python version : 2.7.11.final.0
     requests version : 2.7.0
     root environment : C:\Anaconda2  (writable)
  default environment : C:\Anaconda2
     envs directories : C:\Anaconda2\envs
        package cache : C:\Anaconda2\pkgs
         channel URLs : https://repo.continuum.io/pkgs/free/win-64/
                        https://repo.continuum.io/pkgs/free/noarch/
                        https://repo.continuum.io/pkgs/pro/win-64/
                        https://repo.continuum.io/pkgs/pro/noarch/
          config file : None
         offline mode : False
    is foreign system : False 



Discussion:
Sorry, you're seeing this error because I didn't anticipate people not having Visual Studio installed.  For now, I recommend downgrading to an earlier conda-build version.  1.21.0 should work for you.  I will fix this as soon as I can.

Thanks! It works with conda-build 1.21.0


Resolving patch:
diff --git a/conda_build/windows.py b/conda_build/windows.py
--- a/conda_build/windows.py
+++ b/conda_build/windows.py
@@ -20,20 +20,7 @@
 
 assert sys.platform == 'win32'
 
-# Set up a load of paths that can be imported from the tests
-if 'ProgramFiles(x86)' in os.environ:
-    PROGRAM_FILES_PATH = os.environ['ProgramFiles(x86)']
-else:
-    PROGRAM_FILES_PATH = os.environ['ProgramFiles']
-
-WIN_SDK_71_PATH = Reg.get_value(os.path.join(WINSDK_BASE, 'v7.1'),
-                                'installationfolder')
-WIN_SDK_71_BAT_PATH = os.path.join(WIN_SDK_71_PATH, 'Bin', 'SetEnv.cmd')
-# Get the Visual Studio 2008 path (not the Visual C++ for Python path)
-# and get the 'vcvars64.bat' from inside the bin (in the directory above
-# that returned by distutils_find_vcvarsall)
-VCVARS64_VS9_BAT_PATH = os.path.join(os.path.dirname(distutils_find_vcvarsall(9)),
-                                     'bin', 'vcvars64.bat')
+
 VS_VERSION_STRING = {
     '8.0': 'Visual Studio 8 2005',
     '9.0': 'Visual Studio 9 2008',
@@ -81,6 +68,12 @@ def build_vcvarsall_vs_path(version):
     Microsoft Visual Studio vcvarsall.bat file.
     Expected versions are of the form {9, 10, 12, 14}
     """
+    # Set up a load of paths that can be imported from the tests
+    if 'ProgramFiles(x86)' in os.environ:
+        PROGRAM_FILES_PATH = os.environ['ProgramFiles(x86)']
+    else:
+        PROGRAM_FILES_PATH = os.environ['ProgramFiles']
+
     vstools = "VS{0}0COMNTOOLS".format(version)
     if vstools in os.environ:
         return os.path.join(os.environ[vstools], '..\\..\\VC\\vcvarsall.bat')
@@ -133,6 +126,10 @@ def build_vcvarsall_cmd(cmd, arch=arch_selector):
     msvc_env_lines.append('set "MSYS2_ARG_CONV_EXCL=/AI;/AL;/OUT;/out;%MSYS2_ARG_CONV_EXCL%"')
     msvc_env_lines.append('set "MSYS2_ENV_CONV_EXCL=CL"')
     if version == '10.0':
+        WIN_SDK_71_PATH = Reg.get_value(os.path.join(WINSDK_BASE, 'v7.1'),
+                                        'installationfolder')
+        WIN_SDK_71_BAT_PATH = os.path.join(WIN_SDK_71_PATH, 'Bin', 'SetEnv.cmd')
+
         win_sdk_arch = '/Release /x86' if bits == 32 else '/Release /x64'
         win_sdk_cmd = build_vcvarsall_cmd(WIN_SDK_71_BAT_PATH, arch=win_sdk_arch)
 
@@ -154,6 +151,16 @@ def build_vcvarsall_cmd(cmd, arch=arch_selector):
         msvc_env_lines.append('if not "%WindowsSDKDir%" == "{}" ( {} )'.format(
             WIN_SDK_71_PATH, build_vcvarsall_cmd(vcvarsall_vs_path)))
     elif version == '9.0':
+        # Get the Visual Studio 2008 path (not the Visual C++ for Python path)
+        # and get the 'vcvars64.bat' from inside the bin (in the directory above
+        # that returned by distutils_find_vcvarsall)
+        try:
+            VCVARS64_VS9_BAT_PATH = os.path.join(os.path.dirname(distutils_find_vcvarsall(9)),
+                                                'bin', 'vcvars64.bat')
+        # there's an exception if VS or the VC compiler for python are not actually installed.
+        except (KeyError, TypeError):
+            VCVARS64_VS9_BAT_PATH = None
+
         error1 = 'if errorlevel 1 {}'
 
         # Setuptools captures the logic of preferring the Microsoft Visual C++
@@ -163,7 +170,7 @@ def build_vcvarsall_cmd(cmd, arch=arch_selector):
         # the amd64 build files, so we call the vcvars64.bat manually,
         # rather than using the vcvarsall.bat which would try and call the
         # missing bat file.
-        if arch_selector == 'amd64':
+        if arch_selector == 'amd64' and VCVARS64_VS9_BAT_PATH:
             msvc_env_lines.append(error1.format(
                 build_vcvarsall_cmd(VCVARS64_VS9_BAT_PATH)))
     else:


Test patch: