Skip to content

Commit

Permalink
Merge pull request #3 from DSGT-DLP/mamba-poetry
Browse files Browse the repository at this point in the history
Mamba poetry
  • Loading branch information
dwu359 authored Sep 18, 2023
2 parents b2c9c8d + 5119f20 commit c31c5e4
Show file tree
Hide file tree
Showing 28 changed files with 268 additions and 105 deletions.
29 changes: 22 additions & 7 deletions cmd/backend/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,42 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
package add

import (
"strconv"

"github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/backend"
"github.com/DSGT-DLP/Deep-Learning-Playground/cli/pkg"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

// AddCmd represents the backend add command
var AddCmd = &cobra.Command{
Use: "add {package}",
Short: "Add package to pyproject.toml",
Long: `Add package to pyproject.toml from /training`,
Short: "Add package to conda environment, defaults to installation via poetry unless otherwise specified",
Long: `Add package to conda environment from /training, defaults to installation via poetry unless otherwise specified`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
bash_args := []string{"add", args[0]}
if cmd.Flag("dev").Value.String() == "true" {
bash_args = append(bash_args, "--group", "dev")
mamba, _ := strconv.ParseBool(cmd.Flag("mamba").Value.String())
channel := cmd.Flag("channel").Value.String()
env_name := cmd.Flag("env-name").Value.String()
dev, _ := strconv.ParseBool(cmd.Flag("dev").Value.String())
if mamba {
backend.ExecBashCmd("mamba", "run", "--live-stream", "-n", env_name, "mamba", "install", "-c", channel, args[0])
pterm.DefaultSection.Println("IMPORTANT")
pterm.Info.Println("Add the following line in dependencies section in environment.yml:\n" + " - " + channel + "::" + args[0])
pterm.Info.Println("Add the following line at the bottom of the channels section in environment.yml above defaults:\n" + " - " + channel)
pterm.Info.Println("Anaconda docs also recommend reinstalling the conda environment to reduce conflicts between conda-forge and PyPI dependencies, so after adding the above line, run:\ndlp-cli backend install --force")
} else if dev {
backend.ExecBashCmd("mamba", "run", "--live-stream", "-n", env_name, "poetry", "add", args[0], "--group", "dev")
} else {
backend.ExecBashCmd("mamba", "run", "--live-stream", "-n", env_name, "poetry", "add", args[0])
}
pkg.ExecBashCmd(backend.BackendDir, "poetry", bash_args...)
},
}

func init() {
backend.BackendCmd.AddCommand(AddCmd)
AddCmd.Flags().BoolP("dev", "d", false, "Add package as dev dependency")
AddCmd.Flags().BoolP("mamba", "m", false, "Add package via mamba (used mainly if cross-platform compatibility is needed)")
AddCmd.Flags().StringP("channel", "c", "conda-forge", "Specify conda channel to install from (only used if --mamba flag is set)")
AddCmd.MarkFlagsMutuallyExclusive("dev", "mamba")
}
5 changes: 5 additions & 0 deletions cmd/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ var BackendCmd = &cobra.Command{

func init() {
cmd.RootCmd.AddCommand(BackendCmd)
BackendCmd.PersistentFlags().String("env-name", "dlp", "Name of the conda environment you want to create")
}

func ExecBashCmd(name string, args ...string) string {
return cmd.ExecBashCmd(BackendDir, name, args...)
}
4 changes: 1 addition & 3 deletions cmd/backend/id_token/id_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>

import (
"github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/backend"
"github.com/DSGT-DLP/Deep-Learning-Playground/cli/pkg"
"github.com/spf13/cobra"
)

Expand All @@ -17,11 +16,10 @@ var IdTokenCmd = &cobra.Command{
Long: `gets a user's id token by email from the backend`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
pkg.ExecBashCmd(backend.BackendDir, "poetry", "run", "python", "cli.py", "get-id-token", args[0])
backend.ExecBashCmd("poetry", "run", "python", "cli.py", "get-id-token", args[0])
},
}

func init() {
backend.BackendCmd.AddCommand(IdTokenCmd)
//IdTokenCmd.Flags().StringP("email", "")
}
40 changes: 30 additions & 10 deletions cmd/backend/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,49 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
package install

import (
"fmt"
"strconv"
"strings"

"github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/backend"
"github.com/DSGT-DLP/Deep-Learning-Playground/cli/pkg"
"github.com/spf13/cobra"
)

// InstallCmd represents the backend install command
var InstallCmd = &cobra.Command{
Use: "install",
Short: "Installs training backend packages from pyproject.toml",
Long: `Installs training backend packages from pyproject.toml from /training in .venv`,
Short: "Installs training backend packages from pyproject.toml using poetry and environment.yml using conda in a conda environment for dlp, creates conda environment if it doesn't exist",
Long: "Installs training backend packages from pyproject.toml using poetry and environment.yml using conda in a conda environment for dlp, creates conda environment if it doesn't exist",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if cmd.Flag("force").Value.String() == "true" {
pkg.ExecBashCmd(backend.BackendDir, "poetry", "env", "remove", "--all")
force, _ := strconv.ParseBool(cmd.Flag("force").Value.String())
env_name := cmd.Flag("env-name").Value.String()
if cmd.Flag("reference").Value.String() == "true" {
fmt.Println("Check if " + env_name + " conda environment is created:")
fmt.Println("\tmamba info --envs")
fmt.Println("If not created, create conda environment:")
fmt.Println("\tmamba create --name " + env_name + " python=3.9")
fmt.Println("Activate conda env if not already activated:")
fmt.Println("\tmamba activate dlp")
fmt.Println("Install packages from environment.yml:")
fmt.Println("\tmamba env update --file environment.yml --prune")
fmt.Println("Install python packages from pyproject.toml with poetry:")
fmt.Println("\tpoetry install")
return
}
res := backend.ExecBashCmd("mamba", "info", "--envs")
if strings.Contains(res, env_name) && force {
backend.ExecBashCmd("mamba", "remove", "-n", env_name, "-y", "--all")
}
if !strings.Contains(res, env_name) || force {
backend.ExecBashCmd("mamba", "create", "--name", env_name, "-y")
}
pkg.ExecBashCmd(backend.BackendDir, "pyenv", "local", "3.9")
pkg.ExecBashCmd(backend.BackendDir, "poetry", "install")
pkg.ExecBashCmd(backend.BackendDir, "poetry", "run", "ggshield", "auth", "login")
pkg.ExecBashCmd(backend.BackendDir, "poetry", "run", "pre-commit", "install")
backend.ExecBashCmd("mamba", "run", "--live-stream", "-n", env_name, "mamba", "env", "update", "--file", "environment.yml", "--prune")
backend.ExecBashCmd("mamba", "run", "--live-stream", "-n", env_name, "poetry", "install")
},
}

func init() {
backend.BackendCmd.AddCommand(InstallCmd)
InstallCmd.Flags().BoolP("force", "f", false, "Force a reinstall of the backend")
InstallCmd.Flags().BoolP("force", "f", false, "Force a reinstall of backend packages")
}
29 changes: 21 additions & 8 deletions cmd/backend/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,40 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
package remove

import (
"strconv"

"github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/backend"
"github.com/DSGT-DLP/Deep-Learning-Playground/cli/pkg"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

// AddCmd represents the backend add command
// RemoveCmd represents the backend remove command
var RemoveCmd = &cobra.Command{
Use: "remove {package}",
Short: "Remove package from pyproject.toml",
Long: `Remove package from pyproject.toml from /training`,
Short: "Remove package from conda environment, defaults to removal via poetry unless otherwise specified",
Long: `Remove package from conda environment from /training, defaults to removal via poetry unless otherwise specified`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
bash_args := []string{"remove", args[0]}
if cmd.Flag("dev").Value.String() == "true" {
bash_args = append(bash_args, "--group", "dev")
mamba, _ := strconv.ParseBool(cmd.Flag("mamba").Value.String())
channel := cmd.Flag("channel").Value.String()
env_name := cmd.Flag("env-name").Value.String()
dev, _ := strconv.ParseBool(cmd.Flag("dev").Value.String())
if mamba {
backend.ExecBashCmd("mamba", "run", "--live-stream", "-n", env_name, "mamba", "remove", "-c", channel, args[0])
pterm.DefaultSection.Println("IMPORTANT")
pterm.Info.Println("Remove the following line in dependencies section in environment.yml:\n" + " - " + channel + "::" + args[0])
} else if dev {
backend.ExecBashCmd("mamba", "run", "--live-stream", "-n", env_name, "poetry", "remove", args[0], "--group", "dev")
} else {
backend.ExecBashCmd("mamba", "run", "--live-stream", "-n", env_name, "poetry", "remove", args[0])
}
pkg.ExecBashCmd(backend.BackendDir, "poetry", bash_args...)
},
}

func init() {
backend.BackendCmd.AddCommand(RemoveCmd)
RemoveCmd.Flags().BoolP("dev", "d", false, "Remove package from dev dependencies")
RemoveCmd.Flags().BoolP("mamba", "m", false, "Remove package via conda (used mainly if cross-platform compatibility is needed)")
RemoveCmd.Flags().StringP("channel", "c", "conda-forge", "Specify conda channel to install from (only used if --mamba flag is set)")
RemoveCmd.MarkFlagsMutuallyExclusive("dev", "mamba")
}
4 changes: 2 additions & 2 deletions cmd/backend/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"

"github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/backend"
"github.com/DSGT-DLP/Deep-Learning-Playground/cli/pkg"
"github.com/spf13/cobra"
)

Expand All @@ -18,7 +17,8 @@ var StartCmd = &cobra.Command{
Long: `Starts an instance of the training backend Django app in /training in the terminal`,
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
pkg.ExecBashCmd(backend.BackendDir, "poetry", "run", "python", "manage.py", "runserver", fmt.Sprintf("%v", cmd.Flag("port").Value))
env_name := cmd.Flag("env-name").Value.String()
backend.ExecBashCmd("mamba", "run", "--live-stream", "-n", env_name, "poetry", "run", "python", "manage.py", "runserver", fmt.Sprintf("%v", cmd.Flag("port").Value))
},
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/backend/uid/uid.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>

import (
"github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/backend"
"github.com/DSGT-DLP/Deep-Learning-Playground/cli/pkg"
"github.com/spf13/cobra"
)

Expand All @@ -17,7 +16,7 @@ var UidCmd = &cobra.Command{
Long: `gets a user's uid by email from the backend`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
pkg.ExecBashCmd(backend.BackendDir, "poetry", "run", "python", "cli.py", "get-uid", args[0])
backend.ExecBashCmd("poetry", "run", "python", "cli.py", "get-uid", args[0])
},
}

