Files
leleka/scripts/install-qwen-native-api.py
Pavlo Ratushnyi 4013e0c9bd
Desktop foundation / core (ubuntu-latest) (push) Failing after 28s
Desktop foundation / core (macos-latest) (push) Canceled after 0s
Desktop foundation / core (windows-latest) (push) Canceled after 0s
Linux / verify (push) Canceled after 0s
Linux / keyless-cuda (push) Canceled after 0s
Linux / static-cuda-package (push) Canceled after 0s
Linux / hosted-cuda-package (push) Canceled after 0s
Linux / linux-static-cli (push) Canceled after 0s
Linux / linux-cli-container (push) Canceled after 0s
Linux / release-cli (push) Canceled after 0s
Import tracked Strata Next source snapshot
2026-10-05 18:15:08 +02:00

80 lines
3.0 KiB
Python

#!/usr/bin/env python3
"""Install opt-in native-api commands after install-qwen-native.py.
Keeps the selected connection unchanged and backs up the launcher before patching.
"""
import argparse
import ast
import hashlib
import os
from pathlib import Path
import tempfile
MARKER = '# Strata Next native API integration v1'
HELPER_MARKER = '# Strata Next native API client v1'
def patched(original):
if MARKER in original:
return original
if '# Strata Next native bearer file integration v1' not in original:
raise RuntimeError('Install native Responses first with scripts/install-qwen-native.py')
anchors = {
'def ctl_main():\n': '''def ctl_main():
if len(sys.argv) > 1 and sys.argv[1] == 'native-api':
from qwen_native_api import main as native_api_main
return native_api_main(sys.argv[2:], connection=connection, read_key=native_key)
''',
" sub = parser.add_subparsers(dest='command', required=True)\n":
" sub = parser.add_subparsers(dest='command', required=True)\n"
" sub.add_parser('native-api', help='native API: status, metrics, lifecycle, drain та resume')\n",
}
text = original
for old, new in anchors.items():
if text.count(old) != 1:
raise RuntimeError('Launcher changed: expected exactly one native-api anchor')
text = text.replace(old, new)
text += '\n' + MARKER + '\n'
ast.parse(text)
return text
def atomic(path, text, mode):
fd, temporary = tempfile.mkstemp(prefix='native-api-', dir=path.parent)
try:
with os.fdopen(fd, 'w') as output:
output.write(text)
os.chmod(temporary, mode)
os.replace(temporary, path)
finally:
if os.path.exists(temporary):
os.unlink(temporary)
def install(path):
os.umask(0o077)
original = path.read_text()
text = patched(original)
source = Path(__file__).with_name('qwen_native_api.py').read_text()
ast.parse(source)
target = path.with_name('qwen_native_api.py')
if target.exists() and HELPER_MARKER not in target.read_text():
raise RuntimeError('Refusing to overwrite an unrelated qwen_native_api.py')
backup = path.with_name(path.name + '.before-native-api-' + hashlib.sha256(original.encode()).hexdigest()[:12])
if text != original and not backup.exists():
with backup.open('x') as output:
output.write(original)
if not target.exists() or target.read_text() != source:
atomic(target, source, 0o600)
if path.read_text() != original:
raise RuntimeError('Launcher changed during installation; retry against the current file')
if text != original:
atomic(path, text, path.stat().st_mode & 0o777)
print('Native API commands installed; connection unchanged')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--path', type=Path, default=Path.home()/'.local/share/codex-qwen/qwen_local.py')
install(parser.parse_args().path)