-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.dang
More file actions
303 lines (271 loc) · 9.56 KB
/
Copy pathmain.dang
File metadata and controls
303 lines (271 loc) · 9.56 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
pub description = "A toolchain for testing Python applications with automatic OpenTelemetry tracing"
enum Runner {
AUTO
UV
PIP
}
enum EnvTarget {
AUTO
VENV
SYSTEM
}
type Pytest {
"""
The uv binary extracted from the bundled Docker image
"""
let uvBinary: Directory! {
currentModule.source.directory("images/uv").dockerBuild.rootfs
}
"""
The alpine base image
"""
let alpine: Container! {
currentModule.source.directory("images/alpine").dockerBuild
}
"""
The pytest_otel source directory bundled with this module
"""
let pytestOtelSource = currentModule.source.directory("pytest_otel")
"""
The source directory containing your Python project, taken from the
workspace at `sourcePath` in the constructor.
"""
pub source: Directory!
"""
Additional arguments to pass to pytest (e.g., ["-x", "--tb=short"])
"""
pub args: [String!]! = ["-v"]
"""
Optional: Python version to use (e.g., "3.14", "3.13", "3.12")
Only relevant when using the default base.
"""
pub pythonVersion: String!
"""
Optional: a custom image that replaces the default Alpine+uv base.
When set, Python provisioning (`uv venv`) is skipped.
"""
pub container: Container = null
"""
Which Python tool to use. AUTO probes the container (prefers uv, else pip).
"""
pub runner: Runner = Runner.AUTO
"""
Skip installing project dependencies and mounting the source code directory
before running `test`. Set true when the container already has both baked in.
"""
pub skipInstallDeps: Boolean!
"""
Construct the toolchain from a workspace.
`source` is taken from the workspace at `sourcePath`; every other field is
exposed here so consumers and `customizations` can configure the full
pipeline (pytest args, Python version, custom container, runner, dependency
installation).
"""
new(
ws: Workspace!,
sourcePath: String! = "/",
args: [String!]! = ["-v"],
"""
Optional: Python version to use (e.g., "3.14", "3.13", "3.12")
Only relevant when using the default base.
"""
pythonVersion: String! = "3.14",
container: Container = null,
runner: Runner = Runner.AUTO,
"""
Skip installing project dependencies and mounting the source code directory
before running `test`. Set true when the container already has both baked in.
"""
skipInstallDeps: Boolean! = false,
) {
self.source = ws.directory(sourcePath)
self.args = args
self.pythonVersion = pythonVersion
self.container = container
self.runner = runner
self.skipInstallDeps = skipInstallDeps
self
}
"""
The default Alpine + uv base: Python tooling only.
"""
let defaultBase: Container! {
alpine
.withExec(["apk", "add", "libgcc"])
.withEnvVariable("PYTHONUNBUFFERED", "1")
.withEnvVariable("PATH", "/root/.local/bin:/usr/local/bin:$PATH", expand: true)
.withEnvVariable("UV_LINK_MODE", "copy")
.withEnvVariable("UV_PROJECT_ENVIRONMENT", "/opt/venv")
.withEnvVariable("VIRTUAL_ENV", "/opt/venv")
.withMountedCache("/root/.cache/uv", cacheVolume("pytest-toolchain-uv"))
.withDirectory("/usr/local/bin", uvBinary, include: ["uv*"])
.withExec(["uv", "venv", "/opt/venv", "-p", pythonVersion])
}
"""
The base used by `test`: the custom container if provided, else the default.
"""
let base: Container! {
if (container != null) {
container
} else {
defaultBase
}
}
"""
Test a Python project with pytest and OpenTelemetry tracing.
Builds a container from the base (custom or default), adds your source,
optionally installs dependencies, then dispatches to the uv or pip run path
(auto-detected unless `runner` is set explicitly).
"""
pub test: Void @check {
let chosen = resolveRunner(base)
let result = if (chosen == Runner.UV) {
testUv(installDepsUv(base))
} else {
testPip(installDepsPip(base))
}
result.sync
null
}
"""
Resolve the effective runner: when `runner` is AUTO, probe the container
(prefer uv, else pip); otherwise use the configured value.
"""
let resolveRunner(ctr: Container!): Runner {
if (runner == Runner.AUTO) {
if (ctr.withExec(["sh", "-c", "command -v uv"], expect: ReturnType.ANY).exitCode == 0) {
Runner.UV
} else {
Runner.PIP
}
} else {
runner
}
}
let installDepsUv(ctr: Container!): Container! {
if (!skipInstallDeps) {
# requirements.txt-only projects need explicit install; pyproject.toml
# projects are synced by `uv run` at test time.
let ctr = addSourceDir(ctr, source)
if (source.exists("requirements.txt") and !source.exists("pyproject.toml")) {
ctr = ctr.withExec(["uv", "pip", "install", "-r", "requirements.txt"])
}
ctr
} else {
ctr
}
}
let installDepsPip(ctr: Container!): Container! {
if (!skipInstallDeps) {
let ctr = addSourceDir(ctr, source)
if (source.exists("requirements.txt")) {
ctr = ctr.withExec(["pip", "install", "-r", "requirements.txt"])
} else if (source.exists("pyproject.toml")) {
ctr = ctr.withExec(["pip", "install", "."])
}
ctr
} else {
ctr
}
}
let addSourceDir(
ctr: Container!,
dir: Directory!,
): Container! {
ctr
.withDirectory("/app", dir)
.withWorkdir("/app")
}
"""
Build the pip command that installs pytest_otel for the given `envTarget`.
`pip` is the invocation prefix (`pip`, `python3 -m pip`, ...). AUTO tries a
plain install and retries with `--break-system-packages` (works in a venv, a
plain system, or an externally-managed system); VENV installs as-is and fails
loudly when no usable env is active; SYSTEM forces `--break-system-packages`.
"""
let pipInstall(pip: String!, envTarget: EnvTarget): String! {
if (envTarget == EnvTarget.SYSTEM) {
pip + " install --break-system-packages /opt/pytest_otel"
} else if (envTarget == EnvTarget.VENV) {
pip + " install /opt/pytest_otel"
} else {
pip + " install /opt/pytest_otel || " + pip + " install --break-system-packages /opt/pytest_otel"
}
}
"""
Run pytest with uv on a container that already has source + deps. Installs
pytest_otel (forced uv, into the project venv since `uv run` is venv-based),
then `uv run pytest`. Returns the post-run container.
"""
pub testUv(
ctr: Container!,
): Container! {
installPytestOtel(ctr, runner: Runner.UV, envTarget: EnvTarget.VENV)
.withExec(["uv", "run", "pytest"] + args)
}
"""
Run pytest with pip on a container that already has source + deps. Installs
pytest_otel (forced pip), then `python -m pytest`. Returns the post-run container.
"""
pub testPip(
ctr: Container!,
): Container! {
installPytestOtel(ctr, runner: Runner.PIP)
.withExec(["python", "-m", "pytest"] + args)
}
"""
Install pytest_otel into a container you manage yourself.
`runner` AUTO detects the tool at runtime (uv -> pip -> python -m pip); UV or
PIP forces that tool even when others are present.
`envTarget` chooses where pytest_otel lands. AUTO installs into an existing
virtual environment when one is present and otherwise falls back to a system
install; VENV forces a virtual environment, creating it when missing; SYSTEM
forces a system install (`uv ... --system --break-system-packages` /
`pip ... --break-system-packages`). Use SYSTEM (or AUTO) for containers
without a `.venv`.
Returns the container with pytest_otel installed; run pytest yourself on it.
"""
pub installPytestOtel(
ctr: Container!,
runner: Runner = Runner.AUTO,
envTarget: EnvTarget = EnvTarget.AUTO,
): Container! {
# uv resolves its `pip install` target from VIRTUAL_ENV / an active `.venv`
# (ignoring UV_PROJECT_ENVIRONMENT), so resolve the env path explicitly into
# $T. AUTO installs into that env when it exists, else falls back to a system
# install (`--system --break-system-packages` is the only form uv honors for
# a non-virtual environment; UV_BREAK_SYSTEM_PACKAGES alone is a no-op).
# VENV creates the env when missing; SYSTEM always targets the base Python.
let uvResolve = "T=\"${VIRTUAL_ENV:-${UV_PROJECT_ENVIRONMENT:-.venv}}\"; "
let uvVenv = "uv pip install --python \"$T/bin/python\" /opt/pytest_otel"
let uvSystem = "uv pip install --system --break-system-packages /opt/pytest_otel"
let uvInstall = if (envTarget == EnvTarget.SYSTEM) {
uvSystem
} else if (envTarget == EnvTarget.VENV) {
uvResolve + "[ -x \"$T/bin/python\" ] || uv venv \"$T\"; " + uvVenv
} else {
uvResolve + "if [ -x \"$T/bin/python\" ]; then " + uvVenv + "; else " + uvSystem + "; fi"
}
let installCmd = if (runner == Runner.UV) {
uvInstall
} else if (runner == Runner.PIP) {
pipInstall("pip", envTarget)
} else {
"if command -v uv >/dev/null 2>&1; then " + uvInstall + "; "
+ "elif command -v pip >/dev/null 2>&1; then " + pipInstall("pip", envTarget) + "; "
+ "elif command -v python3 >/dev/null 2>&1; then " + pipInstall("python3 -m pip", envTarget) + "; "
+ "elif command -v python >/dev/null 2>&1; then " + pipInstall("python -m pip", envTarget) + "; "
+ "else echo 'pytest toolchain: could not find uv, pip, or python to install pytest_otel' >&2; exit 1; fi"
}
ctr
.withDirectory("/opt/pytest_otel", pytestOtelSource)
.withExec(["sh", "-c", "set -e; " + installCmd])
}
"""
The bundled pytest_otel library, as a Directory, for fully-manual integration
(the package is not yet published to PyPI).
"""
pub pytestOtel: Directory! {
pytestOtelSource
}
}