From 84d25808e5f6f9590fc8c9b595da3d8cc391fa3f Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 22 Nov 2024 11:15:14 -0600 Subject: [PATCH] parse packages from mirrors.wit.com --- package.proto | 3 +- wit.go | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 wit.go diff --git a/package.proto b/package.proto index 500db12..644b722 100644 --- a/package.proto +++ b/package.proto @@ -10,7 +10,8 @@ message Package { string version = 2; // version: 0.0.3 google.protobuf.Timestamp laststamp = 3; // the last time this package was seen (used to timeout entries) string srcPath = 4; // path to the sources (go.wit.com/apps/zookeeper) - bool installed = 5; // installed: true + bool installed = 5; // if installed on your machine, this should be set to true + string pkgName = 6; // the apt filename pool/main/f/foo/foo_2.2.2_riscv64.deb } message Packages { diff --git a/wit.go b/wit.go new file mode 100644 index 0000000..d5f4e7b --- /dev/null +++ b/wit.go @@ -0,0 +1,120 @@ +package zoopb + +import ( + "bufio" + "os" + "strings" +) + +// sent via -ldflags +var VERSION string +var BUILDTIME string + +// read the package list file from mirrors.wit.com +func (m *Machine) initWit() error { + err := m.scanPackageListFile("/var/lib/apt/lists/mirrors.wit.com_wit_dists_sid_main_binary-amd64_Packages") + return err + /* + for _, p := range allpackages { + var all []string + for _, ver := range p.versions { + all = append(all, ver.v) + } + // log.Info(name, strings.Join(all, ", ")) + } + */ +} + +// breaks up the apt list file into sections +// then sends each section to be processed +// and added to zoopb.Machine.Wit +func (m *Machine) scanPackageListFile(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) + m.Wit.Append(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) *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 = parts[1] + } + if parts[0] == "GoPath:" { + gopath = parts[1] + } + } + + p := Package{ + Name: name, + Version: version, + PkgName: filename, + SrcPath: gopath, + } + + return &p +}