-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathterm-sd.sh
executable file
·1435 lines (1311 loc) · 57.7 KB
/
term-sd.sh
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Term-SD 启动参数处理
# 设置 TERM_SD_SCRIPT_NAME 全局变量读取要启动的 Term-SD 扩展脚本
term_sd_launch_arg_parse() {
local argument_input
local argument
local i
# 用别的方法实现了 getopt 命令的功能
# 加一个 --null 是为了增加一次循环, 保证那些需要参数的选项能成功执行
for i in "$@" "--null"; do
argument=$i # 用作判断是参数还是选项
# 参数检测部分
if [[ ! -z "${argument_input}" ]]; then
if term_sd_is_launch_arg "${argument}"; then # 测试输入值是参数还是选项
unset argument # 检测到选项的下一项是选项, 直接清除
fi
# 检测输入的选项
case "${argument_input}" in
--set-python-path)
set_python_path "${argument}"
;;
--extra)
TERM_SD_SCRIPT_NAME=$argument
;;
--bar)
term_sd_loading_bar_setting "${argument}"
;;
esac
unset argument_input # 清除选项, 留给下一次判断
fi
####################
# 选项检测部分(如果选项要跟参数值,则将启动选项赋值给 argument_input)
case "${i}" in
--help)
term_sd_print_line
term_sd_args_help
term_sd_print_line
exit 0
;;
--reinstall-term-sd)
# 防止重启后再执行重装
case "${TERM_SD_IS_REINSTALL}" in
1)
;;
*)
TERM_SD_IS_REINSTALL=1
term_sd_reinstall
;;
esac
;;
--set-python-path)
argument_input="--set-python-path"
;;
--unset-python-path)
rm -f "${START_PATH}/term-sd/config/python-path.conf"
term_sd_echo "已删除自定义 Python 解释器路径配置"
;;
--bar)
argument_input="--bar"
;;
--update-pip)
ENABLE_PIP_VER_CHECK=1
PIP_DISABLE_PIP_VERSION_CHECK=0
term_sd_echo "进入虚拟环境时将更新 Pip 软件包管理器"
;;
--remove-term-sd)
term_sd_remove
;;
--quick-cmd)
install_cmd_to_shell
exit 0
;;
--extra)
argument_input="--extra"
;;
--debug)
term_sd_echo "显示 Term-SD 调试信息"
TERM_SD_ENABLE_DEBUG=1
;;
--unset-tcmalloc)
USE_TCMALLOC=0
term_sd_echo "禁用加载 TCMalloc 内存优化"
;;
*)
term_sd_unknown_args_echo ${i}
;;
esac
done
}
# Term-SD 命令行帮助信息
term_sd_args_help() {
cat<<EOF
Term-SD 启动参数使用方法:
term-sd.sh [--help] [--extra script_name] [--reinstall-term-sd] [--remove-term-sd] [--quick-cmd] [--set-python-path python_path] [--unset-python-path] [--update-pip] [--bar display_mode] [--debug]
选项:
--help
显示启动参数帮助
--extra script_name
启动扩展脚本选择列表, 当选项后面输入了脚本名, 则直接启动指定的脚本, 否则启动扩展脚本选择界面
--reinstall-term-sd
重新安装 Term-SD
--remove-term-sd
卸载 Term-SD
--quick-cmd
添加 Term-SD 快捷启动命令到 Shell
--set-python-path python_path
手动指定 Python 解释器路径, 当选项后面输入了路径, 则直接使用输入的路径来设置 Python 解释器路径 (建议用" "把路径括起来, 防止路径输入错误), 否则启动设置界面
--unset-python-path
删除自定义 Python 解释器路径配置
--update-pip
进入虚拟环境时更新 Pip 软件包管理器
--bar display_mode
设置 Term-SD 初始化进度条的显示样式, 有以下显示模式:
none: 禁用进度条显示
normal: 使用默认的显示模式
new: 使用新的进度条显示
--debug
显示 Term-SD 安装 AI 软件时使用的命令
--unset-tcmalloc
禁用加载内存优化
EOF
}
# Term-SD 扩展脚本启动功能
term_sd_extra_scripts_launch() {
if [[ -z "$@" ]]; then
term_sd_extra_scripts
else
if [[ -f "term-sd/extra/${@%.sh}.sh" ]]; then
term_sd_print_line "${@%.sh} 脚本启动"
term_sd_echo "启动 ${@%.sh} 脚本中"
. "${START_PATH}/term-sd/extra/${@%.sh}.sh"
term_sd_print_line
term_sd_echo "退出 ${@%.sh} 脚本"
exit 0
else
term_sd_print_line
term_sd_echo "未找到 ${@%.sh} 脚本"
term_sd_echo "退出 Term-SD"
exit 1
fi
fi
}
# 扩展脚本选择
term_sd_extra_scripts() {
local extra_script
extra_script=$(dialog --erase-on-exit \
--title "Term-SD" \
--backtitle "扩展脚本选项" \
--ok-label "确认" --cancel-label "取消" \
--menu "请选择要启动的脚本" \
$(get_dialog_size_menu) \
"Term-SD" "<---------" \
$(ls -l "term-sd/extra" --time-style=+"%Y-%m-%d" | awk '{ print $7 " " $6 }') \
"退出" "<---------" \
3>&1 1>&2 2>&3)
case "$?" in
0)
case "${extra_script}" in
Term-SD)
. "${START_PATH}/term-sd/modules/init.sh"
term_sd_version
main
;;
退出)
term_sd_print_line
term_sd_echo "退出Term-SD"
exit 0
;;
*)
term_sd_print_line "${extra_script%.sh} 脚本启动"
. "${START_PATH}/term-sd/extra/"${extra_script}""
term_sd_print_line
term_sd_echo "退出 ${extra_script%.sh} 脚本"
exit 0
esac
;;
*)
term_sd_echo "退出 Term-SD"
exit 0
;;
esac
}
# 格式化信息输出
# 使用:
# term_sd_echo <输出信息>
term_sd_echo() {
echo -e "[\033[33m$(date "+%Y-%m-%d %H:%M:%S")\033[0m][\033[36mTerm-SD\033[0m]\033[36m::\033[0m $@"
}
# 键盘输入读取
# 回车接收读取到的输入并输出
term_sd_read() {
local input_text
read -p "===============================> " input_text
echo "${input_text}"
}
# 暂停运行(用于显示运行结果)
term_sd_pause() {
term_sd_print_line
term_sd_echo "执行结束, 请按回车键继续"
read
}
# 测试输入值是参数还是选项, 是选项返回 0, 是参返回 1 (用于实现 getopt 命令的功能)
term_sd_is_launch_arg() {
if [[ "$(echo $@ | awk '{for (i = 1; i <= NF; i++) {if (substr($i, 1, 2) == "--") {print "0"} else {print "1"}}}')" == 0 ]]; then
return 0
else
return 1
fi
}
# 提示未知启动参数
term_sd_unknown_args_echo() {
if term_sd_is_launch_arg "$@" && [[ ! "$@" == "--null" ]]; then # 测试输入值是参数还是选项
term_sd_echo "未知参数: $@"
fi
}
# 创建目录
# 如果目录不存在则自动创建一个
# 使用:
# term_sd_mkdir <需创建的文件夹名>
term_sd_mkdir() {
if [[ ! -d "$@" ]]; then
mkdir -p "$@"
else
true
fi
}
# 暂停执行
# 使用:
# term_sd_sleep <暂停的时间>
term_sd_sleep() {
local pause_time=$1
local i
for (( i = pause_time; i >= 0; i-- )); do
printf "[\033[33m$(date "+%Y-%m-%d %H:%M:%S")\033[0m][\033[36mTerm-SD\033[0m]\033[36m::\033[0m 等待中: ${i} \r"
sleep 1
done
printf " \r"
}
# 路径格式转换(将 Windows 风格的文件路径转换成 Linux / Unix 风格的路径)
term_sd_win2unix_path() {
if is_windows_platform; then
echo "$(cd "$(dirname "$@" 2> /dev/null)" ; pwd)/$(basename "$@" 2> /dev/null)"
else
echo "$@"
fi
}
# 检测目录是否为空, 为空是返回 0, 不为空返回 1
term_sd_is_dir_empty() {
if [[ $(ls "$@" -al --format=horizontal | wc --words) -le 2 ]]; then
return 0
else
return 1
fi
}
# 系统判断
# 当 Term-SD 未初始化完成时(term_sd_python 命令未能使用), 使用 OS 环境变量判断系统的类型
is_windows_platform() {
local sys_platform
if term_sd_python --version &> /dev/null; then
sys_platform=$(term_sd_python -c "$(py_is_windows_platform)")
else
case "${OS}" in
"Windows_NT")
sys_platform="win32"
;;
*)
sys_platform="other"
;;
esac
fi
if [[ "${sys_platform}" = "win32" ]]; then
return 0
else
return 1
fi
}
# 系统判断(Python)
py_is_windows_platform() {
cat<<EOF
import sys
if sys.platform == "win32":
print("win32")
else:
print("other")
EOF
}
# 加载进度条设置
# 配置保存在 <Start Path>/term-sd/config/term-sd-bar.conf
term_sd_loading_bar_setting() {
if [[ -z "$@" ]]; then
term_sd_echo "未指定 Term-SD 初始化进度条的显示模式"
else
case "$@" in
"none")
echo "none" > "${START_PATH}/term-sd/config/term-sd-bar.conf"
term_sd_echo "禁用 Term-SD 初始化进度显示"
;;
"normal")
rm -f "${START_PATH}/term-sd/config/term-sd-bar.conf"
term_sd_echo "使用默认 Term-SD 初始化进度显示模式"
;;
"new")
echo "new" > "${START_PATH}/term-sd/config/term-sd-bar.conf"
term_sd_echo "使用新的 Term-SD 初始化进度显示模式"
;;
*)
term_sd_echo "未知的 Term-SD 初始化进度条显示模式"
;;
esac
fi
}
# 终端横线显示功能
# 使用:
# term_sd_print_line <输出的文本>
# 使用 SHELL_WIDTH 全局变量获取终端宽度
term_sd_print_line() {
local shell_width
local input_text
local input_text_length
local input_zh_text_length
local shell_width_info
local text_length_info
local print_mode
if [[ -z "$@" ]]; then # 输出方法选择
print_mode=1
else
shell_width=$SHELL_WIDTH # 获取终端宽度
input_text=$(echo "$@" | awk '{gsub(/ /,"-")}1') # 将空格转换为"-"
input_text_length=$(( $(echo "${input_text}" | wc -c) - 1 )) # 总共的字符长度
input_zh_text_length=$(( $(echo "${input_text}" | awk '{gsub(/[a-zA-Z]/,"") ; gsub(/[0-9]/, "") ; gsub(/[=+()()、。,./\-_\\]/, "")}1' | wc -c) - 1 )) # 计算中文字符的长度
input_text_length=$(( input_text_length - input_zh_text_length )) # 除去中文之后的长度
# 中文的字符长度为3,但终端中只占2个字符位
input_zh_text_length=$(( input_zh_text_length / 3 * 2 )) # 转换中文在终端占用的实际字符长度
input_text_length=$(( input_text_length + input_zh_text_length )) # 最终显示文字的长度
# 横线输出长度的计算
shell_width=$(( (shell_width - input_text_length) / 2 )) # 除去输出字符后的横线宽度
# 判断终端宽度大小是否是单双数
shell_width_info=$(( shell_width % 2 ))
# 判断字符宽度大小是否是单双数
text_length_info=$(( input_text_length % 2 ))
case "${shell_width_info}" in
0)
# 如果终端宽度大小是双数
case "${text_length_info}" in
0)
# 如果字符宽度大小是双数
print_mode=2
;;
1)
# 如果字符宽度大小是单数
print_mode=3
;;
esac
;;
1)
# 如果终端宽度大小是单数数
case "${text_length_info}" in
0)
# 如果字符宽度大小是双数
print_mode=2
;;
1)
# 如果字符宽度大小是单数
print_mode=3
;;
esac
;;
esac
fi
# 输出
case "${print_mode}" in
1)
shell_width=$SHELL_WIDTH # 获取终端宽度
yes "-" | sed $shell_width'q' | tr -d '\n' # 输出横杠
;;
2)
# 解决显示字符为单数时少显示一个字符导致不对成的问题
echo "$(yes "-" | sed "${shell_width}"'q' | tr -d '\n')"$@"$(yes "-" | sed "${shell_width}"'q' | tr -d '\n')"
;;
3)
echo "$(yes "-" | sed "${shell_width}"'q' | tr -d '\n')"$@"$(yes "-" | sed $(( shell_width + 1 ))'q' | tr -d '\n')"
;;
esac
}
# Term-SD 自动更新触发功能
# 使用 <Start Path>/term-sd/config/term-sd-auto-update.lock 检测是否启用的自动更新
# 使用 <Start Path>/term-sd/config/term-sd-auto-update-time.conf 获取上次更新的时间
term_sd_auto_update_trigger() {
local start_time
local end_time
local start_time_sec
local end_time_sec
local time_span
local normal_time_span=3600 # 检查更新时间间隔
if [[ -f "${START_PATH}/term-sd/config/term-sd-auto-update.lock" ]] && [[ -d "${START_PATH}/term-sd/.git" ]]; then # 找到自动更新配置
if [[ -f "${START_PATH}/term-sd/config/term-sd-auto-update-time.conf" ]]; then # 有上次运行记录
start_time=`date +'%Y-%m-%d %H:%M:%S'` # 查看当前时间
end_time=$(cat "${START_PATH}/term-sd/config/term-sd-auto-update-time.conf") # 获取上次更新时间
start_time_sec=$(date --date="${start_time}" +%s) # 转换时间单位
end_time_sec=$(date --date="${end_time}" +%s)
time_span=$(( start_time_sec - end_time_sec )) # 计算相隔时间
if (( time_span >= normal_time_span )); then # 判断时间间隔
term_sd_auto_update
date +'%Y-%m-%d %H:%M:%S' > "${START_PATH}/term-sd/config/term-sd-auto-update-time.conf" # 记录自动更新功能的启动时间
fi
else # 没有时直接执行
term_sd_auto_update
date +'%Y-%m-%d %H:%M:%S' > "${START_PATH}/term-sd/config/term-sd-auto-update-time.conf" # 记录自动更新功能的启动时间
fi
fi
}
# Term-SD 自动更新功能
term_sd_auto_update() {
local ref
local origin_branch
local commit_hash
local local_commit_hash
term_sd_echo "检查更新中"
git -C term-sd fetch
if [[ "$?" = 0 ]]; then # 拉取远端内容成功后再更新
ref=$(git -C "${START_PATH}/term-sd" symbolic-ref --quiet HEAD 2> /dev/null)
if [[ "$?" = 0 ]]; then # 未出现分支游离
origin_branch="origin/${ref#refs/heads/}"
else # 出现分支游离时查询HEAD所指的分支
origin_branch="origin/$(git -C "${START_PATH}/term-sd" branch -a | grep "/HEAD" | awk -F '/' '{print $NF}')"
fi
commit_hash=$(git -C "${START_PATH}/term-sd" log "${origin_branch}" --max-count 1 --format="%h")
local_commit_hash=$(git -C "${START_PATH}/term-sd" show -s --format="%h")
if [[ ! "${commit_hash}" == "${local_commit_hash}" ]]; then
term_sd_echo "检测到 Term-SD 有新版本"
term_sd_echo "是否选择更新(yes/no) ?"
term_sd_echo "提示: 输入 yes 或 no 后回车"
case "$(term_sd_read)" in
yes|y|YES|Y)
term_sd_echo "更新 Term-SD 中"
git -C "${START_PATH}/term-sd" reset --hard "${commit_hash}"
cp -f "${START_PATH}/term-sd/term-sd.sh" "${START_PATH}/"
chmod +x "${START_PATH}/term-sd.sh"
TERM_SD_TO_RESTART=1
term_sd_echo "Term-SD 更新完成"
;;
*)
term_sd_echo "跳过 Term-SD 的更新"
;;
esac
else
term_sd_echo "Term-SD 已经是最新版本"
fi
else
term_sd_echo "Term-SD 连接更新源失败, 跳过更新"
term_sd_echo "提示: 请检查网络连接是否正常, 若网络正常, 可尝试更换更新源或使用科学上网解决"
fi
}
# Term-SD 安装功能
term_sd_install() {
if [[ ! -d "${START_PATH}/term-sd" ]]; then
term_sd_echo "检测到 Term-SD 组件未安装, 开始下载组件中"
term_sd_clone_modules
if [[ "$?" == 0 ]]; then
term_sd_set_up_normal_setting
TERM_SD_TO_RESTART=1
cp -f "${START_PATH}/term-sd/term-sd.sh" "${START_PATH}/"
chmod +x "${START_PATH}/term-sd.sh"
term_sd_echo "Term-SD 安装成功"
else
term_sd_echo "Term-SD 安装失败, 可尝试重新运行"
exit 1
fi
elif [[ ! -d "${START_PATH}/term-sd/.git" ]]; then
term_sd_echo "检测到 Term-SD 的 .git 目录不存在, 将会导致 Term-SD 无法更新, 是否重新安装 (yes/no) ?"
term_sd_echo "警告: 该操作将永久删除 Term-SD 目录中的所有文件 (除了 Term-SD 缓存文件夹和配置文件将备份到临时文件夹并在安装完成还原)"
term_sd_echo "提示: 输入 yes 或 no 后回车"
case "$(term_sd_read)" in
yes|y|YES|Y)
term_sd_backup_config
term_sd_echo "清除 Term-SD 文件中"
rm -rf "${START_PATH}/term-sd"
term_sd_echo "Term-SD 文件清除完成"
term_sd_clone_modules
if [[ "$?" == 0 ]]; then
term_sd_restore_config
TERM_SD_TO_RESTART=1
cp -f "${START_PATH}/term-sd/term-sd.sh" "${START_PATH}/"
chmod +x "${START_PATH}/term-sd.sh"
term_sd_echo "Term-SD 重新安装成功"
else
term_sd_echo "Term-SD 重新安装失败"
exit 1
fi
;;
*)
term_sd_echo "取消重新安装 Term-SD 操作"
;;
esac
fi
}
# Term-SD 重新安装功能
# 使用 TERM_SD_TO_RESTART 全局变量标记 Term-SD 需要进行重载
term_sd_reinstall() {
if which git &> /dev/null; then
term_sd_echo "是否重新安装 Term-SD (yes/no) ?"
term_sd_echo "警告: 该操作将永久删除 Term-SD 目录中的所有文件 (除了 Term-SD 缓存文件夹和配置文件将备份到临时文件夹并在安装完成还原)"
term_sd_echo "提示: 输入 yes 或 no 后回车"
case "$(term_sd_read)" in
yes|y|YES|Y)
term_sd_backup_config
term_sd_echo "清除 Term-SD 文件中"
rm -rf "${START_PATH}/term-sd"
term_sd_echo "Term-SD 文件清除完成"
term_sd_clone_modules
if [[ "$?" == 0 ]]; then
term_sd_restore_config
TERM_SD_TO_RESTART=1
cp -f "${START_PATH}/term-sd/term-sd.sh" "${START_PATH}/"
chmod +x "${START_PATH}/term-sd.sh"
term_sd_echo "Term-SD 重新安装成功"
else
term_sd_echo "Term-SD 重新安装失败"
exit 1
fi
;;
*)
term_sd_echo "退出 Term-SD"
exit 0
;;
esac
else
term_sd_echo "缺少 Git, 无法重新安装 Term-SD"
fi
}
# 下载 Term-SD
term_sd_clone_modules() {
local i
local count=0
local repo_urls="https://github.com/licyk/term-sd https://gitee.com/licyk/term-sd https://[email protected]/licyks/term-sd https://gitlab.com/licyk/term-sd"
term_sd_echo "下载 Term-SD 中"
for i in ${repo_urls}; do
count=$((count + 1))
git clone "${i}" "${START_PATH}/term-sd"
if [[ "$?" == 0 ]]; then
term_sd_echo "Term-SD 下载成功"
return 0
else
term_sd_echo "Term-SD 下载失败"
if [[ "${count}" -lt "$(echo "${repo_urls}" | wc --words)" ]]; then
term_sd_echo "更换 Term-SD 下载源进行下载中"
else
return 1
fi
fi
done
}
# 备份 cache 文件夹
# 备份的 cache 文件夹保存在 <Start Path>/term-sd-tmp
term_sd_backup_config() {
term_sd_echo "备份 Term-SD 缓存文件夹和配置文件中"
term_sd_mkdir "${START_PATH}/term-sd-tmp"
term_sd_mkdir "${START_PATH}/term-sd-tmp/config"
rm -f "${START_PATH}/term-sd/config/note.md"
[[ -d "${START_PATH}/term-sd/config" ]] && mv -f "${START_PATH}"/term-sd/config/* "${START_PATH}"/term-sd-tmp/config
[[ -d "${START_PATH}/term-sd/cache" ]] && mv -f "${START_PATH}"/term-sd/cache "${START_PATH}"/term-sd-tmp
[[ -d "${START_PATH}/term-sd/requirements-backup" ]] && mv -f "${START_PATH}"/term-sd/requirements-backup "${START_PATH}"/term-sd-tmp
[[ -d "${START_PATH}/term-sd/backup" ]] && mv -f "${START_PATH}"/term-sd/backup "${START_PATH}"/term-sd-tmp
}
# 恢复 cache 文件夹
# 从 <Start Path>/term-sd-tmp 恢复文件到 <Start Path>/term-sd 中
term_sd_restore_config() {
term_sd_echo "恢复 Term-SD 缓存文件夹和配置文件中"
[[ -d "${START_PATH}/term-sd-tmp/cache" ]] && mv -f "${START_PATH}/term-sd-tmp/cache" "${START_PATH}/term-sd"
[[ -d "${START_PATH}/term-sd-tmp/config" ]] && mv -f "${START_PATH}"/term-sd-tmp/config/* "${START_PATH}/term-sd/config"
[[ -d "${START_PATH}/term-sd-tmp/requirements-backup" ]] && mv -f "${START_PATH}/term-sd-tmp/requirements-backup" "${START_PATH}/term-sd"
[[ -d "${START_PATH}/term-sd-tmp/backup" ]] && mv -f "${START_PATH}/term-sd-tmp/backup" "${START_PATH}/term-sd"
rm -rf "${START_PATH}/term-sd-tmp"
}
# 设置默认 Term-SD 设置
# 设置完成后使用 <Start Path>/term-sd/config/install-by-launch-script.lock 标记已执行设置
term_sd_set_up_normal_setting() {
if [[ ! -f "${START_PATH}/term-sd/config/term-sd-watch-retry.conf" ]]; then
echo "3" > "${START_PATH}/term-sd/config/term-sd-watch-retry.conf"
TERM_SD_CMD_RETRY=3
term_sd_echo "Term-SD 命令执行监测设置已自动设置"
fi
if [[ ! -f "${START_PATH}/term-sd/config/term-sd-auto-update.lock" ]]; then
touch "${START_PATH}/term-sd/config/term-sd-auto-update.lock"
date +'%Y-%m-%d %H:%M:%S' > "${START_PATH}/term-sd/config/term-sd-auto-update-time.conf"
term_sd_echo "Term-SD 自动更新已自动设置"
fi
if [[ ! -f "${START_PATH}/term-sd/config/set-aria2-thread.conf" ]]; then
echo "16" > "${START_PATH}/term-sd/config/set-aria2-thread.conf"
term_sd_echo "Term-SD 设置 Aria2 下载线程为 16"
fi
if [[ ! -f "${START_PATH}/term-sd/config/term-sd-pip-mirror.conf" ]]; then
echo "2" > "${START_PATH}/term-sd/config/term-sd-pip-mirror.conf"
term_sd_echo "Term-SD 设置 Pip 镜像源为国内镜像源"
fi
if [[ ! -f "${START_PATH}/term-sd/config/set-dynamic-global-github-mirror.lock" ]]; then
touch "${START_PATH}"/term-sd/config/set-dynamic-global-github-mirror.lock
term_sd_echo "Term-SD 启用 Github 镜像源"
fi
if [[ ! -f "${START_PATH}/term-sd/config/set-dynamic-global-huggingface-mirror.lock" ]]; then
touch "${START_PATH}"/term-sd/config/set-dynamic-global-huggingface-mirror.lock
term_sd_echo "Term-SD 启用 HuggingFace 镜像源"
fi
if [[ ! -f "${START_PATH}/term-sd/config/set-cuda-memory-alloc.lock" ]];then
touch "${START_PATH}/term-sd/config/set-cuda-memory-alloc.lock"
term_sd_echo "Term-SD 启用 CUDA 内存分配器设置"
fi
if [[ ! -f "${START_PATH}/term-sd/config/enable-dynamic-proxy.lock" ]]; then
touch -f "${START_PATH}/term-sd/config/enable-dynamic-proxy.lock"
term_sd_echo "Term-SD 启动动态代理设置"
fi
touch "${START_PATH}/term-sd/config/install-by-launch-script.lock"
}
# Term-SD 卸载功能
term_sd_remove() {
term_sd_echo "是否卸载 Term-SD ?"
term_sd_echo "警告: 该操作将永久删除 Term-SD 目录中的所有文件, 包括 AI 软件下载的部分模型文件 (存在于 Term-SD 目录中的 cache 文件夹, 如有必要, 请备份该文件夹)"
term_sd_echo "提示: 输入 yes 或 no 后回车"
case "$(term_sd_read)" in
y|yes|YES|Y)
term_sd_echo "开始卸载 Term-SD"
rm -rf "${START_PATH}/term-sd"
rm -rf "${START_PATH}/term-sd.sh"
if [[ "${USER_SHELL}" == "bash" ]] || [[ "${USER_SHELL}" == "zsh" ]]; then
remove_config_from_shell
fi
term_sd_echo "Term-SD 卸载完成"
;;
*)
term_sd_echo "取消操作"
;;
esac
exit 0
}
# Term-SD 添加快捷命令功能
# 仅支持在 Bash, Zsh 中添加快捷启动 Term-SD 命令
# 快捷启动命令保存在 .bashrc / .zshrc 文件中
install_cmd_to_shell() {
while true; do
case "${USER_SHELL}" in
bash|zsh)
term_sd_echo "是否将 Term-SD 快捷启动命令添加到 Shell 环境中 ?"
term_sd_echo "添加后可使用 term_sd, tsd 命令启动 Term-SD"
term_sd_echo "1、添加"
term_sd_echo "2、删除"
term_sd_echo "3、退出"
term_sd_echo "提示: 输入数字后回车"
case "$(term_sd_read)" in
1)
if cat ~/."${USER_SHELL}"rc | grep term_sd > /dev/null; then
term_sd_echo "Term-SD 快捷启动命令已存在, 是否刷新 (yes/no) ? "
term_sd_echo "提示: 输入 yes 或 no 后回车"
case "$(term_sd_read)" in
y|yes|YES|Y)
remove_config_from_shell
install_config_to_shell
term_sd_echo "Term-SD 快捷启动命令刷新完成, 可使用 term_sd, tsd 命令启动 Term-SD, 退出 Term-SD 并重启 Shell"
exec "${SHELL}"
;;
*)
term_sd_echo "取消更新 Term-SD 快捷启动命令操作"
;;
esac
else
install_config_to_shell
term_sd_echo "Term-SD 快捷启动命令添加完成, 可使用 term_sd, tsd 命令启动 Term-SD, 退出 Term-SD 并重启 Shell"
exec "${SHELL}"
fi
break
;;
2)
remove_config_from_shell
term_sd_echo "Term-SD 快捷启动命令已删除, 退出 Term-SD 并重启 Shell"
exec "${SHELL}"
break
;;
3)
exit 0
;;
*)
term_sd_echo "输入有误, 请重试"
;;
esac
;;
*)
term_sd_echo "不支持该 Shell"
;;
esac
done
}
# 将快捷命令写入 Shell 配置文件中
install_config_to_shell() {
cat<<EOF >> ~/."${USER_SHELL}"rc
# Term-SD
term_sd(){ "$(pwd)/term-sd.sh" "\$@" || echo -e "[\033[33m\$(date "+%Y-%m-%d %H:%M:%S")\033[0m][\033[36mTerm-SD\033[0m]\033[36m::\033[0m Term-SD 异常退出" ; }
alias tsd="term_sd"
EOF
}
# 将快捷命令从 Shell 配置文件中删除
remove_config_from_shell() {
sed -i '/# Term-SD/d' ~/."${USER_SHELL}"rc
sed -i '/term_sd(){/d' ~/."${USER_SHELL}"rc
sed -i '/alias tsd/d' ~/."${USER_SHELL}"rc
}
# 手动指定 Python 路径功能
# 如果 Python 可用, 则使用 TERM_SD_PYTHON_PATH 全局变量保存 Python 路径
# 将 Python 路径保存在 <Start Path>/term-sd/config/python-path.conf 文件中
set_python_path() {
local input_python_path
while true; do
if [[ -z "$@" ]]; then
term_sd_echo "请输入 Python 解释器的路径"
term_sd_echo "提示: 输入完后请回车保存, 或者输入 exit 退出"
read -p "===============================> " input_python_path
if [[ -z "${input_python_path}" ]]; then
term_sd_echo "未输入, 请重试"
elif [[ "${input_python_path}" = "exit" ]]; then
term_sd_echo "退出 Python 解释器路径指定功能"
break
elif [[ -f "${input_python_path}" ]]; then
TERM_SD_PYTHON_PATH=$(term_sd_win2unix_path "${input_python_path}")
echo "${TERM_SD_PYTHON_PATH}" > "${START_PATH}/term-sd/config/python-path.conf"
term_sd_echo "Python 解释器路径指定完成"
term_sd_echo "提示:"
term_sd_echo "使用 --set-python-path 重新设置 Python 解释器路径"
term_sd_echo "使用 --unset-python-path 删除 Python 解释器路径设置"
break
else
term_sd_echo "输入的路径有误, 请重试"
fi
else # 直接将选项后面的参数作为路径
if [[ -f "$@" ]]; then
term_sd_echo "设置 Python 解释器路径: $@"
TERM_SD_PYTHON_PATH=$(term_sd_win2unix_path "$@")
echo "$TERM_SD_PYTHON_PATH" > "${START_PATH}/term-sd/config/python-path.conf"
term_sd_echo "Python 解释器路径指定完成"
term_sd_echo "提示:"
term_sd_echo "使用 --set-python-path 重新设置 Python 解释器路径"
term_sd_echo "使用 --unset-python-path 删除 Python 解释器路径设置"
break
else
term_sd_echo "输入的路径有误, 跳过指定 Python 解释器路径"
break
fi
fi
done
}
# 配置内存优化(仅限Linux)
prepare_tcmalloc() {
local LIBC_VER
local libc_vernum
local libc_v234
local TCMALLOC_LIBS
local lib
local TCMALLOC
local TC_INFO
case "${USE_TCMALLOC}" in
0)
term_sd_echo "取消加载内存优化"
;;
*)
if [[ "${OSTYPE}" == "linux"* ]] && [[ -z "${LD_PRELOAD}" ]]; then
term_sd_echo "检测到系统为 Linux, 尝试启用内存优化"
# 检查glibc版本
LIBC_VER=$(echo $(ldd --version | awk 'NR==1 {print $NF}') | grep -oP '\d+\.\d+')
term_sd_echo "glibc 版本为 ${LIBC_VER}"
libc_vernum=$(expr ${LIBC_VER})
# 从 2.34 开始,libpthread 已经集成到 libc.so 中
libc_v234=2.34
# 定义 Tcmalloc 库数组
TCMALLOC_LIBS=("libtcmalloc(_minimal|)\.so\.\d" "libtcmalloc\.so\.\d")
# 遍历数组
for lib in "${TCMALLOC_LIBS[@]}"
do
# 确定库支持的 Tcmalloc 类型
TCMALLOC="$(PATH=/usr/sbin:${PATH} ldconfig -p | grep -P "${lib}" | head -n 1)"
TC_INFO=(${TCMALLOC//=>/})
if [[ ! -z "${TC_INFO}" ]]; then
term_sd_echo "检查 TCMalloc: ${TC_INFO}"
# 确定库是否链接到 libpthread 和解析未定义符号: pthread_key_create
if [ $(echo "$libc_vernum < $libc_v234" | bc) -eq 1 ]; then
# glibc < 2.34,pthread_key_create 在 libpthread.so 中。检查链接到 libpthread.so
if ldd ${TC_INFO[2]} | grep -q 'libpthread'; then
term_sd_echo "$TC_INFO 链接到 libpthread, 执行 LD_PRELOAD=${TC_INFO[2]}"
# 设置完整路径 LD_PRELOAD
export LD_PRELOAD="${TC_INFO[2]}"
break
else
term_sd_echo "${TC_INFO} 没有链接到 libpthread, 将触发未定义符号: pthread_Key_create 错误"
fi
else
# libc.so(glibc)的2.34版本已将pthread库集成到glibc内部。在Ubuntu 22.04系统以及现代Linux系统和WSL(Windows Subsystem for Linux)环境下
# libc.so(glibc)链接了一个几乎能在所有Linux用户态环境中运行的库,因此通常无需额外检查
term_sd_echo "${TC_INFO} 链接到 libc.so, 执行 LD_PRELOAD=${TC_INFO[2]}"
# 设置完整路径 LD_PRELOAD
export LD_PRELOAD="${TC_INFO[2]}"
break
fi
fi
done
if [[ -z "${LD_PRELOAD}" ]]; then
term_sd_echo "无法定位 TCMalloc。未在系统上找到 tcmalloc 或 google-perftool"
term_sd_echo "取消加载内存优化"
term_sd_echo "提示: 可根据 Term-SD 帮助文档安装 google-perftool"
term_sd_sleep 3
fi
fi
;;
esac
}
# 配置默认 Git 配置
set_normal_git_config() {
git config --global --add safe.directory "*"
git config --global core.longpaths true
}
# 自动选择 Github 镜像源
# 如果有可用的镜像源, 则使用 GIT_CONFIG_GLOBAL 环境变量指定 Git 配置文件路径
# 使用 <Start Path>/term-sd/config/set-global-github-mirror.conf 保存镜像源地址
term_sd_auto_setup_github_mirror() {
if [[ -f "${START_PATH}/term-sd/config/set-dynamic-global-github-mirror.lock" ]]; then
export GIT_CONFIG_GLOBAL="${START_PATH}/term-sd/config/.gitconfig"
local mirror_status=0
local i
local url
local github_mirror
local HTTP_PROXY
local HTTPS_PROXY
HTTP_PROXY=
HTTPS_PROXY=
rm -f "${START_PATH}"/term-sd/config/.gitconfig
rm -f "${START_PATH}"/term-sd/config/set-global-github-mirror.conf
for i in ${GITHUB_MIRROR_LIST}; do
rm -rf "${START_PATH}/term-sd/task/github_mirror_test" &> /dev/null
github_mirror=$(echo ${i} | awk '{sub("/term_sd_git_user/term_sd_git_repo","")}1')
term_sd_echo "测试 Github 镜像源: ${github_mirror}"
url=$(echo ${i} | awk '{sub("term_sd_git_user","licyk")}1' | awk '{sub("term_sd_git_repo","empty")}1') # 生成格式化之后的链接
git clone "${url}" "${START_PATH}/term-sd/task/github_mirror_test" --depth=1 &> /dev/null # 测试镜像源是否正常连接
git_req=$?
rm -rf "${START_PATH}/term-sd/task/github_mirror_test" &> /dev/null
if [[ "${git_req}" == 0 ]]; then
term_sd_echo "该 Github 镜像源可用"
mirror_status=1
break
fi
done
if [[ "${mirror_status}" == 1 ]]; then
term_sd_echo "设置 Github 镜像源"
set_normal_git_config
git config --global url."${github_mirror}".insteadOf "https://github.com"
echo "${github_mirror}" > "${START_PATH}"/term-sd/config/set-global-github-mirror.conf
else
term_sd_echo "无可用 Github 镜像源, 取消使用 Github 镜像源"
unset GIT_CONFIG_GLOBAL
fi
fi
}
# 自动选择 HuggingFace 镜像源
# 如果有可用的 HuggingFace 镜像, 则使用 HF_ENDPOINT 环境变量指定 HuggingFace 镜像源
# 使用 <Start Path>/term-sd/config/set-global-huggingface-mirror.conf 保存镜像源地址
term_sd_auto_setup_huggingface_mirror() {
if [[ -f "${START_PATH}/term-sd/config/set-dynamic-global-huggingface-mirror.lock" ]]; then
local mirror_status=0
local i
local huggingface_mirror
local HTTP_PROXY
local HTTPS_PROXY
HTTP_PROXY=
HTTPS_PROXY=
rm -f "${START_PATH}"/term-sd/config/set-global-huggingface-mirror.conf
for i in ${HUGGINGFACE_MIRROR_LIST}; do
term_sd_echo "测试 HuggingFace 镜像源: ${i}"
curl ${i}/licyk/sd-model/resolve/main/README.md -o /dev/null --connect-timeout 10 --silent
if [[ "$?" == 0 ]]; then
term_sd_echo "该 HuggingFace 镜像源可用"
huggingface_mirror=$i
mirror_status=1
break
fi
done
if [[ "${mirror_status}" == 1 ]]; then
term_sd_echo "设置 HuggingFace 镜像源"
export HF_ENDPOINT=$huggingface_mirror
echo "${huggingface_mirror}" > "${START_PATH}"/term-sd/config/set-global-huggingface-mirror.conf
else
term_sd_echo "无可用 HuggingFace 镜像源, 取消设置 HuggingFace 镜像源"
fi
fi
}
# 用户协议
# 同意用户协议后使用 <Start Path>/term-sd/config/agree-user-agreement.lock 标记
term_sd_user_agreement() {
if [[ ! -f "${START_PATH}/term-sd/config/agree-user-agreement.lock" ]]; then
term_sd_print_line "用户协议"
cat term-sd/help/user_agreement.md
echo
term_sd_print_line
term_sd_echo "是否同意该用户协议 (yes/no) ?"
case "$(term_sd_read)" in
yes|y|YES|Y)
touch "${START_PATH}/term-sd/config/agree-user-agreement.lock"
term_sd_echo "确认同意该用户协议"
;;
*)
term_sd_echo "取消同意该用户协议"
term_sd_echo "退出 Term-SD"
exit 0
;;
esac
fi
}
# 获取 Dialog 的宽高度
get_dialog_size() {
echo "${DIALOG_HEIGHT}" "${DIALOG_WIDTH}"
}
# 获取 Dialog 的宽高度(附带 Dialog 菜单高度)
get_dialog_size_menu() {
echo "${DIALOG_HEIGHT}" "${DIALOG_WIDTH}" "${DIALOG_MENU_HEIGHT}"
}
main() {
# 切换到 term-sd.sh 文件所在位置
cd "$(cd "$(dirname "$0")" ; pwd)"
# 目录结构检测, 发现错误时修正路径
if [[ ! -d "term-sd" ]] \
&& [[ -d "modules" ]] \
&& [[ -f "modules/init.sh" ]] \
&& [[ -d "python_modules" ]] \
&& [[ -d "extra" ]] \
&& [[ -d "install" ]] \