Merge pull request #477 from Flow-Launcher/switch_python_embeddable

dev branch: switch to python 3.8.9 embeddable
This commit is contained in:
Jeremy Wu 2021-06-19 14:04:29 +10:00 committed by GitHub
commit fd96d926c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 105 deletions

View file

@ -53,7 +53,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Droplex" Version="1.2.0" /> <PackageReference Include="Droplex" Version="1.3.1" />
<PackageReference Include="FSharp.Core" Version="4.7.1" /> <PackageReference Include="FSharp.Core" Version="4.7.1" />
<PackageReference Include="squirrel.windows" Version="1.5.2" /> <PackageReference Include="squirrel.windows" Version="1.5.2" />
</ItemGroup> </ItemGroup>

View file

@ -18,8 +18,6 @@ namespace Flow.Launcher.Core.Plugin
{ {
public static class PluginsLoader public static class PluginsLoader
{ {
public const string PATH = "PATH";
public const string Python = "python";
public const string PythonExecutable = "pythonw.exe"; public const string PythonExecutable = "pythonw.exe";
public static List<PluginPair> Plugins(List<PluginMetadata> metadatas, PluginsSettings settings) public static List<PluginPair> Plugins(List<PluginMetadata> metadatas, PluginsSettings settings)
@ -117,119 +115,65 @@ namespace Flow.Launcher.Core.Plugin
if (!source.Any(o => o.Language.ToUpper() == AllowedLanguage.Python)) if (!source.Any(o => o.Language.ToUpper() == AllowedLanguage.Python))
return new List<PluginPair>(); return new List<PluginPair>();
// Try setting Constant.PythonPath first, either from if (!string.IsNullOrEmpty(settings.PythonDirectory) && FilesFolders.LocationExists(settings.PythonDirectory))
// PATH or from the given pythonDirectory return SetPythonPathForPluginPairs(source, Path.Combine(settings.PythonDirectory, PythonExecutable));
if (string.IsNullOrEmpty(settings.PythonDirectory))
var pythonPath = string.Empty;
if (MessageBox.Show("Flow detected you have installed Python plugins, " +
"would you like to install Python to run them? " +
Environment.NewLine + Environment.NewLine +
"Click no if it's already installed, " +
"and you will be prompted to select the folder that contains the Python executable",
string.Empty, MessageBoxButtons.YesNo) == DialogResult.No
&& string.IsNullOrEmpty(settings.PythonDirectory))
{ {
var whereProcess = Process.Start(new ProcessStartInfo var dlg = new FolderBrowserDialog
{ {
FileName = "where.exe", SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
Arguments = "pythonw", };
RedirectStandardOutput = true,
CreateNoWindow = true,
UseShellExecute = false
});
var pythonPath = whereProcess?.StandardOutput.ReadToEnd().Trim(); var result = dlg.ShowDialog();
if (result == DialogResult.OK)
if (!string.IsNullOrEmpty(pythonPath))
{ {
pythonPath = FilesFolders.GetPreviousExistingDirectory(FilesFolders.LocationExists, pythonPath); string pythonDirectory = dlg.SelectedPath;
} if (!string.IsNullOrEmpty(pythonDirectory))
{
if (string.IsNullOrEmpty(pythonPath)) pythonPath = Path.Combine(pythonDirectory, PythonExecutable);
{ if (File.Exists(pythonPath))
var paths = Environment.GetEnvironmentVariable(PATH); {
settings.PythonDirectory = pythonDirectory;
pythonPath = paths? Constant.PythonPath = pythonPath;
.Split(';') }
.FirstOrDefault(p => p.ToLower().Contains(Python)); else
} {
MessageBox.Show("Can't find python in given directory");
if (!string.IsNullOrEmpty(pythonPath)) }
{ }
Constant.PythonPath = Path.Combine(pythonPath, PythonExecutable);
settings.PythonDirectory =
FilesFolders.GetPreviousExistingDirectory(FilesFolders.LocationExists, Constant.PythonPath);
}
else
{
Log.Error("PluginsLoader",
"Failed to set Python path despite the environment variable PATH is found",
"PythonPlugins");
} }
} }
else else
{ {
var path = Path.Combine(settings.PythonDirectory, PythonExecutable); var installedPythonDirectory = Path.Combine(DataLocation.DataDirectory(), "Python Embeddable");
if (File.Exists(path))
// Python 3.8.9 is used for Windows 7 compatibility
DroplexPackage.Drop(App.python_3_8_9_embeddable, installedPythonDirectory).Wait();
pythonPath = Path.Combine(installedPythonDirectory, PythonExecutable);
if (FilesFolders.FileExists(pythonPath))
{ {
Constant.PythonPath = path; settings.PythonDirectory = installedPythonDirectory;
Constant.PythonPath = pythonPath;
} }
else else
{ {
Log.Error("PluginsLoader", $"Tried to automatically set from Settings.PythonDirectory " + Log.Error("PluginsLoader",
$"but can't find python executable in {path}", "PythonPlugins"); $"Failed to set Python path after Droplex install, {pythonPath} does not exist",
"PythonPlugins");
} }
} }
if (string.IsNullOrEmpty(settings.PythonDirectory)) if (string.IsNullOrEmpty(settings.PythonDirectory) || string.IsNullOrEmpty(pythonPath))
{
if (MessageBox.Show("Flow detected you have installed Python plugins, " +
"would you like to install Python to run them? " +
Environment.NewLine + Environment.NewLine +
"Click no if it's already installed, " +
"and you will be prompted to select the folder that contains the Python executable",
string.Empty, MessageBoxButtons.YesNo) == DialogResult.No
&& string.IsNullOrEmpty(settings.PythonDirectory))
{
var dlg = new FolderBrowserDialog
{
SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
};
var result = dlg.ShowDialog();
if (result == DialogResult.OK)
{
string pythonDirectory = dlg.SelectedPath;
if (!string.IsNullOrEmpty(pythonDirectory))
{
var pythonPath = Path.Combine(pythonDirectory, PythonExecutable);
if (File.Exists(pythonPath))
{
settings.PythonDirectory = pythonDirectory;
Constant.PythonPath = pythonPath;
}
else
{
MessageBox.Show("Can't find python in given directory");
}
}
}
}
else
{
DroplexPackage.Drop(App.python3_9_1).Wait();
var installedPythonDirectory =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
@"Programs\Python\Python39");
var pythonPath = Path.Combine(installedPythonDirectory, PythonExecutable);
if (FilesFolders.FileExists(pythonPath))
{
settings.PythonDirectory = installedPythonDirectory;
Constant.PythonPath = pythonPath;
}
else
{
Log.Error("PluginsLoader",
$"Failed to set Python path after Droplex install, {pythonPath} does not exist",
"PythonPlugins");
}
}
}
if (string.IsNullOrEmpty(settings.PythonDirectory))
{ {
MessageBox.Show( MessageBox.Show(
"Unable to set Python executable path, please try from Flow's settings (scroll down to the bottom)."); "Unable to set Python executable path, please try from Flow's settings (scroll down to the bottom).");
@ -240,16 +184,20 @@ namespace Flow.Launcher.Core.Plugin
return new List<PluginPair>(); return new List<PluginPair>();
} }
return source return SetPythonPathForPluginPairs(source, pythonPath);
}
private static IEnumerable<PluginPair> SetPythonPathForPluginPairs(List<PluginMetadata> source, string pythonPath)
=> source
.Where(o => o.Language.ToUpper() == AllowedLanguage.Python) .Where(o => o.Language.ToUpper() == AllowedLanguage.Python)
.Select(metadata => new PluginPair .Select(metadata => new PluginPair
{ {
Plugin = new PythonPlugin(Constant.PythonPath), Metadata = metadata Plugin = new PythonPlugin(pythonPath),
Metadata = metadata
}) })
.ToList(); .ToList();
}
public static IEnumerable<PluginPair> ExecutablePlugins(IEnumerable<PluginMetadata> source) public static IEnumerable<PluginPair> ExecutablePlugins(IEnumerable<PluginMetadata> source)
{ {
return source return source
.Where(o => o.Language.ToUpper() == AllowedLanguage.Executable) .Where(o => o.Language.ToUpper() == AllowedLanguage.Executable)