Files
ecs-relay-service/deployable/scripts/apply_notification_client.py
T

65 lines
3.2 KiB
Python

"""Apply only the notification integration, preserving installed TLS/document fixes."""
import argparse
import shutil
from datetime import datetime, timezone
from pathlib import Path
def prepare(relay_dir, source_root=None):
relay_dir=Path(relay_dir)
source_root=Path(source_root or Path(__file__).resolve().parents[1])
client=(relay_dir/'work_client.py').read_text(encoding='utf-8-sig')
tools=(relay_dir/'work_tools.py').read_text(encoding='utf-8-sig')
if 'from .notification_client import notify_authorization' not in client:
start=client.index(' def authorize(self):')
end=client.index(' def save_tokens(',start)
block=client[start:end]
old1=' return data["authorization_url"]'
old2=' return data["authorization_url"]'
if block.count(old1)!=1 or block.count('\n'+old2)!=1:
raise ValueError('Unknown authorize implementation; stopped without modifying files')
block=block.replace(old1,' from .notification_client import notify_authorization\n'
' notify_authorization(self, data)\n'+old1)
block=block.replace('\n'+old2,'\n from .notification_client import notify_authorization\n'
' notify_authorization(self, data)\n'+old2)
client=client[:start]+block+client[end:]
if 'register_notification_tool(directory)' not in tools:
marker='\ndef calendar(client, args):'
if tools.count(marker)!=1 or 'def register(directory):' not in tools:
raise ValueError('Unknown tool registration implementation; stopped without modifying files')
tools=tools.replace(marker,' from .notification_client import register_notification_tool\n'
' register_notification_tool(directory)\n\n'+marker)
updates={'work_client.py':client,'work_tools.py':tools}
for name in ['notification_client.py','notification_cli.py']:
updates[name]=(source_root/'relay'/name).read_text(encoding='utf-8-sig')
for name,text in updates.items():compile(text,name,'exec')
return updates
def apply(relay_dir, source_root=None):
relay_dir=Path(relay_dir)
updates=prepare(relay_dir,source_root)
backup=relay_dir.parent/('notification-backup-'+datetime.now(timezone.utc).strftime('%Y%m%d-%H%M%S-%f'))
backup.mkdir()
for name in updates:
target=relay_dir/name
if target.exists():shutil.copy2(target,backup/name)
for name,text in updates.items():
target=relay_dir/name
temporary=target.with_suffix('.notification-tmp')
temporary.write_text(text,encoding='utf-8')
temporary.replace(target)
print('Client integration applied. Backup:',backup)
print('Restart the native Agent after the ECS upgrade. No credentials were read or printed.')
if __name__=='__main__':
parser=argparse.ArgumentParser()
parser.add_argument('--relay-dir',type=Path,required=True)
parser.add_argument('--apply',action='store_true')
args=parser.parse_args()
if args.apply:apply(args.relay_dir)
else:
prepare(args.relay_dir)
print('Compatible client source detected; dry run only. Use --apply to write with backup.')