2023-08-28 16:29:04 +00:00
|
|
|
package whl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/config"
|
|
|
|
"github.com/databricks/cli/bundle/libraries"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2023-08-28 16:29:04 +00:00
|
|
|
"github.com/databricks/cli/libs/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type fromLibraries struct{}
|
|
|
|
|
|
|
|
func DefineArtifactsFromLibraries() bundle.Mutator {
|
|
|
|
return &fromLibraries{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *fromLibraries) Name() string {
|
|
|
|
return "artifacts.whl.DefineArtifactsFromLibraries"
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (*fromLibraries) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2023-08-28 16:29:04 +00:00
|
|
|
if len(b.Config.Artifacts) != 0 {
|
|
|
|
log.Debugf(ctx, "Skipping defining artifacts from libraries because artifacts section is explicitly defined")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-05 14:44:23 +00:00
|
|
|
tasks := libraries.FindTasksWithLocalLibraries(b)
|
2023-08-28 16:29:04 +00:00
|
|
|
for _, task := range tasks {
|
2024-08-05 14:44:23 +00:00
|
|
|
// Skip tasks that are not PythonWheelTasks for now, we can later support Jars too
|
|
|
|
if task.PythonWheelTask == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-08-28 16:29:04 +00:00
|
|
|
for _, lib := range task.Libraries {
|
2024-04-22 11:44:34 +00:00
|
|
|
matchAndAdd(ctx, lib.Whl, b)
|
|
|
|
}
|
|
|
|
}
|
2023-08-28 16:29:04 +00:00
|
|
|
|
2024-04-22 11:44:34 +00:00
|
|
|
envs := libraries.FindAllEnvironments(b)
|
|
|
|
for _, jobEnvs := range envs {
|
|
|
|
for _, env := range jobEnvs {
|
|
|
|
if env.Spec != nil {
|
|
|
|
for _, dep := range env.Spec.Dependencies {
|
|
|
|
if libraries.IsEnvironmentDependencyLocal(dep) {
|
|
|
|
matchAndAdd(ctx, dep, b)
|
|
|
|
}
|
2023-08-28 16:29:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-04-22 11:44:34 +00:00
|
|
|
|
|
|
|
func matchAndAdd(ctx context.Context, lib string, b *bundle.Bundle) {
|
|
|
|
matches, err := filepath.Glob(filepath.Join(b.RootPath, lib))
|
|
|
|
// File referenced from libraries section does not exists, skipping
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, match := range matches {
|
|
|
|
name := filepath.Base(match)
|
|
|
|
if b.Config.Artifacts == nil {
|
|
|
|
b.Config.Artifacts = make(map[string]*config.Artifact)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf(ctx, "Adding an artifact block for %s", match)
|
|
|
|
b.Config.Artifacts[name] = &config.Artifact{
|
|
|
|
Files: []config.ArtifactFile{
|
|
|
|
{Source: match},
|
|
|
|
},
|
|
|
|
Type: config.ArtifactPythonWheel,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|