Skip to content

Commit

Permalink
Refactor R package installation (#1381)
Browse files Browse the repository at this point in the history
* Support installing R environment in DEB822 format

Signed-off-by: Botong Ou <[email protected]>

* Support installing R environment in DEB822 format

Signed-off-by: Botong Ou <[email protected]>

* Support installing R environment in DEB822 format

Signed-off-by: Botong Ou <[email protected]>

* Support installing R environment in DEB822 format

Signed-off-by: Botong Ou <[email protected]>

* Temporarily removed return error from installRLang

Signed-off-by: Botong Ou <[email protected]>

* Append to apt.conf

Signed-off-by: Botong Ou <[email protected]>

* Move apt configuration to a separate file /etc/apt/apt.conf.d/DEB822.conf

Signed-off-by: Botong Ou <[email protected]>

* Add comments and change some typos

Signed-off-by: Botong Ou <[email protected]>

* Adjust comments

Signed-off-by: Botong Ou <[email protected]>

* Adjust comments

Signed-off-by: Botong Ou <[email protected]>

* Fix typo

Signed-off-by: Botong Ou <[email protected]>

* Change to const

Signed-off-by: Botong Ou <[email protected]>

* Change to const

Signed-off-by: Botong Ou <[email protected]>

* Change internal message when install R environment

Signed-off-by: Botong Ou <[email protected]>

* Add e2e test for R language environment

Signed-off-by: Botong Ou <[email protected]>

* Format rlang/build.envd

Signed-off-by: Botong Ou <[email protected]>

* remove incorrect user-switching

Signed-off-by: Botong Ou <[email protected]>

* Discard the changes in main

Signed-off-by: Botong Ou <[email protected]>

* add install.r_packages()

Signed-off-by: Botong Ou <[email protected]>

* update e2e test

Signed-off-by: Botong Ou <[email protected]>

* update e2e test

Signed-off-by: Botong Ou <[email protected]>

* update e2e test

Signed-off-by: Botong Ou <[email protected]>

* switched to small library for e2e test

Signed-off-by: Botong Ou <[email protected]>

* refactor R packcages installation

Signed-off-by: Botong Ou <[email protected]>

* refactor R packcages installation

Signed-off-by: Botong Ou <[email protected]>

* update R packages installation

Signed-off-by: Botong Ou <[email protected]>

* update R packages installation

Signed-off-by: Botong Ou <[email protected]>

* fix lint

Signed-off-by: Botong Ou <[email protected]>

* udpate R packages installation

Signed-off-by: Botong Ou <[email protected]>

* udpate R packages installation

Signed-off-by: Botong Ou <[email protected]>

Signed-off-by: Botong Ou <[email protected]>
  • Loading branch information
oubotong authored Jan 9, 2023
1 parent aab7f53 commit 3f7d699
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
4 changes: 2 additions & 2 deletions pkg/lang/frontend/starlark/v1/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ func ruleFuncRPackage(thread *starlark.Thread, _ *starlark.Builtin,
}

logger.Debugf("rule `%s` is invoked, name=%v", ruleRPackage, nameList)
ir.RPackage(nameList)
err = ir.RPackage(nameList)

return starlark.None, nil
return starlark.None, err
}

func ruleFuncJuliaPackage(thread *starlark.Thread, _ *starlark.Builtin,
Expand Down
2 changes: 1 addition & 1 deletion pkg/lang/ir/v1/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewGraph() ir.Graph {
NumGPUs: 0,

PyPIPackages: [][]string{},
RPackages: []string{},
RPackages: [][]string{},
JuliaPackages: []string{},
SystemPackages: []string{},
Exec: []ir.RunBuildCommand{},
Expand Down
11 changes: 9 additions & 2 deletions pkg/lang/ir/v1/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,17 @@ func PyPIPackage(deps []string, requirementsFile string, wheels []string) error
return nil
}

func RPackage(deps []string) {
func RPackage(deps []string) error {

if len(deps) == 0 {
return errors.New("Can not install empty R package")
}

g := DefaultGraph.(*generalGraph)

g.RPackages = append(g.RPackages, deps...)
g.RPackages = append(g.RPackages, deps)

return nil
}

func JuliaPackage(deps []string) {
Expand Down
28 changes: 15 additions & 13 deletions pkg/lang/ir/v1/r.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,28 @@ func (g generalGraph) installRLang(root llb.State) llb.State {
}

func (g generalGraph) installRPackages(root llb.State) llb.State {

if len(g.RPackages) == 0 {
return root
}
// TODO(terrytangyuan): Support different CRAN mirrors
var sb strings.Builder

mirrorURL := "https://cran.rstudio.com"
if g.CRANMirrorURL != nil {
mirrorURL = *g.CRANMirrorURL
}
sb.WriteString(fmt.Sprintf(`R -e 'options(repos = c(CRAN = "%s")); install.packages(c(`, mirrorURL))
for i, pkg := range g.RPackages {
sb.WriteString(fmt.Sprintf(`"%s"`, pkg))
if i != len(g.RPackages)-1 {
sb.WriteString(", ")
}

lib := "/usr/local/lib/R/site-library/"

root = root.
Run(llb.Shlexf("chmod 777 %s", lib), llb.WithCustomNamef("[internal] setting execute permision for default R package library for envd users")).Root()

for _, packages := range g.RPackages {
command := fmt.Sprintf(`R -e 'options(repos = "%s"); install.packages(c("%s"), lib = "%s")'`, mirrorURL, strings.Join(packages, `","`), lib)
run := root.
Run(llb.Shlex(command), llb.WithCustomNamef("[internal] installing R pacakges: %s", strings.Join(packages, " ")))
root = run.Root()

}
sb.WriteString(`))'`)

// TODO(terrytangyuan): Support cache.
cmd := sb.String()
run := root.Run(llb.Shlex(cmd), llb.WithCustomNamef("install R packages"))
return run.Root()
return root
}
2 changes: 1 addition & 1 deletion pkg/lang/ir/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type generalGraph struct {
PyPIPackages [][]string
RequirementsFile *string
PythonWheels []string
RPackages []string
RPackages [][]string
JuliaPackages []string
SystemPackages []string

Expand Down

0 comments on commit 3f7d699

Please sign in to comment.