Skip to content

Commit

Permalink
reducer: Factor our separate loader
Browse files Browse the repository at this point in the history
RepoLoader captures the repository package loading process separately
from the reducer and introduces an interface between them to make
testing the reducer easier.
  • Loading branch information
kellyma2 committed Jan 9, 2025
1 parent 710f6b0 commit 325d64d
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 88 deletions.
1 change: 1 addition & 0 deletions pkg/reducer/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go_library(
name = "reducer",
srcs = [
"doc.go",
"loader.go",
"reducer.go",
],
importpath = "github.com/rmohr/bazeldnf/pkg/reducer",
Expand Down
96 changes: 96 additions & 0 deletions pkg/reducer/loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package reducer

import (
"encoding/xml"
"os"

"github.com/rmohr/bazeldnf/pkg/api"
"github.com/rmohr/bazeldnf/pkg/api/bazeldnf"
)

type RepoCache interface {
CurrentPrimaries(repos *bazeldnf.Repositories, arch string) (primaries []*api.Repository, err error)
}

type ReducerPackageLoader interface {
Load() ([]api.Package, error)
}

type RepoLoader struct {
repoFiles []string
arch string
architectures []string
repos *bazeldnf.Repositories
cacheHelper RepoCache
}

func (r RepoLoader) Load() ([]api.Package, error) {
packages := []api.Package{}

for _, rpmrepo := range r.repoFiles {
repoFile := &api.Repository{}
f, err := os.Open(rpmrepo)
if err != nil {
return packages, err
}
defer f.Close()
err = xml.NewDecoder(f).Decode(repoFile)
if err != nil {
return packages, err
}
for i, p := range repoFile.Packages {
if skip(p.Arch, r.architectures) {
continue
}
packages = append(packages, repoFile.Packages[i])
}
}

cachedRepos, err := r.cacheHelper.CurrentPrimaries(r.repos, r.arch)
if err != nil {
return packages, err
}
for _, rpmrepo := range cachedRepos {
for i, p := range rpmrepo.Packages {
if skip(p.Arch, r.architectures) {
continue
}
packages = append(packages, rpmrepo.Packages[i])
}
}

for i, _ := range packages {
FixPackages(&packages[i])
}

return packages, nil
}

// FixPackages contains hacks which should probably not have to exist
func FixPackages(p *api.Package) {
// FIXME: This is not a proper modules support for python. We should properly resolve `alternative(python)` and
// not have to add such a hack. On the other hand this seems to have been reverted in fedora and only exists in centos stream.
if p.Name == "platform-python" {
p.Format.Provides.Entries = append(p.Format.Provides.Entries, api.Entry{
Name: "/usr/libexec/platform-python",
})
var requires []api.Entry
for _, entry := range p.Format.Requires.Entries {
if entry.Name != "/usr/libexec/platform-python" {
requires = append(requires, entry)
}
}
p.Format.Requires.Entries = requires
}
}

func skip(arch string, arches []string) bool {
skip := true
for _, a := range arches {
if a == arch {
skip = false
break
}
}
return skip
}
97 changes: 9 additions & 88 deletions pkg/reducer/reducer.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package reducer

import (
"encoding/xml"
"fmt"
"os"
"strings"

"github.com/rmohr/bazeldnf/pkg/api"
Expand All @@ -12,65 +10,15 @@ import (
"github.com/sirupsen/logrus"
)

type RepoCache interface {
CurrentPrimaries(repos *bazeldnf.Repositories, arch string) (primaries []*api.Repository, err error)
}

type RepoReducer struct {
packages []api.Package
repoFiles []string
provides map[string][]*api.Package
implicitRequires []string
arch string
architectures []string
repos *bazeldnf.Repositories
cacheHelper RepoCache
}

func loadRepos(repoFiles, architectures []string, arch string, repos *bazeldnf.Repositories, cacheHelper RepoCache) ([]api.Package, error) {
packages := []api.Package{}

for _, rpmrepo := range repoFiles {
repoFile := &api.Repository{}
f, err := os.Open(rpmrepo)
if err != nil {
return packages, err
}
defer f.Close()
err = xml.NewDecoder(f).Decode(repoFile)
if err != nil {
return packages, err
}
for i, p := range repoFile.Packages {
if skip(p.Arch, architectures) {
continue
}
packages = append(packages, repoFile.Packages[i])
}
}

cachedRepos, err := cacheHelper.CurrentPrimaries(repos, arch)
if err != nil {
return packages, err
}
for _, rpmrepo := range cachedRepos {
for i, p := range rpmrepo.Packages {
if skip(p.Arch, architectures) {
continue
}
packages = append(packages, rpmrepo.Packages[i])
}
}

for i, _ := range packages {
FixPackages(&packages[i])
}

return packages, nil
loader ReducerPackageLoader
}

func (r *RepoReducer) Load() error {
packages, err := loadRepos(r.repoFiles, r.architectures, r.arch, r.repos, r.cacheHelper)
packages, err := r.loader.Load()
if err != nil {
return err
}
Expand Down Expand Up @@ -196,41 +144,14 @@ func NewRepoReducer(repos *bazeldnf.Repositories, repoFiles []string, baseSystem
return &RepoReducer{
packages: nil,
implicitRequires: []string{baseSystem},
repoFiles: repoFiles,
provides: map[string][]*api.Package{},
architectures: []string{"noarch", arch},
arch: arch,
repos: repos,
cacheHelper: &repo.CacheHelper{CacheDir: cachDir},
}
}

func skip(arch string, arches []string) bool {
skip := true
for _, a := range arches {
if a == arch {
skip = false
break
}
}
return skip
}

// FixPackages contains hacks which should probably not have to exist
func FixPackages(p *api.Package) {
// FIXME: This is not a proper modules support for python. We should properly resolve `alternative(python)` and
// not have to add such a hack. On the other hand this seems to have been reverted in fedora and only exists in centos stream.
if p.Name == "platform-python" {
p.Format.Provides.Entries = append(p.Format.Provides.Entries, api.Entry{
Name: "/usr/libexec/platform-python",
})
var requires []api.Entry
for _, entry := range p.Format.Requires.Entries {
if entry.Name != "/usr/libexec/platform-python" {
requires = append(requires, entry)
}
}
p.Format.Requires.Entries = requires
loader: RepoLoader{
repoFiles: repoFiles,
architectures: []string{"noarch", arch},
arch: arch,
repos: repos,
cacheHelper: &repo.CacheHelper{CacheDir: cachDir},
},
}
}

Expand Down

0 comments on commit 325d64d

Please sign in to comment.