ppp-sync/src/main.go

305 lines
6.7 KiB
Go
Raw Normal View History

package main
import (
"bufio"
2023-09-05 12:46:00 +02:00
"compress/gzip"
2023-09-05 01:02:14 +02:00
"fmt"
"io"
"log"
"net/http"
"os"
2023-09-05 02:26:23 +02:00
"os/exec"
"strings"
2023-09-05 01:02:14 +02:00
"sync"
"github.com/ulikunitz/xz"
)
func main() {
2023-09-05 02:26:23 +02:00
if os.Args[1] == "sign" {
signFiles(os.Args[2])
return
}
2023-09-05 01:02:14 +02:00
config := config{
Source: os.Args[1],
Target: os.Args[2],
Download: false,
}
if len(os.Args) > 3 {
config.Download = true
config.DlUrl = os.Args[3]
config.Output = os.Args[4]
}
2023-09-05 01:02:14 +02:00
basePackages := processFile(config.Source)
targetPackages := processFile(config.Target)
2023-09-05 01:02:14 +02:00
changed := compare(basePackages, targetPackages, config.Download)
if config.Download {
download(changed, config.DlUrl, config.Output)
}
}
2023-09-05 01:02:14 +02:00
func processFile(url string) map[string]packageInfo {
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
rdr := io.Reader(resp.Body)
if strings.HasSuffix(url, ".xz") {
r, err := xz.NewReader(resp.Body)
if err != nil {
log.Fatalf("xz error %s", err)
}
rdr = r
}
2023-09-05 12:46:00 +02:00
if strings.HasSuffix(url, ".gz") {
r, err := gzip.NewReader(resp.Body)
if err != nil {
log.Fatalf("gzip error %s", err)
}
rdr = r
}
2023-09-05 01:02:14 +02:00
packages := make(map[string]packageInfo)
var currentPackage string
scanner := bufio.NewScanner(rdr)
for scanner.Scan() {
line := scanner.Text()
if line == "" {
currentPackage = ""
}
if currentPackage == "" {
if strings.HasPrefix(line, "Package: ") {
2023-08-26 14:43:27 +02:00
pkName := strings.TrimPrefix(line, "Package: ") + " "
_, broken := brokenPackages[pkName]
if !broken {
currentPackage = pkName
2023-09-05 01:02:14 +02:00
packages[currentPackage] = packageInfo{
Name: pkName,
}
2023-08-26 14:43:27 +02:00
} else {
currentPackage = ""
}
}
} else {
2023-09-05 01:02:14 +02:00
if strings.HasPrefix(line, "Version: ") {
packages[currentPackage] = packageInfo{
Name: currentPackage,
Version: strings.TrimPrefix(line, "Version: "),
}
}
if strings.HasPrefix(line, "Filename: ") {
packages[currentPackage] = packageInfo{
Name: currentPackage,
Version: packages[currentPackage].Version,
FilePath: strings.TrimPrefix(line, "Filename: "),
}
}
}
}
return packages
}
2023-09-05 01:02:14 +02:00
func compare(basePackages map[string]packageInfo, targetPackages map[string]packageInfo, download bool) map[string]packageInfo {
output := make(map[string]packageInfo)
for pack, info := range targetPackages {
if baseVersion, ok := basePackages[pack]; ok {
2023-09-05 01:02:14 +02:00
if baseVersion.Version != info.Version {
output[pack] = info
if !download {
os.Stdout.WriteString(pack)
}
}
} else {
2023-09-05 01:02:14 +02:00
output[pack] = info
if !download {
os.Stdout.WriteString(pack)
}
}
}
2023-09-05 01:02:14 +02:00
return output
}
2023-09-05 02:26:23 +02:00
func signFiles(path string) {
dir, err := os.Open(path)
if err != nil {
panic(err)
}
defer dir.Close()
files, err := dir.Readdirnames(-1)
if err != nil {
panic(err)
}
signQueue := make(chan string, 10)
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case path, ok := <-signQueue:
if !ok {
return
}
ch := make(chan bool)
go func() {
sign(ch, path)
}()
<-ch
default:
// No more files to sign, exit the goroutine
return
}
2023-09-05 02:26:23 +02:00
}
}()
}
2023-09-05 13:24:52 +02:00
count := 0
totalCount := len(files)
2023-09-05 13:52:11 +02:00
filePath := ""
for _, file := range files {
2023-09-05 13:24:52 +02:00
totalCount--
2023-09-05 13:52:11 +02:00
if count < 10 && totalCount > 0 {
2023-09-05 13:24:52 +02:00
count++
2023-09-05 13:31:55 +02:00
filePath = filePath + " " + path + file
2023-09-05 13:24:52 +02:00
} else {
count = 0
2023-09-05 13:52:11 +02:00
filePath = filePath + " " + path + file
2023-09-05 13:24:52 +02:00
signQueue <- filePath
}
}
close(signQueue)
wg.Wait()
}
func sign(ch chan bool, path string) {
if strings.HasSuffix(path, ".deb") {
fmt.Printf("Signing %s \n", path)
2023-09-05 13:40:45 +02:00
cmd := exec.Command("dpkg-sig", "--sign", "builder", path)
err := cmd.Run()
if err != nil {
panic(err)
}
2023-09-05 02:26:23 +02:00
}
ch <- true
2023-09-05 02:26:23 +02:00
}
2023-09-05 01:02:14 +02:00
func download(packages map[string]packageInfo, url string, output string) {
// Create a buffered channel to store the packages to be downloaded
packageQueue := make(chan packageInfo, 10)
// Create a worker pool with 10 workers
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case pack, ok := <-packageQueue:
if !ok {
return
}
fmt.Printf("Downloading %s \n", pack.Name)
resp, err := http.Get(url + pack.FilePath)
if err != nil {
fmt.Printf("Failed to download %s: %v \n", pack.Name, err)
continue
}
defer resp.Body.Close()
rdr := io.Reader(resp.Body)
path := output + strings.Split(pack.FilePath, "/")[len(strings.Split(pack.FilePath, "/"))-1]
file, err := os.Create(path)
if err != nil {
fmt.Printf("Failed to create file %s: %v \n", path, err)
continue
}
defer file.Close()
_, err = io.Copy(file, rdr)
if err != nil {
fmt.Printf("Failed to save file %s: %v \n", path, err)
continue
}
default:
// No more packages to download, exit the goroutine
return
}
}
}()
}
// Add the packages to the queue
for _, pack := range packages {
packageQueue <- pack
}
// Close the queue to signal the workers to stop
close(packageQueue)
// Wait for all the workers to finish
wg.Wait()
}
type config struct {
Source string
Target string
Download bool
DlUrl string
Output string
}
type packageInfo struct {
Name string
Version string
FilePath string
}
2023-08-26 14:43:27 +02:00
var brokenPackages = map[string]bool{
"libkpim5mbox-data ": true,
"libkpim5identitymanagement-data ": true,
"libkpim5libkdepim-data ": true,
"libkpim5imap-data ": true,
"libkpim5ldap-data ": true,
"libkpim5mailimporter-data ": true,
2023-08-26 14:47:45 +02:00
"libkpim5mailtransport-data ": true,
2023-08-26 14:43:27 +02:00
"libkpim5akonadimime-data ": true,
"libkpim5kontactinterface-data ": true,
"libkpim5ksieve-data ": true,
"libkpim5textedit-data ": true,
"libk3b-data ": true,
"libkpim5eventviews-data ": true,
"libkpim5incidenceeditor-data ": true,
"libkpim5calendarsupport-data ": true,
"libkpim5calendarutils-data ": true,
"libkpim5grantleetheme-data ": true,
"libkpim5pkpass-data ": true,
"libkpim5gapi-data ": true,
"libkpim5akonadisearch-data ": true,
"libkpim5gravatar-data ": true,
"libkpim5akonadicontact-data ": true,
"libkpim5akonadinotes-data ": true,
"libkpim5libkleo-data ": true,
"plasma-mobile-tweaks ": true,
"libkpim5mime-data ": true,
"libkf5textaddons-data ": true,
"libkpim5smtp-data ": true,
"libkpim5tnef-data ": true,
"libkpim5akonadicalendar-data ": true,
"libkpim5akonadi-data ": true,
2023-08-30 17:27:15 +02:00
"libnvidia-common-390 ": true,
2023-08-30 17:33:34 +02:00
"libnvidia-common-530 ": true,
2023-08-26 14:43:27 +02:00
}