Expand Down
32 changes: 32 additions & 0 deletions cmd/backend/uninstall/uninstall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package install

import (
"fmt"

"github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/backend"
"github.com/spf13/cobra"
)

// InstallCmd represents the backend install command
var InstallCmd = &cobra.Command{
Use: "uninstall",
Short: "Uninstalls all training backend packages and removes conda environment for dlp",
Long: `Uninstalls all training backend packages and removes conda environment for dlp`,
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
env_name := cmd.Flag("env-name").Value.String()
if cmd.Flag("reference").Value.String() == "true" {
fmt.Println("Remove " + env_name + " conda environment:")
fmt.Println("\tmamba remove -n " + env_name + " --all")
return
}
backend.ExecBashCmd("mamba", "remove", "-n", env_name, "--all")
},
}

func init() {
backend.BackendCmd.AddCommand(InstallCmd)
}
3 changes: 1 addition & 2 deletions cmd/frontend/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package add

import (
"github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/frontend"
"github.com/DSGT-DLP/Deep-Learning-Playground/cli/pkg"
"github.com/spf13/cobra"
)

Expand All @@ -20,7 +19,7 @@ var AddCmd = &cobra.Command{
if cmd.Flag("dev").Value.String() == "true" {
bash_args = append(bash_args, "--dev")
}
pkg.ExecBashCmd(frontend.FrontendDir, "yarn", bash_args...)
frontend.ExecBashCmd("yarn", bash_args...)
},
}

