<commit_before>from __future__ import absolute_import, division, print_function

import os
import sys
import shutil
from os.path import dirname, isdir, isfile, join, exists

import conda.config as cc

from conda_build.config import config
from conda_build import environ
from conda_build import source
from conda_build.utils import _check_call

assert sys.platform == 'win32'


def fix_staged_scripts():
    """
    Fixes scripts which have been installed unix-style to have a .bat
    helper
    """
    scripts_dir = join(config.build_prefix, 'Scripts')
    if not isdir(scripts_dir):
        return
    for fn in os.listdir(scripts_dir):
        # process all the extensionless files
        if not isfile(join(scripts_dir, fn)) or '.' in fn:
            continue

        with open(join(scripts_dir, fn)) as f:
            line = f.readline().lower()
            # If it's a #!python script
            if not (line.startswith('#!') and 'python' in line.lower()):
                continue
            print('Adjusting unix-style #! script %s, '
                  'and adding a .bat file for it' % fn)
            # copy it with a .py extension (skipping that first #! line)
            with open(join(scripts_dir, fn + '-script.py'), 'w') as fo:
                fo.write(f.read())
            # now create the .exe file
            shutil.copyfile(join(dirname(__file__), 'cli-%d.exe' % cc.bits),
                            join(scripts_dir, fn + '.exe'))

        # remove the original script
        os.remove(join(scripts_dir, fn))


