1"""
2Provides .props file.
3"""
4
5import os
6
7from .constants import *
8
9__all__ = ["PYTHON_PROPS_NAME"]
10
11
12def public(f):
13    __all__.append(f.__name__)
14    return f
15
16
17PYTHON_PROPS_NAME = "python.props"
18
19PROPS_DATA = {
20    "PYTHON_TAG": VER_DOT,
21    "PYTHON_VERSION": os.getenv("PYTHON_NUSPEC_VERSION"),
22    "PYTHON_PLATFORM": os.getenv("PYTHON_PROPS_PLATFORM"),
23    "PYTHON_TARGET": "",
24}
25
26if not PROPS_DATA["PYTHON_VERSION"]:
27    if VER_NAME:
28        PROPS_DATA["PYTHON_VERSION"] = "{}.{}-{}{}".format(
29            VER_DOT, VER_MICRO, VER_NAME, VER_SERIAL
30        )
31    else:
32        PROPS_DATA["PYTHON_VERSION"] = "{}.{}".format(VER_DOT, VER_MICRO)
33
34if not PROPS_DATA["PYTHON_PLATFORM"]:
35    PROPS_DATA["PYTHON_PLATFORM"] = "x64" if IS_X64 else "Win32"
36
37PROPS_DATA["PYTHON_TARGET"] = "_GetPythonRuntimeFilesDependsOn{}{}_{}".format(
38    VER_MAJOR, VER_MINOR, PROPS_DATA["PYTHON_PLATFORM"]
39)
40
41PROPS_TEMPLATE = r"""<?xml version="1.0" encoding="utf-8"?>
42<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
43  <PropertyGroup Condition="$(Platform) == '{PYTHON_PLATFORM}'">
44    <PythonHome Condition="$(Configuration) == 'Debug'">$([msbuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), "python_d.exe")</PythonHome>
45    <PythonHome Condition="$(PythonHome) == ''">$([msbuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), "python.exe")</PythonHome>
46    <PythonInclude>$(PythonHome)\include</PythonInclude>
47    <PythonLibs>$(PythonHome)\libs</PythonLibs>
48    <PythonTag>{PYTHON_TAG}</PythonTag>
49    <PythonVersion>{PYTHON_VERSION}</PythonVersion>
50
51    <IncludePythonExe Condition="$(IncludePythonExe) == ''">true</IncludePythonExe>
52    <IncludeDistutils Condition="$(IncludeDistutils) == ''">false</IncludeDistutils>
53    <IncludeLib2To3 Condition="$(IncludeLib2To3) == ''">false</IncludeLib2To3>
54    <IncludeVEnv Condition="$(IncludeVEnv) == ''">false</IncludeVEnv>
55
56    <GetPythonRuntimeFilesDependsOn>{PYTHON_TARGET};$(GetPythonRuntimeFilesDependsOn)</GetPythonRuntimeFilesDependsOn>
57  </PropertyGroup>
58
59  <ItemDefinitionGroup Condition="$(Platform) == '{PYTHON_PLATFORM}'">
60    <ClCompile>
61      <AdditionalIncludeDirectories>$(PythonInclude);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
62      <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
63    </ClCompile>
64    <Link>
65      <AdditionalLibraryDirectories>$(PythonLibs);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
66    </Link>
67  </ItemDefinitionGroup>
68
69  <Target Name="GetPythonRuntimeFiles" Returns="@(PythonRuntime)" DependsOnTargets="$(GetPythonRuntimeFilesDependsOn)" />
70
71  <Target Name="{PYTHON_TARGET}" Returns="@(PythonRuntime)">
72    <ItemGroup>
73      <_PythonRuntimeExe Include="$(PythonHome)\python*.dll" />
74      <_PythonRuntimeExe Include="$(PythonHome)\python*.exe" Condition="$(IncludePythonExe) == 'true'" />
75      <_PythonRuntimeExe>
76        <Link>%(Filename)%(Extension)</Link>
77      </_PythonRuntimeExe>
78      <_PythonRuntimeDlls Include="$(PythonHome)\DLLs\*.pyd" />
79      <_PythonRuntimeDlls Include="$(PythonHome)\DLLs\*.dll" />
80      <_PythonRuntimeDlls>
81        <Link>DLLs\%(Filename)%(Extension)</Link>
82      </_PythonRuntimeDlls>
83      <_PythonRuntimeLib Include="$(PythonHome)\Lib\**\*" Exclude="$(PythonHome)\Lib\**\*.pyc;$(PythonHome)\Lib\site-packages\**\*" />
84      <_PythonRuntimeLib Remove="$(PythonHome)\Lib\distutils\**\*" Condition="$(IncludeDistutils) != 'true'" />
85      <_PythonRuntimeLib Remove="$(PythonHome)\Lib\lib2to3\**\*" Condition="$(IncludeLib2To3) != 'true'" />
86      <_PythonRuntimeLib Remove="$(PythonHome)\Lib\ensurepip\**\*" Condition="$(IncludeVEnv) != 'true'" />
87      <_PythonRuntimeLib Remove="$(PythonHome)\Lib\venv\**\*" Condition="$(IncludeVEnv) != 'true'" />
88      <_PythonRuntimeLib>
89        <Link>Lib\%(RecursiveDir)%(Filename)%(Extension)</Link>
90      </_PythonRuntimeLib>
91      <PythonRuntime Include="@(_PythonRuntimeExe);@(_PythonRuntimeDlls);@(_PythonRuntimeLib)" />
92    </ItemGroup>
93
94    <Message Importance="low" Text="Collected Python runtime from $(PythonHome):%0D%0A@(PythonRuntime->'  %(Link)','%0D%0A')" />
95  </Target>
96</Project>
97"""
98
99
100@public
101def get_props_layout(ns):
102    if ns.include_all or ns.include_props:
103        yield "python.props", ns.temp / "python.props"
104
105
106@public
107def get_props(ns):
108    # TODO: Filter contents of props file according to included/excluded items
109    props = PROPS_TEMPLATE.format_map(PROPS_DATA)
110    return props.encode("utf-8")
111