Files
kiosk/docs/verification/cleanup-2026-09-20/resource-audit.py
T

37 lines
1.9 KiB
Python

from pathlib import Path
import xml.etree.ElementTree as E, re, zipfile,io,json,subprocess
root=E.parse('docs/verification/cleanup-2026-09-20/baseline-unused-resources.xml').getroot()
# Scan source/config text and uncompressed local SDK contents for literal names.
files=[Path(f) for f in subprocess.check_output(['git','ls-files','-z']).decode().split('\0') if f]
texts={p:p.read_text(errors='ignore') for p in files if not str(p).startswith('docs/') and p.suffix in {'.kt','.java','.xml','.pro','.kts','.toml','.json','.html'}}
blobs=[]
def scan(data):
try:
with zipfile.ZipFile(io.BytesIO(data)) as z:
for n in z.namelist():
b=z.read(n)
if n.endswith(('.jar','.aar')):scan(b)
else:blobs.append(b)
except zipfile.BadZipFile:blobs.append(data)
for p in Path('app/libs').rglob('*'):
if p.is_file():scan(p.read_bytes())
sdk=b'\n'.join(blobs)
remove=[];keep=[]
for i in root.findall('issue'):
if i.get('id')!='UnusedResources':continue
m=re.search(r'R\.(\w+)\.(\w+)',i.get('message',''))
if not m:continue
typ,name=m.groups()
# Limit this batch to standalone bitmap files, preserving all XML/value resources.
if typ not in {'drawable','mipmap'}:continue
variants=[p for p in files if str(p).startswith('app/src/main/res/'+typ) and p.stem==name]
if not variants or any(p.suffix not in {'.png','.webp','.jpg','.jpeg'} for p in variants):continue
refs=[str(p) for p,s in texts.items() if re.search(r'(?<![\w])'+re.escape(name)+r'(?![\w])',s)]
if refs or name.encode() in sdk:keep.append({'name':name,'references':refs,'sdk':name.encode() in sdk});continue
remove.extend(str(p) for p in variants)
r={'remove':sorted(set(remove)),'retain':keep}
# Read-only audit; run on the baseline checkout to reproduce the candidate list.
print(json.dumps(r,ensure_ascii=False,indent=2))
print('Safe bitmap candidates:',len(r['remove']),'bytes:',sum(Path(p).stat().st_size for p in r['remove']))
print('Retained:',keep)