Skip to content

Commit

Permalink
Fix pipenv Test
Browse files Browse the repository at this point in the history
  • Loading branch information
sarao1310 committed Jul 31, 2023
1 parent f94e718 commit cf98bba
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
3 changes: 1 addition & 2 deletions build/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package build

import (
"fmt"
"os"

"github.com/jfrog/build-info-go/entities"
"github.com/jfrog/build-info-go/utils/pythonutils"
"os"
)

type PythonModule struct {
Expand Down
54 changes: 49 additions & 5 deletions utils/pythonutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/jfrog/build-info-go/entities"
"github.com/jfrog/build-info-go/utils"
gofrogcmd "github.com/jfrog/gofrog/io"
"github.com/jfrog/gofrog/version"
)

const (
Expand Down Expand Up @@ -162,11 +163,11 @@ func InstallWithLogParsing(tool PythonTool, commandArgs []string, log utils.Log,

dependenciesMap := map[string]entities.Dependency{}

// Create regular expressions for log parsing.
collectingRegexp := regexp.MustCompile(`^Collecting\s(\w[\w-.]+)`)
downloadingRegexp := regexp.MustCompile(`^\s*Downloading\s([^\s]*)\s\(`)
usingCachedRegexp := regexp.MustCompile(`^\s*Using\scached\s([\S]+)\s\(`)
alreadySatisfiedRegexp := regexp.MustCompile(`^Requirement\salready\ssatisfied:\s(\w[\w-.]+)`)
// Create regular expressions for collecting information from log parsing.
collectingRegexp := regexp.MustCompile(`^Collecting\s*(\w[\w-.]+)`)
downloadingRegexp := regexp.MustCompile(`^\s*Downloading\s*([^\s]*)\s\(`)
usingCachedRegexp := regexp.MustCompile(`\s*Using\scached\s([\S]+)\s\(`)
alreadySatisfiedRegexp := regexp.MustCompile(`^Requirement\salready\ssatisfied:\s*(\w[\w-.]+)`)

var packageName string
expectingPackageFilePath := false
Expand Down Expand Up @@ -254,10 +255,53 @@ func InstallWithLogParsing(tool PythonTool, commandArgs []string, log utils.Log,
},
}

if tool == Pipenv {
ver, err := getPipEnvVersion(log)
if err != nil {
return nil, err
}
if ver.Compare("2023.7.23") == 1 || ver.Compare("2023.7.23") == 0 {
_, stderr, _, err := gofrogcmd.RunCmdWithOutputParser(installCmd, true)
if err != nil {
return nil, fmt.Errorf("failed running %s command with error: '%s - %s'", string(tool), err.Error(), stderr)
}
stderr = strings.ReplaceAll(stderr, "\n", "")
usingCached := usingCachedRegexp.FindAllStringSubmatch(stderr, -1)
for _, current := range usingCached {
filePath := current[1]
lastSlashIndex := strings.LastIndex(filePath, "/")
var fileName string
if lastSlashIndex == -1 {
fileName = filePath
} else {
fileName = filePath[lastSlashIndex+1:]
}
dependenciesMap[strings.ToLower(packageName)] = entities.Dependency{Id: fileName}
}
}
}

// Execute command.
_, errorOut, _, err := gofrogcmd.RunCmdWithOutputParser(installCmd, true, &dependencyNameParser, &downloadedFileParser, &cachedFileParser, &installedPackagesParser)
if err != nil {
return nil, fmt.Errorf("failed running %s command with error: '%s - %s'", string(tool), err.Error(), errorOut)
}
return dependenciesMap, nil
}

// todo: think if you need to pass the log.
func getPipEnvVersion(log utils.Log) (*version.Version, error) {
if log == nil {
log = &utils.NullLog{}

Check failure on line 295 in utils/pythonutils/utils.go

View workflow job for this annotation

GitHub Actions / Static-Check

ineffectual assignment to log (ineffassign)
}
versionData, err := gofrogcmd.RunCmdOutput(utils.NewCommand(string(Pipenv), "--version", []string{}))
if err != nil {
return nil, err
}
_, versionData, found := strings.Cut(versionData, "version ")
if !found {
return nil, errors.New("couldn't find pipenv version")
}
versionData = strings.Replace(versionData, "\n", "", -1)

Check failure on line 305 in utils/pythonutils/utils.go

View workflow job for this annotation

GitHub Actions / Static-Check

wrapperFunc: use strings.ReplaceAll method in `strings.Replace(versionData, "\n", "", -1)` (gocritic)
return version.NewVersion(versionData), nil
}

0 comments on commit cf98bba

Please sign in to comment.