-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapt.go
143 lines (110 loc) · 2.99 KB
/
apt.go
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
package main
import (
"bytes"
"github.com/masif-upgrader/common"
"os"
"os/exec"
"regexp"
"strings"
)
type aptBadOutput struct {
output string
}
func (self *aptBadOutput) Error() string {
return "Got bad output from apt-get -sqq: " + self.output
}
type apt struct {
exe string
}
func newApt() (result *apt, err error) {
path, errGEP := getExePath("apt-get")
if errGEP != nil {
return nil, errGEP
}
if path != "" {
result = &apt{exe: path}
}
return
}
func (self *apt) getName() string {
return "APT"
}
func (self *apt) whatIfUpgradeAll(critOpRunner criticalOperationRunner) (tasks map[common.PkgMgrTask]struct{}, err error) {
critOpRunner.runCritical(func() {
tasks, err = self.whatIf("dist-upgrade")
})
return
}
func (self *apt) whatIfUpgrade(critOpRunner criticalOperationRunner, packageName string) (tasks map[common.PkgMgrTask]struct{}, err error) {
critOpRunner.runCritical(func() {
tasks, err = self.whatIf("dist-upgrade", packageName)
})
return
}
func (self *apt) upgrade(critOpRunner criticalOperationRunner, packageName string) (err error) {
cmd := exec.Command(self.exe, "-yqq", "dist-upgrade", packageName)
cmd.Env = []string{"LC_ALL=C", "DEBIAN_FRONTEND=noninteractive", "PATH=" + os.Getenv("PATH")}
cmd.Dir = "/"
cmd.Stdin = nil
cmd.Stdout = nil
cmd.Stderr = nil
critOpRunner.runCritical(func() {
err = cmd.Run()
})
return
}
var aptLineRgx = regexp.MustCompile(`\A([^ ]+) ([^ ]+)(?: \[([^]]+)])?(?: \(([^)]+)\))?`)
func (self *apt) whatIf(args ...string) (tasks map[common.PkgMgrTask]struct{}, err error) {
cmd := exec.Command(self.exe, append([]string{"-sqq"}, args...)...)
outBuf := bytes.Buffer{}
cmd.Env = []string{"LC_ALL=C"}
cmd.Dir = "/"
cmd.Stdin = nil
cmd.Stdout = &outBuf
cmd.Stderr = nil
if errRun := cmd.Run(); errRun != nil {
return nil, errRun
}
tasks = map[common.PkgMgrTask]struct{}{}
for _, line := range strings.Split(outBuf.String(), "\n") {
if line != "" {
match := aptLineRgx.FindStringSubmatch(line)
if match == nil {
return nil, &aptBadOutput{output: line}
}
nextTask := common.PkgMgrTask{
PackageName: match[2],
FromVersion: match[3],
ToVersion: match[4],
}
switch match[1] {
case "Inst":
if nextTask.ToVersion == "" {
return nil, &aptBadOutput{output: line}
}
if nextTask.FromVersion == "" {
nextTask.Action = common.PkgMgrInstall
} else {
nextTask.Action = common.PkgMgrUpdate
}
case "Conf":
if (nextTask.FromVersion == "") == (nextTask.ToVersion == "") {
return nil, &aptBadOutput{output: line}
}
nextTask.Action = common.PkgMgrConfigure
case "Remv":
if nextTask.FromVersion == "" || nextTask.ToVersion != "" {
return nil, &aptBadOutput{output: line}
}
nextTask.Action = common.PkgMgrRemove
case "Purg":
if nextTask.FromVersion == "" || nextTask.ToVersion != "" {
return nil, &aptBadOutput{output: line}
}
nextTask.Action = common.PkgMgrPurge
}
tasks[nextTask] = struct{}{}
}
}
return
}