def msvc_env_cmd(bits, override=None):
    if 'ProgramFiles(x86)' in os.environ:
        program_files = os.environ['ProgramFiles(x86)']
    else:
        program_files = os.environ['ProgramFiles']
    arch_selector = 'x86' if bits == 32 else 'amd64'

    msvc_env_lines = []

    version = None
    if override is not None:
        version = override
        msvc_env_lines.append('set DISTUTILS_USE_SDK=1')
        msvc_env_lines.append('set MSSdk=1')

    if not version:
        if config.PY3K and config.use_MSVC2015:
            version = '14.0'
        elif config.PY3K:
            version = '10.0'
        else:
            version = '9.0'

    vcvarsall_vs_path = os.path.join(
        program_files, 'Microsoft Visual Studio {}'.format(version), 'VC', 
        'vcvarsall.bat')

    def build_vcvarsall_cmd(cmd, arch=arch_selector):
        return 'call "{cmd}" {arch}'.format(cmd=cmd, arch=arch)

    if version == '10.0':
        vcvarsall = vcvarsall_vs_path
        vcvars_cmd = build_vcvarsall_cmd(vcvarsall)
        
        # Note that we explicitly want "Program Files" and not 
        # "Program Files (x86)"
        win_sdk_bat_path = os.path.join(os.path.abspath(os.sep),
                                        'Program Files', 'Microsoft SDKs',
                                        'Windows', 'v7.1', 'Bin', 'SetEnv.cmd')
        # Unfortunately, the Windows SDK takes a different command format for
        # the arch selector - debug is default so explicitly set 'Release'
        win_sdk_arch = '/x86 /Release' if bits == 32 else '/x64 /Release'
        win_sdk_cmd = build_vcvarsall_cmd(win_sdk_bat_path, arch=win_sdk_arch)
        
        # Always call the Windows SDK first - if VS 2010 exists but was
        # installed using the broken installer then it will try and call the 
        # vcvars script, which will fail but NOT EXIT 1. To work around this,
        # we always call the Windows SDK, and then try calling VS 2010 which
        # will overwrite any environemnt variables it needs, if necessary.
        msvc_env_lines.append(win_sdk_cmd)
        msvc_env_lines.append(vcvars_cmd)
    elif version == '9.0':
        # First, check for Microsoft Visual C++ Compiler for Python 2.7
        localappdata = os.getenv('localappdata', program_files)
        vs_tools_py_local_path = os.path.join(
            localappdata, 'Programs', 'Common', 'Microsoft', 
            'Visual C++ for Python', '9.0', 'vcvarsall.bat')
        msvc_env_lines.append(build_vcvarsall_cmd(vs_tools_py_local_path))
        
        vs_tools_py_common_path = os.path.join(
            localappdata, 'Common Files', 'Microsoft', 'Visual C++ for Python', 
            '9.0', 'vcvarsall.bat')
        msvc_env_lines.append('if errorlevel 1 {}'.format(<commit_msg>Actually, we wanted to use program_files<commit_after>from __future__ import absolute_import, division, print_function

import os
import sys
import shutil
from os.path import dirname, isdir, isfile, join, exists

import conda.config as cc

from conda_build.config import config
from conda_build import environ
from conda_build import source
from conda_build.utils import _check_call

assert sys.platform == 'win32'


def fix_staged_scripts():
    """
    Fixes scripts which have been installed unix-style to have a .bat
    helper
    """
    scripts_dir = join(config.build_prefix, 'Scripts')
    if not isdir(scripts_dir):
        return
    for fn in os.listdir(scripts_dir):
        # process all the extensionless files
        if not isfile(join(scripts_dir, fn)) or '.' in fn:
            continue

        with open(join(scripts_dir, fn)) as f:
            line = f.readline().lower()
            # If it's a #!python script
            if not (line.startswith('#!') and 'python' in line.lower()):
                continue
            print('Adjusting unix-style #! script %s, '
                  'and adding a .bat file for it' % fn)
            # copy it with a .py extension (skipping that first #! line)
            with open(join(scripts_dir, fn + '-script.py'), 'w') as fo:
                fo.write(f.read())
            # now create the .exe file
            shutil.copyfile(join(dirname(__file__), 'cli-%d.exe' % cc.bits),
                            join(scripts_dir, fn + '.exe'))

        # remove the original script
        os.remove(join(scripts_dir, fn))


def msvc_env_cmd(bits, override=None):
    if 'ProgramFiles(x86)' in os.environ:
        program_files = os.environ['ProgramFiles(x86)']
    else:
        program_files = os.environ['ProgramFiles']
    arch_selector = 'x86' if bits == 32 else 'amd64'

    msvc_env_lines = []

    version = None
    if override is not None:
        version = override
        msvc_env_lines.append('set DISTUTILS_USE_SDK=1')
        msvc_env_lines.append('set MSSdk=1')

    if not version:
        if config.PY3K and config.use_MSVC2015:
            version = '14.0'
        elif config.PY3K:
            version = '10.0'
        else:
            version = '9.0'

    vcvarsall_vs_path = os.path.join(
        program_files, 'Microsoft Visual Studio {}'.format(version), 'VC', 
        'vcvarsall.bat')

    def build_vcvarsall_cmd(cmd, arch=arch_selector):
        return 'call "{cmd}" {arch}'.format(cmd=cmd, arch=arch)

    if version == '10.0':
        vcvarsall = vcvarsall_vs_path
        vcvars_cmd = build_vcvarsall_cmd(vcvarsall)
        
        # Note that we explicitly want "Program Files" and not 
        # "Program Files (x86)"
        win_sdk_bat_path = os.path.join(os.path.abspath(os.sep),
                                        'Program Files', 'Microsoft SDKs',
                                        'Windows', 'v7.1', 'Bin', 'SetEnv.cmd')
        # Unfortunately, the Windows SDK takes a different command format for
        # the arch selector - debug is default so explicitly set 'Release'
        win_sdk_arch = '/x86 /Release' if bits == 32 else '/x64 /Release'
        win_sdk_cmd = build_vcvarsall_cmd(win_sdk_bat_path, arch=win_sdk_arch)
        
        # Always call the Windows SDK first - if VS 2010 exists but was
        # installed using the broken installer then it will try and call the 
        # vcvars script, which will fail but NOT EXIT 1. To work around this,
        # we always call the Windows SDK, and then try calling VS 2010 which
        # will overwrite any environemnt variables it needs, if necessary.
        msvc_env_lines.append(win_sdk_cmd)
        msvc_env_lines.append(vcvars_cmd)
    elif version == '9.0':
        # First, check for Microsoft Visual C++ Compiler for Python 2.7
        localappdata = os.getenv('localappdata', os.path.abspath(os.sep))
        vs_tools_py_local_path = os.path.join(
            localappdata, 'Programs', 'Common', 'Microsoft', 
            'Visual C++ for Python', '9.0', 'vcvarsall.bat')
        msvc_env_lines.append(build_vcvarsall_cmd(vs_tools_py_local_path))
        
        vs_tools_py_common_path = os.path.join(
            program_files, 'Common Files', 'Microsoft', 'Visual C++ for Python', 
            '9.0', 'vcvarsall.bat')
        msvc_env_lines.append('if errorlevel 1 {}'.format(
{'fraction_non_alphanumeric': 0.10085194565968225, 'fraction_numerical': 0.013585079438176375, 'mean_word_length': 2.3527595522964107, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '475743', 'n_tokens_mistral': 2728, 'n_tokens_neox': 2597, 'n_words': 766}