ruen-ui/Tools/ruen_migrate.py

54 lines
2.5 KiB
Python
Raw Normal View History

"""Offline upgrade of legacy addon IDs. No Lua execution; callers back up first."""
import re
from pathlib import Path
LEGACY_ADDON = 'BradStockUI'
RENAMES = (('BradStockUI', 'RuenUI'), ('BRADSTOCKUI', 'RUENUI'),
('BRADUI', 'RUENUI'), ('BradTargetTargetTarget', 'RuenTargetTargetTarget'))
def converted(text):
for old, new in RENAMES:
text = text.replace(old, new)
return text
def valid_save_path(relative):
if Path(relative).is_absolute() or '\\' in relative or any(p in ('.', '..') for p in relative.split('/')):
return False
return bool(re.fullmatch(
r'WTF/Account/(?:[^/]+/)+(?:(?:SavedVariables/(?:RuenUI|BradStockUI)\.lua(?:\.bak)?)|bindings-cache\.wtf|AddOns\.txt)', relative))
def plan(root, include_addons=True):
"""Return (relative path, new text or None for removal); never overwrite new saves."""
root = Path(root)
account = root/'WTF/Account'
changes = {}
if not account.exists():
return []
# Resolve the destination first so mixed/new installs keep their current settings.
for old in sorted(account.rglob(LEGACY_ADDON+'.lua*')):
if old.parent.name != 'SavedVariables' or old.name not in (LEGACY_ADDON+'.lua', LEGACY_ADDON+'.lua.bak'):
continue
new = old.with_name(old.name.replace(LEGACY_ADDON, 'RuenUI', 1))
if not new.exists():
changes[new.relative_to(root).as_posix()] = converted(old.read_text(encoding='utf-8-sig'))
changes[old.relative_to(root).as_posix()] = None
candidates = list(account.rglob('bindings-cache.wtf'))
candidates += [p for p in account.rglob('RuenUI.lua*')
if p.parent.name == 'SavedVariables' and p.name in ('RuenUI.lua', 'RuenUI.lua.bak')]
if include_addons:
candidates += list(account.rglob('AddOns.txt'))
for path in candidates:
text = path.read_text(encoding='utf-8-sig')
if path.name == 'AddOns.txt':
lines = text.splitlines()
if not any(re.match(r'^'+LEGACY_ADDON+r'\s*:', line) for line in lines):
continue
lines = [line for line in lines if not re.match(r'^(?:'+LEGACY_ADDON+r'|RuenUI)\s*:', line)]
new_text = '\n'.join(lines+['RuenUI: enabled'])+'\n'
else:
new_text = converted(text)
if new_text != text:
changes[path.relative_to(root).as_posix()] = new_text
assert all(valid_save_path(p) for p in changes), 'Unexpected migration path'
return sorted(changes.items())