loadFiles loads the contents of certain module source files.
(ctx context.Context, m *PythonSdk)
| 283 | |
| 284 | // loadFiles loads the contents of certain module source files. |
| 285 | func (d *Discovery) loadFiles(ctx context.Context, m *PythonSdk) error { |
| 286 | // If there's a dagger.json and no pyproject.toml, it's an init'ed module |
| 287 | // adding sources (`dagger develop --sdk`). |
| 288 | if !m.IsInit && !d.HasFile("pyproject.toml") { |
| 289 | m.IsInit = true |
| 290 | } |
| 291 | |
| 292 | // These paths should be in "exclude" in dagger.json. |
| 293 | // Let's remove them just in case, to avoid conflicts. |
| 294 | for _, exclude := range DirExcludes { |
| 295 | if d.HasFile(exclude) { |
| 296 | m.ContextDir = m.ContextDir.WithoutDirectory( |
| 297 | path.Join(m.SubPath, exclude), |
| 298 | ) |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | eg, gctx := errgroup.WithContext(ctx) |
| 303 | |
| 304 | if d.EnableCustomConfig { |
| 305 | for _, name := range FileContents { |
| 306 | if d.HasFile(name) { |
| 307 | eg.Go(func() error { |
| 308 | contents, err := m.GetFile(name).Contents(gctx) |
| 309 | if err != nil { |
| 310 | return fmt.Errorf("get file contents of %q: %w", name, err) |
| 311 | } |
| 312 | d.mu.Lock() |
| 313 | d.Files[name] = strings.TrimSpace(contents) |
| 314 | d.mu.Unlock() |
| 315 | return nil |
| 316 | }) |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | eg.Go(func() error { |
| 322 | // We'll use a glob pattern in fileSet to check for the existence of |
| 323 | // python files later. The error is normal when the target directory |
| 324 | // on `dagger init` doesn't exist, but just ignore otherwise (best |
| 325 | // effort). |
| 326 | entries, err := m.Source().Glob(gctx, "src/**/*.py|*.py") |
| 327 | if len(entries) > 0 { |
| 328 | d.mu.Lock() |
| 329 | d.FileSet["*.py"] = struct{}{} |
| 330 | d.mu.Unlock() |
| 331 | } else if err == nil && !m.IsInit { |
| 332 | // This can also happen on `dagger develop --sdk` if there's also |
| 333 | // a pyproject.toml present to customize the base container. |
| 334 | return fmt.Errorf("no python files found in module source") |
| 335 | } |
| 336 | return nil |
| 337 | }) |
| 338 | |
| 339 | eg.Go(func() error { |
| 340 | entries, _ := m.SdkSourceDir.Entries(gctx) |
| 341 | d.mu.Lock() |
| 342 | for _, entry := range entries { |
nothing calls this directly
no test coverage detected