From 48f5d76951b0795caf4b84a1ae45f58db37733a0 Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Wed, 30 Aug 2023 17:10:23 +0800 Subject: [PATCH] Add function to check if program exist --- chsrc.c | 47 +++++++++++++++++++++++++++++++++++++++-------- chsrc.h | 8 ++++++++ 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/chsrc.c b/chsrc.c index c418cde..67d999c 100644 --- a/chsrc.c +++ b/chsrc.c @@ -14,22 +14,30 @@ #define Chsrc_Version "v0.1.0" -/* 检测二进制命令是否存在 +/** + * 检测二进制程序是否存在 + * + * @param[in] check_cmd 检测 `progname` 是否存在的一段命令,一般来说,填 `progname` 本身即可, + * 但是某些情况下,需要使用其他命令绕过一些特殊情况,比如 python 这个命令在Windows上 + * 会自动打开 Microsoft Store,需避免 + * + * @param[in] progname 要检测的二进制程序名 */ bool -does_the_command_exist (char* check_cmd) +does_the_program_exist (char* check_cmd, char* progname) { char* which = check_cmd; int ret = system(which); char buf[32] = {0}; - sprintf(buf, "Error code: %d", ret); + sprintf(buf, "错误码: %d", ret); if (0!=ret) { - xy_warn (xy_strjoin("Not exist with ", buf)); + xy_warn (xy_strjoin(progname, xy_strjoin(" 命令不存在,", buf))); return false; } else { + xy_success (xy_strjoin(progname, " 命令存在")); return true; } } @@ -39,11 +47,34 @@ does_the_command_exist (char* check_cmd) * Python换源 * * 参考:https://mirrors.tuna.tsinghua.edu.cn/help/pypi/ + * + * 经测试,Windows上调用换源命令,会写入 C:\Users\RubyMetric\AppData\Roaming\pip\pip.ini */ void pl_python_chsrc (char* option) { - int selected = 0; + int selected = 0; char* check_cmd, *prog = NULL; + + // 不要调用 python 自己,而是使用 python --version,避免Windows弹出Microsoft Store + if (xy_on_windows) check_cmd = "python --version >nul 2>nul"; + else check_cmd = "python --version 1>/dev/null 2>&1"; + + bool exist_b = does_the_program_exist (check_cmd, "python"); + + if (!exist_b) { + if (xy_on_windows) check_cmd = "python3 --version >nul 2>nul"; + else check_cmd = "python3 --version 1>/dev/null 2>&1"; + exist_b = does_the_program_exist (check_cmd, "python3"); + if (exist_b) prog = "python3"; + } + else { + prog = "python"; + } + + if (!exist_b) { + xy_error ("chsrc: 未找到 Python 相关命令,请检查是否存在"); + return; + } for (int i=0;i +#include "helper.h" + typedef struct { const char* abbr; const char* name; @@ -68,3 +71,8 @@ pl_python_sources[] = { {&Jlu, "https://mirrors.jlu.edu.cn/pypi//web/simple"}, {&Bfsu, "https://mirrors.bfsu.edu.cn/pypi/web/simple"} }; + + + +/* 函数签名 */ +bool does_the_program_exist (char* check_cmd, char* progname);