Coverage for /Users/ajo/work/jumpstarter/jumpstarter/packages/jumpstarter/jumpstarter/utils/env.py: 43%

23 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-06 10:20 +0200

1import os 

2from contextlib import ExitStack, asynccontextmanager, contextmanager 

3 

4from anyio.from_thread import start_blocking_portal 

5 

6from jumpstarter.client import client_from_path 

7from jumpstarter.config.client import _allow_from_env 

8from jumpstarter.config.env import JUMPSTARTER_HOST 

9 

10 

11@asynccontextmanager 

12async def env_async(portal, stack): 

13 """Provide a client for an existing JUMPSTARTER_HOST environment variable. 

14 

15 Async version of env() 

16 

17 This is useful when interacting with an already established Jumpstarter shell, 

18 to either a local exporter or a remote one. 

19 """ 

20 host = os.environ.get(JUMPSTARTER_HOST, None) 

21 if host is None: 

22 raise RuntimeError(f"{JUMPSTARTER_HOST} not set") 

23 

24 allow, unsafe = _allow_from_env() 

25 

26 async with client_from_path(host, portal, stack, allow=allow, unsafe=unsafe) as client: 

27 try: 

28 yield client 

29 finally: 

30 if hasattr(client, "close"): 

31 client.close() 

32 

33 

34@contextmanager 

35def env(): 

36 """Provide a client for an existing JUMPSTARTER_HOST environment variable. 

37 

38 This is useful when interacting with an already established Jumpstarter shell, 

39 to either a local exporter or a remote one. 

40 """ 

41 with start_blocking_portal() as portal: 

42 with ExitStack() as stack: 

43 with portal.wrap_async_context_manager(env_async(portal, stack)) as client: 

44 yield client