Support poetry for Python

[Gitee #IAFCJI]
This commit is contained in:
Aoran Zeng 2024-08-08 19:08:47 +08:00
parent eb41991060
commit d17ad2b464
No known key found for this signature in database
GPG Key ID: 8F8BA8488E10ED98
3 changed files with 155 additions and 123 deletions

View File

@ -188,26 +188,6 @@ pl_ruby_sources[] = {
},
/**
* 2024-05-24
*
* @note Zjupypi在校外访问会自动转向Tuna
*/
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小时更新一次
},
/**
* 2024-04-18
@ -963,7 +943,7 @@ wr_tex_sources[] = {
#define def_sources_n(t) const size_t t##_sources_n = xy_arylen(t##_sources)
def_sources_n(pl_ruby); def_sources_n(pl_python); def_sources_n(pl_nodejs);
def_sources_n(pl_ruby); def_sources_n(pl_nodejs);
def_sources_n(pl_perl); def_sources_n(pl_php); def_sources_n(pl_lua);
def_sources_n(pl_go); def_sources_n(pl_rust);
def_sources_n(pl_java); def_sources_n(pl_clojure);

View File

@ -87,106 +87,7 @@ pl_ruby_resetsrc (char *option)
}
/**
* @param[out] prog Python NULL
* @param[out] pdm_exist pdm
*/
void
pl_python_check_cmd (char **prog, bool *pdm_exist)
{
*prog = NULL;
*pdm_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);
}
}
*pdm_exist = chsrc_check_program ("pdm");
}
void
pl_python_getsrc (char *option)
{
char *prog = NULL;
bool pdm_exist = false;
pl_python_check_cmd (&prog, &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);
}
}
/**
* 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
*/
void
pl_python_setsrc (char *option)
{
char *chsrc_type = xy_streql (option, ChsrcTypeReset) ? ChsrcTypeReset : ChsrcTypeAuto;
char *prog = NULL;
bool pdm_exist = false;
pl_python_check_cmd (&prog, &pdm_exist);
SourceInfo source;
chsrc_yield_source (pl_python);
chsrc_confirm_source (&source);
// 这里用的是 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 (Cli_Option_Locally==true)
{
where = " --local ";
}
cmd = xy_strjoin (4, "pdm config", where, "pypi.url ", source.url);
chsrc_run (cmd, RunOpt_Default);
}
chsrc_say_lastly (&source, chsrc_type);
}
void
pl_python_resetsrc (char *option)
{
pl_python_setsrc (ChsrcTypeReset);
}
#include "recipe/lang/python.c"
void
pl_nodejs_check_cmd (bool *npm_exist, bool *yarn_exist, bool *pnpm_exist)
@ -2356,7 +2257,6 @@ wr_anaconda_setsrc (char *option)
/************************************** Begin Target Matrix ****************************************/
def_target_full(pl_ruby);
def_target_full(pl_python);
def_target(pl_nodejs); def_target(pl_perl); def_target(pl_php);
def_target(pl_lua);
def_target(pl_rust); def_target(pl_go); def_target(pl_java); def_target(pl_dart); def_target(pl_ocaml);
@ -2369,7 +2269,7 @@ def_target_noget (pl_haskell);
#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", "pdm", NULL, t(&pl_python_target)},
*pl_python[] = {"pip", "python", "pypi", "py", "poetry", "pdm", NULL, t(&pl_python_target)},
*pl_nodejs[] = {"npm", "node", "nodejs", "js", "yarn", "pnpm", NULL, t(&pl_nodejs_target)},
*pl_perl [] = {"perl", "cpan", NULL, t(&pl_perl_target)},
*pl_php [] = {"php", "composer", NULL, t(&pl_php_target)},

152
src/recipe/lang/python.c Normal file
View File

@ -0,0 +1,152 @@
/** ------------------------------------------------------------
* SPDX-License-Identifier: GPL-3.0-or-later
* -------------------------------------------------------------
* Contributors : Aoran Zeng <ccmywish@qq.com>
* Created on : <2023-09-01>
* Last modified : <2024-08-08>
* ------------------------------------------------------------*/
#include "xy.h"
/**
* 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;
pl_python_check_cmd (&prog, &poetry_exist, &pdm_exist);
SourceInfo source;
chsrc_yield_source (pl_python);
chsrc_confirm_source (&source);
// 这里用的是 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 (Cli_Option_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_say_lastly (&source, chsrc_type);
}
void
pl_python_resetsrc (char *option)
{
pl_python_setsrc (ChsrcTypeReset);
}
def_target_full(pl_python);