Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
454 lines (384 loc) · 17.7 KB
/
Copy pathmain.py
File metadata and controls
454 lines (384 loc) · 17.7 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
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
"""
ScriptDeck - Python 脚本导航器(原 Python Script Navigator)
轻量级 Web 启动台,自动扫描指定目录下的 .bat 文件和 README 文件,提供快速启动入口。
"""
import json
import os
import subprocess
import sys
from fnmatch import fnmatch
from pathlib import Path
from flask import Flask, render_template, jsonify, request, send_from_directory
app = Flask(__name__)
CONFIG_PATH = Path(__file__).parent / "config.json"
# ─── 配置管理 ─────────────────────────────────────────────────────────────────
def load_config():
if not CONFIG_PATH.exists():
default = {"scan_root": "C:\\project", "exclude_dirs": ["node_modules", ".git", "__pycache__", "venv", ".idea"], "exclude_bats": ["start_*"], "exclude_scripts": [], "host": "127.0.0.1", "port": 5000}
save_config(default)
return default
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
return json.load(f)
def save_config(cfg):
with open(CONFIG_PATH, "w", encoding="utf-8") as f:
json.dump(cfg, f, ensure_ascii=False, indent=2)
CFG = load_config()
# ─── 扫描逻辑 ─────────────────────────────────────────────────────────────────
README_NAMES = {"readme", "readme.md", "readme.txt", "readme.rst", "说明.md", "说明.txt"}
def is_readme(name: str) -> bool:
return name.lower() in README_NAMES or name.lower().startswith("readme")
def bat_excluded(name: str, exclude_bats: list) -> bool:
"""检查 bat 文件名是否匹配任一排除规则(支持 * ? % 通配符)"""
if not exclude_bats:
return False
lower_name = name.lower()
for pattern in exclude_bats:
# % 兼容 SQL 风格通配符,转为 fnmatch 的 *
pat = pattern.replace("%", "*").lower()
if fnmatch(lower_name, pat):
return True
return False
PREFIXES = ("::", "REM", "rem")
# Windows 常见编码,按优先级排列
BAT_ENCODINGS = ["utf-8-sig", "utf-8", "gbk", "gb2312", "gb18030"]
def _read_file_with_encoding(filepath: str, max_lines: int = 10):
"""尝试多种编码读取文件前 N 行,返回 (lines_list, used_encoding)。"""
raw_bytes = b""
try:
with open(filepath, "rb") as f:
for _ in range(max_lines):
line = f.readline()
if not line:
break
raw_bytes += line
except Exception:
return [], ""
# 按优先级尝试解码
for enc in BAT_ENCODINGS:
try:
text = raw_bytes.decode(enc)
lines = [line.rstrip("\r\n") for line in text.split("\n")]
return lines, enc
except (UnicodeDecodeError, UnicodeError):
continue
# 最终回退:用 errors='replace'
text = raw_bytes.decode("utf-8", errors="replace")
return [line.rstrip("\r\n") for line in text.split("\n")], "utf-8-fallback"
def parse_bat_metadata(bat_path: str) -> dict:
"""
解析 bat 文件头部的内嵌元数据。
支持的元数据格式:
@echo off
:: title: 脚本标题
:: desc: 脚本描述
或:
@echo off
REM title: 脚本标题
REM desc: 脚本描述
:: 和 REM 两种注释风格均可被识别,且可混合使用。
规则:
- 第一行必须以 @echo 开头
- 从第二行开始匹配连续的注释行(以 :: 或 REM 开头)
- 遇到非注释行就停止
- 返回 {"title": "...", "desc": "..."},未匹配到则返回空字符串
"""
meta = {"title": "", "desc": ""}
lines, _ = _read_file_with_encoding(bat_path, max_lines=10)
if not lines:
return meta
# 第一行必须以 @echo 开头
first_line = lines[0].strip().lower()
if not first_line.startswith("@echo"):
return meta
# 从第二行开始解析连续的注释块
for line in lines[1:]:
stripped = line.strip()
detected_prefix = None
for p in PREFIXES:
if stripped.startswith(p):
detected_prefix = p
break
if detected_prefix is None:
break # 遇到非注释行,停止解析
content = stripped[len(detected_prefix):].strip() # 去掉 :: 或 REM 前缀
if ":" in content:
key, _, value = content.partition(":")
key = key.strip().lower()
value = value.strip()
if key == "title" and value:
meta["title"] = value
elif key == "desc" and value:
meta["desc"] = value
return meta
def parse_vbs_metadata(vbs_path: str) -> dict:
"""
解析 vbs 文件头部的内嵌元数据(单引号注释块)。
"""
return _parse_comment_metadata(vbs_path, "'")
def parse_sh_metadata(sh_path: str) -> dict:
"""
解析 sh 文件头部的内嵌元数据(# 注释块):
# title: 脚本标题
# desc: 脚本描述
sh 脚本头部通常是 shebang + 若干代码行,title/desc 注释可能散布在前几行
(如第 1 行 #!、第 2 行 SCRIPT_DIR=...、第 3 行 # title:),
因此不要求注释连续:在前 10 行内扫描所有 # 注释行(跳过 #! shebang),
握手即取 title: / desc:。
"""
meta = {"title": "", "desc": ""}
lines, _ = _read_file_with_encoding(sh_path, max_lines=10)
for line in lines:
stripped = line.strip()
if not stripped.startswith("#") or stripped.startswith("#!"):
continue # 代码行或 shebang,跳过(不中断扫描)
content = stripped[1:].strip()
if ":" in content:
key, _, value = content.partition(":")
key = key.strip().lower()
value = value.strip()
if key == "title" and value:
meta["title"] = value
elif key == "desc" and value:
meta["desc"] = value
return meta
def _parse_comment_metadata(path: str, prefix: str) -> dict:
meta = {"title": "", "desc": ""}
lines, _ = _read_file_with_encoding(path, max_lines=10)
if not lines:
return meta
for line in lines:
stripped = line.strip()
if not stripped.startswith(prefix):
break # 遇到非注释行,停止解析
content = stripped[len(prefix):].strip()
if ":" in content:
key, _, value = content.partition(":")
key = key.strip().lower()
value = value.strip()
if key == "title" and value:
meta["title"] = value
elif key == "desc" and value:
meta["desc"] = value
return meta
def scan_directory(scan_root: str, exclude_dirs: list, exclude_bats: list = None, exclude_scripts: list = None):
"""
scans scan_root 下的子文件夹,收集 .bat / .vbs / .sh 文件和 readme 文件。
exclude_scripts: 按完整路径排除的脚本列表。
脚本优先级:同目录同时存在多种脚本时,vbs > bat > sh(历史迁移顺序)。
返回按文件夹分组的数据结构:
[
{
"folder": "ai\\bacth_ai",
"folder_name": "bacth_ai",
"parent": "ai",
"scripts": [{"name": "xxx.bat", "path": "C:\\...\\xxx.bat", "type": "bat", "meta": {"title": "...", "desc": "..."}}],
"readme": {"name": "readme.md", "path": "...", "content": "..."}
}
]
"""
root = Path(scan_root)
if not root.exists():
return []
exclude_set = set(exclude_dirs)
exclude_script_set = set(os.path.normpath(p).lower() for p in (exclude_scripts or []))
results = []
for dirpath, dirnames, filenames in os.walk(root):
# 过滤排除目录(原地修改,阻止 os.walk 深入)
dirnames[:] = [d for d in dirnames if d not in exclude_set and not d.startswith(".")]
rel = os.path.relpath(dirpath, root)
if rel == ".":
continue
bats = []
vbs_scripts = []
sh_scripts = []
readme = None
for fname in filenames:
full = os.path.join(dirpath, fname)
lower_name = fname.lower()
if lower_name.endswith(".bat"):
norm_full = os.path.normpath(full).lower()
if norm_full in exclude_script_set:
continue
if not bat_excluded(fname, exclude_bats or []):
meta = parse_bat_metadata(full)
bats.append({"name": fname, "path": full, "meta": meta, "type": "bat"})
elif lower_name.endswith(".vbs"):
norm_full = os.path.normpath(full).lower()
if norm_full in exclude_script_set:
continue
if not bat_excluded(fname, exclude_bats or []):
meta = parse_vbs_metadata(full)
vbs_scripts.append({"name": fname, "path": full, "meta": meta, "type": "vbs"})
elif lower_name.endswith(".sh"):
norm_full = os.path.normpath(full).lower()
if norm_full in exclude_script_set:
continue
if not bat_excluded(fname, exclude_bats or []):
meta = parse_sh_metadata(full)
sh_scripts.append({"name": fname, "path": full, "meta": meta, "type": "sh"})
elif is_readme(fname):
try:
# 尝试多种编码读取 readme,避免中文乱码
raw = Path(full).read_bytes()
content_text = None
for enc in BAT_ENCODINGS:
try:
content_text = raw[:30000].decode(enc)
break
except (UnicodeDecodeError, UnicodeError):
continue
if content_text is None:
content_text = raw[:30000].decode("utf-8", errors="replace")
content = content_text[:3000]
except Exception:
content = "(无法读取)"
readme = {"name": fname, "path": full, "content": content}
# 脚本优先级:vbs > bat > sh(历史迁移顺序,兼容存量 vbs/bat 项目)
scripts = vbs_scripts if vbs_scripts else (bats if bats else sh_scripts)
if scripts or readme:
parts = Path(rel).parts
results.append({
"folder": rel.replace("\\", "/"),
"folder_name": parts[-1],
"parent": parts[0] if len(parts) > 1 else "",
"scripts": scripts,
"readme": readme,
})
# 按 folder 排序
results.sort(key=lambda x: x["folder"].lower())
return results
# ─── 路由 ─────────────────────────────────────────────────────────────────────
@app.route("/")
def index():
return render_template("index.html")
@app.route("/logo.png")
def logo():
return send_from_directory(os.path.join(app.root_path, "templates"), "logo.png")
@app.route("/api/scan")
def api_scan():
global CFG
CFG = load_config()
data = scan_directory(CFG["scan_root"], CFG.get("exclude_dirs", []), CFG.get("exclude_bats", []), CFG.get("exclude_scripts", []))
return jsonify({"scan_root": CFG["scan_root"], "items": data, "total": len(data), "exclude_scripts": CFG.get("exclude_scripts", [])})
@app.route("/api/set-root", methods=["POST"])
def api_set_root():
"""修改扫描根目录"""
global CFG
body = request.json or {}
new_root = body.get("scan_root", "").strip()
if not new_root:
return jsonify({"ok": False, "msg": "路径不能为空"}), 400
if not os.path.isdir(new_root):
return jsonify({"ok": False, "msg": f"路径不存在:{new_root}"}), 400
CFG["scan_root"] = new_root
save_config(CFG)
data = scan_directory(CFG["scan_root"], CFG.get("exclude_dirs", []), CFG.get("exclude_bats", []), CFG.get("exclude_scripts", []))
return jsonify({"ok": True, "scan_root": new_root, "items": data, "total": len(data)})
@app.route("/api/exclude-bats", methods=["GET", "POST"])
def api_exclude_bats():
"""查询或更新 bat 排除规则"""
global CFG
if request.method == "GET":
return jsonify({"exclude_bats": CFG.get("exclude_bats", [])})
body = request.json or {}
patterns = body.get("exclude_bats", [])
if not isinstance(patterns, list):
return jsonify({"ok": False, "msg": "exclude_bats 必须是数组"}), 400
CFG["exclude_bats"] = [p.strip() for p in patterns if p.strip()]
save_config(CFG)
data = scan_directory(CFG["scan_root"], CFG.get("exclude_dirs", []), CFG.get("exclude_bats", []), CFG.get("exclude_scripts", []))
return jsonify({"ok": True, "exclude_bats": CFG["exclude_bats"], "items": data, "total": len(data)})
@app.route("/api/exclude-script", methods=["POST"])
def api_exclude_script():
"""排除或取消排除单个脚本(按完整路径)"""
global CFG
body = request.json or {}
path = body.get("path", "").strip()
action = body.get("action", "add") # "add" or "remove"
if not path:
return jsonify({"ok": False, "msg": "路径不能为空"}), 400
exclude_scripts = CFG.get("exclude_scripts", [])
norm_path = os.path.normpath(path).lower()
if action == "add":
if norm_path not in [os.path.normpath(p).lower() for p in exclude_scripts]:
exclude_scripts.append(path)
elif action == "remove":
exclude_scripts = [p for p in exclude_scripts if os.path.normpath(p).lower() != norm_path]
CFG["exclude_scripts"] = exclude_scripts
save_config(CFG)
data = scan_directory(CFG["scan_root"], CFG.get("exclude_dirs", []), CFG.get("exclude_bats", []), CFG.get("exclude_scripts", []))
return jsonify({"ok": True, "exclude_scripts": CFG["exclude_scripts"], "items": data, "total": len(data)})
# ─── sh 执行辅助(Windows 依赖 Git Bash)──────────────────────────────────────
def _find_git_bash():
"""定位 Git Bash(跳过 WSL 的 System32\\bash.exe,路径语义不同)。"""
candidates = []
for var in ("ProgramFiles", "ProgramFiles(x86)"):
base = os.environ.get(var)
if base:
candidates.append(os.path.join(base, "Git", "bin", "bash.exe"))
local = os.environ.get("LocalAppData")
if local:
candidates.append(os.path.join(local, "Programs", "Git", "bin", "bash.exe"))
for c in candidates:
if os.path.isfile(c):
return c
return None
def _to_unix_path(path: str) -> str:
"""Windows 路径转 Git Bash 风格:D:\\a b\\x.sh → /d/a b/x.sh"""
path = path.replace("/", "\\")
if len(path) >= 2 and path[1] == ":":
return "/" + path[0].lower() + path[2:].replace("\\", "/")
return path.replace("\\", "/")
@app.route("/api/run-bat", methods=["POST"])
def api_run_bat():
"""运行脚本文件(.bat / .vbs / .sh)"""
body = request.json or {}
script_path = body.get("path", "").strip()
if not script_path or not os.path.isfile(script_path):
return jsonify({"ok": False, "msg": f"文件不存在:{script_path}"}), 400
basename = os.path.basename(script_path)
try:
script_dir = os.path.dirname(script_path)
if script_path.lower().endswith(".vbs"):
# VBS 通过 wscript.exe 执行,脚本自身控制窗口(如隐藏 GUI)
subprocess.Popen(f'wscript.exe "{script_path}"', shell=True, cwd=script_dir)
elif script_path.lower().endswith(".sh"):
# sh 通过 Git Bash 在新控制台窗口执行,脚本结束后保留交互 bash(对齐 cmd /k 行为)
bash = _find_git_bash()
if not bash:
return jsonify({"ok": False, "msg": "未找到 Git Bash,请安装 Git for Windows"}), 400
unix_path = _to_unix_path(script_path).replace("'", "'\\''")
subprocess.Popen(
f'"{bash}" --login -i -c "bash \'{unix_path}\'; exec bash --login"',
shell=True, cwd=script_dir, creationflags=subprocess.CREATE_NEW_CONSOLE
)
else:
# chcp 65001 将 cmd 切换到 UTF-8 编码,避免 bat 中的中文路径乱码
subprocess.Popen(
f'start "Running: {basename}" cmd /k "chcp 65001 >nul && "{script_path}""',
shell=True, cwd=script_dir
)
return jsonify({"ok": True, "msg": f"已启动:{basename}"})
except Exception as e:
return jsonify({"ok": False, "msg": str(e)}), 500
@app.route("/api/open-folder", methods=["POST"])
def api_open_folder():
"""在资源管理器中打开文件夹"""
body = request.json or {}
folder = body.get("path", "").strip()
if not folder or not os.path.isdir(folder):
return jsonify({"ok": False, "msg": "文件夹不存在"}), 400
try:
subprocess.Popen(f'explorer "{folder}"', shell=True)
return jsonify({"ok": True})
except Exception as e:
return jsonify({"ok": False, "msg": str(e)}), 500
# ─── 启动 ─────────────────────────────────────────────────────────────────────
if __name__ == "__main__":
host = CFG.get("host", "127.0.0.1")
port = CFG.get("port", 5000)
print(f"ScriptDeck 启动中...")
print(f"扫描目录:{CFG['scan_root']}")
print(f"访问地址:http://{host}:{port}")
print("按 Ctrl+C 停止")
app.run(host=host, port=port, debug=False)