Separate Python packagers

[GitHub #79]
This commit is contained in:
Aoran Zeng 2024-09-13 19:17:51 +08:00
parent 3ff2376351
commit 48cefa1b1d
No known key found for this signature in database
GPG Key ID: 8F8BA8488E10ED98
8 changed files with 199 additions and 176 deletions

View File

@ -13,13 +13,13 @@
* | Terrasse <terrasse@qq.com>
* |
* Created On : <2023-08-28>
* Last Modified : <2024-09-10>
* Last Modified : <2024-09-13>
*
* chsrc: Change Source
* ------------------------------------------------------------*/
#define Chsrc_Version "0.1.8.2"
#define Chsrc_Release_Date "2024/09/10"
#define Chsrc_Version "0.1.8.2.dev1"
#define Chsrc_Release_Date "2024/09/13"
#define Chsrc_Banner_Version "v" Chsrc_Version "-" Chsrc_Release_Date
#define Chsrc_Maintain_URL "https://github.com/RubyMetric/chsrc"
#define Chsrc_Maintain_URL2 "https://gitee.com/RubyMetric/chsrc"
@ -27,7 +27,12 @@
#include "chsrc-framework.h"
#include "recipe/lang/Ruby.c"
#include "recipe/lang/Python.c"
#include "recipe/lang/Python/common.h"
#include "recipe/lang/Python/pip.c"
#include "recipe/lang/Python/Poetry.c"
#include "recipe/lang/Python/PDM.c"
#include "recipe/lang/Python/Python.c"
#include "recipe/lang/Node.js/common.h"
#include "recipe/lang/Node.js/npm.c"

View File

@ -1,160 +0,0 @@
/** ------------------------------------------------------------
* SPDX-License-Identifier: GPL-3.0-or-later
* -------------------------------------------------------------
* File Authors : Aoran Zeng <ccmywish@qq.com>
* Contributors : Nil Null <nil@null.org>
* Created On : <2023-09-03>
* Last Modified : <2024-08-17>
* ------------------------------------------------------------*/
/**
* @time 2024-05-24
* @note Zjupypi在校外访问会自动转向Tuna
*/
static SourceInfo
pl_python_sources[] = {
{&Upstream, "https://pypi.org/simple"},
{&Bfsu, "https://mirrors.bfsu.edu.cn/pypi/web/simple"},
{&Lzuoss, "https://mirror.lzu.edu.cn/pypi/web/simple"},
{&Jlu, "https://mirrors.jlu.edu.cn/pypi/web/simple"},
{&Sjtug_Zhiyuan, "https://mirror.sjtu.edu.cn/pypi/web/simple"},
{&Tuna, "https://pypi.tuna.tsinghua.edu.cn/simple"},
{&Ali, "https://mirrors.aliyun.com/pypi/simple/"},
{&Tencent, "https://mirrors.cloud.tencent.com/pypi/simple"},
{&Huawei, "https://mirrors.huaweicloud.com/repository/pypi/simple"},
{&Hust, "https://mirrors.hust.edu.cn/pypi/web/simple"}
// {&Netease, "https://mirrors.163.com/.help/pypi.html"} // 不用24小时更新一次
};
def_sources_n(pl_python);
/**
* @param[out] prog Python NULL
*/
void
pl_python_check_cmd (char **prog, bool *poetry_exist, bool *pdm_exist)
{
*prog = NULL;
*pdm_exist = false;
*poetry_exist = false;
bool py_exist = false;
// 由于Python2和Python3的历史目前2024-06许多python命令实际上仍然是python2
// https://gitee.com/RubyMetric/chsrc/issues/I9VZL2
// 因此我们首先测试 python3
py_exist = chsrc_check_program ("python3");
if (py_exist) *prog = "python3";
else
{
// 不要调用 python 自己,而是使用 python --version避免Windows弹出Microsoft Store
py_exist = chsrc_check_program ("python");
if (py_exist) *prog = "python";
else
{
chsrc_error ("未找到 Python 相关命令,请检查是否存在");
exit (Exit_UserCause);
}
}
*poetry_exist = chsrc_check_program ("poetry");
*pdm_exist = chsrc_check_program ("pdm");
}
void
pl_python_getsrc (char *option)
{
char *prog = NULL;
bool pdm_exist = false,
poetry_exist = false;
pl_python_check_cmd (&prog, &poetry_exist, &pdm_exist);
char *cmd = xy_2strjoin (prog, " -m pip config get global.index-url");
chsrc_run (cmd, RunOpt_Default);
if (pdm_exist)
{
cmd = "pdm config --global pypi.url";
chsrc_run (cmd, RunOpt_Default);
}
if (poetry_exist)
{
chsrc_note2 ("poetry换源情况: 请查阅本项目 pyproject.toml 中 [[tool.poetry.source]]");
}
}
/**
* Python换源
* 1. https://mirrors.tuna.tsinghua.edu.cn/help/pypi/
* 2. https://github.com/RubyMetric/chsrc/issues/19
*
* Windows上调用换源命令 C:\Users\RubyMetric\AppData\Roaming\pip\pip.ini
*
* 3. Poetry: https://python-poetry.org/docs/repositories/#project-configuration
*/
void
pl_python_setsrc (char *option)
{
char *chsrc_type = xy_streql (option, ChsrcTypeReset) ? ChsrcTypeReset : ChsrcTypeAuto;
char *prog = NULL;
bool pdm_exist = false,
poetry_exist = false,
uv_exist = false; // 2024-08-08: uv 似乎暂时没有实现该功能
pl_python_check_cmd (&prog, &poetry_exist, &pdm_exist);
chsrc_yield_source_and_confirm (pl_python);
// 这里用的是 config --user会写入用户目录而不是项目目录
// GitHub#39
char *cmd = xy_2strjoin (prog, xy_2strjoin (" -m pip config --user set global.index-url ", source.url));
chsrc_run (cmd, RunOpt_Default);
if (pdm_exist)
{
char *where = " --global ";
if (CliOpt_Locally==true)
{
where = " --local ";
}
cmd = xy_strjoin (4, "pdm config", where, "pypi.url ", source.url);
chsrc_run (cmd, RunOpt_Default);
}
if (poetry_exist)
{
cmd = xy_2strjoin ("poetry source add my_mirror ", source.url);
chsrc_run (cmd, RunOpt_Default);
}
chsrc_conclude (&source, chsrc_type);
}
void
pl_python_resetsrc (char *option)
{
pl_python_setsrc (ChsrcTypeReset);
}
FeatInfo
pl_python_feat (char *option)
{
FeatInfo fi = {0};
fi.can_get = true;
fi.can_reset = true;
fi.stcan_locally = CanSemi;
fi.locally = "pip 不支持; pdm 支持 (From v0.1.6); poetry 默认使用项目级 (From v0.1.7.2)";
fi.can_english = false;
fi.can_user_define = true;
return fi;
}
def_target_gsrf(pl_python);

View File

@ -58,7 +58,7 @@ pl_python_pdm_setsrc (char *option)
else
cmd = xy_2strjoin ("pdm config --global pypi.url ", source.url);
chsrc_run (cmd, RunOpt_Default);
chsrc_run (cmd, RunOpt_No_Last_New_Line);
chsrc_conclude (&source, ChsrcTypeAuto);
}

