summaryrefslogtreecommitdiff
path: root/wit.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-10-07 09:01:45 -0500
committerJeff Carr <[email protected]>2025-10-07 09:01:45 -0500
commite9e0dd856c77ad3c2f54a53a3ca0eef23f69ede1 (patch)
treef1f5bc8001b64f5527c3f84b318e4a99c7678340 /wit.go
parentf46ce34f817930945fd5de53a8cdf4a10b62bded (diff)
new package
Diffstat (limited to 'wit.go')
-rw-r--r--wit.go135
1 files changed, 135 insertions, 0 deletions
diff --git a/wit.go b/wit.go
new file mode 100644
index 0000000..10d1f52
--- /dev/null
+++ b/wit.go
@@ -0,0 +1,135 @@
+package debian
+
+import (
+ "bufio"
+ "os"
+ "strings"
+
+ "go.wit.com/lib/fhelp"
+ "go.wit.com/lib/protobuf/zoopb"
+)
+
+// obviously a hack at this point
+// read the package list file from mirrors.wit.com
+func InitWitMirrors(m *zoopb.Machine) error {
+ m.Wit = zoopb.NewPackages()
+ err := scanPackageListFile(m, "/var/lib/apt/lists/mirrors.wit.com_wit_dists_sid_main_binary-amd64_Packages")
+ return err
+}
+
+// adds the package if the name isn't there
+// if it's newer, remove the old package and use the new one
+func AddIfNewer(all *zoopb.Packages, p *zoopb.Package) {
+ check := all.FindByName(p.Name)
+ if check == nil {
+ all.Append(p)
+ return
+ }
+ v1, _ := fhelp.NewDebVersion(check.Version)
+ v2, _ := fhelp.NewDebVersion(p.Version)
+ if v1.Equal(v2) {
+ // log.Info("do nothing", v1, v2)
+ return
+ }
+ if v1.LessThan(v2) {
+ // log.Info("removing", v1, "using", v2)
+ all.Delete(check)
+ all.Append(p)
+ } else {
+ // log.Info("keeping", v1, "got:", v2)
+ }
+}
+
+// breaks up the apt list file into sections
+// then sends each section to be processed
+// and added to zoopb.Machine.Wit
+func scanPackageListFile(m *zoopb.Machine, filename string) error {
+ file, err := os.Open(filename)
+ if err != nil {
+ return err
+ }
+ defer file.Close()
+ scanner := bufio.NewScanner(file)
+ var debInfo string
+ for scanner.Scan() {
+ line := scanner.Text()
+ if line == "" {
+ p := parsePackageInfo(debInfo)
+ AddIfNewer(m.Wit, p)
+ debInfo = ""
+ }
+ debInfo += line + "\n"
+ }
+ p := parsePackageInfo(debInfo)
+ m.Wit.Append(p)
+ return nil
+}
+
+/*
+func (m *Machine) addPackage(name string, version string, filename string) {
+ if name == "" {
+ return
+ }
+ // log.Info("addPackage:", name, version, filename)
+ var deb *DebPackage
+ var ok bool
+ deb, ok = allp[name]
+ if !ok {
+ deb = NewPackage()
+ deb.name.SetLabel(name)
+ allp[name] = deb
+ }
+ newversion := new(Version)
+ newversion.v = version
+ newversion.file = filename
+ deb.versions = append(deb.versions, newversion)
+}
+*/
+
+// parses dpkg -s foo.deb
+func parsePackageInfo(lines string) *zoopb.Package {
+ var name string
+ var version string
+ var filename string
+ var gopath string
+
+ for _, line := range strings.Split(lines, "\n") {
+ if line == "" {
+ continue
+ }
+ if strings.HasPrefix(line, " ") {
+ // these are usually Description: lines
+ continue
+ }
+ if strings.HasPrefix(line, "#") {
+ // skip comment lines. (probably doesn't happen in debian list files
+ continue
+ }
+ line = strings.TrimSpace(line)
+ parts := strings.Split(line, " ")
+ if len(parts) < 2 {
+ continue
+ }
+ if parts[0] == "Package:" {
+ name = parts[1]
+ }
+ if parts[0] == "Version:" {
+ version = parts[1]
+ }
+ if parts[0] == "Filename:" {
+ filename = strings.Join(parts[1:], " ")
+ }
+ if parts[0] == "GoPath:" {
+ gopath = parts[1]
+ }
+ }
+
+ p := zoopb.Package{
+ Name: name,
+ Version: version,
+ PkgName: filename,
+ SrcPath: gopath,
+ }
+
+ return &p
+}