Skip to content

Commit

Permalink
Merge pull request #296 from metrumresearchgroup/windows-fixups
Browse files Browse the repository at this point in the history
Windows fixes
  • Loading branch information
kyleam authored Feb 3, 2023
2 parents 92cd1ab + 60dc21b commit f08dfc3
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 3 deletions.
45 changes: 44 additions & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"regexp"
"runtime"
"strings"

"github.com/metrumresearchgroup/bbi/configlib"
Expand All @@ -28,6 +29,13 @@ func initializer(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("get dir string: %w", err)
}

var find_nm func(string) (string, error)
if runtime.GOOS == "windows" {
find_nm = findNonMemBinaryWindows
} else {
find_nm = findNonMemBinary
}

for _, l := range dir {
var files []os.FileInfo
// For each directory underneath the dir provided. Let's see if it's nonmemmy
Expand All @@ -51,7 +59,7 @@ func initializer(cmd *cobra.Command, _ []string) error {

for _, v := range locations {
var nm string
nm, err = findNonMemBinary(v)
nm, err = find_nm(v)
if err != nil {
log.Println(err)

Expand Down Expand Up @@ -133,6 +141,12 @@ func isPathNonMemmy(path string) bool {
return false
}

// That rest of this function (checking for an executable bit in
// file mode bits) isn't relevant for Windows.
if runtime.GOOS == "windows" {
return true
}

// Are any of them executable?
fails := 0

Expand Down Expand Up @@ -188,6 +202,35 @@ func findNonMemBinary(path string) (string, error) {
return "", errors.New("No nonmem binary could be located in the given path. Please check again or try another directory")
}

// findNonMemBinaryWindows is a Windows-specific variant of
// findNonMemBinary. The key differences are that 1) it expects
// ".bat" at the end of the binary and 2) it doesn't expect an
// executable bit to be set.
func findNonMemBinaryWindows(path string) (string, error) {
rdir := filepath.Join(path, "run")
fd, err := os.Open(rdir)
if err != nil {
return "", err
}
defer fd.Close()

re := regexp.MustCompile(`^nmfe[0-9]{2}\.bat$`)
files, err := fd.Readdirnames(-1)
if err != nil {
return "", err
}

for _, fname := range files {
// Follow findNonMemBinary() and take the first match (rather
// than, e.g., returning an error if multiple files match).
if re.MatchString(fname) {
return fname, nil
}
}

return "", fmt.Errorf("nmfe .bat file not found in %s", rdir)
}

func hasNMQual(path string) bool {
fs := afero.NewOsFs()

Expand Down
35 changes: 35 additions & 0 deletions cmd/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cmd

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

"github.com/metrumresearchgroup/wrapt"
)

func TestFindNonMemBinaryWindows(tt *testing.T) {
t := wrapt.WrapT(tt)
dir := t.TempDir()
rdir := filepath.Join(dir, "run")
if err := os.Mkdir(rdir, 0777); err != nil {
t.Fatal(err)
}

foo := filepath.Join(rdir, "foo.bat")
if err := os.WriteFile(foo, []byte(""), 0666); err != nil {
t.Fatal(err)
}

_, err := findNonMemBinaryWindows(dir)
t.R.Contains(err.Error(), ".bat file not found")

nmfe := filepath.Join(rdir, "nmfe23.bat")
if err = os.WriteFile(nmfe, []byte(""), 0666); err != nil {
t.Fatal(err)
}

binary, err := findNonMemBinaryWindows(dir)
t.R.NoError(err)
t.R.Equal(binary, "nmfe23.bat")
}
11 changes: 10 additions & 1 deletion cmd/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,16 @@ func executeLocalJob(model *NonMemModel) turnstile.ConcurrentError {

log.Debugf("Script location is pegged at %s", scriptLocation)

command := exec.Command(scriptLocation)
bash, err := exec.LookPath("bash")
if err != nil {
return turnstile.ConcurrentError{
Error: err,
RunIdentifier: model.FileName,
Notes: "bash is required to run bbi. Please install bash and then try running this again.",
}
}

command := exec.Command(bash, scriptLocation)
command.Dir = model.OutputDir
command.Env = os.Environ() // Take in OS Environment

Expand Down
7 changes: 6 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,13 @@ func ExecutePostWorkDirectivesWithEnvironment(worker PostWorkExecutor) (string,
return "", err
}

bash, err := exec.LookPath("bash")
if err != nil {
return "", err
}

// Needs to be the processed value, not the config template.
cmd := exec.Command(filepath.Join(worker.GetWorkingPath(), "post_processing.sh"))
cmd := exec.Command(bash, filepath.Join(worker.GetWorkingPath(), "post_processing.sh"))

// Set the environment for the binary.
cmd.Env = environment
Expand Down

0 comments on commit f08dfc3

Please sign in to comment.