"""Run with an installed wheel's Python to verify its real console entry point."""
import http.client
import os
from pathlib import Path
import socket
import subprocess
import sys
import tempfile
import time
launcher = Path(sys.executable).with_name('skirr')
if not launcher.is_file():
raise RuntimeError('Install the wheel in a virtual environment and run with its Python')
env = dict(os.environ)
env.pop('PYTHONPATH', None)
with socket.socket() as probe:
probe.bind(('127.0.0.1', 0))
port = probe.getsockname()[1]
with tempfile.TemporaryDirectory(prefix='skirr-release-check-') as directory:
root = Path(directory)
(root / 'app.py').write_text(
"async def app(scope, receive, send):\n"
" if scope['type'] != 'http':\n"
" raise NotImplementedError\n"
" await send({'type': 'http.response.start', 'status': 200})\n"
" await send({'type': 'http.response.body', 'body': b'installed-cli-ok'})\n"
)
log_path = root / 'server.log'
with log_path.open('w') as log:
process = subprocess.Popen(
[str(launcher), 'app:app', '--port', str(port)],
cwd=root, env=env, stdout=log, stderr=subprocess.STDOUT,
)
try:
deadline = time.monotonic() + 10
while True:
if process.poll() is not None:
raise RuntimeError(log_path.read_text())
client = http.client.HTTPConnection('127.0.0.1', port, timeout=1)
try:
client.request('GET', '/')
response = client.getresponse()
if response.status != 200 or response.read() != b'installed-cli-ok':
raise RuntimeError('Installed CLI returned the wrong response')
break
except (OSError, http.client.HTTPException):
if time.monotonic() >= deadline:
raise
time.sleep(.05)
finally:
client.close()
finally:
if process.poll() is None:
process.terminate()
try:
process.wait(timeout=5)
except subprocess.TimeoutExpired:
process.kill()
process.wait()
raise
if process.returncode:
raise RuntimeError(log_path.read_text())
print('Installed skirr app:app served HTTP from a separate application directory.')