Skip to content

Commit

Permalink
Added separate implementations of certain parts of regolith that are …
Browse files Browse the repository at this point in the history
…Windows specific. (#125)

- Updated Go to 1.17
- Everything related to ACL is implemented only for Windows
- python filters try to run python with "python" and "python3" command
- Regolith uses `venv/Scripts/python` on Windows and `venv/bin/python` on other OSs
  • Loading branch information
Nusiq authored Jan 2, 2022
1 parent 09b5446 commit 033bf6a
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 55 deletions.
37 changes: 28 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,32 +1,51 @@
module bedrock-oss.github.com/regolith

go 1.16
go 1.17

require (
cloud.google.com/go/storage v1.18.2 // indirect
github.com/aws/aws-sdk-go v1.41.11 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
github.com/denisbrodbeck/machineid v1.0.1
github.com/fatih/color v1.13.0
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/go-github/v39 v39.2.0
github.com/hashicorp/go-getter v1.5.9
github.com/otiai10/copy v1.6.0
github.com/urfave/cli/v2 v2.3.0
go.uber.org/zap v1.19.1
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359
)

require (
cloud.google.com/go v0.97.0 // indirect
cloud.google.com/go/storage v1.18.2 // indirect
github.com/aws/aws-sdk-go v1.41.11 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/googleapis/gax-go/v2 v2.1.1 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-version v1.3.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/mattn/go-colorable v0.1.11 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/otiai10/copy v1.6.0
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/urfave/cli/v2 v2.3.0
go.opencensus.io v0.23.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
go.uber.org/zap v1.19.1
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/net v0.0.0-20211020060615-d418f374d309 // indirect
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359
golang.org/x/oauth2 v0.0.0-20211005180243-6b3c2da341f1 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/api v0.59.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20211026145609-4688e4c4e024 // indirect
google.golang.org/grpc v1.41.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
)
17 changes: 17 additions & 0 deletions regolith/compatibility_other_os.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:build !windows
// +build !windows

package regolith

// venvScriptsPath is a folder name between "venv" and "python" that leads to
// the python executable.
const venvScriptsPath = "bin"

// exeSuffix is a suffix for executable files.
const exeSuffix = ""

// copyFileSecurityInfo placeholder for a function which is necessary only
// on Windows.
func copyFileSecurityInfo(source string, target string) error {
return nil
}
45 changes: 45 additions & 0 deletions regolith/compatibility_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//go:build windows
// +build windows

package regolith

import (
"fmt"

"golang.org/x/sys/windows"
)

// venvScriptsPath is a folder name between "venv" and "python" that leads to
// the python executable.
const venvScriptsPath = "Scripts"

// exeSuffix is a suffix for executable files.
const exeSuffix = ".exe"

// copyFileSecurityInfo copies the DACL info from source path to DACL of
// the target path
func copyFileSecurityInfo(source string, target string) error {
securityInfo, err := windows.GetNamedSecurityInfo(
source,
windows.SE_FILE_OBJECT,
windows.DACL_SECURITY_INFORMATION)
if err != nil {
return wrapError(
fmt.Sprintf("Unable to get security info of %q.", source), err)
}
dacl, _, err := securityInfo.DACL()
if err != nil {
return wrapError(
fmt.Sprintf("Unable to get DACL of %q.", source), err)
}
err = windows.SetNamedSecurityInfo(
target,
windows.SE_FILE_OBJECT,
windows.DACL_SECURITY_INFORMATION, nil, nil, dacl, nil,
)
if err != nil {
return wrapError(
fmt.Sprintf("Unable to set DACL of %q.", target), err)
}
return nil
}
29 changes: 0 additions & 29 deletions regolith/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"path/filepath"

"github.com/otiai10/copy"
"golang.org/x/sys/windows"
)

// GetExportPaths returns file paths for exporting behavior pack and
Expand Down Expand Up @@ -125,34 +124,6 @@ func ExportProject(profile Profile, name string) error {
return err
}