View File

@ -57,7 +57,7 @@ pl_python_poetry_setsrc (char *option)
chsrc_note2 ("Poertry 仅支持项目级换源");
cmd = xy_2strjoin ("poetry source add my_mirror ", source.url);
chsrc_run (cmd, RunOpt_Default);
chsrc_run (cmd, RunOpt_No_Last_New_Line);
chsrc_conclude (&source, ChsrcTypeAuto);
}

View File

@ -0,0 +1,122 @@
/** ------------------------------------------------------------
* SPDX-License-Identifier: GPL-3.0-or-later
* -------------------------------------------------------------
* File Authors : Aoran Zeng <ccmywish@qq.com>
* Contributors : Nil Null <nil@null.org>
* Created On : <2023-09-03>
* Last Modified : <2024-09-13>
*
* 2024-08-08: uv
* ------------------------------------------------------------*/
/**
* @update 2024-05-24
* @note Zjupypi在校外访问会自动转向Tuna
*/
static SourceInfo
pl_python_sources[] = {
{&Upstream, "https://pypi.org/simple"},
{&Bfsu, "https://mirrors.bfsu.edu.cn/pypi/web/simple"},
{&Lzuoss, "https://mirror.lzu.edu.cn/pypi/web/simple"},
{&Jlu, "https://mirrors.jlu.edu.cn/pypi/web/simple"},
{&Sjtug_Zhiyuan, "https://mirror.sjtu.edu.cn/pypi/web/simple"},
{&Tuna, "https://pypi.tuna.tsinghua.edu.cn/simple"},
{&Ali, "https://mirrors.aliyun.com/pypi/simple/"},
{&Tencent, "https://mirrors.cloud.tencent.com/pypi/simple"},
{&Huawei, "https://mirrors.huaweicloud.com/repository/pypi/simple"},
{&Hust, "https://mirrors.hust.edu.cn/pypi/web/simple"}
// {&Netease, "https://mirrors.163.com/.help/pypi.html"} // 不用24小时更新一次
};
def_sources_n(pl_python);
void
pl_python_getsrc (char *option)
{
char *prog_name = NULL;
pl_python_get_py_program_name (&prog_name);
bool pdm_exist = false,
poetry_exist = false;
pl_python_check_unofficial_pkger (&poetry_exist, &pdm_exist);
if (prog_name)
{
pl_python_pip_setsrc (option);
say ("");
}
if (poetry_exist)
{
pl_python_poetry_setsrc (option);
say ("");
}
if (pdm_exist)
{
pl_python_pdm_setsrc (option);
}
}
void
pl_python_setsrc (char *option)
{
char *chsrc_type = xy_streql (option, ChsrcTypeReset) ? ChsrcTypeReset : ChsrcTypeAuto;
char *prog_name = NULL;
pl_python_get_py_program_name (&prog_name);
bool pdm_exist = false,
poetry_exist = false;
pl_python_check_unofficial_pkger (&poetry_exist, &pdm_exist);
// chsrc_yield_source_and_confirm (pl_python);
if (prog_name)
{
pl_python_pip_getsrc (option);
say ("");
}
if (poetry_exist)
{
pl_python_poetry_getsrc (option);
say ("");
}
if (pdm_exist)
{
pl_python_pdm_getsrc (option);
}
// chsrc_conclude (&source, chsrc_type);
}
void
pl_python_resetsrc (char *option)
{
pl_python_setsrc (ChsrcTypeReset);
}
FeatInfo
pl_python_feat (char *option)
{
FeatInfo fi = {0};
fi.can_get = true;
fi.can_reset = true;
fi.stcan_locally = CanSemi;
fi.locally = "pip 不支持,其他支持";
fi.can_english = false;
fi.can_user_define = true;
return fi;
}
def_target_gsrf(pl_python);

