Skip to content

Commit

Permalink
Updates python bridge to copy 'ai' folder to azd user config director…
Browse files Browse the repository at this point in the history
…y instead of azd env folder
  • Loading branch information
wbreza committed Apr 25, 2024
1 parent fbaddbd commit 6a2aa89
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
13 changes: 7 additions & 6 deletions cli/azd/pkg/ai/python_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"
"path/filepath"

"github.com/azure/azure-dev/cli/azd/pkg/environment"
"github.com/azure/azure-dev/cli/azd/pkg/config"
"github.com/azure/azure-dev/cli/azd/pkg/environment/azdcontext"
"github.com/azure/azure-dev/cli/azd/pkg/exec"
"github.com/azure/azure-dev/cli/azd/pkg/osutil"
Expand Down Expand Up @@ -36,7 +36,6 @@ type PythonBridge interface {
// pythonBridge is a bridge to execute python components from the embedded AI resources project
type pythonBridge struct {
azdCtx *azdcontext.AzdContext
env *environment.Environment
pythonCli *python.PythonCli
workingDir string
initialized bool
Expand All @@ -45,12 +44,10 @@ type pythonBridge struct {
// NewPythonBridge creates a new PythonBridge instance
func NewPythonBridge(
azdCtx *azdcontext.AzdContext,
env *environment.Environment,
pythonCli *python.PythonCli,
) PythonBridge {
return &pythonBridge{
azdCtx: azdCtx,
env: env,
pythonCli: pythonCli,
}
}
Expand Down Expand Up @@ -84,8 +81,12 @@ func (b *pythonBridge) Run(ctx context.Context, scriptName ScriptPath, args ...s

// initPython initializes the Python environment
func (b *pythonBridge) initPython(ctx context.Context) error {
envDir := b.azdCtx.EnvironmentRoot(b.env.Name())
targetDir := filepath.Join(envDir, "ai")
configDir, err := config.GetUserConfigDir()
if err != nil {
return err
}

targetDir := filepath.Join(configDir, "bin", "ai")
if _, err := os.Stat(targetDir); os.IsNotExist(err) {
if err := os.MkdirAll(targetDir, osutil.PermissionDirectory); err != nil {
return err
Expand Down
22 changes: 14 additions & 8 deletions cli/azd/pkg/ai/python_bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package ai

import (
"context"
"os"
"path/filepath"
"strings"
"testing"

"github.com/azure/azure-dev/cli/azd/pkg/environment"
"github.com/azure/azure-dev/cli/azd/pkg/config"
"github.com/azure/azure-dev/cli/azd/pkg/environment/azdcontext"
"github.com/azure/azure-dev/cli/azd/pkg/exec"
"github.com/azure/azure-dev/cli/azd/pkg/tools/python"
Expand All @@ -15,10 +16,13 @@ import (
)

func Test_PythonBridge_Init(t *testing.T) {
tempDir := t.TempDir()
mockContext := mocks.NewMockContext(context.Background())
env := environment.New("test")
pythonCli := python.NewPythonCli(mockContext.CommandRunner)
azdCtx := azdcontext.NewAzdContextWithDirectory(t.TempDir())
azdCtx := azdcontext.NewAzdContextWithDirectory(tempDir)

azdConfigDir := filepath.Join(tempDir, ".azd")
os.Setenv("AZD_CONFIG_DIR", azdConfigDir)

createdVirtualEnv := false
installedDependencies := false
Expand All @@ -37,10 +41,13 @@ func Test_PythonBridge_Init(t *testing.T) {
return exec.NewRunResult(0, "", ""), nil
})

aiDir := filepath.Join(azdCtx.EnvironmentRoot(env.Name()), "ai")
userConfigDir, err := config.GetUserConfigDir()
require.NoError(t, err)

aiDir := filepath.Join(userConfigDir, "bin", "ai")

bridge := NewPythonBridge(azdCtx, env, pythonCli)
err := bridge.Initialize(*mockContext.Context)
bridge := NewPythonBridge(azdCtx, pythonCli)
err = bridge.Initialize(*mockContext.Context)
require.NoError(t, err)
require.DirExists(t, aiDir)
require.True(t, createdVirtualEnv)
Expand All @@ -49,7 +56,6 @@ func Test_PythonBridge_Init(t *testing.T) {

func Test_PythonBridge_Run(t *testing.T) {
mockContext := mocks.NewMockContext(context.Background())
env := environment.New("test")
pythonCli := python.NewPythonCli(mockContext.CommandRunner)
azdCtx := azdcontext.NewAzdContextWithDirectory(t.TempDir())

Expand All @@ -61,7 +67,7 @@ func Test_PythonBridge_Run(t *testing.T) {
return exec.NewRunResult(0, "result", ""), nil
})

bridge := NewPythonBridge(azdCtx, env, pythonCli)
bridge := NewPythonBridge(azdCtx, pythonCli)
result, err := bridge.Run(*mockContext.Context, "script.py", "arg1", "arg2")
require.NoError(t, err)
require.NotNil(t, result)
Expand Down

0 comments on commit 6a2aa89

Please sign in to comment.