Skip to content

Commit

Permalink
feat: Correctly check for Python in case of Microsoft Store
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Jan 20, 2023
1 parent 884c1dc commit a1d74c9
Showing 1 changed file with 58 additions and 31 deletions.
89 changes: 58 additions & 31 deletions UnoCheck/Checkups/WindowsPythonInstallationCheckup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,66 @@
using DotNetCheck.Models;
using DotNetCheck.Solutions;
using Microsoft.Win32;
using System.Diagnostics;
using System.Threading.Tasks;

namespace DotNetCheck.Checkups
{
public class WindowsPythonInstallationCheckup : Checkup
{
public override string Id => "windowspyhtonInstallation";

public override string Title => "Windows Python Installation Checkup";

public override bool IsPlatformSupported(Platform platform) => platform == Platform.Windows;

public override async Task<DiagnosticResult> Examine(SharedState history)
{
RegistryKey pyLaucherKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\Python\PyLauncher", false);
string? path = pyLaucherKey?.GetValue(InstallDirKey)?.ToString();

if (pyLaucherKey is null || string.IsNullOrEmpty(path))
{
return await Task.FromResult(new DiagnosticResult(
Status.Error,
this,
new Suggestion("In order to build WebAssembly apps using AOT, you will need to install Python from Windows Store, or manually through Python's official site",
new PythonIsInstalledSolution())));
}
else
{
ReportStatus($"Python is installed in {path}.", Status.Ok);
}

return await Task.FromResult(DiagnosticResult.Ok(this));
}

private const string InstallDirKey = "InstallDir";
}
public class WindowsPythonInstallationCheckup : Checkup
{
public override string Id => "windowspyhtonInstallation";

public override string Title => "Windows Python Installation Checkup";

public override bool IsPlatformSupported(Platform platform) => platform == Platform.Windows;

public override async Task<DiagnosticResult> Examine(SharedState history)
{
var checkResult = RegistryCheck();
if (!checkResult.available)
{
// In case of Microsoft Store version, the registry key is not set.
// Fall back to where command.
checkResult = WhereCheck();
}

if (!checkResult.available)
{
return await Task.FromResult(new DiagnosticResult(
Status.Error,
this,
new Suggestion("In order to build WebAssembly apps using AOT, you will need to install Python from Microsoft Store, winget, or manually through Python's official site",
new PythonIsInstalledSolution())));
}
else
{
ReportStatus($"Python is installed in {checkResult.path}.", Status.Ok);
}

return await Task.FromResult(DiagnosticResult.Ok(this));
}

private const string InstallDirKey = "InstallDir";

private (bool available, string? path) RegistryCheck()
{
var pyLaucherKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\Python\PyLauncher", false);
string? path = pyLaucherKey?.GetValue(InstallDirKey)?.ToString();
var available = pyLaucherKey is not null && !string.IsNullOrEmpty(path);
return (available, path);
}

private (bool available, string? path) WhereCheck()
{
using Process process = new Process();
process.StartInfo.FileName = "where";
process.StartInfo.Arguments = "python";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();

string output = process.StandardOutput.ReadToEnd();
return (!string.IsNullOrEmpty(output), output);
}
}
}

0 comments on commit a1d74c9

Please sign in to comment.