From 07bd8863834cd361ccaffdb7aac5b038d8b08a74 Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Thu, 11 May 2023 12:03:18 +0000 Subject: [PATCH] Base mr snapshot wrappers --- distro/debian/snapshot/snapshot.go | 101 ++++++++++++++++++++++++ distro/debian/snapshot/snapshot_test.go | 33 ++++++++ 2 files changed, 134 insertions(+) create mode 100644 distro/debian/snapshot/snapshot.go create mode 100644 distro/debian/snapshot/snapshot_test.go diff --git a/distro/debian/snapshot/snapshot.go b/distro/debian/snapshot/snapshot.go new file mode 100644 index 0000000..2b7df7c --- /dev/null +++ b/distro/debian/snapshot/snapshot.go @@ -0,0 +1,101 @@ +package snapshot + +import ( + "errors" + "regexp" + + "code.dumpstack.io/tools/out-of-tree/distro/debian/snapshot/mr" +) + +func SourcePackageVersions(name string) (versions []string, err error) { + pkg, err := mr.GetPackage(name) + if err != nil { + return + } + + for _, res := range pkg.Result { + versions = append(versions, res.Version) + } + return +} + +type Package struct { + Name string + Source string + Version string + Arch string + Binary struct { + Name string + Hash string + } + Repo struct { + Snapshot string + Archive string + } +} + +func NewPackage(name, srcname, version, arch string) (p Package, err error) { + p.Name = name + p.Source = srcname + p.Version = version + p.Arch = arch + + p.Binary.Hash, err = p.getHash() + if err != nil { + return + } + + info, err := mr.GetInfo(p.Binary.Hash) + if err != nil { + return + } + + p.Binary.Name = info.Result[0].Name + + p.Repo.Archive = info.Result[0].ArchiveName + p.Repo.Snapshot = info.Result[0].FirstSeen + + return +} + +func (p Package) getHash() (hash string, err error) { + binfiles, err := mr.GetBinfiles(p.Name, p.Version) + if err != nil { + return + } + + for _, res := range binfiles.Result { + if res.Architecture == p.Arch { + hash = res.Hash + return + } + } + + err = errors.New("not found") + return +} + +func Packages(srcname, version, arch, regex string) (pkgs []Package, err error) { + binpkgs, err := mr.GetBinpackages(srcname, version) + if err != nil { + return + } + + r := regexp.MustCompile(regex) + + for _, res := range binpkgs.Result { + if !r.MatchString(res.Name) { + continue + } + + var pkg Package + pkg, err = NewPackage(res.Name, srcname, version, arch) + if err != nil { + return + } + + pkgs = append(pkgs, pkg) + } + + return +} diff --git a/distro/debian/snapshot/snapshot_test.go b/distro/debian/snapshot/snapshot_test.go new file mode 100644 index 0000000..5fd4a41 --- /dev/null +++ b/distro/debian/snapshot/snapshot_test.go @@ -0,0 +1,33 @@ +package snapshot + +import ( + "errors" + "testing" +) + +func TestSourcePackageVersions(t *testing.T) { + versions, err := SourcePackageVersions("linux") + if err != nil { + t.Fatal(err) + } + + if len(versions) == 0 { + t.Fatal(errors.New("empty response")) + } + + t.Logf("found %d package versions", len(versions)) +} + +func TestPackages(t *testing.T) { + packages, err := Packages("linux", "6.1.20-2", "amd64", + `^linux-(image|headers)-[0-9\.\-]*-(amd64|amd64-unsigned)$`) + if err != nil { + t.Fatal(err) + } + + if len(packages) == 0 { + t.Fatal(errors.New("empty response")) + } + + t.Log(packages) +}