<commit_before># Copyright (c) 2013 Google Inc. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Handle version information related to Visual Stuio."""

import errno
import os
import re
import subprocess
import sys
import gyp
import glob


def JoinPath(*args):
  return os.path.normpath(os.path.join(*args))


class VisualStudioVersion(object):
  """Information regarding a version of Visual Studio."""

  def __init__(self, short_name, description,
               solution_version, project_version, flat_sln, uses_vcxproj,
               path, sdk_based, default_toolset=None, compatible_sdks=None):
    self.short_name = short_name
    self.description = description
    self.solution_version = solution_version
    self.project_version = project_version
    self.flat_sln = flat_sln
    self.uses_vcxproj = uses_vcxproj
    self.path = path
    self.sdk_based = sdk_based
    self.default_toolset = default_toolset
    compatible_sdks = compatible_sdks or []
    compatible_sdks.sort(key=lambda v: float(v.replace('v', '')), reverse=True)
    self.compatible_sdks = compatible_sdks

  def ShortName(self):
    return self.short_name

  def Description(self):
    """Get the full description of the version."""
    return self.description

  def SolutionVersion(self):
    """Get the version number of the sln files."""
    return self.solution_version

  def ProjectVersion(self):
    """Get the version number of the vcproj or vcxproj files."""
    return self.project_version

  def FlatSolution(self):
    return self.flat_sln

  def UsesVcxproj(self):
    """Returns true if this version uses a vcxproj file."""
    return self.uses_vcxproj

  def ProjectExtension(self):
    """Returns the file extension for the project."""
    return self.uses_vcxproj and '.vcxproj' or '.vcproj'

  def Path(self):
    """Returns the path to Visual Studio installation."""
    return self.path

  def ToolPath(self, tool):
    """Returns the path to a given compiler tool. """
    return os.path.normpath(os.path.join(self.path, "VC/bin", tool))

  def DefaultToolset(self):
    """Returns the msbuild toolset version that will be used in the absence
    of a user override."""
    return self.default_toolset


  def _SetupScriptInternal(self, target_arch):
    """Returns a command (with arguments) to be used to set up the
    environment."""
    assert target_arch in ('x86', 'x64'), "target_arch not supported"
    # If WindowsSDKDir is set and SetEnv.Cmd exists then we are using the
    # depot_tools build tools and should run SetEnv.Cmd to set up the
    # environment. The check for WindowsSDKDir alone is not sufficient because
    # this is set by running vcvarsall.bat.
    sdk_dir = os.environ.get('WindowsSDKDir', '')
    setup_path = JoinPath(sdk_dir, 'Bin', 'SetEnv.Cmd')
    if self.sdk_based and sdk_dir and os.path.exists(setup_path):
      return [setup_path, '/' + target_arch]

    # For VS2017 (and newer) it's faily easy
    if self.short_name >= '2017':
      script_path = JoinPath(self.path,
                             'VC', 'Auxiliary', 'Build', 'vcvarsall.bat')
      return [script_path, target_arch]

    # We try to find the best version of the env setup batch:
    # We don't use VC/vcvarsall.bat for x86 because vcvarsall calls vcvars32,
    # which it can only find if VS??COMNTOOLS is set, which isn't guaranteed.
    vcvarsall = JoinPath(self.path, 'VC', 'vcvarsall.bat')
    is_host_arch_x64 = (
      os.environ.get('PROCESSOR_ARCHITECTURE') == 'AMD64' or
      os.environ.get('PROCESSOR_ARCHITEW6432') == 'AMD64'
    )
    if target_arch == 'x86':
      if self.short_name >= '2013' and self.short_name[-1] != 'e' and \
         is_host_arch_x64:
        # VS2013 and later, non-Express have a x64-x86 cross that we want
        # to prefer.
        arg = 'amd64_x86'
        script_path = vcvarsall
      else:
        # Otherwise, the standard x86 compiler.
        script_path = JoinPath(self.path, 'Common7', 'Tools', 'vsvars32.bat')
      return [script_path, arg]
    elif target_arch == 'x64':
      arg = 'x86_amd64'
      # Use the 64-on-64 compiler if we're not using an express edition and
      # we're running on a 64bit OS.
      if self.short_name[-1] != 'e' and is_host_arch_x64:
        arg = 'amd64'
      return [vcvarsall, arg]

  def SetupScript(self, target_arch):
    script_data = self._SetupScriptInternal(target_arch)
    script_path = script_data[0]
    if not os.path.exists(script_path):
      raise Exception('%s is missing - make sure VC++ tools are installed.' %
                      script_path)
    return script_data


def _RegistryQueryBase(sysdir, key, value):
  """Use reg.exe to read a particular key.

  While ideally we might use the win32 module, we would like gyp to be
  python neutral, so for instance cygwin python lacks this module.

  Arguments:
    sysdir: The system subdirectory to attempt to launch reg.exe from.
    key: The registry key to read from.<commit_msg>Fix MSVC++ 32-on-32 builds after b62d04ff85e6<commit_after># Copyright (c) 2013 Google Inc. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Handle version information related to Visual Stuio."""

