Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overeager pip normalization #206

Merged
merged 3 commits into from
Jan 2, 2024
Merged
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
33 changes: 21 additions & 12 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 Expand Up @@ -434,20 +443,20 @@ func makePythonPipBackend(python string) api.LanguageBackend {
util.RunCmd([]string{"pip", "install", "-r", "requirements.txt"})
},
ListSpecfile: func() map[api.PkgName]api.PkgSpec {
flags, rawPkgs, err := ListRequirementsTxt("requirements.txt")
flags, pkgs, err := ListRequirementsTxt("requirements.txt")
if err != nil {
util.Die("%s", err.Error())
}

normalizedPkgs := make(map[api.PkgName]api.PkgSpec)
for name, spec := range rawPkgs {
normalizedPkgs[normalizePackageName(name)] = spec
}

// Stash the seen flags into a module global
// Stash the seen flags into a module global.
// This isn't great, but the expectation is that ListSpecfile
// is called before we run `Add`.
pipFlags = flags

return normalizedPkgs
// NB: We rely on requirements.txt being populated with the
// Python package _metadata_ name, not the PEP-503/PEP-508
// normalized version.
return pkgs
},
GuessRegexps: pythonGuessRegexps,
Guess: func(ctx context.Context) (map[api.PkgName]bool, bool) { return guess(ctx, python) },
Expand Down
Loading