Files
change_hair/fix_conda_paths.py
xsl 443cfa298f 初始化:换发型/换发色/训练发型服务
包含:
- hair_service_sd: 主服务(换发型/换发色/生发,端口8801)
- photo_service: LoRA调度+训练(端口32678)
- hair_grow_service: 调试测试页(端口8888,含4个测试页)
- 批量训练脚本(batch_train_hairstyles.py)
- 发际线mask自动识别(hairline_mask.py,4种方案)
- 手绘mask换发型(hair_swap_manual.py)
- 文档:README.md + LARGE_FILES.md + docs/

大文件(模型权重200G、训练数据123G)已排除,见 LARGE_FILES.md
OSS/COS密钥已脱敏为环境变量,原文件备份在本地
2026-07-07 13:53:52 +08:00

70 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""安全修复 conda 环境内的硬编码路径前缀。
只处理文本文件(通过二进制检测),跳过 .so/.pyc/权重等二进制文件。
"""
import os
import sys
OLD_PREFIXES = [
b"/home/szlc/miniconda3",
b"/usr/local/miniconda3",
]
NEW_PREFIX = b"/home/xsl/miniconda3"
def is_probably_binary(filepath):
"""通过文件扩展名和内容判断是否为二进制文件"""
# 明确的文本扩展名直接处理
text_exts = ('.py', '.sh', '.txt', '.cfg', '.ini', '.json', '.yaml', '.yml',
'.cmake', '.make', '.pc', '.la', '.template', '.desktop',
'.service', '.conf', '.md', '.rst', '.in', '.bashrc', '.profile')
binary_exts = ('.so', '.pyc', '.pyo', '.a', '.o', '.dylib', '.dll',
'.pth', '.pt', '.bin', '.safetensors', '.ckpt', '.npz',
'.npy', '.pkl', '.zip', '.tar', '.gz', '.bz2', '.png',
'.jpg', '.jpeg', '.woff', '.ttf', '.eot', '.ico')
if filepath.endswith(binary_exts):
return True
if filepath.endswith(text_exts):
return False
# 无明确扩展名:读取前 8KB 检测是否含 null 字节
try:
with open(filepath, 'rb') as f:
chunk = f.read(8192)
return b'\x00' in chunk
except Exception:
return True
def fix_env(env_dir):
fixed = 0
scanned = 0
for root, dirs, files in os.walk(env_dir):
# 跳过明显的二进制/缓存目录
dirs[:] = [d for d in dirs if d not in ('__pycache__', '.git')]
for fname in files:
fpath = os.path.join(root, fname)
scanned += 1
try:
if is_probably_binary(fpath):
continue
with open(fpath, 'rb') as f:
content = f.read()
original = content
for old in OLD_PREFIXES:
content = content.replace(old, NEW_PREFIX)
if content != original:
with open(fpath, 'wb') as f:
f.write(content)
fixed += 1
except (PermissionError, OSError):
continue
print(f" 扫描 {scanned} 文件, 修改 {fixed} 文件")
return fixed
if __name__ == "__main__":
for env in sys.argv[1:]:
env_path = f"/home/xsl/miniconda3/envs/{env}"
if os.path.isdir(env_path):
print(f"[{env}] 修复中...")
fix_env(env_path)
else:
print(f"[{env}] 目录不存在,跳过")