import errno
import os
import re
import subprocess
import sys
import gyp
import glob


def JoinPath(*args):
  return os.path.normpath(os.path.join(*args))


class VisualStudioVersion(object):
  """Information regarding a version of Visual Studio."""

  def __init__(self, short_name, description,
               solution_version, project_version, flat_sln, uses_vcxproj,
               path, sdk_based, default_toolset=None, compatible_sdks=None):
    self.short_name = short_name
    self.description = description
    self.solution_version = solution_version
    self.project_version = project_version
    self.flat_sln = flat_sln
    self.uses_vcxproj = uses_vcxproj
    self.path = path
    self.sdk_based = sdk_based
    self.default_toolset = default_toolset
    compatible_sdks = compatible_sdks or []
    compatible_sdks.sort(key=lambda v: float(v.replace('v', '')), reverse=True)
    self.compatible_sdks = compatible_sdks

  def ShortName(self):
    return self.short_name

  def Description(self):
    """Get the full description of the version."""
    return self.description

  def SolutionVersion(self):
    """Get the version number of the sln files."""
    return self.solution_version

  def ProjectVersion(self):
    """Get the version number of the vcproj or vcxproj files."""
    return self.project_version

  def FlatSolution(self):
    return self.flat_sln

  def UsesVcxproj(self):
    """Returns true if this version uses a vcxproj file."""
    return self.uses_vcxproj

  def ProjectExtension(self):
    """Returns the file extension for the project."""
    return self.uses_vcxproj and '.vcxproj' or '.vcproj'

  def Path(self):
    """Returns the path to Visual Studio installation."""
    return self.path

  def ToolPath(self, tool):
    """Returns the path to a given compiler tool. """
    return os.path.normpath(os.path.join(self.path, "VC/bin", tool))

  def DefaultToolset(self):
    """Returns the msbuild toolset version that will be used in the absence
    of a user override."""
    return self.default_toolset


  def _SetupScriptInternal(self, target_arch):
    """Returns a command (with arguments) to be used to set up the
    environment."""
    assert target_arch in ('x86', 'x64'), "target_arch not supported"
    # If WindowsSDKDir is set and SetEnv.Cmd exists then we are using the
    # depot_tools build tools and should run SetEnv.Cmd to set up the
    # environment. The check for WindowsSDKDir alone is not sufficient because
    # this is set by running vcvarsall.bat.
    sdk_dir = os.environ.get('WindowsSDKDir', '')
    setup_path = JoinPath(sdk_dir, 'Bin', 'SetEnv.Cmd')
    if self.sdk_based and sdk_dir and os.path.exists(setup_path):
      return [setup_path, '/' + target_arch]

    # For VS2017 (and newer) it's fairly easy
    if self.short_name >= '2017':
      script_path = JoinPath(self.path,
                             'VC', 'Auxiliary', 'Build', 'vcvarsall.bat')
      return [script_path, target_arch]

    # We try to find the best version of the env setup batch.
    vcvarsall = JoinPath(self.path, 'VC', 'vcvarsall.bat')
    is_host_arch_x64 = (
      os.environ.get('PROCESSOR_ARCHITECTURE') == 'AMD64' or
      os.environ.get('PROCESSOR_ARCHITEW6432') == 'AMD64'
    )
    if target_arch == 'x86':
      if self.short_name >= '2013' and self.short_name[-1] != 'e' and \
         is_host_arch_x64:
        # VS2013 and later, non-Express have a x64-x86 cross that we want
        # to prefer.
        return [vcvarsall, 'amd64_x86']
      else:
        # Otherwise, the standard x86 compiler. We don't use VC/vcvarsall.bat
        # for x86 because vcvarsall calls vcvars32, which it can only find if
        # VS??COMNTOOLS is set, which isn't guaranteed.
        return [JoinPath(self.path, 'Common7', 'Tools', 'vsvars32.bat')]
    elif target_arch == 'x64':
      arg = 'x86_amd64'
      # Use the 64-on-64 compiler if we're not using an express edition and
      # we're running on a 64bit OS.
      if self.short_name[-1] != 'e' and is_host_arch_x64:
        arg = 'amd64'
      return [vcvarsall, arg]

  def SetupScript(self, target_arch):
    script_data = self._SetupScriptInternal(target_arch)
    script_path = script_data[0]
    if not os.path.exists(script_path):
      raise Exception('%s is missing - make sure VC++ tools are installed.' %
                      script_path)
    return script_data


def _RegistryQueryBase(sysdir, key, value):
  """Use reg.exe to read a particular key.

  While ideally we might use the win32 module, we would like gyp to be
  python neutral, so for instance cygwin python lacks this module.

  Arguments:
    sysdir: The system subdirectory to attempt to launch reg.exe from.
    key: The registry key to read from.
{'fraction_non_alphanumeric': 0.09601990049751244, 'fraction_numerical': 0.016218905472636817, 'mean_word_length': 3.2373524451939293, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4977473', 'n_tokens_mistral': 3244, 'n_tokens_neox': 3031, 'n_words': 1024}