Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Correct Python check in case of Microsoft Store #125

Merged
merged 2 commits into from
Jan 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}