Expand Down
4 changes: 4 additions & 0 deletions cmd/frontend/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ var FrontendCmd = &cobra.Command{
func init() {
cmd.RootCmd.AddCommand(FrontendCmd)
}

func ExecBashCmd(name string, args ...string) string {
return cmd.ExecBashCmd(FrontendDir, name, args...)
}
3 changes: 1 addition & 2 deletions cmd/frontend/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package install

import (
"github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/frontend"
"github.com/DSGT-DLP/Deep-Learning-Playground/cli/pkg"
"github.com/spf13/cobra"
)

Expand All @@ -20,7 +19,7 @@ var InstallCmd = &cobra.Command{
if cmd.Flag("force").Value.String() == "true" {
bash_args = append(bash_args, "--force")
}
pkg.ExecBashCmd(frontend.FrontendDir, "yarn", bash_args...)
frontend.ExecBashCmd("yarn", bash_args...)
},
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/frontend/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package remove

import (
"github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/frontend"
"github.com/DSGT-DLP/Deep-Learning-Playground/cli/pkg"
"github.com/spf13/cobra"
)

Expand All @@ -20,7 +19,7 @@ var RemoveCmd = &cobra.Command{
if cmd.Flag("dev").Value.String() == "true" {
bash_args = append(bash_args, "--dev")
}
pkg.ExecBashCmd(frontend.FrontendDir, "yarn", bash_args...)
frontend.ExecBashCmd("yarn", bash_args...)
},
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/frontend/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"

"github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/frontend"
"github.com/DSGT-DLP/Deep-Learning-Playground/cli/pkg"
"github.com/spf13/cobra"
)

