#!/usr/bin/env python3 """Install only xb.loc -> 127.0.0.1:8594; requires local administrator execution.""" from pathlib import Path import os,re,subprocess,time,shutil DOMAIN='xb.loc' HOSTS=Path('/etc/hosts') CONF=Path('/usr/local/etc/nginx/servers/xb.loc.conf') NGINX='/usr/local/bin/nginx' CONTENT='''# Isolated XB Fuc theme preview. Dedicated Fuc acceptance instance; no DPL/HN data. server { listen 127.0.0.1:80; listen [::1]:80; server_name xb.loc; location / { proxy_pass http://127.0.0.1:8594; proxy_set_header Host $host; proxy_http_version 1.1; } } ''' def main(): if os.geteuid()!=0: raise SystemExit('Run with sudo; no files have been changed.') original=HOSTS.read_text() for line in original.splitlines(): fields=line.split('#',1)[0].split() if DOMAIN in fields[1:] and fields[0] not in ('127.0.0.1','::1'):raise SystemExit('Existing xb.loc mapping conflicts; stopped.') if CONF.exists() and CONF.read_text()!=CONTENT:raise SystemExit('Existing xb.loc Nginx configuration differs; stopped.') subprocess.run([NGINX,'-t'],check=True) backup=Path('/private/var/tmp')/('xb-fuc-domain-'+time.strftime('%Y%m%d-%H%M%S')) backup.mkdir(mode=0o700) shutil.copy2(HOSTS,backup/'hosts') had_conf=CONF.exists() if had_conf:shutil.copy2(CONF,backup/'xb.loc.conf') lines=original.splitlines() for ip in ['127.0.0.1','::1']: if not any((f:=line.split('#',1)[0].split()) and f[0]==ip and DOMAIN in f[1:] for line in lines):lines.append(ip+' '+DOMAIN) desired='\n'.join(lines)+'\n' try: HOSTS.write_text(desired);CONF.write_text(CONTENT);CONF.chmod(0o644) subprocess.run([NGINX,'-t'],check=True) subprocess.run([NGINX,'-s','reload'],check=True) except BaseException: if HOSTS.read_text()==desired:HOSTS.write_text(original) if CONF.exists() and CONF.read_text()==CONTENT: if had_conf:shutil.copy2(backup/'xb.loc.conf',CONF) else:CONF.unlink() raise print('Configured http://xb.loc -> isolated local XB acceptance instance on 8594; verify HTTP separately.') print('Recovery files:',backup) if __name__=='__main__':main()