diff --git a/include/source.h b/include/source.h index 02b5c9b..c5eaf36 100644 --- a/include/source.h +++ b/include/source.h @@ -188,26 +188,6 @@ pl_ruby_sources[] = { }, -/** - * 2024-05-24 更新 - * - * @note 不要添加Zju,浙大的pypi在校外访问会自动转向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); diff --git a/src/chsrc.c b/src/chsrc.c index c3ce5b6..3d3976c 100644 --- a/src/chsrc.c +++ b/src/chsrc.c @@ -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)}, diff --git a/src/recipe/lang/python.c b/src/recipe/lang/python.c new file mode 100644 index 0000000..86d34b8 --- /dev/null +++ b/src/recipe/lang/python.c @@ -0,0 +1,152 @@ +/** ------------------------------------------------------------ + * SPDX-License-Identifier: GPL-3.0-or-later + * ------------------------------------------------------------- + * Contributors : Aoran Zeng + * Created on : <2023-09-01> + * Last modified : <2024-08-08> + * ------------------------------------------------------------*/ + +#include "xy.h" + +/** + * 2024-05-24 更新 + * + * @note 不要添加Zju,浙大的pypi在校外访问会自动转向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);