Files
wecom_it_smart_desk/cmp_docs2.py
T
2026-08-11 14:16:24 +08:00

78 lines
3.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""二次验证:按内容 hash(忽略文件名)判断旧目录文件是否已存在于新结构;并比较同名文件的新旧时间。"""
import os, hashlib, json, collections, datetime
ROOT = r"D:\资料\03-项目开发\wecom_it_smart_desk\docs"
OLD = ["01-产品设计","02-产品需求","03-技术架构","04-原型设计","06-测试质量","08-安全审计","09-部署运维","10-项目管理","11-历史归档"]
NEW = ["01-产品文档","02-技术文档","03-测试文档","04-运维文档","05-运营文档","06-安全审计","07-项目管理","08-历史归档"]
OLD_ROOT_FILES = ["class-diagram.mermaid","sequence-diagram.mermaid","system_design.md"]
def h(p):
return hashlib.md5(open(p,'rb').read()).hexdigest()
def mt(p):
return datetime.datetime.fromtimestamp(os.path.getmtime(p)).strftime("%Y-%m-%d %H:%M")
# 新结构:content-hash -> [relpath]name -> [(relpath,hash)]
hash2new = collections.defaultdict(list)
name2new = collections.defaultdict(list)
for d in NEW:
base = os.path.join(ROOT, d)
if not os.path.isdir(base): continue
for dp, dn, fn in os.walk(base):
for f in fn:
fp = os.path.join(dp, f)
hh = h(fp)
rel = os.path.relpath(fp, ROOT)
hash2new[hh].append(rel)
name2new[f].append((rel, hh))
oldfiles = []
for d in OLD + OLD_ROOT_FILES:
base = os.path.join(ROOT, d)
if os.path.isdir(base):
for dp, dn, fn in os.walk(base):
for f in fn:
oldfiles.append(os.path.join(dp,f))
elif os.path.isfile(base):
oldfiles.append(base)
content_dup = [] # 内容已在新结构中(含改名)
name_diff = [] # 同名不同内容
truly_missing = [] # 内容+文件名都不在新结构
for fp in oldfiles:
rel = os.path.relpath(fp, ROOT)
bn = os.path.basename(fp)
hh = h(fp)
if hh in hash2new:
content_dup.append((rel, hash2new[hh][0]))
elif bn in name2new:
cands = name2new[bn]
newest = cands[0][0]
name_diff.append((rel, mt(fp), os.path.getsize(fp), newest, mt(os.path.join(ROOT,newest)), os.path.getsize(os.path.join(ROOT,newest))))
else:
truly_missing.append((rel, mt(fp), os.path.getsize(fp)))
print("旧目录文件总数:", len(oldfiles))
print("A. 内容已存在于新结构(含改名迁移,可安全删除):", len(content_dup))
print("B. 同名但内容不同(需判定哪边权威):", len(name_diff))
print("C. 内容与文件名均不在新结构(疑似唯一,需人工确认):", len(truly_missing))
print("\n===== B. 同名内容不同:旧 vs 新(时间/大小) =====")
print(f"{'旧路径':<62} {'旧时间':<17}{'旧KB':>7} | {'新路径':<58} {'新时间':<17}{'新KB':>7}")
newer_in_new = 0
for a, amt, asz, b, bmt, bsz in name_diff:
flag = "新更新" if bmt > amt else ("旧更新" if amt > bmt else "同时间")
if bmt >= amt: newer_in_new += 1
print(f"{a:<62} {amt:<17}{asz/1024:>7.1f} | {b:<58} {bmt:<17}{bsz/1024:>7.1f} {flag}")
print(f"\n其中新结构版本更新或同时间的: {newer_in_new}/{len(name_diff)}")
print("\n===== C. 疑似唯一文件(内容不在新结构任何位置) =====")
for rel, m, s in truly_missing:
print(f" - {rel:<70} {m} {s/1024:.1f}KB")
json.dump({"content_dup":content_dup,"name_diff":name_diff,"truly_missing":truly_missing},
open(r"D:\资料\03-项目开发\wecom_it_smart_desk\docs_cmp_result2.json","w",encoding="utf-8"),
ensure_ascii=False, indent=1)