<commit_before>import json
import os
import ntpath
import uuid
from itertools import chain
from lxml import etree
from lxml.builder import E

from .. import iterutils
from .. import path
from .. import shell
from ..makedirs import makedirs

Path = path.Path

def uuid_str(uuid):
    return '{{{}}}'.format(str(uuid).upper())

class SlnElement(object):
    def __init__(self, name, arg=None, value=None):
        if arg is not None and value is None:
            raise TypeError('if arg is passed, value must be too')
        self.name = name
        self.arg = arg
        self.value = value
        self.children = []

    def __call__(self, *args):
        return self.extend(args)

    def append(self, item):
        self.children.append(item)
        return self

    def extend(self, args):
        self.children.extend(args)
        return self

    def _write(self, out, depth):
        out.write('\t' * depth)
        out.write(self.name)
        if self.arg:
            out.write('({}) = {}'.format(self.arg, self.value))
        out.write('\n')

        for i in self.children:
            i._write(out, depth + 1)

        out.write('\t' * depth + 'End' + self.name + '\n')

class SlnVariable(object):
    def __init__(self, name, value):
        self.name = name
        self.value = value

    def _write(self, out, depth):
        out.write('\t' * depth + '{} = {}\n'.format(self.name, self.value))

class SlnMaker(object):
    def __init__(self):
        pass

    def __call__(self, *args, **kwargs):
        return SlnElement(*args, **kwargs)

    def __getattribute__(self, name):
        def closure(*args, **kwargs):
            return SlnElement(name, *args, **kwargs)
        return closure

def sln_write(out, x):
    if isinstance(x, list):
        for i in x:
            i._write(out, 0)
    else:
        x._write(out, 0)

def write_solution(out, uuid, projects):
    S = SlnMaker()
    Var = SlnVariable

    uuid = uuid_str(uuid)
    out.write('Microsoft Visual Studio Solution File, Format Version 12.00\n')
    out.write('# Visual Studio 14\n')

    sln = [
        Var('VisualStudioVersion', '14.0.22609.0'),
        Var('MinimumVisualStudioVersion', '10.0.40219.1')
    ]

    configs = set()
    project_info = []

    for p in projects:
        proj = S.Project(
            '"{}"'.format(uuid),
            '"{name}", "{path}", "{uuid}"'.format(
                name=p.name, path=p.path, uuid=p.uuid_str
            )
        )
        if p.dependencies:
            proj.append(
                S.ProjectSection('ProjectDependencies', 'postProject')
                 .extend(Var(i.uuid_str, i.uuid_str) for i in p.dependencies)
            )
        sln.append(proj)

        configs.add(p.config_plat)
        project_info.append(Var('{uuid}.{cfg}.ActiveCfg'.format(
            uuid=p.uuid_str, cfg=p.config_plat
        ), p.config_plat))
        project_info.append(Var('{uuid}.{cfg}.Build.0'.format(
            uuid=p.uuid_str, cfg=p.config_plat
        ), p.config_plat))

    sln.append(S.Global()(
        S.GlobalSection('SolutionConfigurationPlatforms', 'preSolution')
         .extend(Var(i, i) for i in configs),
        S.GlobalSection('ProjectConfigurationPlatforms', 'postSolution')
         .extend(project_info),
        S.GlobalSection('SolutionProperties', 'preSolution')(
            Var('HideSolutionNode', 'FALSE')
        )
    ))

    sln_write(out, sln)

_path_vars = {
    Path.srcdir: '$(SourceDir)',
    Path.builddir: None,
}
def path_str(node):
    return ntpath.normpath(node.path.realize(_path_vars))

