mirror of
https://github.com/EnumeratedDev/bpm-utils.git
synced 2026-09-16 11:06:11 +00:00
bpm-package: improve old package removal
This commit is contained in:
+101
-37
@@ -5,6 +5,7 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log"
|
||||
"maps"
|
||||
"os"
|
||||
@@ -143,22 +144,12 @@ func createArchive() string {
|
||||
|
||||
// Remove old package from source dir
|
||||
if repo := bpmutilsshared.GetRepository(); repo != "" {
|
||||
if database, err := bpmutilsshared.ReadDatabase(path.Join(repo, "source/database.bpmdb")); err == nil {
|
||||
if entry, ok := database.Entries[pkgInfo.Name]; ok {
|
||||
pkgFilepath := path.Join(repo, "source", entry.Filepath)
|
||||
err := os.Remove(pkgFilepath)
|
||||
removedFiles, err := RemoveSourcePackage(repo, pkgInfo.Name)
|
||||
for _, file := range removedFiles {
|
||||
fmt.Println("Removed " + file)
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("Warning: could not remove old source package (%s): %s", pkgFilepath, err)
|
||||
}
|
||||
|
||||
// Remove package signature
|
||||
if _, err := os.Stat(pkgFilepath + ".sig"); err == nil {
|
||||
err := os.Remove(pkgFilepath + ".sig")
|
||||
if err != nil {
|
||||
log.Printf("Warning: could not remove old source package signature (%s): %s", pkgFilepath+".str", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
log.Printf("Warning: could not remove old source packages: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -270,31 +261,22 @@ func compilePackage(archive string) {
|
||||
for _, line := range strings.Split(strings.TrimSpace(string(cmdOutput)), "\n") {
|
||||
// Read generated package info
|
||||
pkgInfo, err := bpmutilsshared.ReadPacakgeInfoFromTarball(line)
|
||||
|
||||
if repo := bpmutilsshared.GetRepository(); repo != "" {
|
||||
// Remove old package from binary dir
|
||||
if database, err := bpmutilsshared.ReadDatabase(path.Join(repo, "binary/database.bpmdb")); err == nil {
|
||||
if entry, ok := database.Entries[pkgInfo.Name]; ok {
|
||||
pkgFilepath := path.Join(repo, "binary", entry.Filepath)
|
||||
err := os.Remove(pkgFilepath)
|
||||
if err != nil {
|
||||
log.Printf("Warning: could not remove old binary package (%s): %s", pkgFilepath, err)
|
||||
}
|
||||
|
||||
// Remove package signature
|
||||
if _, err := os.Stat(pkgFilepath + ".sig"); err == nil {
|
||||
err := os.Remove(pkgFilepath + ".sig")
|
||||
if err != nil {
|
||||
log.Printf("Warning: could not remove old binary package signature (%s): %s", pkgFilepath+".str", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Move package to binary dir
|
||||
if err != nil {
|
||||
log.Fatalf("Error: could not read package info: %s", err)
|
||||
}
|
||||
|
||||
if repo := bpmutilsshared.GetRepository(); repo != "" {
|
||||
// Remove old package from binary dir
|
||||
|
||||
removedFiles, err := RemoveBinaryPackage(repo, pkgInfo.Name)
|
||||
for _, file := range removedFiles {
|
||||
fmt.Println("Removed " + file)
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("Warning: could not remove old binary packages: %s", err)
|
||||
}
|
||||
|
||||
// Move package to binary dir
|
||||
newPath := path.Join(repo, "binary", pkgInfo.Arch, path.Base(line))
|
||||
os.MkdirAll(path.Dir(newPath), 0755)
|
||||
os.Rename(line, newPath)
|
||||
@@ -364,3 +346,85 @@ func setupFlagsAndHelp(usage, desc string) {
|
||||
}
|
||||
flag.Parse()
|
||||
}
|
||||
|
||||
func RemoveSourcePackage(repo, pkgName string) (removedFiles []string, err error) {
|
||||
if _, err := os.Stat(path.Join(repo, "source")); err != nil {
|
||||
return removedFiles, nil
|
||||
}
|
||||
|
||||
err = filepath.Walk(path.Join(repo, "source"), func(packagePath string, info fs.FileInfo, err error) error {
|
||||
if !strings.HasSuffix(packagePath, ".bpm") {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get package info
|
||||
pkgInfo, err := bpmutilsshared.ReadPacakgeInfoFromTarball(packagePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if pkgInfo.Name != pkgName {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Remove package and its signature
|
||||
err = os.Remove(packagePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if relPath, err := filepath.Rel(repo, packagePath); err != nil {
|
||||
removedFiles = append(removedFiles, packagePath)
|
||||
} else {
|
||||
removedFiles = append(removedFiles, relPath)
|
||||
}
|
||||
err = os.Remove(packagePath + ".sig")
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return removedFiles, err
|
||||
}
|
||||
|
||||
func RemoveBinaryPackage(repo, pkgName string) (removedFiles []string, err error) {
|
||||
if _, err := os.Stat(path.Join(repo, "binary")); err != nil {
|
||||
return removedFiles, nil
|
||||
}
|
||||
|
||||
err = filepath.Walk(path.Join(repo, "binary"), func(packagePath string, info fs.FileInfo, err error) error {
|
||||
if !strings.HasSuffix(packagePath, ".bpm") {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get package info
|
||||
pkgInfo, err := bpmutilsshared.ReadPacakgeInfoFromTarball(packagePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if pkgInfo.Name != pkgName {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Remove package and its signature
|
||||
err = os.Remove(packagePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if relPath, err := filepath.Rel(repo, packagePath); err != nil {
|
||||
removedFiles = append(removedFiles, packagePath)
|
||||
} else {
|
||||
removedFiles = append(removedFiles, relPath)
|
||||
}
|
||||
err = os.Remove(packagePath + ".sig")
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return removedFiles, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user