-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.py
72 lines (57 loc) · 2.31 KB
/
core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
from .types import EnvVar, EnvchainNamespace, EnvchainEnv, EnvchainTuple
def export_envchain_keyring(
env_var: EnvVar,
envchain_namespace: EnvchainNamespace,
envchain_env: EnvchainEnv,
) -> str:
"""set env_var to the password of envchain_env of account envchain-{envchain_namespace} in the keychain
return a string to be eval'd by the shell"""
import keyring
password = keyring.get_password(f"envchain-{envchain_namespace}", envchain_env)
return f"export {env_var}='{password}'"
async def export_envchain_envchain(
env_var: EnvVar,
envchain_namespace: EnvchainNamespace,
envchain_env: EnvchainEnv,
) -> str:
"""get the env vars with envchain executable"""
import asyncio
from asyncio.subprocess import Process
# run this command in async subprocess:
# envchain envchain_namespace printenv envchain_env
proc: Process = await asyncio.create_subprocess_exec(
"envchain",
envchain_namespace,
"printenv",
envchain_env,
stdout=asyncio.subprocess.PIPE,
)
# wait for the process to finish:
stdout, _ = await proc.communicate()
# return the env var:
return f"export {env_var}='{stdout.decode().strip()}'"
# return f"export {env_var}='{password}'"
async def export_envchain_keyring_multi(envchain_pairs: list[EnvchainTuple]) -> str:
import asyncio
from concurrent.futures import ThreadPoolExecutor
from functools import partial
loop = asyncio.get_running_loop()
with ThreadPoolExecutor() as pool:
tasks = [
loop.run_in_executor(pool, partial(export_envchain_keyring, *pair))
for pair in envchain_pairs
]
export_commands = await asyncio.gather(*tasks)
return '\n'.join(export_commands)
async def export_envchain_envchain_multi(envchain_pairs: list[EnvchainTuple]) -> str:
import asyncio
tasks = [
asyncio.create_task(export_envchain_envchain(*pair)) for pair in envchain_pairs
]
export_commands = await asyncio.gather(*tasks)
return '\n'.join(export_commands)
def unset_env_vars(env_vars: list[EnvVar | EnvchainTuple]) -> str:
if isinstance(env_vars[0], EnvVar):
return '\n'.join(f"unset {env_var}" for env_var in env_vars)
return '\n'.join(f"unset {env_var}" for env_var, _, _ in env_vars)