class VcxProject(object):
    _XMLNS = 'http://schemas.microsoft.com/developer/msbuild/2003'
    _DOCTYPE = '<?xml version="1.0" encoding="utf-8"?>'

    def __init__(self, name, uuid, mode='Application', configuration=None,
                 platform=None, output_file=None,
                 import_lib=None, srcdir=None, files=None, compile_options=None,
                 includes=None, libs=None, libdirs=None, dependencies=None):
        self.name = name
        self.uuid = uuid
        self.mode = mode
        self.configuration = configuration or 'Debug'
        self.platform = platform or 'Win32'
        self.output_files = iterutils.listify(output_file)
        self.srcdir = srcdir
        self.files = files or []
        self.compile_options = compile_options or []
        self.includes = includes or []
        self.libs = libs or []
        self.libdirs = libdirs or []
        self.dependencies = dependencies or []

    @property
    def path(self):
        return os.path.join(self.name, '{}.vcxproj'.format(
            os.path.basename(self.name)
        ))

    @property
    def config_plat(self):
        return '{config}|{platform}'.format(
            config=self.configuration,
            platform=self.platform
        )

    @property
    def uuid_str(self):
        return uuid_str(self.uuid)

    def write(self, out):
        override_props = E.PropertyGroup()
        if self.libdirs:
            override_props.append(E.LibraryPath(
                ';'.join(self.libdirs + ['$(LibraryPath)'])
            ))
        override_props.append(E.TargetPath(
            '$(OutDir)' + self.output_files[0]
        ))

        compile_opts = E.ClCompile(
            E.WarningLevel('Level3')
        )
        if self.compile_options:
            compile_opts.append(E.AdditionalOptions(
                ' '.join(shell.quote(i) for i in self.compile_options) +
                ' %(AdditionalOptions)'
            ))
        if self.includes:
            compile_opts.append(E.AdditionalIncludeDirectories(
                ';'.join(self.includes + ['%(AdditionalIncludeDirectories)'])
            ))

        link_opts = E.Link()
        if len(self.output_files) >= 1:
            link_opts.append(E.OutputFile('$(TargetPath)'))
        if len(self.output_files) == 2:
            link_opts.append(E.ImportLibrary(
                '$(OutDir)' + self.output_files[1]
            ))
        if self.libs:
            libs = ';'.join(self.libs + ['%(AdditionalDependencies)'])
            link_opts.append(E.AdditionalDependencies(libs))

        project = E.Project({'DefaultTargets': 'Build'},
                            {'ToolsVersion': '14.0'}, {'xmlns': self._XMLNS},
            E.ItemGroup({'Label' : 'ProjectConfigurations'},
                E.ProjectConfiguration({'Include' : self.config_plat},
                    E.Configuration(self.configuration),
                    E.Platform(self.platform)
                )
            ),
            E.PropertyGroup({'Label': 'Globals'},
                E.ProjectGuid(self.uuid_str),<commit_msg>Emit the proper node for linking static libraries via MSBuild<commit_after>import json
import os
import ntpath
import uuid
from itertools import chain
from lxml import etree
from lxml.builder import E

from .. import iterutils
from .. import path
from .. import shell
from ..makedirs import makedirs

Path = path.Path

def uuid_str(uuid):
    return '{{{}}}'.format(str(uuid).upper())

class SlnElement(object):
    def __init__(self, name, arg=None, value=None):
        if arg is not None and value is None:
            raise TypeError('if arg is passed, value must be too')
        self.name = name
        self.arg = arg
        self.value = value
        self.children = []

    def __call__(self, *args):
        return self.extend(args)

    def append(self, item):
        self.children.append(item)
        return self

    def extend(self, args):
        self.children.extend(args)
        return self

    def _write(self, out, depth):
        out.write('\t' * depth)
        out.write(self.name)
        if self.arg:
            out.write('({}) = {}'.format(self.arg, self.value))
        out.write('\n')

        for i in self.children:
            i._write(out, depth + 1)

        out.write('\t' * depth + 'End' + self.name + '\n')

class SlnVariable(object):
    def __init__(self, name, value):
        self.name = name
        self.value = value

    def _write(self, out, depth):
        out.write('\t' * depth + '{} = {}\n'.format(self.name, self.value))

class SlnMaker(object):
    def __init__(self):
        pass

    def __call__(self, *args, **kwargs):
        return SlnElement(*args, **kwargs)

    def __getattribute__(self, name):
        def closure(*args, **kwargs):
            return SlnElement(name, *args, **kwargs)
        return closure

def sln_write(out, x):
    if isinstance(x, list):
        for i in x:
            i._write(out, 0)
    else:
        x._write(out, 0)

def write_solution(out, uuid, projects):
    S = SlnMaker()
    Var = SlnVariable

    uuid = uuid_str(uuid)
    out.write('Microsoft Visual Studio Solution File, Format Version 12.00\n')
    out.write('# Visual Studio 14\n')

    sln = [
        Var('VisualStudioVersion', '14.0.22609.0'),
        Var('MinimumVisualStudioVersion', '10.0.40219.1')
    ]

    configs = set()
    project_info = []

    for p in projects:
        proj = S.Project(
            '"{}"'.format(uuid),
            '"{name}", "{path}", "{uuid}"'.format(
                name=p.name, path=p.path, uuid=p.uuid_str
            )
        )
        if p.dependencies:
            proj.append(
                S.ProjectSection('ProjectDependencies', 'postProject')
                 .extend(Var(i.uuid_str, i.uuid_str) for i in p.dependencies)
            )
        sln.append(proj)

        configs.add(p.config_plat)
        project_info.append(Var('{uuid}.{cfg}.ActiveCfg'.format(
            uuid=p.uuid_str, cfg=p.config_plat
        ), p.config_plat))
        project_info.append(Var('{uuid}.{cfg}.Build.0'.format(
            uuid=p.uuid_str, cfg=p.config_plat
        ), p.config_plat))

    sln.append(S.Global()(
        S.GlobalSection('SolutionConfigurationPlatforms', 'preSolution')
         .extend(Var(i, i) for i in configs),
        S.GlobalSection('ProjectConfigurationPlatforms', 'postSolution')
         .extend(project_info),
        S.GlobalSection('SolutionProperties', 'preSolution')(
            Var('HideSolutionNode', 'FALSE')
        )
    ))

    sln_write(out, sln)

_path_vars = {
    Path.srcdir: '$(SourceDir)',
    Path.builddir: None,
}
def path_str(node):
    return ntpath.normpath(node.path.realize(_path_vars))

class VcxProject(object):
    _XMLNS = 'http://schemas.microsoft.com/developer/msbuild/2003'
    _DOCTYPE = '<?xml version="1.0" encoding="utf-8"?>'

    def __init__(self, name, uuid, mode='Application', configuration=None,
                 platform=None, output_file=None,
                 import_lib=None, srcdir=None, files=None, compile_options=None,
                 includes=None, libs=None, libdirs=None, dependencies=None):
        self.name = name
        self.uuid = uuid
        self.mode = mode
        self.configuration = configuration or 'Debug'
        self.platform = platform or 'Win32'
        self.output_files = iterutils.listify(output_file)
        self.srcdir = srcdir
        self.files = files or []
        self.compile_options = compile_options or []
        self.includes = includes or []
        self.libs = libs or []
        self.libdirs = libdirs or []
        self.dependencies = dependencies or []

    @property
    def path(self):
        return os.path.join(self.name, '{}.vcxproj'.format(
            os.path.basename(self.name)
        ))

    @property
    def config_plat(self):
        return '{config}|{platform}'.format(
            config=self.configuration,
            platform=self.platform
        )

    @property
    def uuid_str(self):
        return uuid_str(self.uuid)

    def write(self, out):
        override_props = E.PropertyGroup()
        if self.libdirs:
            override_props.append(E.LibraryPath(
                ';'.join(self.libdirs + ['$(LibraryPath)'])
            ))
        override_props.append(E.TargetPath(
            '$(OutDir)' + self.output_files[0]
        ))

        compile_opts = E.ClCompile(
            E.WarningLevel('Level3')
        )
        if self.compile_options:
            compile_opts.append(E.AdditionalOptions(
                ' '.join(shell.quote(i) for i in self.compile_options) +
                ' %(AdditionalOptions)'
            ))
        if self.includes:
            compile_opts.append(E.AdditionalIncludeDirectories(
                ';'.join(self.includes + ['%(AdditionalIncludeDirectories)'])
            ))

        if self.mode == 'StaticLibrary':
            link_opts = E.Lib()
        else:
            link_opts = E.Link()
        if len(self.output_files) >= 1:
            link_opts.append(E.OutputFile('$(TargetPath)'))
        if len(self.output_files) == 2:
            link_opts.append(E.ImportLibrary(
                '$(OutDir)' + self.output_files[1]
            ))
        if self.libs:
            libs = ';'.join(self.libs + ['%(AdditionalDependencies)'])
            link_opts.append(E.AdditionalDependencies(libs))

        project = E.Project({'DefaultTargets': 'Build'},
                            {'ToolsVersion': '14.0'}, {'xmlns': self._XMLNS},
            E.ItemGroup({'Label' : 'ProjectConfigurations'},
                E.ProjectConfiguration({'Include' : self.config_plat},
                    E.Configuration(self.configuration),
                    E.Platform(self.platform)
                )
            ),
            E.PropertyGroup({'Label': 'Globals'},
                E.ProjectGuid(self.uuid_str),
{'fraction_non_alphanumeric': 0.13360625232601414, 'fraction_numerical': 0.006698920729438035, 'mean_word_length': 2.1101851851851854, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 2, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '2731501', 'n_tokens_mistral': 4187, 'n_tokens_neox': 4010, 'n_words': 822}