2024-12-05 08:18:13 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Diagnostics;
|
2016-06-21 23:42:24 +00:00
|
|
|
|
using System.IO;
|
2023-03-26 19:04:06 +00:00
|
|
|
|
using System.Text.Json;
|
2021-02-08 06:37:37 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2020-04-21 09:12:17 +00:00
|
|
|
|
using Flow.Launcher.Infrastructure;
|
|
|
|
|
|
using Flow.Launcher.Plugin;
|
2013-12-20 17:20:17 +00:00
|
|
|
|
|
2020-04-21 09:12:17 +00:00
|
|
|
|
namespace Flow.Launcher.Core.Plugin
|
2013-12-20 17:20:17 +00:00
|
|
|
|
{
|
2022-09-01 02:37:29 +00:00
|
|
|
|
internal class PythonPlugin : JsonRPCPlugin
|
2013-12-20 17:20:17 +00:00
|
|
|
|
{
|
2016-01-08 01:55:24 +00:00
|
|
|
|
private readonly ProcessStartInfo _startInfo;
|
2013-12-20 17:20:17 +00:00
|
|
|
|
|
2016-05-05 00:57:03 +00:00
|
|
|
|
public PythonPlugin(string filename)
|
2014-07-18 12:00:55 +00:00
|
|
|
|
{
|
2016-01-08 01:55:24 +00:00
|
|
|
|
_startInfo = new ProcessStartInfo
|
|
|
|
|
|
{
|
2016-05-05 11:30:23 +00:00
|
|
|
|
FileName = filename,
|
2016-01-08 01:55:24 +00:00
|
|
|
|
UseShellExecute = false,
|
|
|
|
|
|
CreateNoWindow = true,
|
|
|
|
|
|
RedirectStandardOutput = true,
|
2016-06-21 23:42:24 +00:00
|
|
|
|
RedirectStandardError = true,
|
2016-01-08 01:55:24 +00:00
|
|
|
|
};
|
2016-06-21 23:42:24 +00:00
|
|
|
|
|
2022-09-01 02:38:10 +00:00
|
|
|
|
var path = Path.Combine(Constant.ProgramDirectory, JsonRPC);
|
2016-06-21 23:42:24 +00:00
|
|
|
|
_startInfo.EnvironmentVariables["PYTHONPATH"] = path;
|
2024-12-05 11:37:54 +00:00
|
|
|
|
// Prevent Python from writing .py[co] files.
|
|
|
|
|
|
// Because .pyc contains location infos which will prevent python portable.
|
|
|
|
|
|
_startInfo.EnvironmentVariables["PYTHONDONTWRITEBYTECODE"] = "1";
|
2016-06-21 23:42:24 +00:00
|
|
|
|
|
2021-11-30 08:36:38 +00:00
|
|
|
|
_startInfo.EnvironmentVariables["FLOW_VERSION"] = Constant.Version;
|
|
|
|
|
|
_startInfo.EnvironmentVariables["FLOW_PROGRAM_DIRECTORY"] = Constant.ProgramDirectory;
|
|
|
|
|
|
_startInfo.EnvironmentVariables["FLOW_APPLICATION_DIRECTORY"] = Constant.ApplicationDirectory;
|
2014-07-18 12:00:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-13 05:55:07 +00:00
|
|
|
|
protected override Task<Stream> RequestAsync(JsonRPCRequestModel request, CancellationToken token = default)
|
2014-01-10 16:19:14 +00:00
|
|
|
|
{
|
2023-03-26 19:21:36 +00:00
|
|
|
|
_startInfo.ArgumentList[2] = JsonSerializer.Serialize(request, RequestSerializeOption);
|
2021-06-07 05:03:59 +00:00
|
|
|
|
|
2021-02-08 06:37:37 +00:00
|
|
|
|
return ExecuteAsync(_startInfo, token);
|
2014-02-09 12:55:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-13 05:55:07 +00:00
|
|
|
|
protected override string Request(JsonRPCRequestModel rpcRequest, CancellationToken token = default)
|
2014-02-09 12:55:18 +00:00
|
|
|
|
{
|
2022-10-23 21:47:09 +00:00
|
|
|
|
// since this is not static, request strings will build up in ArgumentList if index is not specified
|
2023-03-26 19:21:36 +00:00
|
|
|
|
_startInfo.ArgumentList[2] = JsonSerializer.Serialize(rpcRequest, RequestSerializeOption);
|
2022-09-01 02:34:47 +00:00
|
|
|
|
_startInfo.WorkingDirectory = Context.CurrentPluginMetadata.PluginDirectory;
|
2021-02-08 06:37:37 +00:00
|
|
|
|
// TODO: Async Action
|
2021-02-08 06:43:17 +00:00
|
|
|
|
return Execute(_startInfo);
|
2013-12-20 17:20:17 +00:00
|
|
|
|
}
|
2024-12-05 08:18:13 +00:00
|
|
|
|
|
2021-08-16 16:50:36 +00:00
|
|
|
|
public override async Task InitAsync(PluginInitContext context)
|
2021-06-07 05:03:59 +00:00
|
|
|
|
{
|
2024-12-05 08:18:13 +00:00
|
|
|
|
// Run .py files via `-c <code>`
|
|
|
|
|
|
if (context.CurrentPluginMetadata.ExecuteFilePath.EndsWith(".py", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
{
|
|
|
|
|
|
var rootDirectory = context.CurrentPluginMetadata.PluginDirectory;
|
|
|
|
|
|
var libDirectory = Path.Combine(rootDirectory, "lib");
|
2025-02-02 09:27:02 +00:00
|
|
|
|
var libPyWin32Directory = Path.Combine(libDirectory, "win32");
|
|
|
|
|
|
var libPyWin32LibDirectory = Path.Combine(libPyWin32Directory, "lib");
|
2024-12-05 08:18:13 +00:00
|
|
|
|
var pluginDirectory = Path.Combine(rootDirectory, "plugin");
|
|
|
|
|
|
|
|
|
|
|
|
// This makes it easier for plugin authors to import their own modules.
|
|
|
|
|
|
// They won't have to add `.`, `./lib`, or `./plugin` to their sys.path manually.
|
|
|
|
|
|
// Instead of running the .py file directly, we pass the code we want to run as a CLI argument.
|
|
|
|
|
|
// This code sets sys.path for the plugin author and then runs the .py file via runpy.
|
|
|
|
|
|
_startInfo.ArgumentList.Add("-c");
|
|
|
|
|
|
_startInfo.ArgumentList.Add(
|
|
|
|
|
|
$"""
|
|
|
|
|
|
import sys
|
|
|
|
|
|
sys.path.append(r'{rootDirectory}')
|
|
|
|
|
|
sys.path.append(r'{libDirectory}')
|
2025-02-02 09:27:02 +00:00
|
|
|
|
sys.path.append(r'{libPyWin32LibDirectory}')
|
|
|
|
|
|
sys.path.append(r'{libPyWin32Directory}')
|
2024-12-05 08:18:13 +00:00
|
|
|
|
sys.path.append(r'{pluginDirectory}')
|
|
|
|
|
|
|
|
|
|
|
|
import runpy
|
|
|
|
|
|
runpy.run_path(r'{context.CurrentPluginMetadata.ExecuteFilePath}', None, '__main__')
|
|
|
|
|
|
"""
|
|
|
|
|
|
);
|
|
|
|
|
|
// Plugins always expect the JSON data to be in the third argument
|
|
|
|
|
|
// (we're always setting it as _startInfo.ArgumentList[2] = ...).
|
|
|
|
|
|
_startInfo.ArgumentList.Add("");
|
|
|
|
|
|
}
|
|
|
|
|
|
// Run .pyz files as is
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2024-12-05 11:37:54 +00:00
|
|
|
|
// No need for -B flag because we're using PYTHONDONTWRITEBYTECODE env variable now,
|
|
|
|
|
|
// but the plugins still expect data to be sent as the third argument, so we're keeping
|
|
|
|
|
|
// the flag here, even though it's not necessary anymore.
|
2024-12-05 08:18:13 +00:00
|
|
|
|
_startInfo.ArgumentList.Add("-B");
|
|
|
|
|
|
_startInfo.ArgumentList.Add(context.CurrentPluginMetadata.ExecuteFilePath);
|
|
|
|
|
|
// Plugins always expect the JSON data to be in the third argument
|
|
|
|
|
|
// (we're always setting it as _startInfo.ArgumentList[2] = ...).
|
|
|
|
|
|
_startInfo.ArgumentList.Add("");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-16 16:50:36 +00:00
|
|
|
|
await base.InitAsync(context);
|
2021-08-13 22:51:11 +00:00
|
|
|
|
_startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory;
|
2021-06-07 05:03:59 +00:00
|
|
|
|
}
|
2013-12-20 17:20:17 +00:00
|
|
|
|
}
|
2021-11-30 08:36:38 +00:00
|
|
|
|
}
|