Repository: conda/conda-build

Issue:
Unicode values in subprocess env causing failures on Python 2.7 at `run_script` time
```
Traceback (most recent call last):
  File "C:\Users\ray\mc-x64-2.7\Scripts\conda-build-script.py", line 9, in <module>
    load_entry_point('conda-build==0+unknown', 'console_scripts', 'conda-build')()
  File "c:\users\ray\mc-x64-2.7\lib\site-packages\conda_build-0+unknown-py2.7.egg\conda_build\main_build.py", line 151, in main
    args_func(args, p)
  File "c:\users\ray\mc-x64-2.7\lib\site-packages\conda_build-0+unknown-py2.7.egg\conda_build\main_build.py", line 412, in args_func
    args.func(args, p)
  File "c:\users\ray\mc-x64-2.7\lib\site-packages\conda_build-0+unknown-py2.7.egg\conda_build\main_build.py", line 355, in execute
    debug=args.debug)
  File "c:\users\ray\mc-x64-2.7\lib\site-packages\conda_build-0+unknown-py2.7.egg\conda_build\build.py", line 548, in build
    create_env(config.build_prefix, specs, debug=debug)
  File "c:\users\ray\mc-x64-2.7\lib\site-packages\conda_build-0+unknown-py2.7.egg\conda_build\build.py", line 403, in create_env
    plan.execute_actions(actions, index, verbose=debug)
  File "c:\users\ray\mc-x64-2.7\lib\site-packages\conda-4.2.3.dev23+97248c5-py2.7.egg\conda\plan.py", line 639, in execute_actions
    inst.execute_instructions(plan, index, verbose)
  File "c:\users\ray\mc-x64-2.7\lib\site-packages\conda-4.2.3.dev23+97248c5-py2.7.egg\conda\instructions.py", line 132, in execute_instructions
    cmd(state, arg)
  File "c:\users\ray\mc-x64-2.7\lib\site-packages\conda-4.2.3.dev23+97248c5-py2.7.egg\conda\instructions.py", line 77, in LINK_CMD
    link(state['prefix'], dist, lt, index=state['index'])
  File "c:\users\ray\mc-x64-2.7\lib\site-packages\conda-4.2.3.dev23+97248c5-py2.7.egg\conda\install.py", line 1103, in link
    if not run_script(prefix, dist, 'post-link'):
  File "c:\users\ray\mc-x64-2.7\lib\site-packages\conda-4.2.3.dev23+97248c5-py2.7.egg\conda\install.py", line 554, in run_script
    subprocess.check_call(args, env=env)
  File "C:\Users\ray\mc-x64-2.7\lib\subprocess.py", line 536, in check_call
    retcode = call(*popenargs, **kwargs)
  File "C:\Users\ray\mc-x64-2.7\lib\subprocess.py", line 523, in call
    return Popen(*popenargs, **kwargs).wait()
  File "C:\Users\ray\mc-x64-2.7\lib\subprocess.py", line 711, in __init__
    errread, errwrite)
  File "C:\Users\ray\mc-x64-2.7\lib\subprocess.py", line 960, in _execute_child
    startupinfo)
TypeError: environment can only contain strings
```

I added a `print()` just above and got the following env. vars:

```
u'ROOT_PREFIX': 'C:\Users\ray\mc-x64-2.7'
u'PKG_VERSION': u'1.0.0'
u'PREFIX': 'C:\Users\ray\mc-x64-2.7\envs\_build'
u'PKG_BUILDNUM': u'1'
u'PKG_NAME': u'm2-base
```



Discussion:

Resolving patch:
diff --git a/conda_build/build.py b/conda_build/build.py
--- a/conda_build/build.py
+++ b/conda_build/build.py
@@ -36,7 +36,7 @@
 from conda_build.scripts import create_entry_points, prepend_bin_path
 from conda_build.post import (post_process, post_build,
                               fix_permissions, get_build_metadata)
-from conda_build.utils import rm_rf, _check_call
+from conda_build.utils import rm_rf, _check_call, on_win, codec
 from conda_build.index import update_index
 from conda_build.create_test import (create_files, create_shell_files,
                                      create_py_files, create_pl_files)
@@ -47,7 +47,6 @@
 #    eventually.
 import encodings.idna  # NOQA
 
-on_win = (sys.platform == 'win32')
 if 'bsd' in sys.platform:
     shell_path = '/bin/sh'
 else:
@@ -104,13 +103,13 @@ def have_prefix_files(files):
     :type files: list of tuples containing strings (prefix, mode, filename)
     '''
     prefix = config.build_prefix
-    prefix_bytes = prefix.encode('utf-8')
-    prefix_placeholder_bytes = prefix_placeholder.encode('utf-8')
+    prefix_bytes = prefix.encode(codec)
+    prefix_placeholder_bytes = prefix_placeholder.encode(codec)
     if on_win:
         forward_slash_prefix = prefix.replace('\\', '/')
-        forward_slash_prefix_bytes = forward_slash_prefix.encode('utf-8')
+        forward_slash_prefix_bytes = forward_slash_prefix.encode(codec)
         double_backslash_prefix = prefix.replace('\\', '\\\\')
-        double_backslash_prefix_bytes = double_backslash_prefix.encode('utf-8')
+        double_backslash_prefix_bytes = double_backslash_prefix.encode(codec)
 
     for f in files:
         if f.endswith(('.pyc', '.pyo', '.a')):
@@ -412,6 +411,10 @@ def create_env(prefix, specs, clear_cache=True, debug=False):
                 log.warn("Falling back to legacy prefix length of 80 characters.")
                 log.warn("Your package will not install into prefixes longer than 80 characters.")
                 config.prefix_length = 80
+                if on_win:
+                    os.environ = {k.encode(codec) if hasattr(k, 'encode') else k:
+                                    v.encode(codec) if hasattr(v, 'encode') else v
+                                    for k, v in os.environ.items()}
                 create_env(config.build_prefix, specs, clear_cache=clear_cache, debug=debug)
 
         os.environ['PATH'] = old_path
diff --git a/conda_build/utils.py b/conda_build/utils.py
--- a/conda_build/utils.py
+++ b/conda_build/utils.py
@@ -1,6 +1,7 @@
 from __future__ import absolute_import, division, print_function
 
 import fnmatch
+from locale import getpreferredencoding
 import os
 import sys
 import shutil
@@ -20,6 +21,9 @@
 # Backwards compatibility import. Do not remove.
 from .conda_interface import rm_rf  # NOQA
 
+codec = getpreferredencoding() or 'utf-8'
+on_win = sys.platform == "win32"
+
 
 def find_recipe(path):
     """recurse through a folder, locating meta.yaml.  Raises error if more than one is found.


Test patch: