Skip to content

Commit

Permalink
feat(lang): Support command exec (#119)
Browse files Browse the repository at this point in the history
Signed-off-by: Ce Gao <cegao@tensorchord.ai>
  • Loading branch information
gaocegege committed May 9, 2022
1 parent b95aee9 commit 6d505a2
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 11 deletions.
1 change: 1 addition & 0 deletions pkg/lang/frontend/starlark/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ const (
rulePyPIMirror = "pip_mirror"
ruleShell = "shell"
ruleJupyter = "jupyter"
ruleRun = "run"
)
24 changes: 23 additions & 1 deletion pkg/lang/frontend/starlark/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func registerMIDIRules() {
starlark.Universe[rulePyPIMirror] = starlark.NewBuiltin(rulePyPIMirror, ruleFuncPyPIMirror)
starlark.Universe[ruleShell] = starlark.NewBuiltin(ruleShell, ruleFuncShell)
starlark.Universe[ruleJupyter] = starlark.NewBuiltin(ruleJupyter, ruleFuncJupyter)

starlark.Universe[ruleRun] = starlark.NewBuiltin(ruleRun, ruleFuncRun)
}

func ruleFuncBase(thread *starlark.Thread, _ *starlark.Builtin,
Expand Down Expand Up @@ -262,3 +262,25 @@ func ruleFuncJupyter(thread *starlark.Thread, _ *starlark.Builtin,

return starlark.None, nil
}

func ruleFuncRun(thread *starlark.Thread, _ *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var commands *starlark.List

if err := starlark.UnpackArgs(rulePyPIPackage,
args, kwargs, "commands?", &commands); err != nil {
return nil, err
}

goCommands := []string{}
if commands != nil {
for i := 0; i < commands.Len(); i++ {
goCommands = append(goCommands, commands.Index(i).(starlark.String).GoString())
}
}

logger.Debugf("rule `%s` is invoked, commands=%v", ruleRun, goCommands)
ir.Run(goCommands)

return starlark.None, nil
}
33 changes: 26 additions & 7 deletions pkg/lang/ir/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewGraph() *Graph {

PyPIPackages: []string{},
SystemPackages: []string{},
Exec: []llb.State{},
Exec: []string{},
Shell: shellBASH,
}
}
Expand Down Expand Up @@ -90,16 +90,21 @@ func (g Graph) Compile() (llb.State, error) {
if err != nil {
return llb.State{}, errors.Wrap(err, "failed to get vscode plugins")
}

var merged llb.State
if vscodeStage != nil {
merged := llb.Merge([]llb.State{
merged = llb.Merge([]llb.State{
builtinSystemStage, systemStage, pypiStage, sshStage, *vscodeStage, diffShellStage,
})
return merged, nil
} else {
merged = llb.Merge([]llb.State{
builtinSystemStage, systemStage, pypiStage, sshStage, diffShellStage,
})
}
merged := llb.Merge([]llb.State{
builtinSystemStage, systemStage, pypiStage, sshStage, diffShellStage,
})
return merged, nil

// TODO(gaocegege): Support order-based exec.
run := g.compileRun(merged)
return run, nil
}

func (g *Graph) compileBase() llb.State {
Expand Down Expand Up @@ -274,3 +279,17 @@ func (g Graph) compileZSH(root llb.State) (llb.State, error) {
run := zshStage.Run(llb.Shlex(fmt.Sprintf("bash %s", installPath)))
return run.Root(), nil
}

func (g Graph) compileRun(root llb.State) llb.State {
if len(g.Exec) == 0 {
return root
} else if len(g.Exec) == 1 {
return root.Run(llb.Shlex(g.Exec[0])).Root()
}

run := root.Run(llb.Shlex(g.Exec[0]))
for _, c := range g.Exec[1:] {
run = run.Run(llb.Shlex(c))
}
return run.Root()
}
6 changes: 6 additions & 0 deletions pkg/lang/ir/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,9 @@ func Jupyter(pwd string, port int64) error {
}
return nil
}

func Run(commands []string) error {
// TODO(gaocegege): Support order-based exec.
DefaultGraph.Exec = commands
return nil
}
4 changes: 1 addition & 3 deletions pkg/lang/ir/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package ir

import (
"github.com/moby/buildkit/client/llb"

"github.com/tensorchord/MIDI/pkg/vscode"
)

Expand All @@ -37,7 +35,7 @@ type Graph struct {
SystemPackages []string
VSCodePlugins []vscode.Plugin

Exec []llb.State
Exec []string
*JupyterConfig
}

Expand Down

0 comments on commit 6d505a2

Please sign in to comment.