<commit_before>#!/usr/bin/python


import os.path
import subprocess
import sys
import gyp.common
import gyp.MSVSNew as MSVSNew
import gyp.MSVSProject as MSVSProject


generator_default_variables = {
    'OS': 'win',
    }


def _FixPath(path):
  """Convert paths to a form that will make sense in a vcproj file.

  Arguments:
    path: The path to convert, may contain / etc.
  Returns:
    The path with cygwinisms removed, and all slashes made into backslashes.
  """
  if sys.platform == 'cygwin':
    path = subprocess.Popen(['cygpath', '-w', path],
                            stdout=subprocess.PIPE).communicate()[0]
    path = path.replace('\n', '')
    return path
  else:
    return path.replace('/', '\\')


def _SourceInFolders(sources, prefix=None):
  """Converts a list split source file paths into a vcproj folder hierarchy.

  Arguments:
    sources: A list of source file paths split.
    prefix: A list of source file path layers meant to apply to each of sources.
  Returns:
    A hierarchy of filenames and MSVSProject.Filter objects that matches the
    layout of the source tree.
    For example:
    _SourceInFolders([['a', 'bob1.c'], ['b', 'bob2.c']], prefix=['joe'])
    -->
    [MSVSProject.Filter('a', contents=['joe\\a\\bob1.c']),
     MSVSProject.Filter('b', contents=['joe\\b\\bob2.c'])]
  """
  if not prefix: prefix = []
  result = []
  folders = dict()
  for s in sources:
    if len(s) == 1:
      filename = '\\'.join(prefix + s)
      result.append(filename)
    else:
      if not folders.get(s[0]):
        folders[s[0]] = []
      folders[s[0]].append(s[1:])
  for f in folders:
    contents = _SourceInFolders(folders[f], prefix + [f])
    contents = MSVSProject.Filter(f, contents=contents)
    result.append(contents)

  return result


