brunel/packages/packages.go

477 lines
11 KiB
Go
Raw Normal View History

2024-07-28 20:59:50 +02:00
package packages
import (
"brunel/config"
"brunel/deb"
"brunel/domain"
"brunel/helpers"
"compress/bzip2"
"fmt"
"io"
"log/slog"
"net/http"
"slices"
"strings"
"time"
"pault.ag/go/debian/version"
2024-07-30 02:24:18 +02:00
"github.com/alphadose/haxmap"
2024-07-28 20:59:50 +02:00
"github.com/klauspost/compress/gzip"
"github.com/ulikunitz/xz"
)
var LastUpdateTime time.Time
2024-08-02 22:07:39 +02:00
var currentPackages = haxmap.New[string, domain.SourcePackage]()
2024-07-28 20:59:50 +02:00
func ProcessPackages() error {
2024-07-30 02:24:18 +02:00
var internalPackages = haxmap.New[string, domain.SourcePackage]()
var externalPackages = haxmap.New[string, domain.SourcePackage]()
2024-07-28 20:59:50 +02:00
err := LoadInternalPackages(internalPackages)
if err != nil {
return err
}
err = LoadExternalPackages(externalPackages)
if err != nil {
return err
}
2024-07-30 02:24:18 +02:00
combinePackages(internalPackages)
combinePackages(externalPackages)
2024-07-28 20:59:50 +02:00
ProcessMissingPackages(internalPackages, externalPackages)
2024-08-04 12:20:13 +02:00
ProcessStalePackages(internalPackages, externalPackages)
2024-07-28 20:59:50 +02:00
2024-07-30 09:20:32 +02:00
updatedPackages := haxmap.New[string, domain.SourcePackage]()
2024-07-30 02:24:18 +02:00
internalPackages.ForEach(func(k string, v domain.SourcePackage) bool {
2024-08-02 22:07:39 +02:00
curr, exists := currentPackages.Get(k)
2024-07-30 09:20:32 +02:00
if !exists {
updatedPackages.Set(k, v)
2024-07-30 09:08:00 +02:00
return true
}
2024-07-30 09:20:32 +02:00
mergedPackage := domain.SourcePackage{
2024-08-04 13:25:24 +02:00
Name: curr.Name,
Has32bit: curr.Has32bit,
Version: v.Version,
NewVersion: v.NewVersion,
Status: v.Status,
BuildAttempts: curr.BuildAttempts,
LastBuildStatus: curr.LastBuildStatus,
Packages: haxmap.New[string, domain.PackageInfo](),
2024-07-30 09:20:32 +02:00
}
updatedPackages.Set(k, mergedPackage)
2024-07-28 20:59:50 +02:00
return true
})
2024-08-02 22:07:39 +02:00
updatedPackages.ForEach(func(k string, v domain.SourcePackage) bool {
for _, pkg := range config.Configs.I386List {
2024-08-02 22:35:32 +02:00
if v.Name == pkg || v.Name == pkg+"-dmo" {
2024-08-02 22:07:39 +02:00
v.Has32bit = true
2024-08-02 22:35:32 +02:00
updatedPackages.Set(k, v)
2024-08-02 22:07:39 +02:00
return true
}
}
v.Has32bit = false
2024-08-02 22:35:32 +02:00
updatedPackages.Set(k, v)
2024-08-02 22:07:39 +02:00
return true
})
currentPackages = updatedPackages
2024-07-30 09:20:32 +02:00
2024-07-28 20:59:50 +02:00
LastUpdateTime = time.Now()
err = helpers.DBInst.DropPackages()
if err != nil {
return err
}
2024-07-28 20:59:50 +02:00
err = SaveToDb()
if err != nil {
return err
}
return nil
}
2024-07-30 02:24:18 +02:00
func GetPackages() *haxmap.Map[string, domain.SourcePackage] {
2024-08-02 22:07:39 +02:00
return currentPackages
2024-07-28 20:59:50 +02:00
}
func UpdatePackage(pkg domain.PackageInfo) error {
2024-08-02 22:07:39 +02:00
curr, ok := currentPackages.Get(pkg.Source)
2024-07-28 20:59:50 +02:00
if !ok {
return fmt.Errorf("package %s not found", pkg.Source)
}
curr.Packages.Set(pkg.PackageName, pkg)
2024-08-02 22:07:39 +02:00
currentPackages.Set(pkg.Source, curr)
2024-07-28 20:59:50 +02:00
return saveSingleToDb(curr)
}
2024-07-29 23:40:26 +02:00
func UpdateSourcePackage(pkg domain.SourcePackage) error {
2024-08-02 22:07:39 +02:00
currentPackages.Set(pkg.Name, pkg)
2024-07-29 23:40:26 +02:00
return saveSingleToDb(pkg)
}
2024-07-28 20:59:50 +02:00
func saveSingleToDb(pkg domain.SourcePackage) error {
err := helpers.DBInst.UpdatePackage(pkg)
if err != nil {
return err
}
LastUpdateTime = time.Now()
err = helpers.DBInst.UpdateLastUpdateTime(LastUpdateTime)
if err != nil {
return err
}
return nil
}
func SaveToDb() error {
2024-08-02 22:07:39 +02:00
err := helpers.DBInst.SavePackages(currentPackages)
2024-07-28 20:59:50 +02:00
if err != nil {
slog.Error(err.Error())
return err
}
LastUpdateTime = time.Now()
return helpers.DBInst.UpdateLastUpdateTime(LastUpdateTime)
}
func LoadFromDb() error {
packages, err := helpers.DBInst.GetPackages()
if err != nil {
slog.Error(err.Error())
return nil
}
slices.SortStableFunc(packages, func(a, b domain.SourcePackage) int {
if a.Name == b.Name {
return 0
}
if a.Name > b.Name {
return 1
}
return -1
})
2024-08-02 22:07:39 +02:00
currentPackages.Clear()
2024-07-28 20:59:50 +02:00
for _, pkg := range packages {
2024-08-02 22:07:39 +02:00
currentPackages.Set(pkg.Name, pkg)
2024-07-28 20:59:50 +02:00
}
return nil
}
2024-07-30 02:24:18 +02:00
func LoadInternalPackages(internalPackages *haxmap.Map[string, domain.SourcePackage]) error {
2024-07-28 20:59:50 +02:00
localPackageFile := config.Configs.LocalPackageFiles
slices.SortStableFunc(localPackageFile, func(a, b config.PackageFile) int {
if a.Priority == b.Priority {
return 0
}
if a.Priority < b.Priority {
return 1
}
return -1
})
for _, pkg := range config.Configs.LocalPackageFiles {
for _, repo := range pkg.Subrepos {
packages, err := fetchPackageFile(pkg, repo)
if err != nil {
return err
}
2024-07-30 02:24:18 +02:00
packages.ForEach(func(newKey string, newPkg domain.PackageInfo) bool {
2024-07-28 20:59:50 +02:00
pk, ok := internalPackages.Get(newPkg.Source)
if !ok {
2024-07-30 02:24:18 +02:00
newMap := haxmap.New[string, domain.PackageInfo]()
2024-07-28 20:59:50 +02:00
newMap.Set(newKey, newPkg)
internalPackages.Set(newPkg.Source, domain.SourcePackage{
Name: newPkg.Source,
Packages: newMap,
Status: domain.Current,
Version: newPkg.Version,
2024-07-28 20:59:50 +02:00
})
return true
}
pkg, ok := pk.Packages.Get(newKey)
if !ok {
pk.Packages.Set(newKey, newPkg)
return true
}
mVer, _ := version.Parse(pkg.Version)
2024-08-04 13:25:24 +02:00
extVer, _ := version.Parse(strings.Split(newPkg.Version, "+b")[0])
2024-07-28 20:59:50 +02:00
cmpVal := version.Compare(extVer, mVer)
if cmpVal >= 0 {
pk.Version = getHighestVer(pkg.Version, newPkg.Version)
2024-07-28 20:59:50 +02:00
pk.Packages.Set(newKey, newPkg)
internalPackages.Set(newPkg.Source, pk)
2024-07-28 20:59:50 +02:00
return true
}
return true
})
}
}
return nil
}
2024-07-30 02:24:18 +02:00
func LoadExternalPackages(externalPackages *haxmap.Map[string, domain.SourcePackage]) error {
2024-07-28 20:59:50 +02:00
externalPackageFile := config.Configs.ExternalPackageFiles
slices.SortStableFunc(externalPackageFile, func(a, b config.PackageFile) int {
if a.Priority == b.Priority {
return 0
}
if a.Priority < b.Priority {
return -1
}
return 1
})
for _, pkg := range config.Configs.ExternalPackageFiles {
for _, repo := range pkg.Subrepos {
packages, err := fetchPackageFile(pkg, repo)
if err != nil {
return err
}
2024-08-04 13:25:24 +02:00
packages.ForEach(func(newKey string, newPkg domain.PackageInfo) bool {
pk, ok := externalPackages.Get(newPkg.Source)
2024-07-28 20:59:50 +02:00
if !ok {
2024-07-30 02:24:18 +02:00
newMap := haxmap.New[string, domain.PackageInfo]()
2024-08-04 13:25:24 +02:00
newMap.Set(newKey, newPkg)
externalPackages.Set(newPkg.Source, domain.SourcePackage{
Name: newPkg.Source,
2024-07-28 20:59:50 +02:00
Packages: newMap,
2024-08-04 13:25:24 +02:00
Status: domain.Current,
Version: newPkg.Version,
2024-07-28 20:59:50 +02:00
})
return true
}
2024-08-04 13:25:24 +02:00
pkg, ok := pk.Packages.Get(newKey)
2024-07-28 20:59:50 +02:00
if !ok {
2024-08-04 13:25:24 +02:00
pk.Packages.Set(newKey, newPkg)
2024-07-28 20:59:50 +02:00
return true
}
mVer, _ := version.Parse(pkg.Version)
2024-08-04 13:25:24 +02:00
extVer, _ := version.Parse(strings.Split(newPkg.Version, "+b")[0])
2024-07-28 20:59:50 +02:00
cmpVal := version.Compare(extVer, mVer)
if cmpVal >= 0 {
2024-08-04 13:25:24 +02:00
pk.Version = getHighestVer(pkg.Version, newPkg.Version)
pk.Packages.Set(newKey, newPkg)
externalPackages.Set(newPkg.Source, pk)
2024-07-28 20:59:50 +02:00
return true
}
return true
})
}
}
return nil
}
2024-07-30 02:24:18 +02:00
func ProcessMissingPackages(internalPackages *haxmap.Map[string, domain.SourcePackage], externalPackages *haxmap.Map[string, domain.SourcePackage]) {
externalPackages.ForEach(func(k string, src domain.SourcePackage) bool {
2024-07-28 20:59:50 +02:00
_, ok := internalPackages.Get(k)
if !ok && src.Packages.Len() > 0 {
2024-08-04 13:25:24 +02:00
src.Status = domain.Missing
2024-07-28 20:59:50 +02:00
internalPackages.Set(k, src)
}
return true
})
}
2024-07-30 02:24:18 +02:00
func ProcessStalePackages(internalPackages *haxmap.Map[string, domain.SourcePackage], externalPackages *haxmap.Map[string, domain.SourcePackage]) {
externalPackages.ForEach(func(newPackage string, newSource domain.SourcePackage) bool {
2024-07-28 20:59:50 +02:00
matchedPackage, ok := internalPackages.Get(newPackage)
if !ok || matchedPackage.Packages.Len() == 0 {
return true
}
2024-08-04 13:25:24 +02:00
ver := newSource.Version
ver = getHighestVer(ver, matchedPackage.Version)
if ver != matchedPackage.Version {
matchedPackage.NewVersion = ver
2024-08-04 13:25:24 +02:00
matchedPackage.Status = domain.Stale
}
2024-08-04 13:25:24 +02:00
internalPackages.Set(newPackage, matchedPackage)
2024-07-28 20:59:50 +02:00
return true
})
}
2024-07-30 02:24:18 +02:00
func fetchPackageFile(pkg config.PackageFile, selectedRepo string) (*haxmap.Map[string, domain.PackageInfo], error) {
2024-07-28 20:59:50 +02:00
resp, err := http.Get(pkg.Url + selectedRepo + "/" + pkg.Packagepath + "." + pkg.Compression)
if err != nil {
return nil, err
}
defer resp.Body.Close()
rdr := io.Reader(resp.Body)
if pkg.Compression == "bz2" {
r := bzip2.NewReader(resp.Body)
rdr = r
}
if pkg.Compression == "xz" {
r, err := xz.NewReader(resp.Body)
if err != nil {
return nil, err
}
rdr = r
}
if pkg.Compression == "gz" {
r, err := gzip.NewReader(resp.Body)
if err != nil {
return nil, err
}
rdr = r
}
2024-07-30 02:24:18 +02:00
packages := haxmap.New[string, domain.PackageInfo]()
2024-07-28 20:59:50 +02:00
sreader := deb.NewControlFileReader(rdr, false, false)
for {
stanza, err := sreader.ReadStanza()
if err != nil || stanza == nil {
break
}
if stanza["Section"] == "debian-installer" {
continue
}
name := stanza["Package"]
useWhitelist := pkg.UseWhitelist && len(pkg.Whitelist) > 0
if useWhitelist {
contained := nameContains(name, pkg.Whitelist)
if !contained {
continue
}
}
broken := nameContains(name, pkg.Blacklist)
if broken {
continue
}
sourceSplit := strings.Split(stanza["Source"], " ")
source := sourceSplit[0]
if source == "" {
source = name
}
// Extract version from Source if available
sourceVersion := ""
if len(sourceSplit) > 1 {
sourceVersion = strings.Trim(sourceSplit[1], "()")
}
// Use sourceVersion if available, otherwise use stanza["Version"]
versionStr := stanza["Version"]
if sourceVersion != "" {
versionStr = sourceVersion
}
ver, err := version.Parse(versionStr)
2024-07-28 20:59:50 +02:00
if err != nil {
return nil, err
}
pk, ok := packages.Get(name)
if ok {
matchedVer, _ := version.Parse(pk.Version)
cmpVal := version.Compare(ver, matchedVer)
if cmpVal < 0 {
continue
}
}
packages.Set(name, domain.PackageInfo{
PackageName: name,
Version: ver.String(),
Source: source,
Architecture: stanza["Architecture"],
Description: stanza["Description"],
Status: domain.Current,
})
}
return packages, nil
}
func nameContains(name string, match []string) bool {
for _, m := range match {
if strings.Contains(name, m) {
return true
}
}
return false
}
func GetPackagesCount() domain.PackagesCount {
count := domain.PackagesCount{
Stale: 0,
Missing: 0,
Current: 0,
Error: 0,
Queued: 0,
Building: 0,
}
2024-08-04 13:02:55 +02:00
currentPackages.ForEach(func(k string, pkg domain.SourcePackage) bool {
switch pkg.Status {
case domain.Stale:
count.Stale++
case domain.Missing:
count.Missing++
case domain.Built:
count.Current++
case domain.Current:
count.Current++
case domain.Error:
count.Error++
}
2024-07-28 20:59:50 +02:00
return true
})
2024-07-29 23:49:20 +02:00
2024-07-28 20:59:50 +02:00
return count
}
2024-07-30 02:24:18 +02:00
func combinePackages(packages *haxmap.Map[string, domain.SourcePackage]) {
dmoPackages := haxmap.New[string, domain.SourcePackage]()
// First pass: identify and collect -dmo packages
packages.ForEach(func(k string, v domain.SourcePackage) bool {
if strings.HasSuffix(k, "-dmo") {
dmoPackages.Set(k, v)
packages.Del(k)
}
return true
})
// Second pass: combine -dmo packages with base packages or add them back
dmoPackages.ForEach(func(dmoName string, dmoPkg domain.SourcePackage) bool {
baseName := strings.TrimSuffix(dmoName, "-dmo")
basePkg, hasBase := packages.Get(baseName)
if hasBase {
// Combine packages, prioritizing -dmo
combinedPkg := dmoPkg // Start with the -dmo package
basePkg.Packages.ForEach(func(pkgName string, pkgInfo domain.PackageInfo) bool {
if _, exists := combinedPkg.Packages.Get(pkgName); !exists {
combinedPkg.Packages.Set(pkgName, pkgInfo)
}
return true
})
packages.Set(dmoName, combinedPkg) // Store under the -dmo name
packages.Del(baseName) // Remove the base package
} else {
// If there's no base package, just add the dmo package back
packages.Set(dmoName, dmoPkg)
}
return true
})
}
2024-08-04 13:25:24 +02:00
func getHighestVer(ver string, newVer string) string {
mVer, _ := version.Parse(ver)
extVer, _ := version.Parse(newVer)
cmpVal := version.Compare(mVer, extVer)
if cmpVal < 0 {
return newVer
}
return ver
}