docs: 更新文档导航和解析器指南

- 添加演练场(Playground)文档导航区到主 README
- 新增 Python 解析器文档链接(开发指南、测试报告、LSP集成)
- 更新前端版本号至 0.1.9b19p
- 补充 Python 解析器 requests 库使用章节和官方文档链接
- 添加 JavaScript 和 Python 解析器的语言版本和官方文档
- 优化文档结构,分类为项目文档和外部资源
This commit is contained in:
q
2026-01-11 22:35:45 +08:00
parent b8eee2b8a7
commit 2fcf9cfab1
60 changed files with 10132 additions and 436 deletions
@@ -0,0 +1,202 @@
package cn.qaiu.parser.custompy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Python 代码安全检查器
* 在执行前对代码进行静态分析,检测危险操作
*/
public class PyCodeSecurityChecker {
private static final Logger log = LoggerFactory.getLogger(PyCodeSecurityChecker.class);
/**
* 危险的导入模块
*/
private static final Set<String> DANGEROUS_IMPORTS = Set.of(
"subprocess", // 子进程执行
"socket", // 原始网络套接字
"ctypes", // C 语言接口
"_ctypes", // C 语言接口
"multiprocessing", // 多进程
"threading", // 多线程(可选禁止)
"asyncio", // 异步IO(可选禁止)
"pty", // 伪终端
"fcntl", // 文件控制
"resource", // 资源限制
"syslog", // 系统日志
"signal" // 信号处理
);
/**
* 危险的 os 模块方法
*/
private static final Set<String> DANGEROUS_OS_METHODS = Set.of(
"system", // 执行系统命令
"popen", // 打开进程管道
"spawn", // 生成进程
"spawnl", "spawnle", "spawnlp", "spawnlpe",
"spawnv", "spawnve", "spawnvp", "spawnvpe",
"exec", "execl", "execle", "execlp", "execlpe",
"execv", "execve", "execvp", "execvpe",
"fork", "forkpty",
"kill", "killpg",
"remove", "unlink",
"rmdir", "removedirs",
"mkdir", "makedirs",
"rename", "renames", "replace",
"chmod", "chown", "lchown",
"chroot",
"mknod", "mkfifo",
"link", "symlink"
);
/**
* 危险的内置函数
*/
private static final Set<String> DANGEROUS_BUILTINS = Set.of(
"exec", // 执行代码
"eval", // 评估表达式
"compile", // 编译代码
"__import__" // 动态导入
);
/**
* 检查代码安全性
* @param code Python 代码
* @return 安全检查结果
*/
public static SecurityCheckResult check(String code) {
if (code == null || code.trim().isEmpty()) {
return SecurityCheckResult.fail("代码为空");
}
List<String> violations = new ArrayList<>();
// 1. 检查危险导入
for (String module : DANGEROUS_IMPORTS) {
if (containsImport(code, module)) {
violations.add("禁止导入危险模块: " + module);
}
}
// 2. 检查危险的 os 方法调用
for (String method : DANGEROUS_OS_METHODS) {
if (containsOsMethodCall(code, method)) {
violations.add("禁止使用危险的 os 方法: os." + method + "()");
}
}
// 3. 检查危险的内置函数
for (String builtin : DANGEROUS_BUILTINS) {
if (containsBuiltinCall(code, builtin)) {
violations.add("禁止使用危险的内置函数: " + builtin + "()");
}
}
// 4. 检查危险的文件操作模式
if (containsDangerousFileOperation(code)) {
violations.add("禁止使用危险的文件写入操作");
}
if (violations.isEmpty()) {
return SecurityCheckResult.pass();
} else {
return SecurityCheckResult.fail(String.join("; ", violations));
}
}
/**
* 检查是否包含指定模块的导入
*/
private static boolean containsImport(String code, String module) {
// 匹配: import module / from module import xxx
String pattern1 = "(?m)^\\s*import\\s+" + Pattern.quote(module) + "\\b";
String pattern2 = "(?m)^\\s*from\\s+" + Pattern.quote(module) + "\\s+import";
return Pattern.compile(pattern1).matcher(code).find() ||
Pattern.compile(pattern2).matcher(code).find();
}
/**
* 检查是否包含指定的 os 方法调用
*/
private static boolean containsOsMethodCall(String code, String method) {
// 匹配: os.method(
String pattern = "\\bos\\s*\\.\\s*" + Pattern.quote(method) + "\\s*\\(";
return Pattern.compile(pattern).matcher(code).find();
}
/**
* 检查是否包含指定的内置函数调用
*/
private static boolean containsBuiltinCall(String code, String builtin) {
// 匹配: builtin( 但排除方法调用 xxx.builtin(
String pattern = "(?<!\\.)\\b" + Pattern.quote(builtin) + "\\s*\\(";
return Pattern.compile(pattern).matcher(code).find();
}
/**
* 检查是否包含危险的文件操作
*/
private static boolean containsDangerousFileOperation(String code) {
// 检查 open() 的写入模式
Pattern openPattern = Pattern.compile("\\bopen\\s*\\([^)]*['\"][wax+]['\"]");
if (openPattern.matcher(code).find()) {
return true;
}
// 检查直接的文件写入
Pattern writePattern = Pattern.compile("\\.write\\s*\\(|\\.writelines\\s*\\(");
if (writePattern.matcher(code).find()) {
// 需要进一步判断是否是文件写入而不是 response 写入等
// 这里简单处理,如果有 write 调用但没有 requests/http 相关的上下文,则禁止
if (!code.contains("requests") && !code.contains("http")) {
return true;
}
}
return false;
}
/**
* 安全检查结果
*/
public static class SecurityCheckResult {
private final boolean passed;
private final String message;
private SecurityCheckResult(boolean passed, String message) {
this.passed = passed;
this.message = message;
}
public static SecurityCheckResult pass() {
return new SecurityCheckResult(true, null);
}
public static SecurityCheckResult fail(String message) {
return new SecurityCheckResult(false, message);
}
public boolean isPassed() {
return passed;
}
public String getMessage() {
return message;
}
@Override
public String toString() {
return passed ? "PASSED" : "FAILED: " + message;
}
}
}
@@ -3,25 +3,33 @@ package cn.qaiu.parser.custompy;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Engine;
import org.graalvm.polyglot.HostAccess;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.io.IOAccess;
import org.graalvm.python.embedding.utils.GraalPyResources;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.List;
import java.util.ArrayList;
/**
* GraalPy Context 池化管理器
* 提供共享的 Engine 实例和 Context 池化支持
* 支持真正的 pip 包(如 requests
*
* <p>特性:
* <ul>
* <li>共享单个 Engine 实例,减少内存占用和启动时间</li>
* <li>Context 对象池,避免重复创建和销毁的开销</li>
* <li>支持真正的 pip 包(通过 GraalPy Resources</li>
* <li>支持安全的沙箱配置</li>
* <li>线程安全的池化管理</li>
* <li>支持优雅关闭和资源清理</li>
* <li>路径缓存,避免重复检测文件系统</li>
* <li>预热机制,在后台预导入常用模块</li>
* </ul>
*
* @author QAIU
@@ -30,11 +38,15 @@ public class PyContextPool {
private static final Logger log = LoggerFactory.getLogger(PyContextPool.class);
// 池化配置
private static final int INITIAL_POOL_SIZE = 2;
// 池化配置 - 增加初始池大小和延长生命周期
private static final int INITIAL_POOL_SIZE = 4;
private static final int MAX_POOL_SIZE = 10;
private static final long CONTEXT_TIMEOUT_MS = 30000; // 30秒获取超时
private static final long CONTEXT_MAX_AGE_MS = 300000; // 5分钟最大使用时间
private static final long CONTEXT_MAX_AGE_MS = 900000; // 15分钟最大使用时间
// 路径缓存 - 避免重复检测文件系统
private static volatile List<String> cachedValidPaths = null;
private static final Object PATH_CACHE_LOCK = new Object();
// 单例实例
private static volatile PyContextPool instance;
@@ -226,22 +238,64 @@ public class PyContextPool {
/**
* 预热Context池
* 在后台线程中预创建 Context 并预导入常用模块
*/
private void warmup() {
log.info("开始预热 Context 池,目标数量: {}", INITIAL_POOL_SIZE);
// 使用线程池并行预热
for (int i = 0; i < INITIAL_POOL_SIZE; i++) {
try {
PooledContext pc = createPooledContext();
if (!contextPool.offer(pc)) {
pc.forceClose();
final int index = i;
pythonExecutor.submit(() -> {
try {
long start = System.currentTimeMillis();
PooledContext pc = createPooledContext();
// 预导入 requests 模块(主要耗时点)
try {
warmupContext(pc.getContext());
} catch (Exception e) {
log.debug("预热 Context {} 导入模块失败(非首个Context的NativeModules限制): {}",
index, e.getMessage());
}
if (!contextPool.offer(pc)) {
pc.forceClose();
} else {
long elapsed = System.currentTimeMillis() - start;
log.info("预热 Context {} 完成,耗时: {}ms", index, elapsed);
}
} catch (Exception e) {
log.warn("预热 Context {} 失败: {}", index, e.getMessage());
}
} catch (Exception e) {
log.warn("预热Context失败: {}", e.getMessage());
}
});
}
}
/**
* 预热单个 Context - 预导入常用模块
*/
private void warmupContext(Context context) {
String warmupScript = """
# 预导入常用模块
import json
import re
import base64
import hashlib
import urllib.parse
# 尝试导入 requests(可能因 NativeModules 限制失败)
try:
import requests
except (ImportError, SystemError):
pass
""";
context.eval("python", warmupScript);
}
/**
* 创建新的池化Context
* 使用 GraalPyResources 支持 pip 包
*/
private PooledContext createPooledContext() {
if (closed.get()) {
@@ -250,9 +304,14 @@ public class PyContextPool {
Context context;
try {
// 首先尝试使用共享Engine创建
context = Context.newBuilder("python")
.engine(sharedEngine)
// 检查 VFS 资源是否存在
var vfsResource = getClass().getClassLoader().getResource("org.graalvm.python.vfs/venv");
log.info("GraalPy VFS资源检查: venv={}", vfsResource != null ? "存在" : "不存在");
// 使用 GraalPyResources 创建支持 pip 包的 Context
// 注意:不传入共享 Engine,让 GraalPyResources 管理自己的 Engine
log.info("正在创建 GraalPyResources Context...");
context = GraalPyResources.contextBuilder()
.allowHostAccess(HostAccess.newBuilder(HostAccess.EXPLICIT)
.allowArrayAccess(true)
.allowListAccess(true)
@@ -260,42 +319,21 @@ public class PyContextPool {
.allowIterableAccess(true)
.allowIteratorAccess(true)
.build())
.allowHostClassLookup(className -> false)
.allowExperimentalOptions(true)
.allowCreateThread(true)
.allowNativeAccess(false)
.allowCreateProcess(false)
.allowIO(IOAccess.newBuilder()
.allowHostFileAccess(false)
.allowHostSocketAccess(false)
.build())
.option("python.PythonHome", "")
.option("python.ForceImportSite", "false")
.build();
} catch (Exception e) {
log.warn("使用共享Engine创建Context失败,尝试不使用共享Engine: {}", e.getMessage());
// 不使用共享Engine作为备选
context = Context.newBuilder("python")
.allowHostAccess(HostAccess.newBuilder(HostAccess.EXPLICIT)
.allowArrayAccess(true)
.allowListAccess(true)
.allowMapAccess(true)
.allowIterableAccess(true)
.allowIteratorAccess(true)
.build())
.allowHostClassLookup(className -> false)
.allowExperimentalOptions(true)
.allowCreateThread(true)
.allowNativeAccess(false)
.allowCreateProcess(false)
.allowIO(IOAccess.newBuilder()
.allowHostFileAccess(false)
.allowHostSocketAccess(false)
.build())
// 允许 IO 以支持 pip 包加载和网络请求
.allowIO(IOAccess.ALL)
.allowNativeAccess(true)
.option("engine.WarnInterpreterOnly", "false")
.option("python.PythonHome", "")
.option("python.ForceImportSite", "false")
.build();
log.info("GraalPyResources Context 创建成功");
// 配置 Python 路径
setupPythonPath(context);
} catch (Exception e) {
log.error("使用GraalPyResources创建Context失败: {}", e.getMessage(), e);
throw new RuntimeException("无法创建支持pip包的Python Context: " + e.getMessage(), e);
}
createdCount.incrementAndGet();
@@ -363,31 +401,266 @@ public class PyContextPool {
/**
* 创建一个新的非池化Context(用于需要独立生命周期的场景)
* 调用者负责管理其生命周期
* 支持真正的 pip 包(如 requests, zlib 等)
*
* 注意:GraalPyResources 需要独立的 Engine,不能与共享 Engine 一起使用
*/
public Context createFreshContext() {
return Context.newBuilder("python")
.engine(sharedEngine)
.allowHostAccess(HostAccess.newBuilder(HostAccess.EXPLICIT)
.allowArrayAccess(true)
.allowListAccess(true)
.allowMapAccess(true)
.allowIterableAccess(true)
.allowIteratorAccess(true)
.build())
.allowHostClassLookup(className -> false)
.allowExperimentalOptions(true)
.allowCreateThread(true)
.allowNativeAccess(false)
.allowCreateProcess(false)
.allowIO(IOAccess.newBuilder()
.allowHostFileAccess(false)
.allowHostSocketAccess(false)
.build())
.option("python.PythonHome", "")
.option("python.ForceImportSite", "false")
.build();
try {
// 检查 VFS 资源是否存在
var vfsResource = getClass().getClassLoader().getResource("org.graalvm.python.vfs/venv");
var homeResource = getClass().getClassLoader().getResource("org.graalvm.python.vfs/home");
log.info("GraalPy VFS资源检查: venv={}, home={}",
vfsResource != null ? "存在" : "不存在",
homeResource != null ? "存在" : "不存在");
// 使用 GraalPyResources 创建支持 pip 包的 Context
// 注意:不传入共享 Engine,让 GraalPyResources 管理自己的 Engine
log.info("正在创建 GraalPyResources FreshContext...");
Context ctx = GraalPyResources.contextBuilder()
.allowHostAccess(HostAccess.newBuilder(HostAccess.EXPLICIT)
.allowArrayAccess(true)
.allowListAccess(true)
.allowMapAccess(true)
.allowIterableAccess(true)
.allowIteratorAccess(true)
.build())
.allowExperimentalOptions(true)
.allowCreateThread(true)
// 允许 IO 以支持 pip 包加载和网络请求
.allowIO(IOAccess.ALL)
.allowNativeAccess(true)
.option("engine.WarnInterpreterOnly", "false")
.build();
log.info("GraalPyResources FreshContext 创建成功");
// 手动配置 Python 路径以加载 VFS 中的 pip 包
setupPythonPath(ctx);
return ctx;
} catch (Exception e) {
log.error("使用GraalPyResources创建Context失败: {}", e.getMessage(), e);
throw new RuntimeException("无法创建支持pip包的Python Context: " + e.getMessage(), e);
}
}
/**
* 配置 Python 路径,确保能够加载 pip 包
* 使用路径缓存机制,避免重复检测文件系统
*
* pip 包安装在 src/main/resources/graalpy-packages/ 中,会打包进 jar。
* 运行时从 classpath 或文件系统加载。
*
* 注意:GraalPy 的 NativeModules 限制 - 只有进程中的第一个 Context 可以使用原生模块。
* 后续 Context 会回退到 LLVM 模式,这可能导致某些依赖原生模块的库无法正常工作。
*
* 安装方法:运行 parser/setup-graalpy-packages.sh
*/
private void setupPythonPath(Context context) {
try {
log.debug("配置 Python 环境...");
// 使用缓存的有效路径
List<String> validPaths = getValidPythonPaths();
if (validPaths.isEmpty()) {
log.warn("未找到有效的 Python 包路径");
return;
}
// 构建添加路径的脚本 - 使用已验证的路径,跳过文件系统检测
StringBuilder pathsJson = new StringBuilder("[");
boolean first = true;
for (String path : validPaths) {
if (!first) pathsJson.append(", ");
first = false;
pathsJson.append("'").append(path.replace("\\", "/").replace("'", "\\'")).append("'");
}
pathsJson.append("]");
// 简化的路径添加脚本 - 不再调用 os.path.isdir,直接添加已验证的路径
String addPathScript = String.format("""
import sys
_paths_to_add = %s
_added_paths = []
for path in _paths_to_add:
if path not in sys.path:
sys.path.insert(0, path)
_added_paths.append(path)
_added_paths_str = ', '.join(_added_paths) if _added_paths else ''
""", pathsJson);
context.eval("python", addPathScript);
Value bindings = context.getBindings("python");
String addedPaths = bindings.getMember("_added_paths_str").asString();
if (!addedPaths.isEmpty()) {
log.debug("添加的 Python 路径: {}", addedPaths);
}
// 验证 requests 是否可用(简化版,不阻塞)
// 注意:在多 Context 环境中,可能因 NativeModules 限制而失败
String verifyScript = """
import sys
_requests_available = False
_requests_version = ''
_error_msg = ''
_native_module_error = False
try:
import requests
_requests_available = True
_requests_version = requests.__version__
except SystemError as e:
# NativeModules 冲突 - GraalPy 限制
_error_msg = str(e)
if 'NativeModules' in _error_msg or 'llvm' in _error_msg:
_native_module_error = True
except ImportError as e:
_error_msg = str(e)
_sys_path_length = len(sys.path)
""";
context.eval("python", verifyScript);
boolean requestsAvailable = bindings.getMember("_requests_available").asBoolean();
boolean nativeModuleError = bindings.getMember("_native_module_error").asBoolean();
int pathLength = bindings.getMember("_sys_path_length").asInt();
if (requestsAvailable) {
String version = bindings.getMember("_requests_version").asString();
log.info("Python 环境配置完成: requests {} 可用, sys.path长度: {}", version, pathLength);
} else if (nativeModuleError) {
// GraalPy 的 NativeModules 限制 - 这是已知限制,不是配置错误
log.debug("Python 环境配置: requests 因 NativeModules 限制不可用 (非首个 Context). " +
"这是 GraalPy 的已知限制,标准库仍可正常使用。");
} else {
String error = bindings.getMember("_error_msg").asString();
log.warn("Python 环境配置: requests 不可用 ({}), sys.path长度: {}. " +
"请运行: ./setup-graalpy-packages.sh", error, pathLength);
}
} catch (Exception e) {
String msg = e.getMessage();
// 检查是否是 NativeModules 相关的错误
if (msg != null && (msg.contains("NativeModules") || msg.contains("llvm"))) {
log.debug("Python 环境配置: 因 NativeModules 限制跳过 requests 验证 (非首个 Context)");
} else {
log.warn("Python 环境配置失败,继续使用默认配置: {}", msg);
}
// 不抛出异常,允许 Context 继续使用
}
}
/**
* 设置安全的 OS 模块限制
* 只允许安全的读取操作,禁止危险的文件系统操作
*
* 注意:此方法应在所有必要的库导入完成后调用,
* 因为替换 os 模块会影响依赖它的库(如 requests)
*/
private void setupSecureOsModule(Context context) {
// 此方法当前禁用,因为会影响 requests 库的正常工作
// 安全限制将在代码执行层面实现,而不是替换系统模块
log.debug("OS 模块安全策略:通过代码审查实现,不替换系统模块");
}
/**
* 获取有效的 Python 包路径(带缓存)
* 首次调用时检测文件系统,后续直接返回缓存
*/
private List<String> getValidPythonPaths() {
if (cachedValidPaths != null) {
return cachedValidPaths;
}
synchronized (PATH_CACHE_LOCK) {
if (cachedValidPaths != null) {
return cachedValidPaths;
}
log.debug("首次检测 Python 包路径...");
long start = System.currentTimeMillis();
List<String> validPaths = new ArrayList<>();
String userDir = System.getProperty("user.dir");
// 尝试从 classpath 获取 graalpy-packages 路径
String classpathPackages = null;
try {
var resource = getClass().getClassLoader().getResource("graalpy-packages");
if (resource != null) {
classpathPackages = resource.getPath();
// 处理 jar 内路径
if (classpathPackages.contains("!")) {
classpathPackages = null; // jar 内无法直接作为文件系统路径
}
}
} catch (Exception e) {
log.debug("无法从 classpath 获取 graalpy-packages: {}", e.getMessage());
}
// 可能的 pip 包路径列表
String[] possiblePaths = {
classpathPackages,
userDir + "/resources/graalpy-packages",
userDir + "/src/main/resources/graalpy-packages",
userDir + "/parser/src/main/resources/graalpy-packages",
userDir + "/target/classes/graalpy-packages",
userDir + "/parser/target/classes/graalpy-packages",
userDir + "/graalpy-venv/lib/python3.11/site-packages",
userDir + "/parser/graalpy-venv/lib/python3.11/site-packages",
};
// 检测有效路径
for (String path : possiblePaths) {
if (path != null) {
java.io.File dir = new java.io.File(path);
if (dir.isDirectory()) {
validPaths.add(path);
}
}
}
long elapsed = System.currentTimeMillis() - start;
log.info("Python 包路径检测完成,耗时: {}ms,有效路径数: {}", elapsed, validPaths.size());
if (!validPaths.isEmpty()) {
log.debug("有效路径: {}", validPaths);
}
cachedValidPaths = validPaths;
return validPaths;
}
}
/**
* 安全策略说明:
*
* 由于 requests 等第三方库内部会使用 os 模块的功能,
* 直接替换 os 模块会导致这些库无法正常工作。
*
* 因此,安全控制通过以下方式实现:
* 1. 代码静态检查(在执行前扫描危险的 os.system 等调用)
* 2. 在 PyPlaygroundExecutor 中对用户代码进行预处理
* 3. 使用 GraalPy 的沙箱机制限制文件系统访问
*
* 禁止的操作:
* - os.system(), os.popen() - 系统命令执行
* - os.remove(), os.unlink(), os.rmdir() - 文件删除
* - os.mkdir(), os.makedirs() - 目录创建
* - subprocess.* - 子进程操作
*
* 允许的操作:
* - requests.* - HTTP 请求
* - os.path.* - 路径操作(只读)
* - os.getcwd() - 获取当前目录
* - json, re, base64, hashlib 等标准库
*/
/**
* 归还Context到池中
*/
@@ -71,7 +71,9 @@ public class PyParserExecutor implements IPanTool {
pyLogger.info("开始执行Python解析器: {}", config.getType());
return EXECUTOR.executeBlocking(() -> {
try (Context context = CONTEXT_POOL.createFreshContext()) {
// 使用池化的 Context,自动归还
try (PyContextPool.PooledContext pc = CONTEXT_POOL.acquire()) {
Context context = pc.getContext();
// 注入Java对象到Python环境
Value bindings = context.getBindings("python");
bindings.putMember("http", httpClient);
@@ -79,7 +81,7 @@ public class PyParserExecutor implements IPanTool {
bindings.putMember("share_link_info", shareLinkInfoWrapper);
bindings.putMember("crypto", cryptoUtils);
// 执行Python代码
// 执行Python代码(已支持真正的 pip 包如 requests, zlib 等)
context.eval("python", config.getPyCode());
// 调用parse函数
@@ -111,7 +113,9 @@ public class PyParserExecutor implements IPanTool {
pyLogger.info("开始执行Python文件列表解析: {}", config.getType());
return EXECUTOR.executeBlocking(() -> {
try (Context context = CONTEXT_POOL.createFreshContext()) {
// 使用池化的 Context,自动归还
try (PyContextPool.PooledContext pc = CONTEXT_POOL.acquire()) {
Context context = pc.getContext();
// 注入Java对象到Python环境
Value bindings = context.getBindings("python");
bindings.putMember("http", httpClient);
@@ -119,7 +123,7 @@ public class PyParserExecutor implements IPanTool {
bindings.putMember("share_link_info", shareLinkInfoWrapper);
bindings.putMember("crypto", cryptoUtils);
// 执行Python代码
// 执行Python代码(已支持真正的 pip 包)
context.eval("python", config.getPyCode());
// 调用parseFileList函数
@@ -145,7 +149,9 @@ public class PyParserExecutor implements IPanTool {
pyLogger.info("开始执行Python按ID解析: {}", config.getType());
return EXECUTOR.executeBlocking(() -> {
try (Context context = CONTEXT_POOL.createFreshContext()) {
// 使用池化的 Context,自动归还
try (PyContextPool.PooledContext pc = CONTEXT_POOL.acquire()) {
Context context = pc.getContext();
// 注入Java对象到Python环境
Value bindings = context.getBindings("python");
bindings.putMember("http", httpClient);
@@ -153,7 +159,7 @@ public class PyParserExecutor implements IPanTool {
bindings.putMember("share_link_info", shareLinkInfoWrapper);
bindings.putMember("crypto", cryptoUtils);
// 执行Python代码
// 执行Python代码(已支持真正的 pip 包)
context.eval("python", config.getPyCode());
// 调用parseById函数
@@ -67,11 +67,21 @@ public class PyPlaygroundExecutor {
public Future<String> executeParseAsync() {
Promise<String> promise = Promise.promise();
// 在执行前进行安全检查
PyCodeSecurityChecker.SecurityCheckResult securityResult = PyCodeSecurityChecker.check(pyCode);
if (!securityResult.isPassed()) {
playgroundLogger.errorJava("安全检查失败: " + securityResult.getMessage());
promise.fail(new SecurityException("代码安全检查失败: " + securityResult.getMessage()));
return promise.future();
}
playgroundLogger.debugJava("安全检查通过");
CompletableFuture<String> executionFuture = CompletableFuture.supplyAsync(() -> {
playgroundLogger.infoJava("开始执行parse方法");
// 使用池化的Context(每次执行创建新的Context以保证状态隔离)
try (Context context = CONTEXT_POOL.createFreshContext()) {
// 使用池化的 Context,自动归还
try (PyContextPool.PooledContext pc = CONTEXT_POOL.acquire()) {
Context context = pc.getContext();
// 注入Java对象到Python环境
Value bindings = context.getBindings("python");
bindings.putMember("http", httpClient);
@@ -79,7 +89,7 @@ public class PyPlaygroundExecutor {
bindings.putMember("share_link_info", shareLinkInfoWrapper);
bindings.putMember("crypto", cryptoUtils);
// 执行Python代码
// 执行Python代码(已支持真正的 pip 包如 requests, zlib 等)
playgroundLogger.debugJava("执行Python代码");
context.eval("python", pyCode);
@@ -104,8 +114,16 @@ public class PyPlaygroundExecutor {
throw new RuntimeException(errorMsg);
}
} catch (Exception e) {
playgroundLogger.errorJava("执行parse方法失败: " + e.getMessage(), e);
throw new RuntimeException(e);
String errorMsg = e.getMessage();
if (errorMsg == null || errorMsg.isEmpty()) {
errorMsg = e.getClass().getName();
if (e.getCause() != null) {
errorMsg += ": " + (e.getCause().getMessage() != null ?
e.getCause().getMessage() : e.getCause().getClass().getName());
}
}
playgroundLogger.errorJava("执行parse方法失败: " + errorMsg, e);
throw new RuntimeException(errorMsg, e);
}
}, CONTEXT_POOL.getPythonExecutor());
@@ -149,13 +167,16 @@ public class PyPlaygroundExecutor {
CompletableFuture<List<FileInfo>> executionFuture = CompletableFuture.supplyAsync(() -> {
playgroundLogger.infoJava("开始执行parse_file_list方法");
try (Context context = CONTEXT_POOL.createFreshContext()) {
// 使用池化的 Context,自动归还
try (PyContextPool.PooledContext pc = CONTEXT_POOL.acquire()) {
Context context = pc.getContext();
Value bindings = context.getBindings("python");
bindings.putMember("http", httpClient);
bindings.putMember("logger", playgroundLogger);
bindings.putMember("share_link_info", shareLinkInfoWrapper);
bindings.putMember("crypto", cryptoUtils);
// 执行Python代码(已支持真正的 pip 包)
context.eval("python", pyCode);
Value parseFileListFunc = bindings.getMember("parse_file_list");
@@ -211,13 +232,16 @@ public class PyPlaygroundExecutor {
CompletableFuture<String> executionFuture = CompletableFuture.supplyAsync(() -> {
playgroundLogger.infoJava("开始执行parse_by_id方法");
try (Context context = CONTEXT_POOL.createFreshContext()) {
// 使用池化的 Context,自动归还
try (PyContextPool.PooledContext pc = CONTEXT_POOL.acquire()) {
Context context = pc.getContext();
Value bindings = context.getBindings("python");
bindings.putMember("http", httpClient);
bindings.putMember("logger", playgroundLogger);
bindings.putMember("share_link_info", shareLinkInfoWrapper);
bindings.putMember("crypto", cryptoUtils);
// 执行Python代码(已支持真正的 pip 包)
context.eval("python", pyCode);
Value parseByIdFunc = bindings.getMember("parse_by_id");