Skip to content

Commit

Permalink
SCA: add python dependency generator
Browse files Browse the repository at this point in the history
Signed-off-by: Ariadne Conill <[email protected]>
  • Loading branch information
kaniini committed Oct 25, 2023
1 parent b5cea0e commit 9020b68
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions pkg/build/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,54 @@ func generatePkgConfigDeps(pc *PackageBuild, generated *config.Dependencies) err
return nil
}

// generatePythonDeps generates a python3~$VERSION dependency for packages which ship
// Python modules.
func generatePythonDeps(pc *PackageBuild, generated *config.Dependencies) error {
var pythonModuleVer string
pc.Logger.Printf("scanning for python modules...")

fsys := readlinkFS(pc.WorkspaceSubdir())
if err := fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

basename := filepath.Base(path)
if !strings.HasPrefix(basename, "python") {
return nil
}

// This probably shouldn't ever happen, but lets check to make sure.
if !d.IsDir() {
return nil
}

pythonModuleVer = basename[6:]
return nil
}); err != nil {
return err
}

// Nothing to do...
if pythonModuleVer == "" {
return nil
}

// Do not add a Python dependency if one already exists.
for _, dep := range pc.Dependencies.Runtime {
if strings.HasPrefix(dep, "python") {
pc.Logger.Warnf("%s: Python dependency %q already specified, consider removing it in favor of SCA-generated dependency", pc.PackageName, dep)
return nil
}
}

// We use the python3 name here instead of the python-3 name so that we can be
// compatible with Alpine and Adelie. Only Wolfi provides the python-3 name.
generated.Runtime = append(generated.Runtime, fmt.Sprintf("python3~%s", pythonModuleVer))

return nil
}

// removeSelfProvidedDeps removes dependencies which are provided by the package itself.
func removeSelfProvidedDeps(runtimeDeps, providedDeps []string) []string {
providedDepsMap := map[string]bool{}
Expand Down Expand Up @@ -681,6 +729,7 @@ func (pc *PackageBuild) GenerateDependencies() error {
generateSharedObjectNameDeps,
generateCmdProviders,
generatePkgConfigDeps,
generatePythonDeps,
}

for _, gen := range generators {
Expand Down

0 comments on commit 9020b68

Please sign in to comment.