"""Real translate and display-only shell calls; never source a VM creator.""" from pathlib import Path import importlib.util,json,os,re,shlex,subprocess,tempfile,unittest from html.parser import HTMLParser ROOT=Path(__file__).resolve().parents[1] SPEC=importlib.util.spec_from_file_location('official_i18n',ROOT/'.github/scripts/build_translation_cache.py') OFFICIAL=importlib.util.module_from_spec(SPEC);SPEC.loader.exec_module(OFFICIAL) TRANSLATE=re.search(r'(?ms)^translate\(\) \{.*?^\}',(ROOT/'scripts/utils.sh').read_text()).group() FILES=('scripts/vm/synology.sh','scripts/utilities/uup_dump_iso_creator.sh') DISPLAY=re.compile(r'^\s*(?:msg_(?:info2?|ok|warn|error|success|title)|echo -[en])\s+".*"\s*(?:>&2)?\s*$') def display_calls(source): return [line for line in source.splitlines() if DISPLAY.fullmatch(line) and not re.search(r'\$\((?!(?:translate|printf|basename)\b)',line)] def render(calls,language): with tempfile.TemporaryDirectory(prefix='remaining-sk-display-') as tmp: blocked=Path(tmp)/'blocked';blocked.write_text('') definitions='\n'.join(name+'() { printf "%s\\n" "$1"; }' for name in ('msg_info','msg_info2','msg_ok','msg_warn','msg_error','msg_success','msg_title')) forbidden='\n'.join(name+'() { echo '+shlex.quote(name)+' >> '+shlex.quote(str(blocked))+'; return 97; }' for name in ('qm','pct','pvesh','apt-get','curl','wget','mount','reboot','systemctl','rm','touch','mkdir','chmod')) values={'NAME':'Synology VM','VMID':'999','IMAGES_DIR':'/fixture/images','EFI_STORAGE':'efi-store','LOADER_STORAGE':'loader-store','STORAGE':'data-store','STORAGE_TYPE':'zfspool','LOADER_NAME':'Arc','LOADER_FILE':'/fixture/loader.img','FILE':'loader.img','DISK_NUM':'2','SATA_ID':'sata2','SIZE':'32','DISK_MODEL':'Fixture SSD','DISK_SIZE':'32G','DISK_ID':'sata3','DISK':'/dev/fixture','PCI_DEV':'0000:01:00.0','HOSTPCI_INDEX':'0','HN':'fixture-vm','CORE_COUNT':'2','RAM_SIZE':'4096','BRG':'vmbr0','MAC':'02:00:00:00:00:01','VLAN1':'Default','MTU1':'Default','SERIAL_PORT':'socket','START_VM':'no','MACH':'q35','OUT_DIR':'/fixture/iso','BUILD_ID':'fixture','EDITION':'professional','ARCH':'amd64','DEFAULT_TMP':'/fixture/tmp','TMP_DIR':'/fixture/tmp','ISO_FILE':'fixture.iso','user_path':'/fixture'} catalog={} if language=='en' else json.loads((ROOT/'lang'/f'{language}.json').read_text()) values.update({key:catalog.get('Default','Default') for key in ('VLAN1','MTU1')}) assignments='\n'.join(key+'='+shlex.quote(value) for key,value in values.items()) script='LANGUAGE='+shlex.quote(language)+'\nLANG_DIR='+shlex.quote(str(ROOT/'lang'))+'\n'+TRANSLATE+'\n'+definitions+'\n'+forbidden+'\n'+assignments+'\n'+'\n'.join(calls)+'\n' result=subprocess.run(['bash','-c',script],text=True,capture_output=True,timeout=15,check=True) assert blocked.read_text()=='' return result.stdout+result.stderr class RemainingCliI18n(unittest.TestCase): def test_badges_remain_valid_with_hostile_translations_without_host_calls(self): source=(ROOT/FILES[0]).read_text() html=re.search(r'(?ms)^HTML_DESC=".*?^"',source).group() class Images(HTMLParser): def __init__(self):super().__init__();self.images=[] def handle_starttag(self,tag,attrs): if tag=='img':self.images.append(dict(attrs)) with tempfile.TemporaryDirectory() as tmp: blocked=Path(tmp)/'blocked';blocked.write_text('') forbidden='\n'.join(name+'(){ echo '+shlex.quote(name)+' >> '+shlex.quote(str(blocked))+'; return 97; }' for name in ('qm','pct','pvesh','curl','wget','mount','reboot','systemctl')) script=forbidden+'\ntranslate(){ printf "%s" "O\'Brien &