2024-11-22 11:15:14 -06:00
|
|
|
package zoopb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// sent via -ldflags
|
|
|
|
var VERSION string
|
|
|
|
var BUILDTIME string
|
|
|
|
|
2024-11-22 11:31:41 -06:00
|
|
|
func (m *Machine) IsInstalled(name string) bool {
|
2024-11-22 14:33:06 -06:00
|
|
|
loop := m.Packages.SortByName()
|
|
|
|
for loop.Scan() {
|
|
|
|
p := loop.Package()
|
|
|
|
if name == p.Name {
|
|
|
|
// log.Info("package installed:", p.Name, p.Version, p.PkgName)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// log.Info("package not-installed:", name)
|
2024-11-22 11:31:41 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-11-22 19:11:36 -06:00
|
|
|
func (m *Machine) FindInstalledByName(name string) *Package {
|
|
|
|
loop := m.Packages.SortByName()
|
|
|
|
for loop.Scan() {
|
|
|
|
p := loop.Package()
|
|
|
|
if name == p.Name {
|
|
|
|
// log.Info("package installed:", p.Name, p.Version, p.PkgName)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// log.Info("package not-installed:", name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// looks to see if any package matches a name and version
|
|
|
|
// if version == "", return the first name found
|
|
|
|
func (m *Machine) FindVersion(name string, version string) *Package {
|
|
|
|
// first check all installed versions
|
|
|
|
loop := m.Packages.SortByName()
|
|
|
|
for loop.Scan() {
|
|
|
|
p := loop.Package()
|
|
|
|
if name == p.Name {
|
|
|
|
if version == "" {
|
|
|
|
return p
|
|
|
|
} else {
|
|
|
|
if version == p.Version {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check all wit packages
|
|
|
|
loop = m.Wit.SortByName()
|
|
|
|
for loop.Scan() {
|
|
|
|
p := loop.Package()
|
|
|
|
if name == p.Name {
|
|
|
|
if version == "" {
|
|
|
|
return p
|
|
|
|
} else {
|
|
|
|
if version == p.Version {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// log.Info("package not-installed:", name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-22 11:15:14 -06:00
|
|
|
// read the package list file from mirrors.wit.com
|
2024-11-22 11:31:41 -06:00
|
|
|
func (m *Machine) InitWit() error {
|
|
|
|
if m.Wit == nil {
|
|
|
|
m.Wit = new(Packages)
|
|
|
|
}
|
2024-11-22 11:15:14 -06:00
|
|
|
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{
|
2024-11-22 14:33:06 -06:00
|
|
|
Name: name,
|
|
|
|
Version: version,
|
2024-11-22 11:15:14 -06:00
|
|
|
PkgName: filename,
|
2024-11-22 14:33:06 -06:00
|
|
|
SrcPath: gopath,
|
2024-11-22 11:15:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return &p
|
|
|
|
}
|