-
Notifications
You must be signed in to change notification settings - Fork 76
/
estimate.go
202 lines (178 loc) · 4.99 KB
/
estimate.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"flag"
"fmt"
"go/build"
"log"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"golang.org/x/tools/go/vcs"
"golang.org/x/tools/refactor/importgraph"
)
func get(gopath, repo string) error {
done := make(chan struct{})
defer close(done)
go progressSize("go get", filepath.Join(gopath, "src"), done)
// As per https://groups.google.com/forum/#!topic/golang-nuts/N5apfenE4m4,
// the arguments to “go get” are packages, not repositories. Hence, we
// specify “gopkg/...” in order to cover all packages.
// As a concrete example, github.com/jacobsa/util is a repository we want
// to package into a single Debian package, and using “go get -d
// github.com/jacobsa/util” fails because there are no buildable go files
// in the top level of that repository.
cmd := exec.Command("go", "get", "-d", "-t", repo+"/...")
cmd.Stderr = os.Stderr
cmd.Env = append([]string{
"GO111MODULE=off",
"GOPATH=" + gopath,
}, passthroughEnv()...)
return cmd.Run()
}
func removeVendor(gopath string) (found bool, _ error) {
err := filepath.Walk(filepath.Join(gopath, "src"), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
return nil // skip non-directories
}
if info.Name() != "vendor" {
return nil
}
found = true
if err := os.RemoveAll(path); err != nil {
return fmt.Errorf("remove all: %w", err)
}
return filepath.SkipDir
})
return found, err
}
func estimate(importpath string) error {
// construct a separate GOPATH in a temporary directory
gopath, err := os.MkdirTemp("", "dh-make-golang")
if err != nil {
return fmt.Errorf("create temp dir: %w", err)
}
defer os.RemoveAll(gopath)
if err := get(gopath, importpath); err != nil {
return fmt.Errorf("go get: %w", err)
}
found, err := removeVendor(gopath)
if err != nil {
return fmt.Errorf("remove vendor: %w", err)
}
if found {
// Fetch un-vendored dependencies
if err := get(gopath, importpath); err != nil {
return fmt.Errorf("fetch un-vendored: go get: %w", err)
}
}
// Remove standard lib packages
cmd := exec.Command("go", "list", "std")
cmd.Stderr = os.Stderr
cmd.Env = append([]string{
"GO111MODULE=off",
"GOPATH=" + gopath,
}, passthroughEnv()...)
out, err := cmd.Output()
if err != nil {
return fmt.Errorf("go list std: args: %v; error: %w", cmd.Args, err)
}
stdlib := make(map[string]bool)
for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
stdlib[line] = true
}
stdlib["C"] = true // would fail resolving anyway
// Filter out all already-packaged ones:
golangBinaries, err := getGolangBinaries()
if err != nil {
return nil
}
build.Default.GOPATH = gopath
forward, _, errors := importgraph.Build(&build.Default)
if len(errors) > 0 {
lines := make([]string, 0, len(errors))
for importPath, err := range errors {
lines = append(lines, fmt.Sprintf("%s: %v", importPath, err))
}
return fmt.Errorf("could not load packages: %v", strings.Join(lines, "\n"))
}
var lines []string
seen := make(map[string]bool)
rrseen := make(map[string]bool)
node := func(importPath string, indent int) {
rr, err := vcs.RepoRootForImportPath(importPath, false)
if err != nil {
log.Printf("Could not determine repo path for import path %q: %v\n", importPath, err)
return
}
if rrseen[rr.Root] {
return
}
rrseen[rr.Root] = true
if _, ok := golangBinaries[rr.Root]; ok {
return // already packaged in Debian
}
lines = append(lines, fmt.Sprintf("%s%s", strings.Repeat(" ", indent), rr.Root))
}
var visit func(x string, indent int)
visit = func(x string, indent int) {
if seen[x] {
return
}
seen[x] = true
if !stdlib[x] {
node(x, indent)
}
for y := range forward[x] {
visit(y, indent+1)
}
}
keys := make([]string, 0, len(forward))
for key := range forward {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
if !strings.HasPrefix(key, importpath) {
continue
}
if seen[key] {
continue // already covered in a previous visit call
}
visit(key, 0)
}
if len(lines) == 0 {
log.Printf("%s is already fully packaged in Debian", importpath)
return nil
}
log.Printf("Bringing %s to Debian requires packaging the following Go packages:", importpath)
for _, line := range lines {
fmt.Println(line)
}
return nil
}
func execEstimate(args []string) {
fs := flag.NewFlagSet("estimate", flag.ExitOnError)
fs.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s estimate <go-package-importpath>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Estimates the work necessary to bring <go-package-importpath> into Debian\n"+
"by printing all currently unpacked repositories.\n")
fmt.Fprintf(os.Stderr, "Example: %s estimate github.com/Debian/dh-make-golang\n", os.Args[0])
}
err := fs.Parse(args)
if err != nil {
log.Fatalf("parse args: %s", err)
}
if fs.NArg() != 1 {
fs.Usage()
os.Exit(1)
}
// TODO: support the -git_revision flag
if err := estimate(fs.Arg(0)); err != nil {
log.Fatalf("estimate: %s", err)
}
}