19
19
import sys
20
20
import traceback
21
21
from collections .abc import AsyncIterable
22
- from contextlib import redirect_stdout
23
22
from dataclasses import dataclass , field
24
23
from enum import Enum
25
- from io import StringIO
26
24
from os import system
27
25
from shutil import make_archive
28
- from typing import Any , Literal , NewType , Optional , Union , cast
26
+ from typing import Any , Literal , NewType , cast
29
27
30
28
import aiohttp
31
29
import msgpack
@@ -66,14 +64,14 @@ class ConfigurationPayload:
66
64
code : bytes
67
65
encoding : Encoding
68
66
entrypoint : str
69
- ip : Optional [ str ] = None
70
- ipv6 : Optional [ str ] = None
71
- route : Optional [ str ] = None
72
- ipv6_gateway : Optional [ str ] = None
67
+ ip : str | None = None
68
+ ipv6 : str | None = None
69
+ route : str | None = None
70
+ ipv6_gateway : str | None = None
73
71
dns_servers : list [str ] = field (default_factory = list )
74
72
volumes : list [Volume ] = field (default_factory = list )
75
- variables : Optional [ dict [str , str ]] = None
76
- authorized_keys : Optional [ list [str ]] = None
73
+ variables : dict [str , str ] | None = None
74
+ authorized_keys : list [str ] | None = None
77
75
78
76
79
77
@dataclass
@@ -107,19 +105,19 @@ def setup_hostname(hostname: str):
107
105
system (f"hostname { hostname } " )
108
106
109
107
110
- def setup_variables (variables : Optional [ dict [str , str ]] ):
108
+ def setup_variables (variables : dict [str , str ] | None ):
111
109
if variables is None :
112
110
return
113
111
for key , value in variables .items ():
114
112
os .environ [key ] = value
115
113
116
114
117
115
def setup_network (
118
- ipv4 : Optional [ str ] ,
119
- ipv6 : Optional [ str ] ,
120
- ipv4_gateway : Optional [ str ] ,
121
- ipv6_gateway : Optional [ str ] ,
122
- dns_servers : Optional [ list [str ]] = None ,
116
+ ipv4 : str | None ,
117
+ ipv6 : str | None ,
118
+ ipv4_gateway : str | None ,
119
+ ipv6_gateway : str | None ,
120
+ dns_servers : list [str ] | None = None ,
123
121
):
124
122
"""Setup the system with info from the host."""
125
123
dns_servers = dns_servers or []
@@ -189,7 +187,7 @@ def setup_volumes(volumes: list[Volume]):
189
187
190
188
191
189
async def wait_for_lifespan_event_completion (
192
- application : ASGIApplication , event : Union [ Literal ["startup" , "shutdown" ] ]
190
+ application : ASGIApplication , event : Literal ["startup" , "shutdown" ]
193
191
):
194
192
"""
195
193
Send the startup lifespan signal to the ASGI app.
@@ -295,7 +293,7 @@ async def setup_code(
295
293
encoding : Encoding ,
296
294
entrypoint : str ,
297
295
interface : Interface ,
298
- ) -> Union [ ASGIApplication , subprocess .Popen ] :
296
+ ) -> ASGIApplication | subprocess .Popen :
299
297
if interface == Interface .asgi :
300
298
return await setup_code_asgi (code = code , encoding = encoding , entrypoint = entrypoint )
301
299
elif interface == Interface .executable :
@@ -304,7 +302,7 @@ async def setup_code(
304
302
raise ValueError ("Invalid interface. This should never happen." )
305
303
306
304
307
- async def run_python_code_http (application : ASGIApplication , scope : dict ) -> tuple [dict , dict , str , Optional [ bytes ] ]:
305
+ async def run_python_code_http (application : ASGIApplication , scope : dict ) -> tuple [dict , dict , str , bytes | None ]:
308
306
logger .debug ("Running code" )
309
307
# Execute in the same process, saves ~20ms than a subprocess
310
308
@@ -386,7 +384,7 @@ def show_loading():
386
384
return headers , body
387
385
388
386
389
- async def run_executable_http (scope : dict ) -> tuple [dict , dict , str , Optional [ bytes ] ]:
387
+ async def run_executable_http (scope : dict ) -> tuple [dict , dict , str , bytes | None ]:
390
388
logger .debug ("Calling localhost" )
391
389
392
390
tries = 0
@@ -413,7 +411,7 @@ async def run_executable_http(scope: dict) -> tuple[dict, dict, str, Optional[by
413
411
async def process_instruction (
414
412
instruction : bytes ,
415
413
interface : Interface ,
416
- application : Union [ ASGIApplication , subprocess .Popen ] ,
414
+ application : ASGIApplication | subprocess .Popen ,
417
415
) -> AsyncIterable [bytes ]:
418
416
if instruction == b"halt" :
419
417
logger .info ("Received halt command" )
@@ -443,11 +441,11 @@ async def process_instruction(
443
441
logger .debug ("msgpack.loads )" )
444
442
payload = RunCodePayload (** msg_ )
445
443
446
- output : Optional [ str ] = None
444
+ output : str | None = None
447
445
try :
448
446
headers : dict
449
447
body : dict
450
- output_data : Optional [ bytes ]
448
+ output_data : bytes | None
451
449
452
450
if interface == Interface .asgi :
453
451
application = cast (ASGIApplication , application )
@@ -540,7 +538,7 @@ async def main() -> None:
540
538
setup_system (config )
541
539
542
540
try :
543
- app : Union [ ASGIApplication , subprocess .Popen ] = await setup_code (
541
+ app : ASGIApplication | subprocess .Popen = await setup_code (
544
542
config .code , config .encoding , config .entrypoint , config .interface
545
543
)
546
544
client .send (msgpack .dumps ({"success" : True }))
0 commit comments