Simplify whl artifact autodetection code (#2371)

## Changes
- Get rid of artifacts.DetectPackages which is a thin wrapper around
artifacts/whl.DetectPackage
- Get rid of parsing name out of setup.py. Do not randomize either, use
a static one.

## Tests
Existing tests.
This commit is contained in:
Denis Bilenko 2025-02-25 14:10:25 +01:00 committed by GitHub
parent 0eeb3ca647
commit 14f0292598
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 13 additions and 84 deletions

View File

@ -1,32 +0,0 @@
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(),
))
}

View File

@ -2,11 +2,8 @@ package whl
import (
"context"
"fmt"
"os"
"path/filepath"
"regexp"
"time"
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
@ -26,11 +23,17 @@ func (m *detectPkg) Name() string {
}
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)
if len(tasks) == 0 {
log.Infof(ctx, "No local tasks in databricks.yml config, skipping auto detect")
return nil
}
log.Infof(ctx, "Detecting Python wheel project...")
// checking if there is setup.py in the bundle root
@ -42,39 +45,18 @@ func (m *detectPkg) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostic
}
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)
if err != nil {
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,
Type: config.ArtifactPythonWheel,
// BuildCommand will be set by bundle/artifacts/whl/infer.go to "python3 setup.py bdist_wheel"
}
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())
}

View File

@ -1,22 +0,0 @@
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")
}

View File

@ -3,6 +3,7 @@ package phases
import (
"github.com/databricks/cli/bundle"
"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/mutator"
"github.com/databricks/cli/bundle/scripts"
@ -14,7 +15,7 @@ func Build() bundle.Mutator {
"build",
[]bundle.Mutator{
scripts.Execute(config.ScriptPreBuild),
artifacts.DetectPackages(),
whl.DetectPackage(),
artifacts.InferMissingProperties(),
artifacts.PrepareAll(),
artifacts.BuildAll(),

View File

@ -1,4 +1,4 @@
Building project_name_$UNIQUE_PRJ...
Building python_artifact...
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...
Deploying resources...