2023-07-26 10:07:26 +00:00
|
|
|
package whl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/config"
|
2023-08-17 09:11:39 +00:00
|
|
|
"github.com/databricks/cli/bundle/libraries"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2023-08-17 09:11:39 +00:00
|
|
|
"github.com/databricks/cli/libs/log"
|
2023-07-26 10:07:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type detectPkg struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func DetectPackage() bundle.Mutator {
|
|
|
|
return &detectPkg{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *detectPkg) Name() string {
|
|
|
|
return "artifacts.whl.AutoDetect"
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (m *detectPkg) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2023-09-08 11:08:21 +00:00
|
|
|
wheelTasks := libraries.FindAllWheelTasksWithLocalLibraries(b)
|
2023-08-17 09:11:39 +00:00
|
|
|
if len(wheelTasks) == 0 {
|
2023-09-08 11:08:21 +00:00
|
|
|
log.Infof(ctx, "No local wheel tasks in databricks.yml config, skipping auto detect")
|
2023-08-17 09:11:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-12-21 08:00:37 +00:00
|
|
|
log.Infof(ctx, "Detecting Python wheel project...")
|
2023-07-26 10:07:26 +00:00
|
|
|
|
|
|
|
// checking if there is setup.py in the bundle root
|
2024-03-27 09:03:24 +00:00
|
|
|
setupPy := filepath.Join(b.RootPath, "setup.py")
|
2023-07-26 10:07:26 +00:00
|
|
|
_, err := os.Stat(setupPy)
|
|
|
|
if err != nil {
|
2023-12-21 08:00:37 +00:00
|
|
|
log.Infof(ctx, "No Python wheel project found at bundle root folder")
|
2023-07-26 10:07:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-27 09:03:24 +00:00
|
|
|
log.Infof(ctx, fmt.Sprintf("Found Python wheel project at %s", b.RootPath))
|
2023-07-26 10:07:26 +00:00
|
|
|
module := extractModuleName(setupPy)
|
|
|
|
|
|
|
|
if b.Config.Artifacts == nil {
|
|
|
|
b.Config.Artifacts = make(map[string]*config.Artifact)
|
|
|
|
}
|
|
|
|
|
2024-03-27 09:03:24 +00:00
|
|
|
pkgPath, err := filepath.Abs(b.RootPath)
|
2023-07-26 10:07:26 +00:00
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2023-07-26 10:07:26 +00:00
|
|
|
}
|
|
|
|
b.Config.Artifacts[module] = &config.Artifact{
|
|
|
|
Path: pkgPath,
|
|
|
|
Type: config.ArtifactPythonWheel,
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractModuleName(setupPy string) string {
|
|
|
|
bytes, err := os.ReadFile(setupPy)
|
|
|
|
if err != nil {
|
|
|
|
return randomName()
|
|
|
|
}
|
|
|
|
|
|
|
|
content := string(bytes)
|
|
|
|
r := regexp.MustCompile(`name=['"](.*)['"]`)
|
|
|
|
matches := r.FindStringSubmatch(content)
|
|
|
|
if len(matches) == 0 {
|
|
|
|
return randomName()
|
|
|
|
}
|
|
|
|
return matches[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomName() string {
|
|
|
|
return fmt.Sprintf("artifact%d", time.Now().Unix())
|
|
|
|
}
|