mirror of
https://github.com/RubyMetric/chsrc
synced 2025-06-29 07:52:47 +08:00
Quiet and noisy at your disposal
This commit is contained in:
parent
31b4658477
commit
6831cfaede
@ -163,6 +163,12 @@ is_url (const char *str)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#define Quiet_When_Exist 0x00
|
||||||
|
#define Noisy_When_Exist 0x01
|
||||||
|
#define Quiet_When_NonExist 0x00
|
||||||
|
#define Noisy_When_NonExist 0x10
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检测二进制程序是否存在
|
* 检测二进制程序是否存在
|
||||||
*
|
*
|
||||||
@ -175,49 +181,84 @@ is_url (const char *str)
|
|||||||
* @translation Done
|
* @translation Done
|
||||||
*/
|
*/
|
||||||
bool
|
bool
|
||||||
query_program_exist (char *check_cmd, char *prog_name)
|
query_program_exist (char *check_cmd, char *prog_name, int mode)
|
||||||
{
|
{
|
||||||
char *which = check_cmd;
|
char *which = check_cmd;
|
||||||
|
|
||||||
int ret = system(which);
|
int status = system (which);
|
||||||
|
|
||||||
// char buf[32] = {0}; sprintf(buf, "错误码: %d", ret);
|
// char buf[32] = {0}; sprintf(buf, "错误码: %d", status);
|
||||||
|
|
||||||
char *msg = CliOpt_InEnglish ? "command" : "命令";
|
char *msg = CliOpt_InEnglish ? "command" : "命令";
|
||||||
|
|
||||||
if (0 != ret)
|
if (0 != status)
|
||||||
{
|
{
|
||||||
// xy_warn (xy_strjoin(4, "× 命令 ", progname, " 不存在,", buf));
|
if (mode & Noisy_When_NonExist)
|
||||||
log_check_result (prog_name, msg, false);
|
{
|
||||||
return false;
|
// xy_warn (xy_strjoin(4, "× 命令 ", progname, " 不存在,", buf));
|
||||||
|
log_check_result (prog_name, msg, false);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
log_check_result (prog_name, msg, true);
|
if (mode & Noisy_When_Exist)
|
||||||
|
log_check_result (prog_name, msg, true);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @note 此函数只能对接受 --version 选项的程序有效
|
* @note
|
||||||
|
* 1. 一般只在 Recipe 中使用,显式检测每一个需要用到的 program
|
||||||
|
* 2. 无论存在与否,**均输出**
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
bool
|
bool
|
||||||
chsrc_check_program (char *prog_name)
|
chsrc_check_program (char *prog_name)
|
||||||
{
|
{
|
||||||
char *quiet_cmd = xy_str_to_quietcmd (xy_2strjoin (prog_name, " --version"));
|
char *quiet_cmd = xy_str_to_quietcmd (xy_2strjoin (prog_name, " --version"));
|
||||||
return query_program_exist (quiet_cmd, prog_name);
|
return query_program_exist (quiet_cmd, prog_name, Noisy_When_Exist|Noisy_When_NonExist);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @note
|
||||||
|
* 1. 此函数没有强制性,只返回检查结果
|
||||||
|
* 2. 无论存在与否,**均不输出**
|
||||||
|
* 3. 此函数只能对接受 --version 选项的命令行程序有效
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
bool
|
||||||
|
chsrc_check_program_quietly (char *prog_name)
|
||||||
|
{
|
||||||
|
char *quiet_cmd = xy_str_to_quietcmd (xy_2strjoin (prog_name, " --version"));
|
||||||
|
return query_program_exist (quiet_cmd, prog_name, Quiet_When_Exist|Quiet_When_NonExist);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @note 存在时不输出,不存在时才输出
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
bool
|
||||||
|
chsrc_check_program_quietly_when_exist (char *prog_name)
|
||||||
|
{
|
||||||
|
char *quiet_cmd = xy_str_to_quietcmd (xy_2strjoin (prog_name, " --version"));
|
||||||
|
return query_program_exist (quiet_cmd, prog_name, Quiet_When_Exist|Noisy_When_NonExist);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @note 此函数具有强制性,检测不到就直接退出
|
* @note
|
||||||
|
* 1. 此函数具有强制性,检测不到就直接退出
|
||||||
|
* 2. 检查到存在时不输出,检查到不存在时输出
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
chsrc_ensure_program (char *prog_name)
|
chsrc_ensure_program (char *prog_name)
|
||||||
{
|
{
|
||||||
char *quiet_cmd = xy_str_to_quietcmd (xy_2strjoin (prog_name, " --version"));
|
char *quiet_cmd = xy_str_to_quietcmd (xy_2strjoin (prog_name, " --version"));
|
||||||
bool exist = query_program_exist (quiet_cmd, prog_name);
|
bool exist = query_program_exist (quiet_cmd, prog_name, Quiet_When_Exist|Noisy_When_NonExist);
|
||||||
if (exist)
|
if (exist)
|
||||||
{
|
{
|
||||||
// OK, nothing should be done
|
// OK, nothing should be done
|
||||||
@ -455,8 +496,9 @@ int
|
|||||||
auto_select_ (SourceInfo *sources, size_t size, const char *target_name)
|
auto_select_ (SourceInfo *sources, size_t size, const char *target_name)
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
char *msg = CliOpt_InEnglish ? "Auto speed measuring..." : "自动测速中...";
|
char *msg = CliOpt_InEnglish ? "Auto speed measuring" : "自动测速中";
|
||||||
xy_log_brkt (App_Name, bdblue (CliOpt_InEnglish ? "MEASURE" : "测速"), msg);
|
xy_log_brkt (App_Name, bdpurple (CliOpt_InEnglish ? "MEASURE" : "测速"), msg);
|
||||||
|
say ("");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (0==size || 1==size)
|
if (0==size || 1==size)
|
||||||
@ -475,14 +517,15 @@ auto_select_ (SourceInfo *sources, size_t size, const char *target_name)
|
|||||||
bool only_one = false;
|
bool only_one = false;
|
||||||
if (2==size) only_one = true;
|
if (2==size) only_one = true;
|
||||||
|
|
||||||
char *check_curl = xy_str_to_quietcmd ("curl --version");
|
/** --------------------------------------------- */
|
||||||
bool exist_curl = query_program_exist (check_curl, "curl");
|
bool exist_curl = chsrc_check_program_quietly_when_exist ("curl");
|
||||||
if (!exist_curl)
|
if (!exist_curl)
|
||||||
{
|
{
|
||||||
char *msg = CliOpt_InEnglish ? "No curl, unable to measure speed" : "没有curl命令,无法测速";
|
char *msg = CliOpt_InEnglish ? "No curl, unable to measure speed" : "没有curl命令,无法测速";
|
||||||
chsrc_error (msg);
|
chsrc_error (msg);
|
||||||
exit (Exit_UserCause);
|
exit (Exit_UserCause);
|
||||||
}
|
}
|
||||||
|
/** --------------------------------------------- */
|
||||||
|
|
||||||
double speeds[size];
|
double speeds[size];
|
||||||
bool get_measured[size]; /* 是否测速了 */
|
bool get_measured[size]; /* 是否测速了 */
|
||||||
@ -560,6 +603,7 @@ auto_select_ (SourceInfo *sources, size_t size, const char *target_name)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* 汇总结束 */
|
/* 汇总结束 */
|
||||||
|
say ("");
|
||||||
|
|
||||||
/* DEBUG */
|
/* DEBUG */
|
||||||
// for (int i=0; i<size; i++)
|
// for (int i=0; i<size; i++)
|
||||||
@ -1038,6 +1082,9 @@ chsrc_backup (const char *path)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查过程中全程保持安静
|
||||||
|
*/
|
||||||
static char *
|
static char *
|
||||||
chsrc_get_cpuarch ()
|
chsrc_get_cpuarch ()
|
||||||
{
|
{
|
||||||
@ -1049,14 +1096,14 @@ chsrc_get_cpuarch ()
|
|||||||
xy_unimplement;
|
xy_unimplement;
|
||||||
}
|
}
|
||||||
|
|
||||||
exist = chsrc_check_program ("arch");
|
exist = chsrc_check_program_quietly ("arch");
|
||||||
if (exist)
|
if (exist)
|
||||||
{
|
{
|
||||||
ret = xy_run ("arch", 0, NULL);
|
ret = xy_run ("arch", 0, NULL);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
exist = chsrc_check_program ("uname");
|
exist = chsrc_check_program_quietly ("uname");
|
||||||
if (exist)
|
if (exist)
|
||||||
{
|
{
|
||||||
ret = xy_run ("uname -m", 0, NULL);
|
ret = xy_run ("uname -m", 0, NULL);
|
||||||
|
@ -35,7 +35,7 @@ void
|
|||||||
pl_go_check_cmd ()
|
pl_go_check_cmd ()
|
||||||
{
|
{
|
||||||
char *check_cmd = xy_str_to_quietcmd ("go version");
|
char *check_cmd = xy_str_to_quietcmd ("go version");
|
||||||
bool exist = query_program_exist (check_cmd, "go");
|
bool exist = query_program_exist (check_cmd, "go", Noisy_When_Exist|Noisy_When_NonExist);
|
||||||
|
|
||||||
if (!exist)
|
if (!exist)
|
||||||
{
|
{
|
||||||
|
@ -33,14 +33,9 @@ def_sources_n(pl_nodejs);
|
|||||||
void
|
void
|
||||||
pl_nodejs_check_cmd (bool *npm_exist, bool *yarn_exist, bool *pnpm_exist)
|
pl_nodejs_check_cmd (bool *npm_exist, bool *yarn_exist, bool *pnpm_exist)
|
||||||
{
|
{
|
||||||
char *check_cmd = xy_str_to_quietcmd ("npm -v");
|
*npm_exist = chsrc_check_program ("npm");
|
||||||
*npm_exist = query_program_exist (check_cmd, "npm");
|
*yarn_exist = chsrc_check_program ("yarn");
|
||||||
|
*pnpm_exist = chsrc_check_program ("yarn");
|
||||||
check_cmd = xy_str_to_quietcmd ("yarn -v");
|
|
||||||
*yarn_exist = query_program_exist (check_cmd, "yarn");
|
|
||||||
|
|
||||||
check_cmd = xy_str_to_quietcmd ("pnpm -v");
|
|
||||||
*pnpm_exist = query_program_exist (check_cmd, "pnpm");
|
|
||||||
|
|
||||||
if (!*npm_exist && !*yarn_exist && !*pnpm_exist)
|
if (!*npm_exist && !*yarn_exist && !*pnpm_exist)
|
||||||
{
|
{
|
||||||
|
@ -67,7 +67,7 @@ os_freebsd_setsrc (char *option)
|
|||||||
// @ccmywish: [2023-09-27] 据 @ykla , NJU的freebsd-ports源没有设置 Git,
|
// @ccmywish: [2023-09-27] 据 @ykla , NJU的freebsd-ports源没有设置 Git,
|
||||||
// 但是我认为由于使用Git还是要比非Git方便许多,我们尽可能坚持使用Git
|
// 但是我认为由于使用Git还是要比非Git方便许多,我们尽可能坚持使用Git
|
||||||
// 而 gitup 又要额外修改它自己的配置,比较麻烦
|
// 而 gitup 又要额外修改它自己的配置,比较麻烦
|
||||||
bool git_exist = query_program_exist (xy_str_to_quietcmd ("git version"), "git");
|
bool git_exist = query_program_exist (xy_str_to_quietcmd ("git version"), "git", Noisy_When_Exist|Noisy_When_NonExist);
|
||||||
if (git_exist)
|
if (git_exist)
|
||||||
{
|
{
|
||||||
if (xy_streql("nju",source.mirror->code))
|
if (xy_streql("nju",source.mirror->code))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user