diff --git a/back/config/util.ts b/back/config/util.ts index c9b939eb..cb9c65c3 100644 --- a/back/config/util.ts +++ b/back/config/util.ts @@ -535,16 +535,30 @@ export async function setSystemTimezone(timezone: string): Promise { } } +// Helper function to check if a name is a GitHub URL +function isGitHubUrl(name: string): boolean { + return !!name.match(/^(https?:\/\/|git\+)/i); +} + +// Helper function to check if a name is a requirements file +function isRequirementsFile(name: string): boolean { + return !!name.match(/requirements.*\.(txt|in)$/i) || name === 'requirements.txt'; +} + +// Helper function to check if a name is a pyproject.toml file +function isPyprojectToml(name: string): boolean { + return name.endsWith('pyproject.toml'); +} + export function getGetCommand(type: DependenceTypes, name: string): string { const trimmedName = name.trim(); // For Python dependencies installed from GitHub or requirements files, // we can't reliably check if they're installed, so skip the check if (type === DependenceTypes.python3) { - if (trimmedName.match(/^(https?:\/\/|git\+)/i) || - trimmedName.match(/\.(txt|in)$/i) || - trimmedName.includes('requirements') || - trimmedName.endsWith('pyproject.toml')) { + if (isGitHubUrl(trimmedName) || + isRequirementsFile(trimmedName) || + isPyprojectToml(trimmedName)) { // Return a command that will always indicate not installed // This ensures GitHub URLs and requirements files are always installed return 'echo ""'; @@ -590,18 +604,24 @@ export function getInstallCommand(type: DependenceTypes, name: string): string { // Handle different installation methods for Python if (type === DependenceTypes.python3) { // Check if it's a GitHub URL (support both git+ and direct URLs) - if (trimmedName.match(/^(https?:\/\/|git\+)/i)) { + if (isGitHubUrl(trimmedName)) { return `${command} ${trimmedName}`; } // Check if it's a requirements file path - if (trimmedName.match(/\.(txt|in)$/i) || trimmedName.includes('requirements')) { + if (isRequirementsFile(trimmedName)) { return `${command} -r ${trimmedName}`; } // Check if it's a pyproject.toml file - if (trimmedName.endsWith('pyproject.toml')) { + if (isPyprojectToml(trimmedName)) { // For pyproject.toml, install from the directory containing it - const dir = trimmedName.replace(/\/pyproject\.toml$/, ''); - return dir && dir !== 'pyproject.toml' ? `${command} ${dir}` : `${command} .`; + const pathMatch = trimmedName.match(/^(.+)\/pyproject\.toml$/); + if (pathMatch) { + // Has a path prefix, use the directory + return `${command} ${pathMatch[1]}`; + } else { + // Just "pyproject.toml", install current directory + return `${command} .`; + } } } diff --git a/shell/update.sh b/shell/update.sh index fb340c11..be7cd5c3 100755 --- a/shell/update.sh +++ b/shell/update.sh @@ -124,22 +124,29 @@ auto_install_python_deps() { if [[ -f "${repo_path}/requirements.txt" ]]; then echo -e "发现 requirements.txt,开始自动安装依赖...\n" local req_file="${dir_scripts}/${uniq_path}/requirements.txt" - cp -f "${repo_path}/requirements.txt" "${req_file}" - # 调用API添加依赖安装任务 - local dep_name="${uniq_path}/requirements.txt" - local currentTimeStamp=$(date +%s) - local result=$(curl -s --noproxy "*" "http://127.0.0.1:${ql_port}/open/dependencies?t=$currentTimeStamp" \ - -X POST \ - -H "Content-Type: application/json;charset=UTF-8" \ - -H "Authorization: Bearer ${__ql_token__}" \ - --data-raw "[{\"name\":\"${dep_name}\",\"type\":1,\"remark\":\"自动检测:${uniq_path} 订阅依赖\"}]" 2>/dev/null) + # 确保目标目录存在 + make_dir "${dir_scripts}/${uniq_path}" - local code=$(echo "$result" | jq -r '.code' 2>/dev/null) - if [[ "$code" == "200" ]]; then - echo -e "已添加 requirements.txt 依赖安装任务\n" + # 复制文件并检查结果 + if cp -f "${repo_path}/requirements.txt" "${req_file}" 2>/dev/null; then + # 调用API添加依赖安装任务 + local dep_name="${uniq_path}/requirements.txt" + local currentTimeStamp=$(date +%s) + local result=$(curl -s --noproxy "*" "http://127.0.0.1:${ql_port}/open/dependencies?t=$currentTimeStamp" \ + -X POST \ + -H "Content-Type: application/json;charset=UTF-8" \ + -H "Authorization: Bearer ${__ql_token__}" \ + --data-raw "[{\"name\":\"${dep_name}\",\"type\":1,\"remark\":\"自动检测:${uniq_path} 订阅依赖\"}]" 2>/dev/null) + + local code=$(echo "$result" | jq -r '.code' 2>/dev/null) + if [[ "$code" == "200" ]]; then + echo -e "已添加 requirements.txt 依赖安装任务\n" + else + echo -e "添加 requirements.txt 依赖失败,请手动添加\n" + fi else - echo -e "添加 requirements.txt 依赖失败,请手动添加\n" + echo -e "复制 requirements.txt 失败,跳过自动安装\n" fi fi @@ -147,22 +154,29 @@ auto_install_python_deps() { if [[ -f "${repo_path}/pyproject.toml" ]]; then echo -e "发现 pyproject.toml,开始自动安装依赖...\n" local pyproject_file="${dir_scripts}/${uniq_path}/pyproject.toml" - cp -f "${repo_path}/pyproject.toml" "${pyproject_file}" - # 调用API添加依赖安装任务 - local dep_name="${uniq_path}/pyproject.toml" - local currentTimeStamp=$(date +%s) - local result=$(curl -s --noproxy "*" "http://127.0.0.1:${ql_port}/open/dependencies?t=$currentTimeStamp" \ - -X POST \ - -H "Content-Type: application/json;charset=UTF-8" \ - -H "Authorization: Bearer ${__ql_token__}" \ - --data-raw "[{\"name\":\"${dep_name}\",\"type\":1,\"remark\":\"自动检测:${uniq_path} 订阅依赖\"}]" 2>/dev/null) + # 确保目标目录存在 + make_dir "${dir_scripts}/${uniq_path}" - local code=$(echo "$result" | jq -r '.code' 2>/dev/null) - if [[ "$code" == "200" ]]; then - echo -e "已添加 pyproject.toml 依赖安装任务\n" + # 复制文件并检查结果 + if cp -f "${repo_path}/pyproject.toml" "${pyproject_file}" 2>/dev/null; then + # 调用API添加依赖安装任务 + local dep_name="${uniq_path}/pyproject.toml" + local currentTimeStamp=$(date +%s) + local result=$(curl -s --noproxy "*" "http://127.0.0.1:${ql_port}/open/dependencies?t=$currentTimeStamp" \ + -X POST \ + -H "Content-Type: application/json;charset=UTF-8" \ + -H "Authorization: Bearer ${__ql_token__}" \ + --data-raw "[{\"name\":\"${dep_name}\",\"type\":1,\"remark\":\"自动检测:${uniq_path} 订阅依赖\"}]" 2>/dev/null) + + local code=$(echo "$result" | jq -r '.code' 2>/dev/null) + if [[ "$code" == "200" ]]; then + echo -e "已添加 pyproject.toml 依赖安装任务\n" + else + echo -e "添加 pyproject.toml 依赖失败,请手动添加\n" + fi else - echo -e "添加 pyproject.toml 依赖失败,请手动添加\n" + echo -e "复制 pyproject.toml 失败,跳过自动安装\n" fi fi }