first commit
This commit is contained in:
176
txt解析版/txt解析.py
Normal file
176
txt解析版/txt解析.py
Normal file
@@ -0,0 +1,176 @@
|
||||
import os
|
||||
import shutil
|
||||
import re
|
||||
from datetime import datetime
|
||||
import time
|
||||
|
||||
|
||||
def find_latest_txt_file():
|
||||
"""
|
||||
在当前运行目录下查找最新修改的 .txt 文件,排除 config.txt
|
||||
"""
|
||||
cwd = os.getcwd()
|
||||
txt_files = [
|
||||
os.path.join(cwd, f)
|
||||
for f in os.listdir(cwd)
|
||||
if f.lower().endswith(".txt") and not f.lower().startswith("config")
|
||||
]
|
||||
|
||||
if not txt_files:
|
||||
raise FileNotFoundError("当前目录下未找到任何非config的 .txt 文件")
|
||||
|
||||
latest_txt = max(txt_files, key=os.path.getmtime)
|
||||
return latest_txt
|
||||
|
||||
|
||||
def load_extensions(config_path):
|
||||
"""
|
||||
从 config.txt 读取需要的文件后缀集合
|
||||
"""
|
||||
exts = set()
|
||||
with open(config_path, "r", encoding="utf-8") as f:
|
||||
for line in f:
|
||||
line = line.strip().lower()
|
||||
if not line or line.startswith("#"):
|
||||
continue
|
||||
if not line.startswith("."):
|
||||
line = "." + line
|
||||
exts.add(line)
|
||||
return exts
|
||||
|
||||
|
||||
def extract_and_copy_files(compile_order_path):
|
||||
"""
|
||||
从 report_compile_order 输出文件中:
|
||||
- 提取 Source Compile Order(Design Sources)第一个段落
|
||||
- 只保留 config.txt 中指定的需要的文件类型
|
||||
- 按完整路径复制文件
|
||||
"""
|
||||
|
||||
try:
|
||||
# 读取 config.txt 中需要的文件后缀
|
||||
config_path = os.path.join(os.getcwd(), "config.txt")
|
||||
if not os.path.exists(config_path):
|
||||
raise FileNotFoundError("当前目录下未找到 config.txt")
|
||||
|
||||
include_exts = load_extensions(config_path)
|
||||
print(f"从 config.txt 加载需要的文件后缀: {include_exts}")
|
||||
|
||||
# ---------- 读取文件 ----------
|
||||
with open(compile_order_path, "r", encoding="utf-8") as f:
|
||||
lines = f.readlines()
|
||||
|
||||
# ---------- 定位 Source Compile Order 段落 ----------
|
||||
start_idx = None
|
||||
end_idx = None
|
||||
|
||||
for i, line in enumerate(lines):
|
||||
if line.startswith("Source compile order for 'synthesis'"):
|
||||
start_idx = i
|
||||
continue
|
||||
|
||||
if start_idx is not None and line.strip() == "":
|
||||
end_idx = i
|
||||
break
|
||||
|
||||
if start_idx is None:
|
||||
raise RuntimeError("未找到 Source compile order for 'synthesis' 段落")
|
||||
|
||||
if end_idx is None:
|
||||
end_idx = len(lines)
|
||||
|
||||
section = lines[start_idx:end_idx]
|
||||
|
||||
# ---------- 跳过标题 + 表头 ----------
|
||||
data_lines = section[2:]
|
||||
|
||||
extracted_paths = []
|
||||
|
||||
for line in data_lines:
|
||||
line = line.rstrip()
|
||||
if not line:
|
||||
continue
|
||||
|
||||
# 行首必须是索引号
|
||||
if not re.match(r"\s*\d+\s+", line):
|
||||
continue
|
||||
|
||||
# 用 2 个及以上空格分割,最后一列一定是完整路径
|
||||
parts = re.split(r"\s{2,}", line)
|
||||
if not parts:
|
||||
continue
|
||||
|
||||
full_path = parts[-1]
|
||||
|
||||
# ---------- 只保留 config.txt 中指定的需要的文件类型 ----------
|
||||
file_ext = os.path.splitext(full_path)[1].lower()
|
||||
if include_exts and file_ext not in include_exts:
|
||||
continue
|
||||
|
||||
extracted_paths.append(full_path)
|
||||
|
||||
print(f"提取到 {len(extracted_paths)} 个需要的文件")
|
||||
|
||||
# ---------- 创建输出目录 ----------
|
||||
now = datetime.now()
|
||||
out_dir = now.strftime("Copy_Files_%Y%m%d_%H%M%S")
|
||||
os.makedirs(out_dir)
|
||||
print(f"创建输出目录: {out_dir}")
|
||||
|
||||
# ---------- 复制文件 ----------
|
||||
copied = 0
|
||||
skipped = 0
|
||||
|
||||
for src in extracted_paths:
|
||||
if not os.path.exists(src):
|
||||
print(f"文件不存在,跳过: {src}")
|
||||
skipped += 1
|
||||
continue
|
||||
|
||||
base = os.path.basename(src)
|
||||
dst = os.path.join(out_dir, base)
|
||||
|
||||
# 避免重名覆盖
|
||||
if os.path.exists(dst):
|
||||
name, ext = os.path.splitext(base)
|
||||
idx = 1
|
||||
while True:
|
||||
new_dst = os.path.join(out_dir, f"{name}_{idx}{ext}")
|
||||
if not os.path.exists(new_dst):
|
||||
dst = new_dst
|
||||
break
|
||||
idx += 1
|
||||
|
||||
shutil.copy2(src, dst)
|
||||
copied += 1
|
||||
|
||||
print("\n复制完成")
|
||||
print(f" 成功复制: {copied}")
|
||||
print(f" 跳过文件: {skipped}")
|
||||
print(f" 输出目录: {os.path.abspath(out_dir)}")
|
||||
|
||||
return extracted_paths
|
||||
|
||||
except Exception as e:
|
||||
print("处理过程中发生错误:")
|
||||
print(e)
|
||||
return None
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
input_file = find_latest_txt_file()
|
||||
print(f"使用最新的 txt 文件: {input_file}")
|
||||
|
||||
result = extract_and_copy_files(input_file)
|
||||
|
||||
if not result:
|
||||
print("未提取到任何文件")
|
||||
|
||||
|
||||
except Exception as e:
|
||||
print("程序异常退出:")
|
||||
print(e)
|
||||
|
||||
finally:
|
||||
input("\n按回车键关闭窗口...")
|
||||
Reference in New Issue
Block a user