// copyFileSecurityInfo copies the DACL info from source path to DACL of
// the target path
func copyFileSecurityInfo(source string, target string) error {
securityInfo, err := windows.GetNamedSecurityInfo(
source,
windows.SE_FILE_OBJECT,
windows.DACL_SECURITY_INFORMATION)
if err != nil {
return wrapError(
fmt.Sprintf("Unable to get security info of %q.", source), err)
}
dacl, _, err := securityInfo.DACL()
if err != nil {
return wrapError(
fmt.Sprintf("Unable to get DACL of %q.", source), err)
}
err = windows.SetNamedSecurityInfo(
target,
windows.SE_FILE_OBJECT,
windows.DACL_SECURITY_INFORMATION, nil, nil, dacl, nil,
)
if err != nil {
return wrapError(
fmt.Sprintf("Unable to set DACL of %q.", target), err)
}
return nil
}

// MoveOrCopy tries to move the the source to destination first and in case
// of failore it copies the files instead.
func MoveOrCopy(
Expand Down
49 changes: 32 additions & 17 deletions regolith/python_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
)
Expand All @@ -25,7 +24,9 @@ func RegisterPythonFilter(filters map[string]filterDefinition) {
func runPythonFilter(
filter Filter, settings map[string]interface{}, absoluteLocation string,
) error {
command := "python"
// command is a list of strings that can possibly run python (it's python3
// on some OSs)
command := []string{"python", "python3"}
scriptPath := filepath.Join(absoluteLocation, filter.Script)

if needsVenv(filepath.Dir(scriptPath)) {
Expand All @@ -34,11 +35,8 @@ func runPythonFilter(
return wrapError("Failed to resolve venv path", err)
}
Logger.Debug("Running Python filter using venv: ", venvPath)
suffix := ""
if runtime.GOOS == "windows" {
suffix = ".exe"
}
command = filepath.Join(venvPath, "Scripts/python"+suffix)
command = []string{
filepath.Join(venvPath, venvScriptsPath, "python"+exeSuffix)}
}
var args []string
if len(settings) == 0 {
Expand All @@ -49,8 +47,14 @@ func runPythonFilter(
[]string{"-u", scriptPath, string(jsonSettings)},
filter.Arguments...)
}
err := RunSubProcess(
command, args, absoluteLocation, GetAbsoluteWorkingDirectory())
var err error
for _, c := range command {
err = RunSubProcess(
c, args, absoluteLocation, GetAbsoluteWorkingDirectory())
if err == nil {
return nil
}
}
if err != nil {
return wrapError("Failed to run Python script", err)
}
Expand All @@ -64,17 +68,20 @@ func installPythonFilter(filter Filter, filterPath string) error {
return wrapError("Failed to resolve venv path", err)
}
Logger.Info("Creating venv...")
err = RunSubProcess("python", []string{"-m", "venv", venvPath}, filterPath, "")
// it's sometimes python3 on some OSs
for _, c := range []string{"python", "python3"} {
err = RunSubProcess(
c, []string{"-m", "venv", venvPath}, filterPath, "")
if err == nil {
break
}
}
if err != nil {
return err
}
suffix := ""
if runtime.GOOS == "windows" {
suffix = ".exe"
}
Logger.Info("Installing pip dependencies...")
err = RunSubProcess(
filepath.Join(venvPath, "Scripts/pip"+suffix),
filepath.Join(venvPath, venvScriptsPath, "pip"+exeSuffix),
[]string{"install", "-r", "requirements.txt"}, filterPath, filterPath)
if err != nil {
return err
Expand All @@ -101,11 +108,19 @@ func resolveVenvPath(filter Filter) (string, error) {
}

func checkPythonRequirements() error {
_, err := exec.LookPath("python")
python := ""
var err error
for _, c := range []string{"python", "python3"} {
_, err = exec.LookPath(c)
if err == nil {
python = c
break
}
}
if err != nil {
return errors.New("Python not found. Download and install it from https://www.python.org/downloads/")
}
cmd, err := exec.Command("python", "--version").Output()
cmd, err := exec.Command(python, "--version").Output()
if err != nil {
return wrapError("Python version check failed", err)
}
Expand Down
3 changes: 3 additions & 0 deletions test/export_test.go → test/export_windows_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build windows
// +build windows

package test

import (
Expand Down

0 comments on commit 033bf6a

Please sign in to comment.