mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 03:07:04 +02:00
Pack python env to zip
This commit is contained in:
@@ -6,9 +6,13 @@ using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Windows.Forms;
|
||||
using Newtonsoft.Json;
|
||||
using Wox.Helper;
|
||||
using Wox.Helper.ErrorReporting;
|
||||
using Wox.JsonRPC;
|
||||
using Wox.Plugin;
|
||||
using MessageBox = System.Windows.MessageBox;
|
||||
|
||||
namespace Wox.PluginLoader
|
||||
{
|
||||
@@ -48,8 +52,8 @@ namespace Wox.PluginLoader
|
||||
{
|
||||
string actionReponse = ExecuteAction(result1.JsonRPCAction);
|
||||
JsonRPCRequestModel jsonRpcRequestModel = JsonConvert.DeserializeObject<JsonRPCRequestModel>(actionReponse);
|
||||
if (jsonRpcRequestModel != null
|
||||
&& string.IsNullOrEmpty(jsonRpcRequestModel.Method)
|
||||
if (jsonRpcRequestModel != null
|
||||
&& string.IsNullOrEmpty(jsonRpcRequestModel.Method)
|
||||
&& jsonRpcRequestModel.Method.StartsWith("Wox."))
|
||||
{
|
||||
ExecuteWoxAPI(jsonRpcRequestModel.Method.Substring(4), jsonRpcRequestModel.Parameters);
|
||||
@@ -63,8 +67,10 @@ namespace Wox.PluginLoader
|
||||
}
|
||||
return results;
|
||||
}
|
||||
catch
|
||||
catch (Exception e)
|
||||
{
|
||||
ErrorReporting.TryShowErrorMessageBox(e.Message, e);
|
||||
Wox.Helper.Log.Error(e.Message);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
@@ -97,22 +103,45 @@ namespace Wox.PluginLoader
|
||||
/// <param name="arguments"></param>
|
||||
/// <returns></returns>
|
||||
protected string Execute(string fileName, string arguments)
|
||||
{
|
||||
ProcessStartInfo start = new ProcessStartInfo();
|
||||
start.FileName = fileName;
|
||||
start.Arguments = arguments;
|
||||
start.UseShellExecute = false;
|
||||
start.CreateNoWindow = true;
|
||||
start.RedirectStandardOutput = true;
|
||||
start.RedirectStandardError = true;
|
||||
return Execute(start);
|
||||
}
|
||||
|
||||
protected string Execute(ProcessStartInfo startInfo)
|
||||
{
|
||||
try
|
||||
{
|
||||
ProcessStartInfo start = new ProcessStartInfo();
|
||||
start.FileName = fileName;
|
||||
start.Arguments = arguments;
|
||||
start.UseShellExecute = false;
|
||||
start.CreateNoWindow = true;
|
||||
start.RedirectStandardOutput = true;
|
||||
using (Process process = Process.Start(start))
|
||||
using (Process process = Process.Start(startInfo))
|
||||
{
|
||||
if (process != null)
|
||||
{
|
||||
using (StreamReader reader = process.StandardOutput)
|
||||
{
|
||||
return reader.ReadToEnd();
|
||||
string result = reader.ReadToEnd();
|
||||
if (result.StartsWith("DEBUG:"))
|
||||
{
|
||||
System.Windows.Forms.MessageBox.Show(new Form { TopMost = true }, result.Substring(6));
|
||||
return "";
|
||||
}
|
||||
if (string.IsNullOrEmpty(result))
|
||||
{
|
||||
using (StreamReader errorReader = process.StandardError)
|
||||
{
|
||||
string error = errorReader.ReadToEnd();
|
||||
if (!string.IsNullOrEmpty(error))
|
||||
{
|
||||
ErrorReporting.TryShowErrorMessageBox(error, new WoxJsonPRCException(error));
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Wox.Helper;
|
||||
using Wox.Infrastructure.Storage.UserSettings;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.PluginLoader
|
||||
@@ -26,6 +27,7 @@ namespace Wox.PluginLoader
|
||||
forker.Fork(() => pair.Plugin.Init(new PluginInitContext()
|
||||
{
|
||||
CurrentPluginMetadata = pair.Metadata,
|
||||
Proxy = HttpProxy.Instance,
|
||||
API = App.Window
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using Wox.Helper;
|
||||
using Wox.JsonRPC;
|
||||
using Wox.Plugin;
|
||||
|
||||
@@ -8,26 +10,56 @@ namespace Wox.PluginLoader
|
||||
public class PythonPlugin : BasePlugin
|
||||
{
|
||||
private static string woxDirectory = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
|
||||
private ProcessStartInfo startInfo;
|
||||
|
||||
public override string SupportedLanguage
|
||||
{
|
||||
get { return AllowedLanguage.Python; }
|
||||
}
|
||||
|
||||
public PythonPlugin()
|
||||
{
|
||||
startInfo = new ProcessStartInfo
|
||||
{
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true
|
||||
};
|
||||
string additionalPythonPath = string.Format("{0};{1}",
|
||||
Path.Combine(woxDirectory, "PythonHome\\DLLs"),
|
||||
Path.Combine(woxDirectory, "PythonHome\\thirdparty"));
|
||||
if (!startInfo.EnvironmentVariables.ContainsKey("PYTHONPATH"))
|
||||
{
|
||||
|
||||
startInfo.EnvironmentVariables.Add("PYTHONPATH", additionalPythonPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
startInfo.EnvironmentVariables["PYTHONPATH"] = additionalPythonPath;
|
||||
}
|
||||
}
|
||||
|
||||
protected override string ExecuteQuery(Query query)
|
||||
{
|
||||
string fileName = Path.Combine(woxDirectory, "PythonHome\\pythonw.exe");
|
||||
string parameters = string.Format("{0} \"{1}\"", context.CurrentPluginMetadata.ExecuteFilePath,
|
||||
string.Format(@"{{\""method\"": \""query\"", \""parameters\"": [\""{0}\""]}}",query.GetAllRemainingParameter()));
|
||||
|
||||
return Execute(fileName, parameters);
|
||||
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel()
|
||||
{
|
||||
Method = "query",
|
||||
Parameters = new object[] { query.GetAllRemainingParameter() },
|
||||
HttpProxy = HttpProxy.Instance
|
||||
};
|
||||
//Add -B flag to tell python don't write .py[co] files. Because .pyc contains location infos which will prevent python portable
|
||||
startInfo.FileName = Path.Combine(woxDirectory, "PythonHome\\pythonw.exe");
|
||||
startInfo.Arguments = string.Format("-B {0} \"{1}\"", context.CurrentPluginMetadata.ExecuteFilePath, request);
|
||||
|
||||
return Execute(startInfo);
|
||||
}
|
||||
|
||||
protected override string ExecuteAction(JsonRPCRequestModel rpcRequest)
|
||||
{
|
||||
string fileName = Path.Combine(woxDirectory, "PythonHome\\pythonw.exe");
|
||||
string parameters = string.Format("{0} \"{1}\"", context.CurrentPluginMetadata.ExecuteFilePath,rpcRequest);
|
||||
return Execute(fileName, parameters);
|
||||
startInfo.FileName = Path.Combine(woxDirectory, "PythonHome\\pythonw.exe");
|
||||
startInfo.Arguments = string.Format("-B {0} \"{1}\"", context.CurrentPluginMetadata.ExecuteFilePath, rpcRequest);
|
||||
return Execute(startInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user