From 0fb89b854b039cf1757ee06905b95b809e2ab7e0 Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Mon, 14 Jul 2025 17:32:09 +0800 Subject: [PATCH] Rewrite `uv` recipe using `rawstr4c` --- src/recipe/lang/Python/common.h | 4 ++- src/recipe/lang/Python/rawstr4c.h | 16 +++++++++ src/recipe/lang/Python/rawstr4c.md | 50 ++++++++++++++++++++++++++ src/recipe/lang/Python/uv.c | 57 ++++++++++++------------------ 4 files changed, 91 insertions(+), 36 deletions(-) create mode 100644 src/recipe/lang/Python/rawstr4c.h create mode 100644 src/recipe/lang/Python/rawstr4c.md diff --git a/src/recipe/lang/Python/common.h b/src/recipe/lang/Python/common.h index f1f7bad..2889e10 100644 --- a/src/recipe/lang/Python/common.h +++ b/src/recipe/lang/Python/common.h @@ -7,9 +7,11 @@ * | * Created On : <2023-09-03> * Major Revision : 1 - * Last Modified : <2025-07-11> + * Last Modified : <2025-07-14> * ------------------------------------------------------------*/ + #include "rawstr4c.h" + /** * 注意这个前缀是 host/pipi/web/pacakges.... * 下面有几个镜像站微调了这个路径,我们只要确认能找到 packages 目录就好 diff --git a/src/recipe/lang/Python/rawstr4c.h b/src/recipe/lang/Python/rawstr4c.h new file mode 100644 index 0000000..9ccea7d --- /dev/null +++ b/src/recipe/lang/Python/rawstr4c.h @@ -0,0 +1,16 @@ +#pragma once + +/** + * Generated by rawstr4c v0.1.0.1-2025/07/14 + * + * Date: 2025-07-14T17:28:23.336582+08:00 + */ + +char RAWSTR_pl_python_uv_config_source_content[] = "\x5b\x5b\x69\x6e\x64\x65\x78\x5d\x5d\x0a\x75\x72\x6c\x20\x3d\x20\x22\x40\x75\x72\x6c\x22\x0a\x64\x65\x66\x61\x75\x6c\x74\x20\x3d\x20\x74\x72\x75\x65\x0a\x0a"; + +char RAWSTR_pl_python_get_uv_config[] = "\x67\x72\x65\x70\x20\x2d\x41\x20\x32\x20\x27\x69\x6e\x64\x65\x78\x27\x20\x40\x66\x40\x20\x7c\x20\x73\x65\x64\x20\x2d\x6e\x20\x27\x73\x2f\x5e\x75\x72\x6c\x20\x3d\x20\x22\x5c\x28\x2e\x2a\x5c\x29\x22\x2f\x5c\x31\x2f\x70\x27\x0a"; + +char RAWSTR_pl_python_set_uv_config[] = "\x40\x73\x65\x64\x40\x20\x27\x2f\x5e\x5c\x5b\x5c\x5b\x69\x6e\x64\x65\x78\x5c\x5d\x5c\x5d\x24\x2f\x2c\x2f\x5e\x64\x65\x66\x61\x75\x6c\x74\x20\x3d\x20\x74\x72\x75\x65\x24\x2f\x7b\x73\x7c\x5e\x75\x72\x6c\x20\x3d\x20\x22\x2e\x2a\x22\x24\x7c\x75\x72\x6c\x20\x3d\x20\x22\x40\x75\x72\x6c\x40\x22\x7c\x7d\x27\x20\x40\x66\x40\x0a"; + +char RAWSTR_pl_python_final_uv_cmd[] = "\x67\x72\x65\x70\x20\x2d\x71\x20\x27\x5e\x5b\x5b\x69\x6e\x64\x65\x78\x5d\x5d\x24\x27\x20\x40\x66\x40\x20\x26\x26\x20\x40\x75\x63\x6d\x64\x40\x20\x7c\x7c\x20\x40\x61\x63\x6d\x64\x40\x0a"; + diff --git a/src/recipe/lang/Python/rawstr4c.md b/src/recipe/lang/Python/rawstr4c.md new file mode 100644 index 0000000..608dc52 --- /dev/null +++ b/src/recipe/lang/Python/rawstr4c.md @@ -0,0 +1,50 @@ + + +# rawstr4c input for Python + +- prefix = `RAWSTR_pl_python` +- output = `:global-variable-only-header` +- translate = `:hex` +- keep-postfix = `false` + + + +### uv config source content + +```toml +[[index]] +url = "@url" +default = true + +``` + + +### Get uv config + +```sh +grep -A 2 'index' @f@ | sed -n 's/^url = "\(.*\)"/\1/p' +``` + + +### Set uv config + +```sh +@sed@ '/^\[\[index\]\]$/,/^default = true$/{s|^url = ".*"$|url = "@url@"|}' @f@ +``` + + + +### Final uv cmd + +```sh +grep -q '^[[index]]$' @f@ && @ucmd@ || @acmd@ +``` diff --git a/src/recipe/lang/Python/uv.c b/src/recipe/lang/Python/uv.c index 3b62f2d..293feb4 100644 --- a/src/recipe/lang/Python/uv.c +++ b/src/recipe/lang/Python/uv.c @@ -6,7 +6,8 @@ * | Aoran Zeng * | * Created On : <2024-12-11> - * Last Modified : <2025-07-11> + * Major Revision : 1 + * Last Modified : <2025-07-14> * ------------------------------------------------------------*/ /** @@ -19,9 +20,9 @@ * 4. /etc/uv/uv.toml */ -#define UV_CONFIG "uv.toml" -#define UV_LOCAL_CONFIG_PATH "./" -#define UV_USER_CONFIG_PATH "~/.config/uv/" +#define PL_Python_uv_ConfigFile "uv.toml" +#define PL_Python_uv_Local_ConfigPath "./" +#define PL_Python_uv_User_ConfigPath "~/.config/uv/" char * @@ -29,7 +30,7 @@ pl_python_find_uv_config (bool mkdir) { if (chsrc_in_local_mode()) { - return xy_2strjoin (UV_LOCAL_CONFIG_PATH, UV_CONFIG); + return xy_2strjoin (PL_Python_uv_Local_ConfigPath, PL_Python_uv_ConfigFile); } else { @@ -49,16 +50,16 @@ pl_python_find_uv_config (bool mkdir) { chsrc_ensure_dir (config_dir); } - return xy_2strjoin (config_dir, UV_CONFIG); + return xy_2strjoin (config_dir, PL_Python_uv_ConfigFile); } else { /* config path on Linux or macOS */ if (mkdir) { - chsrc_ensure_dir (UV_USER_CONFIG_PATH); + chsrc_ensure_dir (PL_Python_uv_User_ConfigPath); } - return xy_2strjoin (UV_USER_CONFIG_PATH, UV_CONFIG); + return xy_2strjoin (PL_Python_uv_User_ConfigPath, PL_Python_uv_ConfigFile); } } } @@ -74,13 +75,8 @@ pl_python_uv_getsrc (char *option) return; } - /** - * grep -A 2 'index' config_file | sed -n 's/^url = "\(.*\)"/\1/p' - * 获取 [[index]] 配置项的 url - */ - char *cmd = xy_strjoin (3, "grep -A 2 'index' ", - uv_config, - " | sed -n 's/^url = \"\\(.*\\)\"/\\1/p'"); + /* 获取 [[index]] 配置项的 url */ + char *cmd = xy_str_gsub (RAWSTR_pl_python_get_uv_config, "@f@", uv_config); chsrc_run (cmd, RunOpt_Default); } @@ -108,26 +104,21 @@ pl_python_uv_setsrc (char *option) } chsrc_backup (uv_config); - const char *source_content = xy_strjoin (5, - "[[index]]\\n", - "url = \"", source.url, "\"\\n", - "default = true\\n"); + const char *source_content = xy_str_gsub (RAWSTR_pl_python_uv_config_source_content, "@url@", source.url); - /** - * sed -i '/^\[\[index\]\]$/,/^default = true$/{s|^url = ".*"$|url = " source.url "|}' uv_config - * 将 [[index]] 到 default = true 之间的 url = ".*" 替换为 url = "source.url" - */ #if defined(XY_On_macOS) || defined(XY_On_BSD) char *sed_cmd = "sed -i '' "; #else char *sed_cmd = "sed -i "; #endif - char *update_source_cmd = xy_strjoin (5, sed_cmd, - "'/^\\[\\[index\\]\\]$/,/^default = true$/{s|^url = \".*\"$|url = \"", - source.url, - "\"|;}' ", - uv_config); + /** + * 将 [[index]] 到 default = true 之间的 url = ".*" 替换为 url = "source.url" + */ + char *update_source_cmd = xy_str_gsub (RAWSTR_pl_python_set_uv_config, "@sed@", sed_cmd); + update_source_cmd = xy_str_gsub (update_source_cmd, "@f@", uv_config); + update_source_cmd = xy_str_gsub (update_source_cmd, "@url@", source.url); + char *append_source_cmd = xy_strjoin (4, "printf '", source_content, "' >> ", uv_config); char *cmd = NULL; @@ -150,14 +141,10 @@ pl_python_uv_setsrc (char *option) { /** * uv_config 存在,如果存在 [[index]] 则更新,否则追加到文件末尾 - * run: grep -q '^[[index]]$' uv_config && update_source_cmd || append_source_cmd */ - cmd = xy_strjoin (6, "grep -q '^\\[\\[index\\]\\]$' ", - uv_config, - " && ", - update_source_cmd, - " || ", - append_source_cmd); + cmd = xy_str_gsub (RAWSTR_pl_python_final_uv_cmd, "@f@", uv_config); + cmd = xy_str_gsub (cmd, "@ucmd@", update_source_cmd); + cmd = xy_str_gsub (cmd, "@acmd@", append_source_cmd); } } if (NULL==cmd)