Skip to content

Commit

Permalink
Remove poetry implementation (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
attiasas authored Nov 26, 2023
1 parent b6719ec commit 422f31a
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 144 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,14 +395,6 @@ bi pipenv [pipenv command] [command options]

Note: checksums calculation is not yet supported for pipenv projects.

#### poetry

```shell
bi poetry [poetry command] [command options]
```

Note: checksums calculation is not yet supported for poetry projects.

#### Dotnet

```shell
Expand Down
2 changes: 1 addition & 1 deletion build/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func newPythonModule(srcPath string, tool pythonutils.PythonTool, containingBuil
}

func (pm *PythonModule) RunInstallAndCollectDependencies(commandArgs []string) error {
dependenciesMap, err := pythonutils.GetPythonDependenciesFiles(pm.tool, commandArgs, pm.containingBuild.logger, pm.srcPath)
dependenciesMap, err := pythonutils.GetPythonDependenciesFiles(pm.tool, commandArgs, pm.containingBuild.buildName, pm.containingBuild.buildNumber, pm.containingBuild.logger, pm.srcPath)
if err != nil {
return err
}
Expand Down
34 changes: 0 additions & 34 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,40 +312,6 @@ func GetCommands(logger utils.Log) []*clitool.Command {
}
},
},
{
Name: "poetry",
Usage: "Generate build-info for a poetry project",
UsageText: "bi pipenv",
Flags: flags,
Action: func(context *clitool.Context) (err error) {
service := build.NewBuildInfoService()
service.SetLogger(logger)
bld, err := service.GetOrCreateBuild("poetry-build", "1")
if err != nil {
return
}
defer func() {
e := bld.Clean()
if err == nil {
err = e
}
}()
pythonModule, err := bld.AddPythonModule("", pythonutils.Poetry)
if err != nil {
return
}
filteredArgs := filterCliFlags(context.Args().Slice(), flags)
if filteredArgs[0] == "install" {
err = pythonModule.RunInstallAndCollectDependencies(filteredArgs[1:])
if err != nil {
return
}
return printBuild(bld, context.String(formatFlag))
} else {
return exec.Command("poetry", filteredArgs[1:]...).Run()
}
},
},
}
}

Expand Down
99 changes: 0 additions & 99 deletions utils/pythonutils/poetryutils.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package pythonutils

import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/BurntSushi/toml"
"github.com/jfrog/build-info-go/entities"
"github.com/jfrog/build-info-go/utils"
gofrogcmd "github.com/jfrog/gofrog/io"
"golang.org/x/exp/maps"
)

Expand Down Expand Up @@ -129,95 +122,3 @@ func extractPackagesFromPoetryLock(lockFilePath string) (dependencies map[string
}
return
}

// Get the project dependencies files (.whl or .tar.gz) by searching the Python package site.
// extractPoetryDependenciesFiles returns a dictionary where the key is the dependency name and the value is a dependency file struct.
func extractPoetryDependenciesFiles(srcPath string, cmdArgs []string, log utils.Log) (dependenciesFiles map[string]entities.Dependency, err error) {
// Run poetry install and extract the site-packages location
sitePackagesPath, err := getSitePackagesPath(cmdArgs, srcPath)
if err != nil {
return
}
// Extract packages names from poetry.lock
filePath, err := getPoetryLockFilePath(srcPath)
if err != nil || filePath == "" {
// Error was returned or poetry.lock does not exist in directory.
return nil, err
}
_, dependenciesVersions, err := extractPackagesFromPoetryLock(filePath)
if err != nil {
return nil, err
}
dependenciesFiles = map[string]entities.Dependency{}
for dependency, version := range dependenciesVersions {
directUrlPath := fmt.Sprintf("%s%s-%s.dist-info%sdirect_url.json", sitePackagesPath, dependency, version, string(os.PathSeparator))
directUrlFile, err := os.ReadFile(directUrlPath)
if err != nil {
log.Debug(fmt.Sprintf("Could not resolve download path for package: %s, error: %s \ncontinuing...", dependency, err))
continue
}
directUrl := packagedDirectUrl{}
err = json.Unmarshal(directUrlFile, &directUrl)
if err != nil {
log.Debug(fmt.Sprintf("Could not resolve download path for package: %s, error: %s \ncontinuing...", dependency, err))
continue
}
lastSeparatorIndex := strings.LastIndex(directUrl.Url, string(os.PathSeparator))
var fileName string
if lastSeparatorIndex == -1 {
fileName = directUrl.Url
} else {
fileName = directUrl.Url[lastSeparatorIndex+1:]
}
dependenciesFiles[strings.ToLower(dependency)] = entities.Dependency{Id: fileName}
log.Debug(fmt.Sprintf("Found package: %s installed with: %s", dependency, fileName))
}
return
}
func getSitePackagesPath(commandArgs []string, srcPath string) (sitePackagesPath string, err error) {
// First run poetry install with verbose logging
commandArgs = append(commandArgs, "-vv")
installCmd := utils.NewCommand("poetry", "install", commandArgs)
installCmd.Dir = srcPath
// Extract the virtuL env path
virtualEnvRegexp := regexp.MustCompile(`^Using\svirtualenv:\s(.*)$`)
virtualEnvNameParser := gofrogcmd.CmdOutputPattern{
RegExp: virtualEnvRegexp,
ExecFunc: func(pattern *gofrogcmd.CmdOutputPattern) (string, error) {
// Check for out of bound results.
if len(pattern.MatchedResults)-1 < 0 {
return "", nil
}
// If found, return the virtual env path
return pattern.MatchedResults[1], nil
},
}
virtualEnvPath, errorOut, _, err := gofrogcmd.RunCmdWithOutputParser(installCmd, true, &virtualEnvNameParser)
if err != nil {
return "", fmt.Errorf("failed running poetry command with error: '%s - %s'", err.Error(), errorOut)
}
if virtualEnvPath != "" {
// Take the first line matches the virtualEnvRegexp
sitePackagesPath = strings.Split(virtualEnvPath, "\n")[0]
// Extract from poetry env(i.e PROJECT-9SrbZw5z-py3.9) the env python version
pythonVersionIndex := strings.LastIndex(sitePackagesPath, "-py")
if pythonVersionIndex == -1 {
return "", fmt.Errorf("failed extracting python site package form the following virtual env %q", sitePackagesPath)
}
pythonVersion := sitePackagesPath[pythonVersionIndex+3:]
// add /lib/python3.10/site-packages
sitePackagesPath = filepath.Join(sitePackagesPath, "lib", "python"+pythonVersion, "site-packages") + string(os.PathSeparator)
} else {
// If no virtuL env is use, return the local python installation site-packages path
siteCmd := utils.NewCommand("python", "site", []string{"-m", "--user-site"})
sitePackagesPath, err = gofrogcmd.RunCmdOutput(siteCmd)
if err != nil {
return "", fmt.Errorf("failed running python -m site --user-site with error: '%s'", err.Error())
}
}
return
}

type packagedDirectUrl struct {
Url string
}
7 changes: 5 additions & 2 deletions utils/pythonutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,15 @@ type packageType struct {
InstalledVersion string `json:"installed_version,omitempty"`
}

func GetPythonDependenciesFiles(tool PythonTool, args []string, log utils.Log, srcPath string) (map[string]entities.Dependency, error) {
func GetPythonDependenciesFiles(tool PythonTool, args []string, buildName, buildNumber string, log utils.Log, srcPath string) (map[string]entities.Dependency, error) {
switch tool {
case Pip, Pipenv:
return InstallWithLogParsing(tool, args, log, srcPath)
case Poetry:
return extractPoetryDependenciesFiles(srcPath, args, log)
if buildName != "" && buildNumber != "" {
log.Warn("Poetry commands are not supporting collecting dependencies files")
}
return make(map[string]entities.Dependency), nil
default:
return nil, errors.New(string(tool) + " commands are not supported.")
}
Expand Down

0 comments on commit 422f31a

Please sign in to comment.