View File

@ -0,0 +1,47 @@
/** ------------------------------------------------------------
* SPDX-License-Identifier: GPL-3.0-or-later
* -------------------------------------------------------------
* File Authors : Aoran Zeng <ccmywish@qq.com>
* Contributors : Nul None <nul@none.org>
* Created On : <2023-09-03>
* Major Revision : 1
* Last Modified : <2024-09-13>
* ------------------------------------------------------------*/
void
pl_python_check_unofficial_pkger (bool *poetry_exist, bool *pdm_exist)
{
*poetry_exist = chsrc_check_program ("poetry");
*pdm_exist = chsrc_check_program ("pdm");
}
/**
* @param[out] prog Python NULL
*/
void
pl_python_get_py_program_name (char **prog_name)
{
*prog_name = NULL;
bool py_exist = false;
// 由于Python2和Python3的历史目前2024-06许多python命令实际上仍然是python2
// https://gitee.com/RubyMetric/chsrc/issues/I9VZL2
// 因此我们首先测试 python3
py_exist = chsrc_check_program ("python3");
if (py_exist) *prog_name = "python3";
else
{
// 不要调用 python 自己,而是使用 python --version避免Windows弹出Microsoft Store
py_exist = chsrc_check_program ("python");
if (py_exist) *prog_name = "python";
else
{
chsrc_error ("未找到 Python 相关命令,请检查是否存在");
exit (Exit_UserCause);
}
}
}

