29 lines
1.8 KiB
Python
29 lines
1.8 KiB
Python
from pathlib import Path
|
|
import hashlib,difflib,json
|
|
source=Path('C:/kefu/wechat_rpa');target=Path('C:/wechat_rpa');out=Path(__file__).resolve().parent
|
|
extensions={'.py','.ps1','.spec','.md','.txt','.bat','.cmd'}
|
|
def files(root):
|
|
result={p.name:p for p in root.iterdir() if p.is_file() and p.suffix in extensions}
|
|
for folder in ['assets','archive_exporter','dsh_desk','backend_deploy']:
|
|
if not (root/folder).exists():continue
|
|
for p in (root/folder).rglob('*'):
|
|
if p.is_file() and not any(x in {'node_modules','__pycache__','.git','dist','build'} for x in p.parts):
|
|
result[p.relative_to(root).as_posix()]=p
|
|
return result
|
|
def text(p):
|
|
try:return p.read_text(encoding='utf-8-sig').replace('\r\n','\n')
|
|
except UnicodeDecodeError:return None
|
|
a,b=files(source),files(target);changed=[];diff=[]
|
|
for name in sorted(a.keys()&b.keys()):
|
|
left,right=text(a[name]),text(b[name])
|
|
if (left==right if left is not None and right is not None else a[name].read_bytes()==b[name].read_bytes()):continue
|
|
entry={'path':name,'source_bytes':a[name].stat().st_size,'target_bytes':b[name].stat().st_size}
|
|
if left is not None and right is not None:
|
|
changes=list(difflib.unified_diff(left.splitlines(),right.splitlines(),fromfile='current/'+name,tofile='protocol/'+name,n=3))
|
|
entry['diff_lines']=len(changes);diff.extend(changes)
|
|
changed.append(entry)
|
|
result={'source_only':sorted(a.keys()-b.keys()),'target_only':sorted(b.keys()-a.keys()),'changed':changed,'same_count':len(a.keys()&b.keys())-len(changed)}
|
|
(out/'inventory.json').write_text(json.dumps(result,ensure_ascii=False,indent=2),encoding='utf-8')
|
|
(out/'comparison.diff').write_text('\n'.join(diff),encoding='utf-8')
|
|
print(json.dumps(result,ensure_ascii=False,indent=2))
|