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

General improvements #89

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")
use_repo(
go_deps,
"com_github_adrg_xdg",
"com_github_bazelbuild_buildtools",
"com_github_crillab_gophersat",
"com_github_onsi_gomega",
Expand Down
3 changes: 2 additions & 1 deletion cmd/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ func NewFetchCmd() *cobra.Command {
if err != nil {
return err
}
return repo.NewRemoteRepoFetcher(repos.Repositories, ".bazeldnf").Fetch()
return repo.NewRemoteRepoFetcher(repos.Repositories).Fetch()
},
}

fetchCmd.Flags().StringArrayVarP(&fetchopts.repofiles, "repofile", "r", []string{"repo.yaml"}, "repository information file. Can be specified multiple times")
repo.AddCacheHelperFlags(fetchCmd)
return fetchCmd
}
5 changes: 4 additions & 1 deletion cmd/reduce.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ which allow reducing huge rpm repos to a smaller problem set for debugging, remo
return err
}
}
repo := reducer.NewRepoReducer(repos, reduceopts.in, reduceopts.lang, reduceopts.baseSystem, reduceopts.arch, ".bazeldnf")
repo := reducer.NewRepoReducer(repos, reduceopts.in, reduceopts.lang, reduceopts.baseSystem, reduceopts.arch, repo.NewCacheHelper())
logrus.Info("Loading packages.")
if err := repo.Load(); err != nil {
return err
Expand Down Expand Up @@ -81,5 +81,8 @@ which allow reducing huge rpm repos to a smaller problem set for debugging, remo
reduceCmd.Flags().MarkDeprecated("fedora-base-system", "use --basesystem instead")
reduceCmd.Flags().MarkShorthandDeprecated("fedora-base-system", "use --basesystem instead")
reduceCmd.Flags().MarkShorthandDeprecated("nobest", "use --nobest instead")

repo.AddCacheHelperFlags(reduceCmd)

return reduceCmd
}
4 changes: 3 additions & 1 deletion cmd/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewResolveCmd() *cobra.Command {
return err
}
}
repo := reducer.NewRepoReducer(repos, resolveopts.in, resolveopts.lang, resolveopts.baseSystem, resolveopts.arch, ".bazeldnf")
repo := reducer.NewRepoReducer(repos, resolveopts.in, resolveopts.lang, resolveopts.baseSystem, resolveopts.arch, repo.NewCacheHelper())
logrus.Info("Loading packages.")
if err := repo.Load(); err != nil {
return err
Expand Down Expand Up @@ -84,5 +84,7 @@ func NewResolveCmd() *cobra.Command {
resolveCmd.Flags().MarkDeprecated("fedora-base-system", "use --basesystem instead")
resolveCmd.Flags().MarkShorthandDeprecated("fedora-base-system", "use --basesystem instead")
resolveCmd.Flags().MarkShorthandDeprecated("nobest", "use --nobest instead")
repo.AddCacheHelperFlags(resolveCmd)

return resolveCmd
}
10 changes: 10 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -16,6 +17,15 @@ var rootCmd = &cobra.Command{
}

func Execute() {
if logLevel, hasIt := os.LookupEnv("LOG_LEVEL") ; hasIt {
level, err := logrus.ParseLevel(logLevel)
if err != nil {
fmt.Println("Unable to parse log level from environment variable LOG_LEVEL")
os.Exit(1)
}
logrus.SetLevel(level)
}

rootCmd.AddCommand(NewXATTRCmd())
rootCmd.AddCommand(NewSandboxCmd())
rootCmd.AddCommand(NewFetchCmd())
Expand Down
4 changes: 3 additions & 1 deletion cmd/rpmtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewRpmTreeCmd() *cobra.Command {
if err != nil {
return err
}
repoReducer := reducer.NewRepoReducer(repos, nil, rpmtreeopts.lang, rpmtreeopts.baseSystem, rpmtreeopts.arch, ".bazeldnf")
repoReducer := reducer.NewRepoReducer(repos, nil, rpmtreeopts.lang, rpmtreeopts.baseSystem, rpmtreeopts.arch, repo.NewCacheHelper())
logrus.Info("Loading packages.")
if err := repoReducer.Load(); err != nil {
return err
Expand Down Expand Up @@ -144,5 +144,7 @@ func NewRpmTreeCmd() *cobra.Command {
rpmtreeCmd.Flags().MarkDeprecated("fedora-base-system", "use --basesystem instead")
rpmtreeCmd.Flags().MarkShorthandDeprecated("fedora-base-system", "use --basesystem instead")
rpmtreeCmd.Flags().MarkShorthandDeprecated("nobest", "use --nobest instead")
repo.AddCacheHelperFlags(rpmtreeCmd)

return rpmtreeCmd
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
)

require (
github.com/adrg/xdg v0.5.3
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/adrg/xdg v0.5.3 h1:xRnxJXne7+oWDatRhR1JLnvuccuIeCoBu2rtuLqQB78=
github.com/adrg/xdg v0.5.3/go.mod h1:nlTsY+NNiCBGCK2tpm09vRqfVzrc2fLmXGpBLF0zlTQ=
github.com/bazelbuild/buildtools v0.0.0-20240823132350-3488089d3661 h1:acJJwAuD2t36RHnvxf3oh9lhg5LISUYXunx+om2ONZw=
github.com/bazelbuild/buildtools v0.0.0-20240823132350-3488089d3661/go.mod h1:yBQGNvRAGhcBTxe4MHiW3Ul7DwoBim4XsKUaXnW1LWc=
github.com/bazelbuild/buildtools v0.0.0-20250110114635-13fa61383b99 h1:3Wvirn7fGxAk5XMO5OV9goElw2ZKya/erxxHChPSPQM=
Expand Down Expand Up @@ -43,6 +45,7 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo=
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
32 changes: 17 additions & 15 deletions pkg/reducer/reducer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type RepoReducer struct {
repoFiles []string
provides map[string][]*api.Package
implicitRequires []string
arch string
architectures []string
architecturesSet map[string]bool
repos *bazeldnf.Repositories
cacheHelper *repo.CacheHelper
}
Expand All @@ -37,19 +37,19 @@ func (r *RepoReducer) Load() error {
return err
}
for i, p := range repoFile.Packages {
if skip(p.Arch, r.architectures) {
if skip(p.Arch, r.architecturesSet) {
continue
}
r.packages = append(r.packages, repoFile.Packages[i])
}
}
repos, err := r.cacheHelper.CurrentPrimaries(r.repos, r.arch)
repos, err := r.cacheHelper.CurrentPrimaries(r.repos, r.architecturesSet)
if err != nil {
return err
}
for _, rpmrepo := range repos {
for i, p := range rpmrepo.Packages {
if skip(p.Arch, r.architectures) {
if skip(p.Arch, r.architecturesSet) {
continue
}
r.packages = append(r.packages, rpmrepo.Packages[i])
Expand Down Expand Up @@ -174,29 +174,31 @@ func (r *RepoReducer) requires(p *api.Package) (wants []*api.Package) {
return wants
}

func NewRepoReducer(repos *bazeldnf.Repositories, repoFiles []string, lang string, baseSystem string, arch string, cachDir string) *RepoReducer {
func NewRepoReducer(repos *bazeldnf.Repositories, repoFiles []string, lang string, baseSystem string, arch string, cacheHelper *repo.CacheHelper) *RepoReducer {

architecturesSet := map[string]bool{}
for _, arch := range []string{"noarch", arch} {
architecturesSet[arch] = true
}

return &RepoReducer{
packages: nil,
lang: lang,
implicitRequires: []string{baseSystem},
repoFiles: repoFiles,
provides: map[string][]*api.Package{},
architectures: []string{"noarch", arch},
arch: arch,
architecturesSet: architecturesSet,
repos: repos,
cacheHelper: &repo.CacheHelper{CacheDir: cachDir},
cacheHelper: cacheHelper,
}
}

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

// FixPackages contains hacks which should probably not have to exist
Expand Down
2 changes: 2 additions & 0 deletions pkg/repo/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ go_library(
"//pkg/api",
"//pkg/api/bazeldnf",
"//pkg/rpm",
"@com_github_adrg_xdg//:xdg",
"@com_github_sirupsen_logrus//:logrus",
"@com_github_spf13_cobra//:cobra",
"@io_k8s_sigs_yaml//:yaml",
],
)
Expand Down
57 changes: 52 additions & 5 deletions pkg/repo/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,63 @@ import (
"fmt"
"io"
"os"
"os/user"
"path/filepath"
"sort"
"strings"

"github.com/adrg/xdg"
"github.com/rmohr/bazeldnf/pkg/api"
"github.com/rmohr/bazeldnf/pkg/api/bazeldnf"
"github.com/rmohr/bazeldnf/pkg/rpm"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

type cacheHelperOpts struct {
cacheDir string
}

var cacheHelperValues = cacheHelperOpts{}

type CacheHelper struct {
CacheDir string
cacheDir string
}

func expand(path string) (string, error) {
if len(path) == 0 || path[0] != '~' {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the recommended approach here is to:
(1) replace ~ with $HOME
(2) os.Expand(path)

This will cover more than just tilde expansion because it'll substitute all of the local environment variables.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, I'll do that

return path, nil
}

usr, err := user.Current()
if err != nil {
return "", err
}
return filepath.Join(usr.HomeDir, path[1:]), nil
}

func NewCacheHelper(cacheDir ...string) *CacheHelper {
if len(cacheDir) == 0 {
cacheDir = append(cacheDir, cacheHelperValues.cacheDir)
} else if len(cacheDir) > 1 {
panic("too many cache directories")
}

logrus.Infof("Using cache directory %s", cacheDir[0])

dir, err := expand(cacheDir[0])

if err != nil {
panic(err)
}

return &CacheHelper{
cacheDir: dir,
}
}

func AddCacheHelperFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&cacheHelperValues.cacheDir, "cache-dir", "c", xdg.CacheHome+"/bazeldnf", "Cache directory")
}

func (r *CacheHelper) LoadMetaLink(repo *bazeldnf.Repository) (*api.Metalink, error) {
Expand All @@ -28,7 +74,7 @@ func (r *CacheHelper) LoadMetaLink(repo *bazeldnf.Repository) (*api.Metalink, er
}

func (r *CacheHelper) WriteToRepoDir(repo *bazeldnf.Repository, body io.Reader, name string) error {
dir := filepath.Join(r.CacheDir, repo.Name)
dir := filepath.Join(r.cacheDir, repo.Name)
file := filepath.Join(dir, name)

err := os.MkdirAll(dir, 0770)
Expand All @@ -48,7 +94,7 @@ func (r *CacheHelper) WriteToRepoDir(repo *bazeldnf.Repository, body io.Reader,
}

func (r *CacheHelper) OpenFromRepoDir(repo *bazeldnf.Repository, name string) (io.ReadCloser, error) {
dir := filepath.Join(r.CacheDir, repo.Name)
dir := filepath.Join(r.cacheDir, repo.Name)
file := filepath.Join(dir, name)
f, err := os.Open(file)
if err != nil {
Expand Down Expand Up @@ -210,9 +256,10 @@ func (r *CacheHelper) CurrentFilelistsForPackages(repo *bazeldnf.Repository, arc
return filelistpkgs, remaining, nil
}

func (r *CacheHelper) CurrentPrimaries(repos *bazeldnf.Repositories, arch string) (primaries []*api.Repository, err error) {
func (r *CacheHelper) CurrentPrimaries(repos *bazeldnf.Repositories, architecturesSet map[string]bool) (primaries []*api.Repository, err error) {
for i, repo := range repos.Repositories {
if repo.Arch != arch {
if _, ok := architecturesSet[repo.Arch]; !ok {
logrus.Infof("Ignoring primary for %s - %s", repo.Name, repo.Arch)
continue
}
primary, err := r.CurrentPrimary(&repos.Repositories[i])
Expand Down
4 changes: 2 additions & 2 deletions pkg/repo/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ func (r *RepoFetcherImpl) Fetch() (err error) {
return nil
}

func NewRemoteRepoFetcher(repos []bazeldnf.Repository, cacheDir string) RepoFetcher {
func NewRemoteRepoFetcher(repos []bazeldnf.Repository) RepoFetcher {
return &RepoFetcherImpl{
Repos: repos,
Getter: &getterImpl{},
CacheHelper: &CacheHelper{CacheDir: cacheDir},
CacheHelper: NewCacheHelper(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/repo/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func Test(t *testing.T) {
fmt.Println(err)
return
}
helper := CacheHelper{CacheDir: ".bazeldnf"}
helper := NewCacheHelper(".bazeldnf")
a, b, err := helper.CurrentFilelistsForPackages(&repos.Repositories[0], []string{"myarch"}, []*api.Package{
{Name: "blub", Arch: "myarch", Version: api.Version{Epoch: "1"}},
{Name: "blub", Arch: "myarch", Version: api.Version{Epoch: "3"}},
Expand Down
Loading