mirror of
http://shenjack.top:5100/shenjack/icalingua-python-bot.git
synced 2025-02-23 01:39:58 +08:00
add -env
This commit is contained in:
parent
f8cd207923
commit
56a6c39df7
|
@ -1,7 +1,7 @@
|
|||
pub mod client;
|
||||
pub mod events;
|
||||
|
||||
use std::sync::OnceLock;
|
||||
// use std::sync::OnceLock;
|
||||
|
||||
use rust_socketio::asynchronous::{Client, ClientBuilder};
|
||||
use rust_socketio::{async_any_callback, async_callback};
|
||||
|
|
|
@ -108,6 +108,8 @@ const CLI_HELP_MSG: &str = r#"{VERSION}
|
|||
trace 模式
|
||||
-h
|
||||
显示帮助信息
|
||||
-env <env>
|
||||
指定虚拟环境路径
|
||||
-c <config_file_path>
|
||||
指定配置文件路径
|
||||
"#;
|
||||
|
|
|
@ -414,33 +414,20 @@ pub fn load_py_file(path: &PathBuf) -> std::io::Result<RawPyPlugin> {
|
|||
Ok((path.clone(), changed_time, content))
|
||||
}
|
||||
|
||||
/// Python 侧初始化
|
||||
pub fn init_py() {
|
||||
// 从 全局配置中获取 python 插件路径
|
||||
let span = span!(Level::INFO, "初始化 python 及其插件.ing");
|
||||
let _enter = span.enter();
|
||||
|
||||
let plugin_path = MainStatus::global_config().py().plugin_path;
|
||||
|
||||
// 根据 VIRTUAL_ENV 环境变量 进行一些处理
|
||||
match std::env::var("VIRTUAL_ENV") {
|
||||
Ok(virtual_env) => {
|
||||
event!(Level::INFO, "找到 VIRTUAL_ENV 环境变量: {} 正在初始化", virtual_env);
|
||||
// 使用 Py_InitializeFromConfig 初始化 python
|
||||
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);
|
||||
|
||||
#[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>>();
|
||||
let wide_path = OsStr::new(path).encode_wide().chain(Some(0)).collect::<Vec<u16>>();
|
||||
|
||||
// 设置 prefix 和 exec_prefix
|
||||
pyo3::ffi::PyConfig_SetString(
|
||||
|
@ -454,7 +441,7 @@ pub fn init_py() {
|
|||
wide_path.as_slice().as_ptr(),
|
||||
);
|
||||
|
||||
// init py
|
||||
// 使用 Py_InitializeFromConfig 初始化 python
|
||||
let status = pyo3::ffi::Py_InitializeFromConfig(&config as *const _);
|
||||
// 清理配置
|
||||
pyo3::ffi::PyConfig_Clear(config_ptr);
|
||||
|
@ -471,6 +458,31 @@ pub fn init_py() {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Python 侧初始化
|
||||
pub fn init_py() {
|
||||
// 从 全局配置中获取 python 插件路径
|
||||
let span = span!(Level::INFO, "初始化 python 及其插件.ing");
|
||||
let _enter = span.enter();
|
||||
|
||||
let plugin_path = MainStatus::global_config().py().plugin_path;
|
||||
|
||||
let cli_args = std::env::args().collect::<Vec<String>>();
|
||||
if cli_args.contains(&"-env".to_string()) {
|
||||
let env_path = cli_args.iter().find(|&arg| arg != "-env").expect("未找到 -env 参数的值");
|
||||
event!(Level::INFO, "找到 -env 参数: {} 正在初始化", env_path);
|
||||
// 判断一下是否有 VIRTUAL_ENV 环境变量
|
||||
if let Ok(virtual_env) = std::env::var("VIRTUAL_ENV") {
|
||||
event!(Level::WARN, "找到 VIRTUAL_ENV 环境变量: {} 将会被 -env 参数覆盖", virtual_env);
|
||||
}
|
||||
init_py_with_env_path(env_path);
|
||||
} else {
|
||||
// 根据 VIRTUAL_ENV 环境变量 进行一些处理
|
||||
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 环境变量, 正常初始化");
|
||||
|
@ -478,6 +490,7 @@ pub fn init_py() {
|
|||
event!(Level::INFO, "prepare_freethreaded_python 完成");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PyStatus::init();
|
||||
let plugin_path = PathBuf::from(plugin_path);
|
||||
|
|
Loading…
Reference in New Issue
Block a user