Skip to content

Commit

Permalink
Filter pip freeze based on normalized form
Browse files Browse the repository at this point in the history
pip freeze output is actually pip's internal canonicalized form, which
can be different from the normalized package name format.

Example: `discord.py` gets normalized into `discord-py`,
but `pip show discord-py` still outputs `Name: discord.py`.
  • Loading branch information
blast-hardcheese committed Jan 2, 2024
1 parent 86410e4 commit 7efa451
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions internal/backends/python/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,15 +388,24 @@ func makePythonPipBackend(python string) api.LanguageBackend {
util.Die("failed to run freeze: %s", err.Error())
}

// As we walk through the output of pip freeze,
// compare the package metadata name to the normalized
// pkgs that we are trying to install, to see which we
// want to track in `requirements.txt`.
normalizedPkgs := make(map[api.PkgName]bool)
for name := range pkgs {
normalizedPkgs[normalizePackageName(name)] = true
}

var toAppend []string
for _, line := range strings.Split(string(outputB), "\n") {
for _, canonicalSpec := range strings.Split(string(outputB), "\n") {
var name api.PkgName
matches := matchPackageAndSpec.FindSubmatch(([]byte)(line))
matches := matchPackageAndSpec.FindSubmatch(([]byte)(canonicalSpec))
if len(matches) > 0 {
name = normalizePackageName(api.PkgName(string(matches[1])))
}
if _, exists := pkgs[name]; exists {
toAppend = append(toAppend, line)
if normalizedPkgs[name] {
toAppend = append(toAppend, canonicalSpec)
}
}

Expand Down

0 comments on commit 7efa451

Please sign in to comment.