View File

@ -59,7 +59,7 @@ pl_python_pip_setsrc (char *option)
// https://github.com/RubyMetric/chsrc/issues/39
// 经测试Windows上调用换源命令会写入 C:\Users\RubyMetric\AppData\Roaming\pip\pip.ini
char *cmd = xy_2strjoin (py_prog_name, xy_2strjoin (" -m pip config --user set global.index-url ", source.url));
chsrc_run (cmd, RunOpt_Default);
chsrc_run (cmd, RunOpt_No_Last_New_Line);
chsrc_conclude (&source, ChsrcTypeAuto);
}

View File

@ -5,14 +5,17 @@
* Contributors : Nil Null <nil@null.org>
* Created On : <2023-09-01>
* Major Revision : 1
* Last Modified : <2024-09-10>
* Last Modified : <2024-09-13>
* ------------------------------------------------------------*/
/* Begin Target Matrix */
#define t(a) (const char*)(a)
static const char
*pl_ruby [] = {"gem", "ruby", "rubygem", "rb", "rubygems", "bundler", NULL, t(&pl_ruby_target)},
*pl_python[] = {"pip", "python", "pypi", "py", "poetry", "pdm", NULL, t(&pl_python_target)},
*pl_ruby [] = {"gem", "ruby", "rb", "rubygem", "rubygems", "bundler", NULL, t(&pl_ruby_target)},
*pl_python[] = {"python", "pypi", "py", NULL, t(&pl_python_target)},
*pl_python_pip[] = {"pip", NULL, t(&pl_python_pip_target)},
*pl_python_poetry[] = {"poetry", NULL, t(&pl_python_poetry_target)},
*pl_python_pdm[] = {"pdm", NULL, t(&pl_python_pdm_target)},
*pl_nodejs[] = {"node", "nodejs", NULL, t(&pl_nodejs_target)},
*pl_nodejs_npm[] = {"npm", NULL, t(&pl_nodejs_npm_target)},
@ -34,12 +37,18 @@ static const char
*pl_julia [] = {"julia", NULL, t(&pl_julia_target)},
**pl_packagers[] =
{
pl_ruby, pl_python,
pl_nodejs, pl_nodejs_npm, pl_nodejs_pnpm, pl_nodejs_yarn,
pl_perl, pl_php, pl_lua,
pl_rust, pl_go, /*pl_nuget,*/ pl_java, pl_clojure, pl_dart,
pl_haskell, pl_ocaml,
pl_r, pl_julia
pl_ruby,
pl_python, pl_python_pip, pl_python_poetry, pl_python_pdm,
pl_nodejs, pl_nodejs_npm, pl_nodejs_pnpm, pl_nodejs_yarn,
pl_perl, pl_php,
pl_lua,
pl_rust, pl_go,
/*pl_nuget,*/
pl_java, pl_clojure,
pl_dart,
pl_ocaml,
pl_r, pl_julia,
pl_haskell,
};