mirror of
http://shenjack.top:5100/shenjack/icalingua-python-bot.git
synced 2025-02-23 10:24:21 +08:00
add -env
This commit is contained in:
parent
f8cd207923
commit
56a6c39df7
|
@ -1,7 +1,7 @@
|
||||||
pub mod client;
|
pub mod client;
|
||||||
pub mod events;
|
pub mod events;
|
||||||
|
|
||||||
use std::sync::OnceLock;
|
// use std::sync::OnceLock;
|
||||||
|
|
||||||
use rust_socketio::asynchronous::{Client, ClientBuilder};
|
use rust_socketio::asynchronous::{Client, ClientBuilder};
|
||||||
use rust_socketio::{async_any_callback, async_callback};
|
use rust_socketio::{async_any_callback, async_callback};
|
||||||
|
|
|
@ -108,6 +108,8 @@ const CLI_HELP_MSG: &str = r#"{VERSION}
|
||||||
trace 模式
|
trace 模式
|
||||||
-h
|
-h
|
||||||
显示帮助信息
|
显示帮助信息
|
||||||
|
-env <env>
|
||||||
|
指定虚拟环境路径
|
||||||
-c <config_file_path>
|
-c <config_file_path>
|
||||||
指定配置文件路径
|
指定配置文件路径
|
||||||
"#;
|
"#;
|
||||||
|
|
|
@ -414,6 +414,52 @@ pub fn load_py_file(path: &PathBuf) -> std::io::Result<RawPyPlugin> {
|
||||||
Ok((path.clone(), changed_time, content))
|
Ok((path.clone(), changed_time, content))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn init_py_with_env_path(path: &str) {
|
||||||
|
unsafe {
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
use std::os::unix::ffi::OsStrExt;
|
||||||
|
#[cfg(target_os = "windows")]
|
||||||
|
use std::os::windows::ffi::OsStrExt;
|
||||||
|
|
||||||
|
let mut config = std::mem::zeroed::<pyo3::ffi::PyConfig>();
|
||||||
|
let config_ptr = &mut config as *mut pyo3::ffi::PyConfig;
|
||||||
|
// 初始化配置
|
||||||
|
// pyo3::ffi::PyConfig_InitIsolatedConfig(config_ptr);
|
||||||
|
pyo3::ffi::PyConfig_InitPythonConfig(config_ptr);
|
||||||
|
|
||||||
|
let wide_path = OsStr::new(path).encode_wide().chain(Some(0)).collect::<Vec<u16>>();
|
||||||
|
|
||||||
|
// 设置 prefix 和 exec_prefix
|
||||||
|
pyo3::ffi::PyConfig_SetString(
|
||||||
|
config_ptr,
|
||||||
|
&mut config.prefix as *mut _,
|
||||||
|
wide_path.as_slice().as_ptr(),
|
||||||
|
);
|
||||||
|
pyo3::ffi::PyConfig_SetString(
|
||||||
|
config_ptr,
|
||||||
|
&mut config.exec_prefix as *mut _,
|
||||||
|
wide_path.as_slice().as_ptr(),
|
||||||
|
);
|
||||||
|
|
||||||
|
// 使用 Py_InitializeFromConfig 初始化 python
|
||||||
|
let status = pyo3::ffi::Py_InitializeFromConfig(&config as *const _);
|
||||||
|
// 清理配置
|
||||||
|
pyo3::ffi::PyConfig_Clear(config_ptr);
|
||||||
|
match status._type {
|
||||||
|
pyo3::ffi::_PyStatus_TYPE::_PyStatus_TYPE_OK => {
|
||||||
|
event!(Level::INFO, "根据配置初始化 python 完成");
|
||||||
|
}
|
||||||
|
pyo3::ffi::_PyStatus_TYPE::_PyStatus_TYPE_EXIT => {
|
||||||
|
event!(Level::ERROR, "初始化 python 时发生错误: EXIT");
|
||||||
|
}
|
||||||
|
pyo3::ffi::_PyStatus_TYPE::_PyStatus_TYPE_ERROR => {
|
||||||
|
event!(Level::ERROR, "初始化 python 时发生错误: ERROR");
|
||||||
|
pyo3::ffi::Py_ExitStatusException(status);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Python 侧初始化
|
/// Python 侧初始化
|
||||||
pub fn init_py() {
|
pub fn init_py() {
|
||||||
// 从 全局配置中获取 python 插件路径
|
// 从 全局配置中获取 python 插件路径
|
||||||
|
@ -422,60 +468,27 @@ pub fn init_py() {
|
||||||
|
|
||||||
let plugin_path = MainStatus::global_config().py().plugin_path;
|
let plugin_path = MainStatus::global_config().py().plugin_path;
|
||||||
|
|
||||||
// 根据 VIRTUAL_ENV 环境变量 进行一些处理
|
let cli_args = std::env::args().collect::<Vec<String>>();
|
||||||
match std::env::var("VIRTUAL_ENV") {
|
if cli_args.contains(&"-env".to_string()) {
|
||||||
Ok(virtual_env) => {
|
let env_path = cli_args.iter().find(|&arg| arg != "-env").expect("未找到 -env 参数的值");
|
||||||
event!(Level::INFO, "找到 VIRTUAL_ENV 环境变量: {} 正在初始化", virtual_env);
|
event!(Level::INFO, "找到 -env 参数: {} 正在初始化", env_path);
|
||||||
// 使用 Py_InitializeFromConfig 初始化 python
|
// 判断一下是否有 VIRTUAL_ENV 环境变量
|
||||||
unsafe {
|
if let Ok(virtual_env) = std::env::var("VIRTUAL_ENV") {
|
||||||
let mut config = std::mem::zeroed::<pyo3::ffi::PyConfig>();
|
event!(Level::WARN, "找到 VIRTUAL_ENV 环境变量: {} 将会被 -env 参数覆盖", virtual_env);
|
||||||
let config_ptr = &mut config as *mut pyo3::ffi::PyConfig;
|
|
||||||
// 初始化配置
|
|
||||||
// pyo3::ffi::PyConfig_InitIsolatedConfig(config_ptr);
|
|
||||||
pyo3::ffi::PyConfig_InitPythonConfig(config_ptr);
|
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
|
||||||
use std::os::unix::ffi::OsStrExt;
|
|
||||||
#[cfg(target_os = "windows")]
|
|
||||||
use std::os::windows::ffi::OsStrExt;
|
|
||||||
|
|
||||||
let wide_path =
|
|
||||||
OsStr::new(&virtual_env).encode_wide().chain(Some(0)).collect::<Vec<u16>>();
|
|
||||||
|
|
||||||
// 设置 prefix 和 exec_prefix
|
|
||||||
pyo3::ffi::PyConfig_SetString(
|
|
||||||
config_ptr,
|
|
||||||
&mut config.prefix as *mut _,
|
|
||||||
wide_path.as_slice().as_ptr(),
|
|
||||||
);
|
|
||||||
pyo3::ffi::PyConfig_SetString(
|
|
||||||
config_ptr,
|
|
||||||
&mut config.exec_prefix as *mut _,
|
|
||||||
wide_path.as_slice().as_ptr(),
|
|
||||||
);
|
|
||||||
|
|
||||||
// init py
|
|
||||||
let status = pyo3::ffi::Py_InitializeFromConfig(&config as *const _);
|
|
||||||
// 清理配置
|
|
||||||
pyo3::ffi::PyConfig_Clear(config_ptr);
|
|
||||||
match status._type {
|
|
||||||
pyo3::ffi::_PyStatus_TYPE::_PyStatus_TYPE_OK => {
|
|
||||||
event!(Level::INFO, "根据配置初始化 python 完成");
|
|
||||||
}
|
|
||||||
pyo3::ffi::_PyStatus_TYPE::_PyStatus_TYPE_EXIT => {
|
|
||||||
event!(Level::ERROR, "初始化 python 时发生错误: EXIT");
|
|
||||||
}
|
|
||||||
pyo3::ffi::_PyStatus_TYPE::_PyStatus_TYPE_ERROR => {
|
|
||||||
event!(Level::ERROR, "初始化 python 时发生错误: ERROR");
|
|
||||||
pyo3::ffi::Py_ExitStatusException(status);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Err(_) => {
|
init_py_with_env_path(env_path);
|
||||||
event!(Level::INFO, "未找到 VIRTUAL_ENV 环境变量, 正常初始化");
|
} else {
|
||||||
pyo3::prepare_freethreaded_python();
|
// 根据 VIRTUAL_ENV 环境变量 进行一些处理
|
||||||
event!(Level::INFO, "prepare_freethreaded_python 完成");
|
match std::env::var("VIRTUAL_ENV") {
|
||||||
|
Ok(virtual_env) => {
|
||||||
|
event!(Level::INFO, "找到 VIRTUAL_ENV 环境变量: {} 正在初始化", virtual_env);
|
||||||
|
init_py_with_env_path(&virtual_env);
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
event!(Level::INFO, "未找到 VIRTUAL_ENV 环境变量, 正常初始化");
|
||||||
|
pyo3::prepare_freethreaded_python();
|
||||||
|
event!(Level::INFO, "prepare_freethreaded_python 完成");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
5
news.md
5
news.md
|
@ -4,6 +4,11 @@
|
||||||
|
|
||||||
- 现在支持通过读取环境变量里的 `VIRTUAL_ENV` 来获取虚拟环境路径
|
- 现在支持通过读取环境变量里的 `VIRTUAL_ENV` 来获取虚拟环境路径
|
||||||
- 用于在虚拟环境中运行插件
|
- 用于在虚拟环境中运行插件
|
||||||
|
- 添加了 `-h` 参数
|
||||||
|
- 用于展示帮助信息
|
||||||
|
- 添加了 `-env` 参数
|
||||||
|
- 用于指定 python 插件的虚拟环境路径
|
||||||
|
- 会覆盖环境变量中的 `VIRTUAL_ENV`
|
||||||
- 现在加入了默认的配置文件路径 `./config.toml`
|
- 现在加入了默认的配置文件路径 `./config.toml`
|
||||||
|
|
||||||
## 0.8.1
|
## 0.8.1
|
||||||
|
|
Loading…
Reference in New Issue
Block a user