Expand All @@ -18,7 +17,7 @@ var StartCmd = &cobra.Command{
Long: `Starts an instance of the nextjs frontend in the terminal`,
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
pkg.ExecBashCmd(frontend.FrontendDir, "yarn", "next", "dev", "-p", fmt.Sprintf("%v", cmd.Flag("port").Value))
frontend.ExecBashCmd("yarn", "next", "dev", "-p", fmt.Sprintf("%v", cmd.Flag("port").Value))
},
}

Expand Down
12 changes: 12 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package cmd
import (
"os"
"path/filepath"
"runtime"

"github.com/DSGT-DLP/Deep-Learning-Playground/cli/pkg"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -40,5 +42,15 @@ func Execute() {

func init() {
RootCmd.PersistentFlags().String("project-dir", ".", "The directory of the project relative to the cli directory (cli-config.yaml project-dir overrides default value)")
RootCmd.PersistentFlags().BoolP("no-unix", "w", false, "Execute bash commands as if on Windows (use if your os doesn't support Unix PTYs)")
RootCmd.PersistentFlags().BoolP("reference", "r", false, "Displays the equivalent shell commands for manual usage")
viper.BindPFlag("project-dir", RootCmd.PersistentFlags().Lookup("project-dir"))
}

func ExecBashCmd(dir string, name string, args ...string) string {
runtime_os := runtime.GOOS
if RootCmd.Flag("no-unix").Value.String() == "true" {
runtime_os = "windows"
}
return pkg.ExecBashCmd(runtime_os, dir, name, args...)
}
3 changes: 1 addition & 2 deletions cmd/serverless/core/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package add

import (
"github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/serverless/core"
"github.com/DSGT-DLP/Deep-Learning-Playground/cli/pkg"
"github.com/spf13/cobra"
)

Expand All @@ -20,7 +19,7 @@ var AddCmd = &cobra.Command{
if cmd.Flag("dev").Value.String() == "true" {
bash_args = append(bash_args, "--dev")
}
pkg.ExecBashCmd(core.CoreDir, "yarn", bash_args...)
core.ExecBashCmd("yarn", bash_args...)
},
}

Expand Down
5 changes: 5 additions & 0 deletions cmd/serverless/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
package core

import (
"github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd"
"github.com/DSGT-DLP/Deep-Learning-Playground/cli/cmd/serverless"
"github.com/spf13/cobra"
)
Expand All @@ -21,3 +22,7 @@ var CoreCmd = &cobra.Command{
func init() {
serverless.ServerlessCmd.AddCommand(CoreCmd)
}

func ExecBashCmd(name string, args ...string) string {
return cmd.ExecBashCmd(CoreDir, name, args...)
}
Loading

0 comments on commit c31c5e4

Please sign in to comment.