def _GenerateProject(vcproj_filename, build_file, spec):
  """Generates a vcproj file.

  Arguments:
    vcproj_filename: Filename of the vcproj file to generate.
    build_file: Filename of the .gyp file that the vcproj file comes from.
    spec: The target dictionary containing the properties of the target.
  """
  print 'Generating %s' % vcproj_filename

  p = MSVSProject.Writer(vcproj_filename)
  p.Create(spec['name'])

  # Get directory project file is in.
  gyp_dir = os.path.split(vcproj_filename)[0]
  # Figure out which vsprops to use.
  vsprops_dirs = spec.get('vs_props', [])
  # Make them relative to the gyp file.
  vsprops_dirs = [os.path.join(gyp_dir, i) for i in vsprops_dirs]
  vsprops_dirs = [_FixPath(i) for i in vsprops_dirs]

  # Prepare compiler tool.
  include_dirs = spec.get('include_dirs', [])
  include_dirs = [os.path.join(gyp_dir, i) for i in include_dirs]
  include_dirs = [_FixPath(i) for i in include_dirs]
  defines = ['OS_WIN'] + spec.get('defines', [])
  disabled_warnings = [str(i) for i in spec.get('vs_disabled_warnings', [])]
  compiler_tool = MSVSProject.Tool(
      'VCCLCompilerTool', {
          'AdditionalIncludeDirectories': ';'.join(include_dirs),
          'PreprocessorDefinitions': ';'.join(defines),
          'DisableSpecificWarnings': ';'.join(disabled_warnings),
      })

  # Pre-build and Post-build.
  prebuild = MSVSProject.Tool(
      'VCPreBuildEventTool', {
          'CommandLine': spec.get('vs_prebuild', ''),
      })
  postbuild = MSVSProject.Tool(
      'VCPostBuildEventTool', {
          'CommandLine': spec.get('vs_postbuild', ''),
      })

  # Prepare tools.
  tools = [
      prebuild,
      'VCCustomBuildTool',
      'VCXMLDataGeneratorTool',
      'VCWebServiceProxyGeneratorTool',
      'VCMIDLTool',<commit_msg>Only emit Command lines for prebuild/postbuild when there is one.<commit_after>#!/usr/bin/python


import os.path
import subprocess
import sys
import gyp.common
import gyp.MSVSNew as MSVSNew
import gyp.MSVSProject as MSVSProject


generator_default_variables = {
    'OS': 'win',
    }


def _FixPath(path):
  """Convert paths to a form that will make sense in a vcproj file.

  Arguments:
    path: The path to convert, may contain / etc.
  Returns:
    The path with cygwinisms removed, and all slashes made into backslashes.
  """
  if sys.platform == 'cygwin':
    path = subprocess.Popen(['cygpath', '-w', path],
                            stdout=subprocess.PIPE).communicate()[0]
    path = path.replace('\n', '')
    return path
  else:
    return path.replace('/', '\\')


def _SourceInFolders(sources, prefix=None):
  """Converts a list split source file paths into a vcproj folder hierarchy.

  Arguments:
    sources: A list of source file paths split.
    prefix: A list of source file path layers meant to apply to each of sources.
  Returns:
    A hierarchy of filenames and MSVSProject.Filter objects that matches the
    layout of the source tree.
    For example:
    _SourceInFolders([['a', 'bob1.c'], ['b', 'bob2.c']], prefix=['joe'])
    -->
    [MSVSProject.Filter('a', contents=['joe\\a\\bob1.c']),
     MSVSProject.Filter('b', contents=['joe\\b\\bob2.c'])]
  """
  if not prefix: prefix = []
  result = []
  folders = dict()
  for s in sources:
    if len(s) == 1:
      filename = '\\'.join(prefix + s)
      result.append(filename)
    else:
      if not folders.get(s[0]):
        folders[s[0]] = []
      folders[s[0]].append(s[1:])
  for f in folders:
    contents = _SourceInFolders(folders[f], prefix + [f])
    contents = MSVSProject.Filter(f, contents=contents)
    result.append(contents)

  return result


def _GenerateProject(vcproj_filename, build_file, spec):
  """Generates a vcproj file.

  Arguments:
    vcproj_filename: Filename of the vcproj file to generate.
    build_file: Filename of the .gyp file that the vcproj file comes from.
    spec: The target dictionary containing the properties of the target.
  """
  print 'Generating %s' % vcproj_filename

  p = MSVSProject.Writer(vcproj_filename)
  p.Create(spec['name'])

  # Get directory project file is in.
  gyp_dir = os.path.split(vcproj_filename)[0]
  # Figure out which vsprops to use.
  vsprops_dirs = spec.get('vs_props', [])
  # Make them relative to the gyp file.
  vsprops_dirs = [os.path.join(gyp_dir, i) for i in vsprops_dirs]
  vsprops_dirs = [_FixPath(i) for i in vsprops_dirs]

  # Prepare compiler tool.
  include_dirs = spec.get('include_dirs', [])
  include_dirs = [os.path.join(gyp_dir, i) for i in include_dirs]
  include_dirs = [_FixPath(i) for i in include_dirs]
  defines = ['OS_WIN'] + spec.get('defines', [])
  disabled_warnings = [str(i) for i in spec.get('vs_disabled_warnings', [])]
  compiler_tool = MSVSProject.Tool(
      'VCCLCompilerTool', {
          'AdditionalIncludeDirectories': ';'.join(include_dirs),
          'PreprocessorDefinitions': ';'.join(defines),
          'DisableSpecificWarnings': ';'.join(disabled_warnings),
      })

  # Pre-build.
  if spec.get('vs_prebuild'):
    prebuild_dict = { 'CommandLine': spec.get('vs_prebuild', '') }
  else:
    prebuild_dict = {}
  prebuild = MSVSProject.Tool('VCPreBuildEventTool', prebuild_dict)
  # Post-build.
  if spec.get('vs_postbuild'):
    postbuild_dict = { 'CommandLine': spec.get('vs_postbuild', '') }
  else:
    postbuild_dict = {}
  postbuild = MSVSProject.Tool('VCPostBuildEventTool', postbuild_dict)

  # Prepare tools.
  tools = [
      prebuild,
      'VCCustomBuildTool',
      'VCXMLDataGeneratorTool',
      'VCWebServiceProxyGeneratorTool',
      'VCMIDLTool',
{'fraction_non_alphanumeric': 0.13080801199073444, 'fraction_numerical': 0.0029976836081209973, 'mean_word_length': 3.3560830860534123, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2238500', 'n_tokens_mistral': 2529, 'n_tokens_neox': 2450, 'n_words': 645}