bitool 0.1.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bitool/__init__.py +27 -0
- bitool/cmd/__init__.py +65 -0
- bitool/cmd/_base.py +105 -0
- bitool/cmd/_condition.py +60 -0
- bitool/cmd/_scheduler.py +548 -0
- bitool/cmd/env.py +454 -0
- bitool/cmd/git.py +123 -0
- bitool/cmd/io.py +248 -0
- bitool/cmd/pdf.py +385 -0
- bitool/cmd/run.py +300 -0
- bitool/cmd/toml.py +237 -0
- bitool/cmd/version.py +630 -0
- bitool/consts.py +14 -0
- bitool/core/__init__.py +7 -0
- bitool/core/app.py +142 -0
- bitool/core/commands.py +194 -0
- bitool/core/config.py +647 -0
- bitool/core/env.py +18 -0
- bitool/core/logger.py +237 -0
- bitool/core/plugin.py +117 -0
- bitool/core/workspace.py +76 -0
- bitool/models/__init__.py +3 -0
- bitool/models/version.py +173 -0
- bitool/scripts/__init__.py +1 -0
- bitool/scripts/bumpversion.py +189 -0
- bitool/scripts/clearscreen.py +37 -0
- bitool/scripts/envpy.py +161 -0
- bitool/scripts/envrs.py +119 -0
- bitool/scripts/filedate.py +246 -0
- bitool/scripts/filelevel.py +191 -0
- bitool/scripts/gittool.py +178 -0
- bitool/scripts/img2pdf.py +151 -0
- bitool/scripts/pdf2img.py +139 -0
- bitool/scripts/piptool.py +130 -0
- bitool/scripts/pymake.py +345 -0
- bitool/scripts/sshcopyid.py +491 -0
- bitool/scripts/taskkill.py +366 -0
- bitool/scripts/which.py +227 -0
- bitool/types.py +7 -0
- bitool/utils/__init__.py +9 -0
- bitool/utils/cli_parser.py +412 -0
- bitool/utils/executor.py +881 -0
- bitool/utils/profiler.py +369 -0
- bitool/utils/task.py +133 -0
- bitool/utils/task_group.py +668 -0
- bitool/utils/tests/__init__.py +0 -0
- bitool/utils/tests/test_profiler.py +487 -0
- bitool-0.1.2.dist-info/METADATA +154 -0
- bitool-0.1.2.dist-info/RECORD +51 -0
- bitool-0.1.2.dist-info/WHEEL +4 -0
- bitool-0.1.2.dist-info/entry_points.txt +15 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"""版本号自动管理工具.
|
|
2
|
+
|
|
3
|
+
参考旧版 bumpversion 和 pymake 的设计, 使用 CommandScheduler 模式实现.
|
|
4
|
+
支持语义化版本管理和多文件格式的版本号更新.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import argparse
|
|
10
|
+
import sys
|
|
11
|
+
|
|
12
|
+
from bitool.cmd import (
|
|
13
|
+
BumpProjectVersionCommand,
|
|
14
|
+
CommandScheduler,
|
|
15
|
+
GitCommitCommand,
|
|
16
|
+
GitTagCommand,
|
|
17
|
+
)
|
|
18
|
+
from bitool.core import ConfigMixin, logger
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class BumpversionConfig(ConfigMixin):
|
|
22
|
+
"""Bumpversion 配置类."""
|
|
23
|
+
|
|
24
|
+
# 排除的目录
|
|
25
|
+
EXCLUDED_DIRS: set[str] = { # noqa: RUF012
|
|
26
|
+
"__pycache__",
|
|
27
|
+
".venv",
|
|
28
|
+
"venv",
|
|
29
|
+
"env",
|
|
30
|
+
".git",
|
|
31
|
+
"dist",
|
|
32
|
+
"build",
|
|
33
|
+
".tox",
|
|
34
|
+
".pytest_cache",
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
conf = BumpversionConfig()
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def create_scheduler_map() -> dict[str, CommandScheduler]:
|
|
42
|
+
"""创建命令调度器映射.
|
|
43
|
+
|
|
44
|
+
Returns
|
|
45
|
+
-------
|
|
46
|
+
dict[str, CommandScheduler]
|
|
47
|
+
操作名称到命令调度器的映射字典.
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
# === 版本号递增命令 ===
|
|
52
|
+
# 递增补丁号 (1.0.0 -> 1.0.1)
|
|
53
|
+
"patch": CommandScheduler(commands=[BumpProjectVersionCommand(part="patch")]),
|
|
54
|
+
# 递增次版本号 (1.0.0 -> 1.1.0)
|
|
55
|
+
"minor": CommandScheduler(commands=[BumpProjectVersionCommand(part="minor")]),
|
|
56
|
+
# 递增主版本号 (1.0.0 -> 2.0.0)
|
|
57
|
+
"major": CommandScheduler(commands=[BumpProjectVersionCommand(part="major")]),
|
|
58
|
+
# 递增补丁号并设置预发布标识 (1.0.0 -> 1.0.1-alpha)
|
|
59
|
+
"patch-alpha": CommandScheduler(
|
|
60
|
+
commands=[BumpProjectVersionCommand(part="patch", prerelease="alpha")]
|
|
61
|
+
),
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
OPTIONS: list[str] = list(create_scheduler_map().keys())
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def show_help() -> str:
|
|
69
|
+
"""显示帮助信息.
|
|
70
|
+
|
|
71
|
+
Returns
|
|
72
|
+
-------
|
|
73
|
+
str
|
|
74
|
+
帮助信息文本
|
|
75
|
+
"""
|
|
76
|
+
help_text = """
|
|
77
|
+
╔══════════════════════════════════════════════════════════╗
|
|
78
|
+
║ Bitool 版本号自动管理工具 ║
|
|
79
|
+
╚══════════════════════════════════════════════════════════╝
|
|
80
|
+
|
|
81
|
+
📈 版本号递增命令:
|
|
82
|
+
bumpversion patch - 递增补丁号 (1.0.0 -> 1.0.1)
|
|
83
|
+
bumpversion minor - 递增次版本号 (1.0.0 -> 1.1.0)
|
|
84
|
+
bumpversion major - 递增主版本号 (1.0.0 -> 2.0.0)
|
|
85
|
+
bumpversion patch-alpha - 递增补丁号并设置预发布标识 (1.0.0 -> 1.0.1-alpha)
|
|
86
|
+
|
|
87
|
+
💡 支持的文件格式:
|
|
88
|
+
- pyproject.toml: version = "1.0.0"
|
|
89
|
+
- __init__.py: __version__ = "1.0.0"
|
|
90
|
+
- setup.py: version = "1.0.0"
|
|
91
|
+
- package.json: "version": "1.0.0"
|
|
92
|
+
- Cargo.toml: version = "1.0.0"
|
|
93
|
+
|
|
94
|
+
📝 示例:
|
|
95
|
+
bumpversion patch # 递增补丁号
|
|
96
|
+
bumpversion minor # 递增次版本号
|
|
97
|
+
bumpversion patch-alpha # 递增补丁号并添加 alpha 预发布标识
|
|
98
|
+
"""
|
|
99
|
+
return help_text
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def parse_args() -> tuple[argparse.ArgumentParser, argparse.Namespace]:
|
|
103
|
+
"""解析命令行参数.
|
|
104
|
+
|
|
105
|
+
Returns
|
|
106
|
+
-------
|
|
107
|
+
tuple[argparse.ArgumentParser, argparse.Namespace]
|
|
108
|
+
(参数解析器, 解析后的参数对象)
|
|
109
|
+
"""
|
|
110
|
+
parser = argparse.ArgumentParser(
|
|
111
|
+
description="Bitool 版本号自动管理工具 (支持语义化版本和多文件格式)",
|
|
112
|
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
113
|
+
epilog=show_help(),
|
|
114
|
+
)
|
|
115
|
+
parser.add_argument(
|
|
116
|
+
"action",
|
|
117
|
+
nargs="?",
|
|
118
|
+
default="patch",
|
|
119
|
+
choices=OPTIONS,
|
|
120
|
+
type=str,
|
|
121
|
+
help="要执行的操作, 可选值: " + ", ".join(OPTIONS),
|
|
122
|
+
)
|
|
123
|
+
parser.add_argument(
|
|
124
|
+
"-t",
|
|
125
|
+
"--tag",
|
|
126
|
+
action="store_true",
|
|
127
|
+
default=False,
|
|
128
|
+
help="递增版本号后创建 Git 标签",
|
|
129
|
+
)
|
|
130
|
+
parser.add_argument(
|
|
131
|
+
"-c",
|
|
132
|
+
"--commit",
|
|
133
|
+
action="store_true",
|
|
134
|
+
default=False,
|
|
135
|
+
help="递增版本号后提交更改到 Git",
|
|
136
|
+
)
|
|
137
|
+
return parser, parser.parse_args()
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def main() -> None:
|
|
141
|
+
"""主函数, 执行版本号管理命令."""
|
|
142
|
+
parser, args = parse_args()
|
|
143
|
+
|
|
144
|
+
# 获取命令调度器
|
|
145
|
+
scheduler: CommandScheduler | None = create_scheduler_map().get(args.action, None)
|
|
146
|
+
if not scheduler:
|
|
147
|
+
logger.error(f"无效的操作: {args.action}")
|
|
148
|
+
parser.print_help(sys.stderr)
|
|
149
|
+
sys.exit(1)
|
|
150
|
+
|
|
151
|
+
# 执行版本号递增
|
|
152
|
+
scheduler.run()
|
|
153
|
+
|
|
154
|
+
# 如果指定了 tag 或 commit, 需要获取更新后的版本号并执行相应操作
|
|
155
|
+
if args.tag or args.commit:
|
|
156
|
+
# 从 BumpProjectVersionCommand 中获取更新后的文件列表
|
|
157
|
+
for cmd in scheduler.commands:
|
|
158
|
+
if isinstance(cmd, BumpProjectVersionCommand) and cmd.updated_files:
|
|
159
|
+
# 从第一个更新的文件中读取新版本号
|
|
160
|
+
from bitool.cmd.version import BumpFileVersionCommand
|
|
161
|
+
|
|
162
|
+
temp_bump_cmd = BumpFileVersionCommand(filepath=cmd.updated_files[0])
|
|
163
|
+
new_version = temp_bump_cmd._read_version()
|
|
164
|
+
|
|
165
|
+
if not new_version:
|
|
166
|
+
logger.warning("未能读取到新版本号, 跳过 Git 操作")
|
|
167
|
+
break
|
|
168
|
+
|
|
169
|
+
logger.info(f"检测到新版本号: {new_version}")
|
|
170
|
+
|
|
171
|
+
# 创建 git tag(如果指定)
|
|
172
|
+
if args.tag:
|
|
173
|
+
tag_cmd = GitTagCommand(
|
|
174
|
+
tag_name=f"v{new_version}", message=f"Release {new_version}"
|
|
175
|
+
)
|
|
176
|
+
tag_cmd.validate_and_run()
|
|
177
|
+
|
|
178
|
+
# 提交更改(如果指定)
|
|
179
|
+
if args.commit:
|
|
180
|
+
commit_cmd = GitCommitCommand(
|
|
181
|
+
message=f"chore: bump version to {new_version}"
|
|
182
|
+
)
|
|
183
|
+
commit_cmd.validate_and_run()
|
|
184
|
+
|
|
185
|
+
break
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
if __name__ == "__main__":
|
|
189
|
+
main()
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""跨平台控制台清屏工具."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
from bitool.cmd import BuiltinConditions, CommandScheduler, RunCommand
|
|
8
|
+
from bitool.consts import Constants
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def reset_cursor() -> None:
|
|
12
|
+
"""将光标重置到控制台的起始位置."""
|
|
13
|
+
sys.stdout.write("\033[H")
|
|
14
|
+
sys.stdout.flush()
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def create_scheduler() -> CommandScheduler:
|
|
18
|
+
return CommandScheduler(
|
|
19
|
+
commands=[
|
|
20
|
+
RunCommand(
|
|
21
|
+
cmd=["cls" if Constants.IS_WINDOWS else "clear"],
|
|
22
|
+
allow_conditions=[BuiltinConditions.IS_WINDOWS],
|
|
23
|
+
),
|
|
24
|
+
RunCommand(cmd=reset_cursor),
|
|
25
|
+
]
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def main() -> None:
|
|
30
|
+
"""运行命令行界面入口点.
|
|
31
|
+
|
|
32
|
+
Returns
|
|
33
|
+
-------
|
|
34
|
+
int: 退出码 (0 表示成功,非 0 表示失败)
|
|
35
|
+
"""
|
|
36
|
+
scheduler = create_scheduler()
|
|
37
|
+
scheduler.run()
|
bitool/scripts/envpy.py
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"""Python环境配置工具模块。
|
|
2
|
+
|
|
3
|
+
用于设置pip镜像源, 支持清华和阿里云等国内镜像源。
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import argparse
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
from bitool.cmd import CommandScheduler, SetEnvCommand, WriteFileCommand
|
|
12
|
+
from bitool.consts import Constants
|
|
13
|
+
from bitool.core import ConfigMixin
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class EnvPyConfig(ConfigMixin):
|
|
17
|
+
"""Python环境配置类。"""
|
|
18
|
+
|
|
19
|
+
# pip 镜像源配置
|
|
20
|
+
PIP_DEFAULT_INDEX_URL: str = "https://pypi.org/simple"
|
|
21
|
+
PIP_INDEX_URLS: dict[str, str] = { # noqa: RUF012
|
|
22
|
+
"tsinghua": "https://pypi.tuna.tsinghua.edu.cn/simple",
|
|
23
|
+
"aliyun": "https://mirrors.aliyun.com/pypi/simple/",
|
|
24
|
+
}
|
|
25
|
+
PIP_TRUSTED_HOSTS: dict[str, str] = { # noqa: RUF012
|
|
26
|
+
"tsinghua": "pypi.tuna.tsinghua.edu.cn",
|
|
27
|
+
"aliyun": "mirrors.aliyun.com",
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
# UV相关配置
|
|
31
|
+
UV_INDEX_URL: str = "https://mirrors.aliyun.com/pypi/simple/"
|
|
32
|
+
UV_DEFAULT_INDEX: str = "https://mirrors.aliyun.com/pypi/simple/"
|
|
33
|
+
UV_HTTP_TIMEOUT: int = 600
|
|
34
|
+
UV_LINK_MODE: str = "copy"
|
|
35
|
+
|
|
36
|
+
# Conda相关配置
|
|
37
|
+
CONDA_DEFAULT_MIRROR = "tsinghua"
|
|
38
|
+
_CONDA_MIRROR_URLS: dict[str, frozenset[str]] = { # noqa: RUF012
|
|
39
|
+
"tsinghua": frozenset(
|
|
40
|
+
[
|
|
41
|
+
"https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/",
|
|
42
|
+
"https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/",
|
|
43
|
+
"https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/",
|
|
44
|
+
"https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2/",
|
|
45
|
+
"https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro/",
|
|
46
|
+
"https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/",
|
|
47
|
+
"https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/",
|
|
48
|
+
"https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/",
|
|
49
|
+
"https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/",
|
|
50
|
+
]
|
|
51
|
+
),
|
|
52
|
+
"ustc": frozenset(
|
|
53
|
+
[
|
|
54
|
+
"https://mirrors.ustc.edu.cn/anaconda/pkgs/main/",
|
|
55
|
+
"https://mirrors.ustc.edu.cn/anaconda/pkgs/free/",
|
|
56
|
+
"https://mirrors.ustc.edu.cn/anaconda/pkgs/r/",
|
|
57
|
+
"https://mirrors.ustc.edu.cn/anaconda/pkgs/msys2/",
|
|
58
|
+
"https://mirrors.ustc.edu.cn/anaconda/pkgs/pro/",
|
|
59
|
+
"https://mirrors.ustc.edu.cn/anaconda/pkgs/dev/",
|
|
60
|
+
"https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/",
|
|
61
|
+
"https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/",
|
|
62
|
+
"https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/",
|
|
63
|
+
"https://mirrors.ustc.edu.cn/anaconda/cloud/pytorch/",
|
|
64
|
+
]
|
|
65
|
+
),
|
|
66
|
+
"bsfu": frozenset(
|
|
67
|
+
[
|
|
68
|
+
"https://mirrors.bsfu.edu.cn/anaconda/pkgs/main/",
|
|
69
|
+
"https://mirrors.bsfu.edu.cn/anaconda/pkgs/free/",
|
|
70
|
+
"https://mirrors.bsfu.edu.cn/anaconda/pkgs/r/",
|
|
71
|
+
"https://mirrors.bsfu.edu.cn/anaconda/pkgs/msys2/",
|
|
72
|
+
"https://mirrors.bsfu.edu.cn/anaconda/pkgs/pro/",
|
|
73
|
+
"https://mirrors.bsfu.edu.cn/anaconda/pkgs/dev/",
|
|
74
|
+
"https://mirrors.bsfu.edu.cn/anaconda/cloud/conda-forge/",
|
|
75
|
+
"https://mirrors.bsfu.edu.cn/anaconda/cloud/bioconda/",
|
|
76
|
+
"https://mirrors.bsfu.edu.cn/anaconda/cloud/menpo/",
|
|
77
|
+
"https://mirrors.bsfu.edu.cn/anaconda/cloud/pytorch/",
|
|
78
|
+
]
|
|
79
|
+
),
|
|
80
|
+
"aliyun": frozenset(
|
|
81
|
+
[
|
|
82
|
+
"https://mirrors.aliyun.com/anaconda/pkgs/main/",
|
|
83
|
+
"https://mirrors.aliyun.com/anaconda/pkgs/free/",
|
|
84
|
+
"https://mirrors.aliyun.com/anaconda/pkgs/r/",
|
|
85
|
+
"https://mirrors.aliyun.com/anaconda/pkgs/msys2/",
|
|
86
|
+
"https://mirrors.aliyun.com/anaconda/pkgs/pro/",
|
|
87
|
+
"https://mirrors.aliyun.com/anaconda/pkgs/dev/",
|
|
88
|
+
"https://mirrors.aliyun.com/anaconda/cloud/conda-forge/",
|
|
89
|
+
"https://mirrors.aliyun.com/anaconda/cloud/bioconda/",
|
|
90
|
+
"https://mirrors.aliyun.com/anaconda/cloud/menpo/",
|
|
91
|
+
"https://mirrors.aliyun.com/anaconda/cloud/pytorch/",
|
|
92
|
+
]
|
|
93
|
+
),
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
conf = EnvPyConfig()
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def create_scheduler(mirror: str = "tsinghua") -> CommandScheduler:
|
|
101
|
+
"""创建设置pip镜像源的命令调度器。
|
|
102
|
+
|
|
103
|
+
Args:
|
|
104
|
+
mirror: 镜像源名称, 默认为"tsinghua"。
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
命令调度器实例。
|
|
108
|
+
"""
|
|
109
|
+
return CommandScheduler(
|
|
110
|
+
commands=[
|
|
111
|
+
SetEnvCommand(
|
|
112
|
+
envs=[
|
|
113
|
+
(
|
|
114
|
+
"PIP_INDEX_URL",
|
|
115
|
+
conf.PIP_INDEX_URLS.get(mirror, conf.PIP_DEFAULT_INDEX_URL),
|
|
116
|
+
),
|
|
117
|
+
("UV_INDEX_URL", conf.UV_INDEX_URL),
|
|
118
|
+
("UV_DEFAULT_INDEX", conf.UV_DEFAULT_INDEX),
|
|
119
|
+
("UV_HTTP_TIMEOUT", str(conf.UV_HTTP_TIMEOUT)),
|
|
120
|
+
("UV_LINK_MODE", conf.UV_LINK_MODE),
|
|
121
|
+
]
|
|
122
|
+
),
|
|
123
|
+
WriteFileCommand(
|
|
124
|
+
filepath=Path.home() / ".pip" / "pip.conf"
|
|
125
|
+
if Constants.IS_UNIX
|
|
126
|
+
else Path.home() / "pip" / "pip.ini",
|
|
127
|
+
content=f"[global]\nindex-url = {conf.PIP_INDEX_URLS.get(mirror, conf.PIP_DEFAULT_INDEX_URL)}\
|
|
128
|
+
\n[install]\ntrusted-host = {conf.PIP_TRUSTED_HOSTS.get(mirror, '')}",
|
|
129
|
+
),
|
|
130
|
+
WriteFileCommand(
|
|
131
|
+
filepath=Path.home() / ".condarc",
|
|
132
|
+
content=f"show_channel_urls: true\nchannels:\n - {conf._CONDA_MIRROR_URLS.get(mirror, conf._CONDA_MIRROR_URLS['tsinghua'])}\n - defaults",
|
|
133
|
+
),
|
|
134
|
+
]
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def parse_args() -> argparse.Namespace:
|
|
139
|
+
"""解析命令行参数。
|
|
140
|
+
|
|
141
|
+
Returns:
|
|
142
|
+
解析后的参数命名空间。
|
|
143
|
+
"""
|
|
144
|
+
parser = argparse.ArgumentParser(description="设置Python包的pip镜像源")
|
|
145
|
+
parser.add_argument(
|
|
146
|
+
"mirror",
|
|
147
|
+
type=str,
|
|
148
|
+
nargs="?",
|
|
149
|
+
default="tsinghua",
|
|
150
|
+
choices=conf.PIP_INDEX_URLS.keys(),
|
|
151
|
+
help="要使用的pip镜像源",
|
|
152
|
+
)
|
|
153
|
+
return parser.parse_args()
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def main() -> None:
|
|
157
|
+
"""主函数, 解析命令行参数并运行调度器。"""
|
|
158
|
+
args = parse_args()
|
|
159
|
+
|
|
160
|
+
scheduler = create_scheduler(mirror=args.mirror)
|
|
161
|
+
scheduler.run()
|
bitool/scripts/envrs.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"""设置 Rust 国内镜像环境变量."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from bitool.cmd import (
|
|
9
|
+
BuiltinConditions,
|
|
10
|
+
CommandScheduler,
|
|
11
|
+
RunCommand,
|
|
12
|
+
SetEnvCommand,
|
|
13
|
+
WriteFileCommand,
|
|
14
|
+
)
|
|
15
|
+
from bitool.core import ConfigMixin, logger
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class EnvRsConfig(ConfigMixin):
|
|
19
|
+
"""EnvRs 配置类."""
|
|
20
|
+
|
|
21
|
+
DEFAULT_INSTALL_VERSION = "nightly"
|
|
22
|
+
TOML_CONTENT = """[source.crates-io]
|
|
23
|
+
replace-with = 'aliyun'
|
|
24
|
+
|
|
25
|
+
[source.aliyun]
|
|
26
|
+
registry = "sparse+https://mirrors.aliyun.com/crates.io-index/"
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
RUSTUP_DIST_SERVER = "https://mirrors.aliyun.com/rustup"
|
|
30
|
+
RUSTUP_UPDATE_ROOT = "https://mirrors.aliyun.com/rustup/rustup"
|
|
31
|
+
RUSTUP_DOWNLOAD_URL = "https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe"
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
conf = EnvRsConfig()
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def create_scheduler(version: str = conf.DEFAULT_INSTALL_VERSION) -> CommandScheduler:
|
|
38
|
+
"""根据版本参数动态创建命令调度器.
|
|
39
|
+
|
|
40
|
+
Parameters
|
|
41
|
+
----------
|
|
42
|
+
version : str
|
|
43
|
+
Rust 版本,默认为 "nightly"
|
|
44
|
+
|
|
45
|
+
Returns
|
|
46
|
+
-------
|
|
47
|
+
CommandScheduler
|
|
48
|
+
配置好命令的调度器实例
|
|
49
|
+
"""
|
|
50
|
+
return CommandScheduler(
|
|
51
|
+
commands=[
|
|
52
|
+
SetEnvCommand(
|
|
53
|
+
envs=[
|
|
54
|
+
("RUSTUP_DIST_SERVER", conf.RUSTUP_DIST_SERVER),
|
|
55
|
+
("RUSTUP_UPDATE_ROOT", conf.RUSTUP_UPDATE_ROOT),
|
|
56
|
+
]
|
|
57
|
+
),
|
|
58
|
+
RunCommand(
|
|
59
|
+
cmd=[
|
|
60
|
+
"powershell",
|
|
61
|
+
"-Command",
|
|
62
|
+
"Invoke-WebRequest",
|
|
63
|
+
"-Uri",
|
|
64
|
+
"https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe",
|
|
65
|
+
"-OutFile",
|
|
66
|
+
"rustup-init.exe",
|
|
67
|
+
],
|
|
68
|
+
allow_conditions=[
|
|
69
|
+
BuiltinConditions.IS_WINDOWS,
|
|
70
|
+
BuiltinConditions.HAS_APP_INSTALLED("rustup"),
|
|
71
|
+
],
|
|
72
|
+
forbid_conditions=[
|
|
73
|
+
BuiltinConditions.HAS_ENTRY_IN_DIR("rustup-init.exe", Path.cwd())
|
|
74
|
+
],
|
|
75
|
+
timeout=240,
|
|
76
|
+
),
|
|
77
|
+
RunCommand(
|
|
78
|
+
cmd=[
|
|
79
|
+
"curl",
|
|
80
|
+
"--proto",
|
|
81
|
+
"=https",
|
|
82
|
+
"--tlsv1.2",
|
|
83
|
+
"-sSf",
|
|
84
|
+
"https://mirrors.aliyun.com/repo/rust/rustup-init.sh",
|
|
85
|
+
"|",
|
|
86
|
+
"sh",
|
|
87
|
+
],
|
|
88
|
+
forbid_conditions=[BuiltinConditions.IS_WINDOWS],
|
|
89
|
+
),
|
|
90
|
+
WriteFileCommand(
|
|
91
|
+
filepath=Path.home() / ".rustup" / "config.toml",
|
|
92
|
+
content=conf.TOML_CONTENT,
|
|
93
|
+
),
|
|
94
|
+
RunCommand(cmd=["rustup", "toolchain", "install", version]),
|
|
95
|
+
]
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def parse_args() -> argparse.Namespace:
|
|
100
|
+
import argparse
|
|
101
|
+
|
|
102
|
+
parser = argparse.ArgumentParser(description="设置 Rust 国内镜像环境变量")
|
|
103
|
+
parser.add_argument(
|
|
104
|
+
"version", type=str, nargs="?", default="nightly", help="Rust 版本"
|
|
105
|
+
)
|
|
106
|
+
return parser.parse_args()
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def main() -> None:
|
|
110
|
+
"""设置 Rust 镜像环境变量并安装指定版本."""
|
|
111
|
+
args = parse_args()
|
|
112
|
+
logger.info(f"安装 Rust {args.version} 版本")
|
|
113
|
+
|
|
114
|
+
scheduler = create_scheduler(args.version)
|
|
115
|
+
scheduler.run()
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
if __name__ == "__main__":
|
|
119
|
+
main()
|