mirror of https://github.com/databricks/cli.git
Revert "Simplify whl artifact autodetection code (#2371)"
This reverts commit 14f0292598
.
This commit is contained in:
parent
14f0292598
commit
fd6f11ba18
|
@ -0,0 +1,32 @@
|
||||||
|
package artifacts
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/databricks/cli/bundle"
|
||||||
|
"github.com/databricks/cli/bundle/artifacts/whl"
|
||||||
|
"github.com/databricks/cli/libs/diag"
|
||||||
|
"github.com/databricks/cli/libs/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func DetectPackages() bundle.Mutator {
|
||||||
|
return &autodetect{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type autodetect struct{}
|
||||||
|
|
||||||
|
func (m *autodetect) Name() string {
|
||||||
|
return "artifacts.DetectPackages"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *autodetect) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
||||||
|
// If artifacts section explicitly defined, do not try to auto detect packages
|
||||||
|
if b.Config.Artifacts != nil {
|
||||||
|
log.Debugf(ctx, "artifacts block is defined, skipping auto-detecting")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return bundle.Apply(ctx, b, bundle.Seq(
|
||||||
|
whl.DetectPackage(),
|
||||||
|
))
|
||||||
|
}
|
|
@ -2,8 +2,11 @@ package whl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/databricks/cli/bundle"
|
"github.com/databricks/cli/bundle"
|
||||||
"github.com/databricks/cli/bundle/config"
|
"github.com/databricks/cli/bundle/config"
|
||||||
|
@ -23,17 +26,11 @@ func (m *detectPkg) Name() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *detectPkg) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
func (m *detectPkg) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
||||||
if b.Config.Artifacts != nil {
|
|
||||||
log.Debugf(ctx, "artifacts block is defined, skipping auto-detecting")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks := libraries.FindTasksWithLocalLibraries(b)
|
tasks := libraries.FindTasksWithLocalLibraries(b)
|
||||||
if len(tasks) == 0 {
|
if len(tasks) == 0 {
|
||||||
log.Infof(ctx, "No local tasks in databricks.yml config, skipping auto detect")
|
log.Infof(ctx, "No local tasks in databricks.yml config, skipping auto detect")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof(ctx, "Detecting Python wheel project...")
|
log.Infof(ctx, "Detecting Python wheel project...")
|
||||||
|
|
||||||
// checking if there is setup.py in the bundle root
|
// checking if there is setup.py in the bundle root
|
||||||
|
@ -45,18 +42,39 @@ func (m *detectPkg) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostic
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof(ctx, "Found Python wheel project at %s", b.BundleRootPath)
|
log.Infof(ctx, "Found Python wheel project at %s", b.BundleRootPath)
|
||||||
|
module := extractModuleName(setupPy)
|
||||||
|
|
||||||
|
if b.Config.Artifacts == nil {
|
||||||
|
b.Config.Artifacts = make(map[string]*config.Artifact)
|
||||||
|
}
|
||||||
|
|
||||||
pkgPath, err := filepath.Abs(b.BundleRootPath)
|
pkgPath, err := filepath.Abs(b.BundleRootPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return diag.FromErr(err)
|
return diag.FromErr(err)
|
||||||
}
|
}
|
||||||
|
b.Config.Artifacts[module] = &config.Artifact{
|
||||||
b.Config.Artifacts = make(map[string]*config.Artifact)
|
|
||||||
b.Config.Artifacts["python_artifact"] = &config.Artifact{
|
|
||||||
Path: pkgPath,
|
Path: pkgPath,
|
||||||
Type: config.ArtifactPythonWheel,
|
Type: config.ArtifactPythonWheel,
|
||||||
// BuildCommand will be set by bundle/artifacts/whl/infer.go to "python3 setup.py bdist_wheel"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
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())
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
package whl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestExtractModuleName(t *testing.T) {
|
||||||
|
moduleName := extractModuleName("./testdata/setup.py")
|
||||||
|
assert.Equal(t, "my_test_code", moduleName)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExtractModuleNameMinimal(t *testing.T) {
|
||||||
|
moduleName := extractModuleName("./testdata/setup_minimal.py")
|
||||||
|
assert.Equal(t, "my_test_code", moduleName)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExtractModuleNameIncorrect(t *testing.T) {
|
||||||
|
moduleName := extractModuleName("./testdata/setup_incorrect.py")
|
||||||
|
assert.Contains(t, moduleName, "artifact")
|
||||||
|
}
|
|
@ -3,7 +3,6 @@ package phases
|
||||||
import (
|
import (
|
||||||
"github.com/databricks/cli/bundle"
|
"github.com/databricks/cli/bundle"
|
||||||
"github.com/databricks/cli/bundle/artifacts"
|
"github.com/databricks/cli/bundle/artifacts"
|
||||||
"github.com/databricks/cli/bundle/artifacts/whl"
|
|
||||||
"github.com/databricks/cli/bundle/config"
|
"github.com/databricks/cli/bundle/config"
|
||||||
"github.com/databricks/cli/bundle/config/mutator"
|
"github.com/databricks/cli/bundle/config/mutator"
|
||||||
"github.com/databricks/cli/bundle/scripts"
|
"github.com/databricks/cli/bundle/scripts"
|
||||||
|
@ -15,7 +14,7 @@ func Build() bundle.Mutator {
|
||||||
"build",
|
"build",
|
||||||
[]bundle.Mutator{
|
[]bundle.Mutator{
|
||||||
scripts.Execute(config.ScriptPreBuild),
|
scripts.Execute(config.ScriptPreBuild),
|
||||||
whl.DetectPackage(),
|
artifacts.DetectPackages(),
|
||||||
artifacts.InferMissingProperties(),
|
artifacts.InferMissingProperties(),
|
||||||
artifacts.PrepareAll(),
|
artifacts.PrepareAll(),
|
||||||
artifacts.BuildAll(),
|
artifacts.BuildAll(),
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
Building python_artifact...
|
Building project_name_$UNIQUE_PRJ...
|
||||||
Uploading project_name_$UNIQUE_PRJ-0.0.1+[NUMID].[NUMID]-py3-none-any.whl...
|
Uploading project_name_$UNIQUE_PRJ-0.0.1+[NUMID].[NUMID]-py3-none-any.whl...
|
||||||
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/project_name_$UNIQUE_PRJ/dev/files...
|
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/project_name_$UNIQUE_PRJ/dev/files...
|
||||||
Deploying resources...
|
Deploying resources...
|
||||||
|
|
Loading…
Reference in New Issue