2022-12-16 13:49:23 +00:00
|
|
|
package mutator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2023-02-20 18:42:55 +00:00
|
|
|
"strings"
|
2022-12-16 13:49:23 +00:00
|
|
|
|
|
|
|
"github.com/databricks/bricks/bundle"
|
2023-02-20 18:42:55 +00:00
|
|
|
"github.com/databricks/bricks/libs/notebook"
|
2022-12-16 13:49:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type translateNotebookPaths struct {
|
|
|
|
seen map[string]string
|
|
|
|
}
|
|
|
|
|
2023-02-20 18:42:55 +00:00
|
|
|
// TranslateNotebookPaths converts paths to local notebook files into paths in the workspace file system.
|
2022-12-16 13:49:23 +00:00
|
|
|
func TranslateNotebookPaths() bundle.Mutator {
|
|
|
|
return &translateNotebookPaths{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *translateNotebookPaths) Name() string {
|
|
|
|
return "TranslateNotebookPaths"
|
|
|
|
}
|
|
|
|
|
2023-02-20 18:42:55 +00:00
|
|
|
func (m *translateNotebookPaths) rewritePath(b *bundle.Bundle, p *string) error {
|
2022-12-16 13:49:23 +00:00
|
|
|
relPath := path.Clean(*p)
|
2023-02-20 18:42:55 +00:00
|
|
|
if interp, ok := m.seen[relPath]; ok {
|
|
|
|
*p = interp
|
|
|
|
return nil
|
|
|
|
}
|
2022-12-16 13:49:23 +00:00
|
|
|
|
2023-02-20 18:42:55 +00:00
|
|
|
absPath := filepath.Join(b.Config.Path, relPath)
|
|
|
|
nb, _, err := notebook.Detect(absPath)
|
2022-12-16 13:49:23 +00:00
|
|
|
if err != nil {
|
2023-02-20 18:42:55 +00:00
|
|
|
// Ignore if this file doesn't exist. Maybe it's an absolute workspace path?
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("unable to determine if %s is a notebook: %w", relPath, err)
|
2022-12-16 13:49:23 +00:00
|
|
|
}
|
|
|
|
|
2023-02-20 18:42:55 +00:00
|
|
|
if !nb {
|
|
|
|
return fmt.Errorf("file at %s is not a notebook", relPath)
|
2022-12-16 13:49:23 +00:00
|
|
|
}
|
|
|
|
|
2023-02-20 18:42:55 +00:00
|
|
|
// Upon import, notebooks are stripped of their extension.
|
|
|
|
withoutExt := strings.TrimSuffix(relPath, filepath.Ext(relPath))
|
2022-12-16 13:49:23 +00:00
|
|
|
|
2023-02-20 18:42:55 +00:00
|
|
|
// We have a notebook on our hands! It will be available under the file path.
|
|
|
|
interp := fmt.Sprintf("${workspace.file_path.workspace}/%s", withoutExt)
|
2022-12-16 13:49:23 +00:00
|
|
|
*p = interp
|
2023-02-20 18:42:55 +00:00
|
|
|
m.seen[relPath] = interp
|
|
|
|
return nil
|
2022-12-16 13:49:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *translateNotebookPaths) Apply(_ context.Context, b *bundle.Bundle) ([]bundle.Mutator, error) {
|
|
|
|
m.seen = make(map[string]string)
|
|
|
|
|
|
|
|
for _, job := range b.Config.Resources.Jobs {
|
|
|
|
for i := 0; i < len(job.Tasks); i++ {
|
|
|
|
task := &job.Tasks[i]
|
|
|
|
if task.NotebookTask == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-02-20 18:42:55 +00:00
|
|
|
err := m.rewritePath(b, &task.NotebookTask.NotebookPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-16 13:49:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pipeline := range b.Config.Resources.Pipelines {
|
|
|
|
for i := 0; i < len(pipeline.Libraries); i++ {
|
|
|
|
library := &pipeline.Libraries[i]
|
|
|
|
if library.Notebook == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-02-20 18:42:55 +00:00
|
|
|
err := m.rewritePath(b, &library.Notebook.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-16 13:49:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|