Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

[WIP] support slow OS commands. #1085

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions internal/gps/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"sync"
"sync/atomic"
"time"

"github.com/Masterminds/vcs"
"strconv"
)

// monitoredCmd wraps a cmd and will keep monitoring the process until it
Expand Down Expand Up @@ -196,6 +198,25 @@ func runFromRepoDir(ctx context.Context, repo vcs.Repo, timeout time.Duration, c
return c.combinedOutput(ctx)
}

func getIntEnv(envName string, defaultVal int) int {
val, found := os.LookupEnv(envName)

num, err := strconv.Atoi(val)
if found && (err == nil) {
return num
}

return defaultVal
}

func getExpensiveCmdTimeout() time.Duration {
return time.Duration(getIntEnv("DEP_EXP_CMD_TIMEOUT", 2)) * time.Minute
}

func getDefaultCmdTimeout() time.Duration {
return time.Duration(getIntEnv("DEP_CMD_TIMEOUT", 10)) * time.Second
}

const (
// expensiveCmdTimeout is meant to be used in a command that is expensive
// in terms of computation and we know it will take long or one that uses
Expand Down
4 changes: 3 additions & 1 deletion internal/gps/strip_vendor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package gps

import (
"fmt"
"os"
"path/filepath"
)
Expand All @@ -33,7 +34,8 @@ func stripVendor(path string, info os.FileInfo, err error) error {

if info.IsDir() {
if err := os.RemoveAll(path); err != nil {
return err
fmt.Printf("could not remove %s completly: %s, skipping\n", path, err)
// return err
}
return filepath.SkipDir
}
Expand Down
8 changes: 4 additions & 4 deletions internal/gps/vcs_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ func (r *gitRepo) defendAgainstSubmodules(ctx context.Context) error {

// Now, do a special extra-aggressive clean in case changing versions caused
// one or more submodules to go away.
out, err = runFromRepoDir(ctx, r, defaultCmdTimeout, "git", "clean", "-x", "-d", "-f", "-f")
out, err = runFromRepoDir(ctx, r, getDefaultCmdTimeout(), "git", "clean", "-x", "-d", "-f", "-f")
if err != nil {
return newVcsLocalErrorOr("unexpected error while defensively cleaning up after possible derelict submodule directories", err, string(out))
}

// Then, repeat just in case there are any nested submodules that went away.
out, err = runFromRepoDir(ctx, r, defaultCmdTimeout, "git", "submodule", "foreach", "--recursive", "git", "clean", "-x", "-d", "-f", "-f")
out, err = runFromRepoDir(ctx, r, getDefaultCmdTimeout(), "git", "submodule", "foreach", "--recursive", "git", "clean", "-x", "-d", "-f", "-f")
if err != nil {
return newVcsLocalErrorOr("unexpected error while defensively cleaning up after possible derelict nested submodule directories", err, string(out))
}
Expand Down Expand Up @@ -250,7 +250,7 @@ func (r *svnRepo) CommitInfo(id string) (*vcs.CommitInfo, error) {
Commit commit `xml:"entry>commit"`
}

out, err := runFromRepoDir(ctx, r, defaultCmdTimeout, "svn", "info", "-r", id, "--xml")
out, err := runFromRepoDir(ctx, r, getDefaultCmdTimeout(), "svn", "info", "-r", id, "--xml")
if err != nil {
return nil, newVcsLocalErrorOr("unable to retrieve commit information", err, string(out))
}
Expand All @@ -267,7 +267,7 @@ func (r *svnRepo) CommitInfo(id string) (*vcs.CommitInfo, error) {
}
}

out, err := runFromRepoDir(ctx, r, defaultCmdTimeout, "svn", "log", "-r", id, "--xml")
out, err := runFromRepoDir(ctx, r, getDefaultCmdTimeout(), "svn", "log", "-r", id, "--xml")
if err != nil {
return nil, newVcsRemoteErrorOr("unable to retrieve commit information", err, string(out))
}
Expand Down
14 changes: 7 additions & 7 deletions internal/gps/vcs_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (s *gitSource) exportRevisionTo(ctx context.Context, rev Revision, to strin
// could have an err here...but it's hard to imagine how?
defer fs.RenameWithFallback(bak, idx)

out, err := runFromRepoDir(ctx, r, defaultCmdTimeout, "git", "read-tree", rev.String())
out, err := runFromRepoDir(ctx, r, getDefaultCmdTimeout(), "git", "read-tree", rev.String())
if err != nil {
return fmt.Errorf("%s: %s", out, err)
}
Expand All @@ -157,7 +157,7 @@ func (s *gitSource) exportRevisionTo(ctx context.Context, rev Revision, to strin
// though we have a bunch of housekeeping to do to set up, then tear
// down, the sparse checkout controls, as well as restore the original
// index and HEAD.
out, err = runFromRepoDir(ctx, r, defaultCmdTimeout, "git", "checkout-index", "-a", "--prefix="+to)
out, err = runFromRepoDir(ctx, r, getDefaultCmdTimeout(), "git", "checkout-index", "-a", "--prefix="+to)
if err != nil {
return fmt.Errorf("%s: %s", out, err)
}
Expand Down Expand Up @@ -382,15 +382,15 @@ func (s *bzrSource) listVersions(ctx context.Context) ([]PairedVersion, error) {
}

// Now, list all the tags
out, err := runFromRepoDir(ctx, r, defaultCmdTimeout, "bzr", "tags", "--show-ids", "-v")
out, err := runFromRepoDir(ctx, r, getDefaultCmdTimeout(), "bzr", "tags", "--show-ids", "-v")
if err != nil {
return nil, fmt.Errorf("%s: %s", err, string(out))
}

all := bytes.Split(bytes.TrimSpace(out), []byte("\n"))

var branchrev []byte
branchrev, err = runFromRepoDir(ctx, r, defaultCmdTimeout, "bzr", "version-info", "--custom", "--template={revision_id}", "--revision=branch:.")
branchrev, err = runFromRepoDir(ctx, r, getDefaultCmdTimeout(), "bzr", "version-info", "--custom", "--template={revision_id}", "--revision=branch:.")
br := string(branchrev)
if err != nil {
return nil, fmt.Errorf("%s: %s", err, br)
Expand Down Expand Up @@ -462,7 +462,7 @@ func (s *hgSource) listVersions(ctx context.Context) ([]PairedVersion, error) {
}

// Now, list all the tags
out, err := runFromRepoDir(ctx, r, defaultCmdTimeout, "hg", "tags", "--debug", "--verbose")
out, err := runFromRepoDir(ctx, r, getDefaultCmdTimeout(), "hg", "tags", "--debug", "--verbose")
if err != nil {
return nil, fmt.Errorf("%s: %s", err, string(out))
}
Expand Down Expand Up @@ -496,7 +496,7 @@ func (s *hgSource) listVersions(ctx context.Context) ([]PairedVersion, error) {
// bookmarks next, because the presence of the magic @ bookmark has to
// determine how we handle the branches
var magicAt bool
out, err = runFromRepoDir(ctx, r, defaultCmdTimeout, "hg", "bookmarks", "--debug")
out, err = runFromRepoDir(ctx, r, getDefaultCmdTimeout(), "hg", "bookmarks", "--debug")
if err != nil {
// better nothing than partial and misleading
return nil, fmt.Errorf("%s: %s", err, string(out))
Expand Down Expand Up @@ -529,7 +529,7 @@ func (s *hgSource) listVersions(ctx context.Context) ([]PairedVersion, error) {
}
}

out, err = runFromRepoDir(ctx, r, defaultCmdTimeout, "hg", "branches", "-c", "--debug")
out, err = runFromRepoDir(ctx, r, getDefaultCmdTimeout(), "hg", "branches", "-c", "--debug")
if err != nil {
// better nothing than partial and misleading
return nil, fmt.Errorf("%s: %s", err, string(out))
Expand Down