修改支持文件
This commit is contained in:
@@ -14,38 +14,34 @@ def _find_support_root(hint: Optional[Path]) -> Optional[Path]:
|
||||
2) 其次:环境变量 SUPPORT_DDI_DIR
|
||||
3) 再次:从 __file__ 所在目录向上搜索 3 层,找名为 'SupportFiles' 的目录
|
||||
"""
|
||||
# 1) 显式参数
|
||||
if hint and hint.exists():
|
||||
return hint.resolve()
|
||||
|
||||
# 2) 环境变量
|
||||
env = os.environ.get("SUPPORT_DDI_DIR")
|
||||
if env:
|
||||
p = Path(env).expanduser()
|
||||
if p.exists():
|
||||
return p.resolve()
|
||||
|
||||
# 3) 向上搜索
|
||||
here = Path(__file__).resolve().parent
|
||||
for i in range(4): # 当前目录 + 向上 3 层
|
||||
for _ in range(4): # 当前目录 + 向上 3 层
|
||||
cand = here / "SupportFiles"
|
||||
if cand.exists():
|
||||
return cand.resolve()
|
||||
here = here.parent
|
||||
|
||||
return None
|
||||
|
||||
|
||||
@dataclass
|
||||
class DevDiskImageDeployer:
|
||||
"""
|
||||
同步 SupportFiles/<version>/... 到 ~/.tidevice/device-support/<version>/...
|
||||
- 自动定位 SupportFiles(显式参数/环境变量/向上搜索)
|
||||
- 已存在则跳过,不存在才复制
|
||||
- 详细日志
|
||||
同步 SupportFiles/<version>/ 或 SupportFiles/<version>.zip 到 ~/.tidevice/device-support
|
||||
- 目录:复制为 ~/.tidevice/device-support/<version>/
|
||||
- zip:原样复制为 ~/.tidevice/device-support/<version>.zip (不解压)
|
||||
- 已存在则跳过;如设置 overwrite=True 则覆盖
|
||||
"""
|
||||
project_support_root: Optional[Path] = None # 可不传,自动发现
|
||||
cache_root: Optional[Path] = None # 默认 ~/.tidevice/device-support
|
||||
project_support_root: Optional[Path] = None
|
||||
cache_root: Optional[Path] = None
|
||||
verbose: bool = True
|
||||
dry_run: bool = False
|
||||
overwrite: bool = False
|
||||
@@ -73,7 +69,6 @@ class DevDiskImageDeployer:
|
||||
print(f"[INFO] resolved SupportFiles = {self._src_dir}")
|
||||
print(f"[INFO] cache_dir = {self._cache_dir}")
|
||||
|
||||
# 打印一眼能看见的兄弟目录,防路径误判
|
||||
parent = self._src_dir.parent
|
||||
try:
|
||||
siblings = ", ".join(sorted(p.name for p in parent.iterdir() if p.is_dir()))
|
||||
@@ -83,29 +78,58 @@ class DevDiskImageDeployer:
|
||||
pass
|
||||
|
||||
def deploy_all(self):
|
||||
candidates = list(self._iter_version_dirs(self._src_dir))
|
||||
entries = list(self._iter_version_entries(self._src_dir))
|
||||
copied = skipped = 0
|
||||
for ver_dir in candidates:
|
||||
dst = self._cache_dir / ver_dir.name
|
||||
if dst.exists() and not self.overwrite:
|
||||
skipped += 1
|
||||
continue
|
||||
|
||||
if dst.exists() and self.overwrite:
|
||||
if not self.dry_run:
|
||||
for p, kind, version in entries:
|
||||
# kind: "dir" 或 "zip"
|
||||
if kind == "dir":
|
||||
dst = self._cache_dir / version
|
||||
exists = dst.exists()
|
||||
if exists and not self.overwrite:
|
||||
skipped += 1
|
||||
if self.verbose:
|
||||
print(f"[SKIP] {dst} 已存在(目录)")
|
||||
continue
|
||||
if exists and self.overwrite and not self.dry_run:
|
||||
shutil.rmtree(dst)
|
||||
|
||||
if self.verbose:
|
||||
print(f"[COPY] {ver_dir} -> {dst}")
|
||||
if not self.dry_run:
|
||||
shutil.copytree(ver_dir, dst)
|
||||
copied += 1
|
||||
if self.verbose:
|
||||
print(f"[COPY] DIR {p} -> {dst}")
|
||||
if not self.dry_run:
|
||||
shutil.copytree(p, dst)
|
||||
copied += 1
|
||||
|
||||
elif kind == "zip":
|
||||
dst = self._cache_dir / f"{version}.zip"
|
||||
exists = dst.exists()
|
||||
if exists and not self.overwrite:
|
||||
skipped += 1
|
||||
if self.verbose:
|
||||
print(f"[SKIP] {dst} 已存在(zip)")
|
||||
continue
|
||||
if exists and self.overwrite and not self.dry_run:
|
||||
dst.unlink()
|
||||
|
||||
if self.verbose:
|
||||
print(f"[COPY] ZIP {p} -> {dst}")
|
||||
if not self.dry_run:
|
||||
# 用 copy2 保留 mtime 等元数据
|
||||
shutil.copy2(p, dst)
|
||||
copied += 1
|
||||
|
||||
if self.verbose:
|
||||
print(f"[SUMMARY] copied={copied}, skipped={skipped}, total={copied+skipped}")
|
||||
|
||||
# -------- helpers --------
|
||||
def _iter_version_dirs(self, root: Path) -> Iterable[Path]:
|
||||
def _iter_version_entries(self, root: Path) -> Iterable[tuple[Path, str, str]]:
|
||||
"""
|
||||
迭代返回 (路径, 类型, 版本号)
|
||||
- 目录:名称需匹配版本号
|
||||
- zip:stem(去除后缀)的名称需匹配版本号
|
||||
"""
|
||||
for p in sorted(root.iterdir()):
|
||||
if p.is_dir() and VERSION_RE.match(p.name):
|
||||
yield p
|
||||
yield (p, "dir", p.name)
|
||||
elif p.is_file() and p.suffix.lower() == ".zip" and VERSION_RE.match(p.stem):
|
||||
yield (p, "zip", p.stem)
|
||||
|
||||
Reference in New Issue
Block a user