Repository navigation
Expand file tree
/
Copy pathhandler.py
More file actions
238 lines (201 loc) · 6.47 KB
/
Copy pathhandler.py
File metadata and controls
238 lines (201 loc) · 6.47 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import json
import asyncio
from pathlib import Path
from dataclasses import dataclass
from typing import IO, TYPE_CHECKING, Any, Literal, cast
from nb_cli import cache
from jinja2 import Environment, FileSystemLoader
from nb_cli.handlers import templates as cli_templates
from nb_cli.config import GLOBAL_CONFIG, SimpleInfo, ConfigManager
from nb_cli.handlers import (
get_project_root,
requires_nonebot,
get_default_python,
get_nonebot_config,
ensure_process_terminated,
probe_environment_manager,
)
from .exception import GetDriverTypeError, ComposeNotAvailable
templates = Environment(
trim_blocks=True,
lstrip_blocks=True,
autoescape=False,
loader=FileSystemLoader(
[
Path(__file__).parent / "template",
*cast(FileSystemLoader, cli_templates.loader).searchpath,
]
),
enable_async=True,
)
templates.globals.update(cli_templates.globals)
templates.filters.update(cli_templates.filters)
@dataclass
class Compose:
command: tuple[str, ...]
info: str
if TYPE_CHECKING:
async def get_compose_command() -> Compose: ...
else:
@cache(ttl=None)
async def get_compose_command() -> Compose:
proc = await asyncio.create_subprocess_exec(
"docker", "compose", "version", stdout=asyncio.subprocess.PIPE
)
stdout, _ = await proc.communicate()
if proc.returncode == 0:
return Compose(command=("docker", "compose"), info=stdout.decode())
proc = await asyncio.create_subprocess_exec(
"docker-compose", "version", stdout=asyncio.subprocess.PIPE
)
stdout, _ = await proc.communicate()
if proc.returncode == 0:
return Compose(command=("docker-compose",), info=stdout.decode())
raise ComposeNotAvailable
@ensure_process_terminated
async def call_compose(
compose_args: list[str] | None = None,
cwd: Path | None = None,
stdin: IO[Any] | int | None = None,
stdout: IO[Any] | int | None = None,
stderr: IO[Any] | int | None = None,
) -> asyncio.subprocess.Process:
if cwd is None:
cwd = get_project_root()
compose = await get_compose_command()
return await asyncio.create_subprocess_exec(
*compose.command,
*(compose_args or []),
cwd=cwd,
stdin=stdin,
stdout=stdout,
stderr=stderr,
)
async def compose_up(
compose_args: list[str] | None = None,
cwd: Path | None = None,
stdin: IO[Any] | int | None = None,
stdout: IO[Any] | int | None = None,
stderr: IO[Any] | int | None = None,
) -> asyncio.subprocess.Process:
return await call_compose(
["up", "-d", "--build", *(compose_args or [])],
cwd=cwd,
stdin=stdin,
stdout=stdout,
stderr=stderr,
)
async def compose_down(
compose_args: list[str] | None = None,
cwd: Path | None = None,
stdin: IO[Any] | int | None = None,
stdout: IO[Any] | int | None = None,
stderr: IO[Any] | int | None = None,
) -> asyncio.subprocess.Process:
return await call_compose(
["down", *(compose_args or [])],
cwd=cwd,
stdin=stdin,
stdout=stdout,
stderr=stderr,
)
async def compose_build(
compose_args: list[str] | None = None,
cwd: Path | None = None,
stdin: IO[Any] | int | None = None,
stdout: IO[Any] | int | None = None,
stderr: IO[Any] | int | None = None,
) -> asyncio.subprocess.Process:
return await call_compose(
["build", *(compose_args or [])],
cwd=cwd,
stdin=stdin,
stdout=stdout,
stderr=stderr,
)
async def compose_logs(
compose_args: list[str] | None = None,
cwd: Path | None = None,
stdin: IO[Any] | int | None = None,
stdout: IO[Any] | int | None = None,
stderr: IO[Any] | int | None = None,
) -> asyncio.subprocess.Process:
return await call_compose(
["logs", *(compose_args or [])],
cwd=cwd,
stdin=stdin,
stdout=stdout,
stderr=stderr,
)
async def compose_ps(
compose_args: list[str] | None = None,
cwd: Path | None = None,
stdin: IO[Any] | int | None = None,
stdout: IO[Any] | int | None = None,
stderr: IO[Any] | int | None = None,
) -> asyncio.subprocess.Process:
return await call_compose(
["ps", *(compose_args or [])],
cwd=cwd,
stdin=stdin,
stdout=stdout,
stderr=stderr,
)
@requires_nonebot
async def get_driver_type(
adapters: list[SimpleInfo] | None = None,
builtin_plugins: list[str] | None = None,
python_path: str | None = None,
cwd: Path | None = None,
) -> bool:
bot_config = get_nonebot_config()
if adapters is None:
adapters = bot_config.get_adapters()
if builtin_plugins is None:
builtin_plugins = bot_config.builtin_plugins
if python_path is None:
python_path = await get_default_python()
if cwd is None:
cwd = get_project_root()
t = templates.get_template("docker/get_driver_type.py.jinja")
proc = await asyncio.create_subprocess_exec(
python_path,
"-W",
"ignore",
"-c",
await t.render_async(adapters=adapters, builtin_plugins=builtin_plugins),
cwd=cwd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await proc.communicate()
if proc.returncode != 0:
raise GetDriverTypeError(stdout, stderr)
try:
return json.loads(stdout.strip())
except Exception as e:
raise GetDriverTypeError(stdout, stderr) from e
async def get_build_backend(
config_manager: ConfigManager | None = None,
) -> Literal["poetry", "pdm", "uv", "pip"] | None:
if config_manager is None:
config_manager = GLOBAL_CONFIG
inferred, _ = await probe_environment_manager(cwd=config_manager.working_dir)
if inferred != "pip":
return cast(Literal["uv", "pdm", "poetry"], inferred)
if (config_manager.project_root / "requirements.txt").exists():
return "pip"
async def generate_dockerfile(
python_version: str, is_asgi: bool, build_backend: str | None
):
t = templates.get_template(
"docker/reverse.Dockerfile.jinja"
if is_asgi
else "docker/forward.Dockerfile.jinja"
)
return await t.render_async(
python_version=python_version, build_backend=build_backend
)
async def generate_compose_file(is_asgi: bool):
t = templates.get_template("docker/docker-compose.yml.jinja")
return await t.render_async(is_asgi=is_asgi)