summaryrefslogtreecommitdiff
path: root/parseDpkgOutputIntoPB.go
blob: ecddf4be087f2ac548284c55f4a0f2c42aa23399 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package debian

import (
	"bufio"
	"strings"

	"go.wit.com/lib/cobol"
	"go.wit.com/lib/config"
	"go.wit.com/lib/protobuf/zoopb"
	"go.wit.com/log"
	"google.golang.org/protobuf/types/known/timestamppb"
)

// PARSES "dpkg -I <p.Filename>"
// panic() on anything missing or unknown
func ParseDpkgOutputIntoPB(pb *zoopb.Package, all string) {
	if pb.DebInfo == nil {
		pb.DebInfo = new(zoopb.DebInfo)
	}
	starting := true
	scanner := bufio.NewScanner(strings.NewReader(all))
	for scanner.Scan() {
		line := scanner.Text()
		parts := strings.Fields(line)
		if len(parts) == 0 {
			continue
		}
		if starting {
			if parts[0] == "new" {
				if config.Verbose() {
					log.Printf("new: %v\n", parts)
				}
				// skip the first dpkg -I line
				continue
			}
			if parts[0] == "size" {
				if config.Verbose() {
					log.Printf("size: %v\n", parts)
				}
				// todo: make this correct
				pb.DebInfo.Size = parts[1]
				pb.DebInfo.InstalledSize = parts[1]
				// scan all the entries from size
				for scanner.Scan() {
					line = scanner.Text()
					parts = strings.Fields(line)
					if strings.HasPrefix(line, "  ") {
						if config.Verbose() {
							log.Printf("sizeline: %v\n", parts)
						}
						continue
					}
					starting = false
					break
				}
			}
			if starting {
				if config.Verbose() {
					log.Printf("probably real data: %v\n", parts)
				}
				if strings.HasSuffix(parts[0], ":") {
					// log.Printf("yep, real data: %s\n", parts[0])
					starting = false
				} else {
					continue
				}
			}
		}
		varname := strings.TrimRight(parts[0], ":")
		varval := strings.Join(parts[1:], " ")
		if config.Verbose() {
			log.Printf("varname:%s varval:%s\n", varname, varval)
		}
		switch varname {
		case "Package":
			pb.Package = varval
		case "Filename":
			// maybe should check this. sometimes it's passed in
			if pb.Filename == "" {
				pb.Filename = varval
			} else {
				log.Info("Filename was already set to", pb.Filename)
			}
		case "Version":
			pb.Version = varval
		case "Architecture":
			pb.Architecture = varval
		case "GoPath":
			pb.Namespace = varval
		case "Maintainer":
			pb.DebInfo.Maintainer = varval
		case "Packager":
			pb.DebInfo.Packager = varval
		case "Depends":
			pb.DebInfo.Depends = varval
		case "Source":
			pb.DebInfo.Source = varval
		case "URL":
			pb.DebInfo.URL = varval
		case "Build-Depends":
			pb.DebInfo.BuildDepends = varval
		case "Namespace":
			pb.Namespace = varval
		case "MD5Sum":
			pb.DebInfo.MD5SUM = varval
		case "SHA256":
			pb.DebInfo.SHA256 = varval
		case "Size":
			pb.DebInfo.Size = varval
		case "Installed-Size":
			pb.DebInfo.InstalledSize = varval
		case "Homepage":
			pb.DebInfo.URL = varval
		case "Conflicts":
			pb.DebInfo.Conflicts = varval
		case "Source-Date":
			t, err := cobol.GetTime(varval)
			if err == nil {
				pb.GitDate = timestamppb.New(t)
			} else {
				if config.Verbose() {
					log.Info("FIXME: Package-Build-Date", varval, err)
				}
			}
		case "Deb-File-Date":
			pb.DebInfo.DebCtime = varval
			t, _ := cobol.GetTime(varval)
			pb.Ctime = timestamppb.New(t)
		case "Build-Date":
			pb.DebInfo.BuildDate = varval
			t, err := cobol.GetTime(varval)
			if err == nil {
				pb.BuildDate = timestamppb.New(t)
			} else {
				if config.Verbose() {
					log.Info("FIXME: Package-Build-Date", varval, err)
				}
			}
		case "Package-Build-Date":
			t, err := cobol.GetTime(varval)
			if err == nil {
				pb.BuildDate = timestamppb.New(t)
			} else {
				if config.Verbose() {
					log.Info("FIXME: Package-Build-Date", varval, err)
				}
			}
		case "Git-Tag-Date":
			if config.Verbose() {
				log.Info("FIXME: Git-Tag-Date", varval)
			}
		case "Description":
			description := varval
			for scanner.Scan() {
				line := scanner.Text()
				if strings.HasPrefix(line, "  ") {
					line = strings.TrimSpace(line)
					description += line + "\n"
					continue
				}
				break
			}
			pb.DebInfo.Description = description
		default:
			// This forces me(it could be you!) to fix this parser
			varname2 := strings.TrimSuffix(varname, ":")
			log.Println("LINE:", line)
			log.Printf("UNKNOWN: varname:%s varval:%s varname2=%s\n", varname, varval, varname2)
			panic("fix go.wit.com/lib/debian/parseDpkgOutputIntoPB.go")
		}
	}
}