MCPcopy Index your code
hub / github.com/dagger/dagger / WithTemplate

Method WithTemplate

sdk/python/runtime/main.go:325–384  ·  view source on GitHub ↗

Add the template files to skaffold a new module The following files are added: - /runtime - /pyproject.toml - /src/ /__init__.py - /src/ /main.py

()

Source from the content-addressed store, hash-verified

323// - <source>/src/<package_name>/__init__.py
324// - <source>/src/<package_name>/main.py
325func (m *PythonSdk) WithTemplate() *PythonSdk {
326 m.Container = m.Container.
327 WithFile(
328 RuntimeExecutablePath,
329 dag.CurrentModule().Source().File("template/runtime.py"),
330 dagger.ContainerWithFileOpts{Permissions: 0o755},
331 ).
332 WithEntrypoint([]string{RuntimeExecutablePath})
333
334 d := m.Discovery
335
336 // NB: We can't detect if it's a new module with `dagger develop --sdk`
337 // if there's also a pyproject.toml file to customize the base container.
338 //
339 // The reason for adding sources only on new modules is because it's
340 // been reported that it's surprising for users to delete the pyhton
341 // file on the host and not fail on `dagger functions` and `dagger call`,
342 // if we always recreate from the template. That's because only `init`
343 // and `develop` export the generated files back to the host, potentially
344 // creating a discrepancy.
345 //
346 // Throwing an error on missing files when not a new module is less
347 // surprising, which is done during discovery.
348
349 if m.IsInit {
350 // On `dagger init --sdk`, one can first set a `pyproject.toml` to
351 // change the base image, but if it's `dagger develop --sdk` the
352 // existence of this file will set d.IsInit = true, thus skipping
353 // this entire branch.
354 if !d.HasFile(ProjectCfg) {
355 projCfg := strings.ReplaceAll(tplToml, "main", m.ProjectName)
356 // Align the template's requires-python with the version the
357 // runtime actually selected (typically from .python-version),
358 // otherwise a freshly created pyproject can declare a higher
359 // minimum than the Python that ends up in the container and uv
360 // will refuse to lock.
361 if version := d.findPythonVersion(); version != "" {
362 projCfg = strings.Replace(
363 projCfg,
364 `requires-python = ">=3.14"`,
365 fmt.Sprintf(`requires-python = ">=%s"`, version),
366 1,
367 )
368 }
369 m.AddNewFile(ProjectCfg, VendorConfig(projCfg, m.VendorPath))
370 }
371 if !d.HasFile("*.py") {
372 m.AddNewFile(
373 path.Join("src", m.PackageName, "__init__.py"),
374 strings.ReplaceAll(tplInit, MainObjectName, m.MainObjectName),
375 )
376 m.AddNewFile(
377 path.Join("src", m.PackageName, "main.py"),
378 strings.ReplaceAll(tplMain, MainObjectName, m.MainObjectName),
379 )
380 }
381 }
382

Callers 2

invokeFunction · 0.80
CommonMethod · 0.80

Calls 10

AddNewFileMethod · 0.95
VendorConfigFunction · 0.85
HasFileMethod · 0.80
findPythonVersionMethod · 0.80
ReplaceMethod · 0.80
FileMethod · 0.65
CurrentModuleMethod · 0.65
WithEntrypointMethod · 0.45
WithFileMethod · 0.45
SourceMethod · 0.45

Tested by

no test coverage detected