Commit 53125a53 authored by Steve Dower's avatar Steve Dower Committed by GitHub

bpo-35067: Remove _distutils_findvs and use vswhere.exe instead. (GH-10095)

parent d855f2fd
...@@ -56,43 +56,43 @@ def _find_vc2015(): ...@@ -56,43 +56,43 @@ def _find_vc2015():
return best_version, best_dir return best_version, best_dir
def _find_vc2017(): def _find_vc2017():
import _distutils_findvs """Returns "15, path" based on the result of invoking vswhere.exe
import threading If no install is found, returns "None, None"
best_version = 0, # tuple for full version comparisons The version is returned to avoid unnecessarily changing the function
best_dir = None result. It may be ignored when the path is not None.
If vswhere.exe is not available, by definition, VS 2017 is not
installed.
"""
import json
root = os.environ.get("ProgramFiles(x86)") or os.environ.get("ProgramFiles")
if not root:
return None, None
# We need to call findall() on its own thread because it will
# initialize COM.
all_packages = []
def _getall():
all_packages.extend(_distutils_findvs.findall())
t = threading.Thread(target=_getall)
t.start()
t.join()
for name, version_str, path, packages in all_packages:
if 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64' in packages:
vc_dir = os.path.join(path, 'VC', 'Auxiliary', 'Build')
if not os.path.isdir(vc_dir):
continue
try:
version = tuple(int(i) for i in version_str.split('.'))
except (ValueError, TypeError):
continue
if version > best_version:
best_version, best_dir = version, vc_dir
try: try:
best_version = best_version[0] path = subprocess.check_output([
except IndexError: os.path.join(root, "Microsoft Visual Studio", "Installer", "vswhere.exe"),
best_version = None "-latest",
return best_version, best_dir "-prerelease",
"-requires", "Microsoft.VisualStudio.Component.VC.Tools.x86.x64",
"-property", "installationPath",
], encoding="mbcs", errors="strict").strip()
except (subprocess.CalledProcessError, OSError, UnicodeDecodeError):
return None, None
path = os.path.join(path, "VC", "Auxiliary", "Build")
if os.path.isdir(path):
return 15, path
return None, None
def _find_vcvarsall(plat_spec): def _find_vcvarsall(plat_spec):
best_version, best_dir = _find_vc2017() _, best_dir = _find_vc2017()
vcruntime = None vcruntime = None
vcruntime_plat = 'x64' if 'amd64' in plat_spec else 'x86' vcruntime_plat = 'x64' if 'amd64' in plat_spec else 'x86'
if best_version: if best_dir:
vcredist = os.path.join(best_dir, "..", "..", "redist", "MSVC", "**", vcredist = os.path.join(best_dir, "..", "..", "redist", "MSVC", "**",
"Microsoft.VC141.CRT", "vcruntime140.dll") "Microsoft.VC141.CRT", "vcruntime140.dll")
try: try:
...@@ -101,13 +101,13 @@ def _find_vcvarsall(plat_spec): ...@@ -101,13 +101,13 @@ def _find_vcvarsall(plat_spec):
except (ImportError, OSError, LookupError): except (ImportError, OSError, LookupError):
vcruntime = None vcruntime = None
if not best_version: if not best_dir:
best_version, best_dir = _find_vc2015() best_version, best_dir = _find_vc2015()
if best_version: if best_version:
vcruntime = os.path.join(best_dir, 'redist', vcruntime_plat, vcruntime = os.path.join(best_dir, 'redist', vcruntime_plat,
"Microsoft.VC140.CRT", "vcruntime140.dll") "Microsoft.VC140.CRT", "vcruntime140.dll")
if not best_version: if not best_dir:
log.debug("No suitable Visual C++ version found") log.debug("No suitable Visual C++ version found")
return None, None return None, None
......
Remove _distutils_findvs module and use vswhere.exe instead.
//
// Helper library for location Visual Studio installations
// using the COM-based query API.
//
// Copyright (c) Microsoft Corporation
// Licensed to PSF under a contributor agreement
//
// Version history
// 2017-05: Initial contribution (Steve Dower)
#include <Windows.h>
#include <Strsafe.h>
#include "external\include\Setup.Configuration.h"
#include <Python.h>
static PyObject *error_from_hr(HRESULT hr)
{
if (FAILED(hr))
PyErr_Format(PyExc_OSError, "Error %08x", hr);
assert(PyErr_Occurred());
return nullptr;
}
static PyObject *get_install_name(ISetupInstance2 *inst)
{
HRESULT hr;
BSTR name;
PyObject *str = nullptr;
if (FAILED(hr = inst->GetDisplayName(LOCALE_USER_DEFAULT, &name)))
goto error;
str = PyUnicode_FromWideChar(name, SysStringLen(name));
SysFreeString(name);
return str;
error:
return error_from_hr(hr);
}
static PyObject *get_install_version(ISetupInstance *inst)
{
HRESULT hr;
BSTR ver;
PyObject *str = nullptr;
if (FAILED(hr = inst->GetInstallationVersion(&ver)))
goto error;
str = PyUnicode_FromWideChar(ver, SysStringLen(ver));
SysFreeString(ver);
return str;
error:
return error_from_hr(hr);
}
static PyObject *get_install_path(ISetupInstance *inst)
{
HRESULT hr;
BSTR path;
PyObject *str = nullptr;
if (FAILED(hr = inst->GetInstallationPath(&path)))
goto error;
str = PyUnicode_FromWideChar(path, SysStringLen(path));
SysFreeString(path);
return str;
error:
return error_from_hr(hr);
}
static PyObject *get_installed_packages(ISetupInstance2 *inst)
{
HRESULT hr;
PyObject *res = nullptr;
LPSAFEARRAY sa_packages = nullptr;
LONG ub = 0;
IUnknown **packages = nullptr;
PyObject *str = nullptr;
if (FAILED(hr = inst->GetPackages(&sa_packages)) ||
FAILED(hr = SafeArrayAccessData(sa_packages, (void**)&packages)) ||
FAILED(SafeArrayGetUBound(sa_packages, 1, &ub)) ||
!(res = PyList_New(0)))
goto error;
for (LONG i = 0; i < ub; ++i) {
ISetupPackageReference *package = nullptr;
BSTR id = nullptr;
PyObject *str = nullptr;
if (FAILED(hr = packages[i]->QueryInterface(&package)) ||
FAILED(hr = package->GetId(&id)))
goto iter_error;
str = PyUnicode_FromWideChar(id, SysStringLen(id));
SysFreeString(id);
if (!str || PyList_Append(res, str) < 0)
goto iter_error;
Py_CLEAR(str);
package->Release();
continue;
iter_error:
if (package) package->Release();
Py_XDECREF(str);
goto error;
}
SafeArrayUnaccessData(sa_packages);
SafeArrayDestroy(sa_packages);
return res;
error:
if (sa_packages && packages) SafeArrayUnaccessData(sa_packages);
if (sa_packages) SafeArrayDestroy(sa_packages);
Py_XDECREF(res);
return error_from_hr(hr);
}
static PyObject *find_all_instances()
{
ISetupConfiguration *sc = nullptr;
ISetupConfiguration2 *sc2 = nullptr;
IEnumSetupInstances *enm = nullptr;
ISetupInstance *inst = nullptr;
ISetupInstance2 *inst2 = nullptr;
PyObject *res = nullptr;
ULONG fetched;
HRESULT hr;
if (!(res = PyList_New(0)))
goto error;
if (FAILED(hr = CoCreateInstance(
__uuidof(SetupConfiguration),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(ISetupConfiguration),
(LPVOID*)&sc
)) && hr != REGDB_E_CLASSNOTREG)
goto error;
// If the class is not registered, there are no VS instances installed
if (hr == REGDB_E_CLASSNOTREG)
return res;
if (FAILED(hr = sc->QueryInterface(&sc2)) ||
FAILED(hr = sc2->EnumAllInstances(&enm)))
goto error;
while (SUCCEEDED(enm->Next(1, &inst, &fetched)) && fetched) {
PyObject *name = nullptr;
PyObject *version = nullptr;
PyObject *path = nullptr;
PyObject *packages = nullptr;
PyObject *tuple = nullptr;
if (FAILED(hr = inst->QueryInterface(&inst2)) ||
!(name = get_install_name(inst2)) ||
!(version = get_install_version(inst)) ||
!(path = get_install_path(inst)) ||
!(packages = get_installed_packages(inst2)) ||
!(tuple = PyTuple_Pack(4, name, version, path, packages)) ||
PyList_Append(res, tuple) < 0)
goto iter_error;
Py_DECREF(tuple);
Py_DECREF(packages);
Py_DECREF(path);
Py_DECREF(version);
Py_DECREF(name);
continue;
iter_error:
if (inst2) inst2->Release();
Py_XDECREF(tuple);
Py_XDECREF(packages);
Py_XDECREF(path);
Py_XDECREF(version);
Py_XDECREF(name);
goto error;
}
enm->Release();
sc2->Release();
sc->Release();
return res;
error:
if (enm) enm->Release();
if (sc2) sc2->Release();
if (sc) sc->Release();
Py_XDECREF(res);
return error_from_hr(hr);
}
PyDoc_STRVAR(findvs_findall_doc, "findall()\
\
Finds all installed versions of Visual Studio.\
\
This function will initialize COM temporarily. To avoid impact on other parts\
of your application, use a new thread to make this call.");
static PyObject *findvs_findall(PyObject *self, PyObject *args, PyObject *kwargs)
{
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
if (hr == RPC_E_CHANGED_MODE)
hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
if (FAILED(hr))
return error_from_hr(hr);
PyObject *res = find_all_instances();
CoUninitialize();
return res;
}
// List of functions to add to findvs in exec_findvs().
static PyMethodDef findvs_functions[] = {
{ "findall", (PyCFunction)findvs_findall, METH_VARARGS | METH_KEYWORDS, findvs_findall_doc },
{ NULL, NULL, 0, NULL }
};
// Initialize findvs. May be called multiple times, so avoid
// using static state.
static int exec_findvs(PyObject *module)
{
PyModule_AddFunctions(module, findvs_functions);
return 0; // success
}
PyDoc_STRVAR(findvs_doc, "The _distutils_findvs helper module");
static PyModuleDef_Slot findvs_slots[] = {
{ Py_mod_exec, exec_findvs },
{ 0, NULL }
};
static PyModuleDef findvs_def = {
PyModuleDef_HEAD_INIT,
"_distutils_findvs",
findvs_doc,
0, // m_size
NULL, // m_methods
findvs_slots,
NULL, // m_traverse
NULL, // m_clear
NULL, // m_free
};
extern "C" {
PyMODINIT_FUNC PyInit__distutils_findvs(void)
{
return PyModuleDef_Init(&findvs_def);
}
}
The files in this folder are from the Microsoft.VisualStudio.Setup.Configuration.Native package on Nuget.
They are licensed under the MIT license.
This diff is collapsed.
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="PGInstrument|Win32">
<Configuration>PGInstrument</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="PGInstrument|x64">
<Configuration>PGInstrument</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="PGUpdate|Win32">
<Configuration>PGUpdate</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="PGUpdate|x64">
<Configuration>PGUpdate</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{41ADEDF9-11D8-474E-B4D7-BB82332C878E}</ProjectGuid>
<RootNamespace>_distutils_findvs</RootNamespace>
<Keyword>Win32Proj</Keyword>
</PropertyGroup>
<Import Project="python.props" />
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<CharacterSet>NotSet</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<PropertyGroup>
<TargetExt>.pyd</TargetExt>
</PropertyGroup>
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="pyproject.props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<_ProjectFileVersion>10.0.30319.1</_ProjectFileVersion>
</PropertyGroup>
<ItemDefinitionGroup>
<Link>
<AdditionalDependencies>version.lib;ole32.lib;oleaut32.lib;Microsoft.VisualStudio.Setup.Configuration.Native.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories);$(PySourcePath)PC\external\$(PlatformToolset)\$(ArchName)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\PC\_findvs.cpp" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="..\PC\python_nt.rc" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="pythoncore.vcxproj">
<Project>{cf7ac3d1-e2df-41d2-bea6-1e2556cdea26}</Project>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ResourceCompile Include="..\PC\python_nt.rc" />
</ItemGroup>
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{c56a5dd3-7838-48e9-a781-855d8be7370f}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\PC\_findvs.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
\ No newline at end of file
...@@ -49,7 +49,7 @@ ...@@ -49,7 +49,7 @@
<!-- pyshellext.dll --> <!-- pyshellext.dll -->
<Projects Include="pyshellext.vcxproj" /> <Projects Include="pyshellext.vcxproj" />
<!-- Extension modules --> <!-- Extension modules -->
<ExtensionModules Include="_asyncio;_contextvars;_ctypes;_decimal;_distutils_findvs;_elementtree;_msi;_multiprocessing;_overlapped;pyexpat;_queue;select;unicodedata;winsound" /> <ExtensionModules Include="_asyncio;_contextvars;_ctypes;_decimal;_elementtree;_msi;_multiprocessing;_overlapped;pyexpat;_queue;select;unicodedata;winsound" />
<!-- Extension modules that require external sources --> <!-- Extension modules that require external sources -->
<ExternalModules Include="_bz2;_lzma;_sqlite3" /> <ExternalModules Include="_bz2;_lzma;_sqlite3" />
<!-- _ssl will build _socket as well, which may cause conflicts in parallel builds --> <!-- _ssl will build _socket as well, which may cause conflicts in parallel builds -->
......
...@@ -93,8 +93,6 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "_queue", "_queue.vcxproj", ...@@ -93,8 +93,6 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "_queue", "_queue.vcxproj",
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "liblzma", "liblzma.vcxproj", "{12728250-16EC-4DC6-94D7-E21DD88947F8}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "liblzma", "liblzma.vcxproj", "{12728250-16EC-4DC6-94D7-E21DD88947F8}"
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "_distutils_findvs", "_distutils_findvs.vcxproj", "{41ADEDF9-11D8-474E-B4D7-BB82332C878E}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32 Debug|Win32 = Debug|Win32
...@@ -695,22 +693,6 @@ Global ...@@ -695,22 +693,6 @@ Global
{12728250-16EC-4DC6-94D7-E21DD88947F8}.Release|Win32.Build.0 = Release|Win32 {12728250-16EC-4DC6-94D7-E21DD88947F8}.Release|Win32.Build.0 = Release|Win32
{12728250-16EC-4DC6-94D7-E21DD88947F8}.Release|x64.ActiveCfg = Release|x64 {12728250-16EC-4DC6-94D7-E21DD88947F8}.Release|x64.ActiveCfg = Release|x64
{12728250-16EC-4DC6-94D7-E21DD88947F8}.Release|x64.Build.0 = Release|x64 {12728250-16EC-4DC6-94D7-E21DD88947F8}.Release|x64.Build.0 = Release|x64
{41ADEDF9-11D8-474E-B4D7-BB82332C878E}.Debug|Win32.ActiveCfg = Debug|Win32
{41ADEDF9-11D8-474E-B4D7-BB82332C878E}.Debug|Win32.Build.0 = Debug|Win32
{41ADEDF9-11D8-474E-B4D7-BB82332C878E}.Debug|x64.ActiveCfg = Debug|x64
{41ADEDF9-11D8-474E-B4D7-BB82332C878E}.Debug|x64.Build.0 = Debug|x64
{41ADEDF9-11D8-474E-B4D7-BB82332C878E}.PGInstrument|Win32.ActiveCfg = PGInstrument|Win32
{41ADEDF9-11D8-474E-B4D7-BB82332C878E}.PGInstrument|Win32.Build.0 = PGInstrument|Win32
{41ADEDF9-11D8-474E-B4D7-BB82332C878E}.PGInstrument|x64.ActiveCfg = PGInstrument|x64
{41ADEDF9-11D8-474E-B4D7-BB82332C878E}.PGInstrument|x64.Build.0 = PGInstrument|x64
{41ADEDF9-11D8-474E-B4D7-BB82332C878E}.PGUpdate|Win32.ActiveCfg = PGUpdate|Win32
{41ADEDF9-11D8-474E-B4D7-BB82332C878E}.PGUpdate|Win32.Build.0 = PGUpdate|Win32
{41ADEDF9-11D8-474E-B4D7-BB82332C878E}.PGUpdate|x64.ActiveCfg = PGUpdate|x64
{41ADEDF9-11D8-474E-B4D7-BB82332C878E}.PGUpdate|x64.Build.0 = PGUpdate|x64
{41ADEDF9-11D8-474E-B4D7-BB82332C878E}.Release|Win32.ActiveCfg = Release|Win32
{41ADEDF9-11D8-474E-B4D7-BB82332C878E}.Release|Win32.Build.0 = Release|Win32
{41ADEDF9-11D8-474E-B4D7-BB82332C878E}.Release|x64.ActiveCfg = Release|x64
{41ADEDF9-11D8-474E-B4D7-BB82332C878E}.Release|x64.Build.0 = Release|x64
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
......
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"> <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<?define exts=pyexpat;select;unicodedata;winsound;_bz2;_elementtree;_socket;_ssl;_msi;_ctypes;_hashlib;_multiprocessing;_lzma;_decimal;_overlapped;_sqlite3;_asyncio;_queue;_distutils_findvs;_contextvars ?> <?define exts=pyexpat;select;unicodedata;winsound;_bz2;_elementtree;_socket;_ssl;_msi;_ctypes;_hashlib;_multiprocessing;_lzma;_decimal;_overlapped;_sqlite3;_asyncio;_queue;_contextvars ?>
<Fragment> <Fragment>
<ComponentGroup Id="lib_extensions"> <ComponentGroup Id="lib_extensions">
<?foreach ext in $(var.exts)?> <?foreach ext in $(var.exts)?>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment