-
-
Notifications
You must be signed in to change notification settings - Fork 276
Expand file tree
/
Copy pathpip.c
More file actions
95 lines (76 loc) · 2.64 KB
/
Copy pathpip.c
File metadata and controls
95 lines (76 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/** ------------------------------------------------------------
* SPDX-License-Identifier: GPL-3.0-or-later
* ------------------------------------------------------------*/
def_target(pl_python_pip, "pip");
void
pl_python_pip_prelude (void)
{
chef_prep_this (pl_python_pip, gsr);
chef_set_created_on (this, "2023-09-03");
chef_set_last_updated (this, "2025-09-10");
chef_set_sources_last_updated (this, "2025-07-11");
chef_set_chef (this, NULL);
chef_set_cooks (this, 1, "@ccmywish");
chef_set_sauciers (this, 1, "@happy-game");
chef_allow_local_mode (this, CanNot, NULL, NULL);
chef_allow_english(this);
chef_allow_user_define(this);
chef_use_other_target_sources (this, &pl_python_group_target);
}
void
pl_python_pip_getsrc (char *option)
{
char *py_prog_name = NULL;
pl_python_get_py_program_name (&py_prog_name);
char *cmd = xy_2strcat (py_prog_name, " -m pip config get global.index-url 2>/dev/null");
int status = system (cmd);
if (0 == status)
{
// 执行成功时显示当前源
}
else
{
// 执行失败时显示默认源
if (ENGLISH)
chsrc_note2 ("No source configured in pip, showing default upstream source:");
else
chsrc_note2 ("pip 中未配置源,显示默认上游源:");
Source_t default_source = chsrc_yield_source (&pl_python_group_target, "upstream");
chsrc_log (default_source.url);
}
}
/**
* @consult https://mirrors.tuna.tsinghua.edu.cn/help/pypi/
*/
void
pl_python_pip_setsrc (char *option)
{
// 对于不支持的情况,尽早结束
if (chsrc_in_local_mode())
{
char *msg = ENGLISH ? "pip doesn't support `-local`. SKIP changing source!" : "pip 不支持 -local,跳过换源";
chsrc_error (msg);
// 不能直接退出,因为 Leader target 不能就此结束
return;
}
Source_t source = chsrc_yield_source (&pl_python_group_target, option);
if (chsrc_in_standalone_mode())
chsrc_confirm_source(&source);
char *py_prog_name = NULL;
pl_python_get_py_program_name (&py_prog_name);
// 这里用的是 config --user,会写入用户目录(而不是项目目录)
// https://github.com/RubyMetric/chsrc/issues/39
// 经测试,Windows上调用换源命令,会写入 C:\Users\RubyMetric\AppData\Roaming\pip\pip.ini
char *cmd = xy_2strcat (py_prog_name, xy_2strcat (" -m pip config --user set global.index-url ", source.url));
chsrc_run (cmd, RunOpt_No_Last_New_Line);
if (chsrc_in_standalone_mode())
{
chsrc_determine_chgtype (ChgType_Auto);
chsrc_conclude (&source);
}
}
void
pl_python_pip_resetsrc (char *option)
{
pl_python_pip_setsrc (option);
}