Skip to content

Commit ee3cc18

Browse files
committed
also remove vendor sub-directories (and add to d/copyright)
fixes #87
1 parent e6dc37f commit ee3cc18

File tree

1 file changed

+57
-22
lines changed

1 file changed

+57
-22
lines changed

make.go

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,43 @@ func passthroughEnv() []string {
3939
return result
4040
}
4141

42+
func findVendorDirs(dir string) ([]string, error) {
43+
var vendorDirs []string
44+
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
45+
if err != nil {
46+
return err
47+
}
48+
if info != nil && !info.IsDir() {
49+
return nil // nothing to do for anything but directories
50+
}
51+
if info.Name() == ".git" ||
52+
info.Name() == ".hg" ||
53+
info.Name() == ".bzr" {
54+
return filepath.SkipDir
55+
}
56+
if info.Name() == "vendor" {
57+
rel, err := filepath.Rel(dir, path)
58+
if err != nil {
59+
return err
60+
}
61+
vendorDirs = append(vendorDirs, rel)
62+
}
63+
return nil
64+
})
65+
return vendorDirs, err
66+
}
67+
4268
// TODO: refactor this function into multiple smaller ones. Currently all the
4369
// code is in this function only due to the os.RemoveAll(tempdir).
44-
func makeUpstreamSourceTarball(gopkg string, gitRevision string, pkgType string) (string, string, map[string]bool, string, error) {
70+
func makeUpstreamSourceTarball(gopkg string, gitRevision string, pkgType string) (string, string, map[string]bool, string, []string, error) {
4571
// dependencies is a map in order to de-duplicate multiple imports
4672
// pointing into the same repository.
4773
dependencies := make(map[string]bool)
4874
autoPkgType := "library"
4975

5076
tempdir, err := ioutil.TempDir("", "dh-make-golang")
5177
if err != nil {
52-
return "", "", dependencies, autoPkgType, err
78+
return "", "", dependencies, autoPkgType, nil, err
5379
}
5480
defer os.RemoveAll(tempdir)
5581

@@ -72,7 +98,7 @@ func makeUpstreamSourceTarball(gopkg string, gitRevision string, pkgType string)
7298

7399
if err := cmd.Run(); err != nil {
74100
done <- true
75-
return "", "", dependencies, autoPkgType, err
101+
return "", "", dependencies, autoPkgType, nil, err
76102
}
77103
done <- true
78104
fmt.Printf("\r")
@@ -96,7 +122,7 @@ func makeUpstreamSourceTarball(gopkg string, gitRevision string, pkgType string)
96122

97123
if err := cmd.Run(); err != nil {
98124
done <- true
99-
return "", "", dependencies, autoPkgType, err
125+
return "", "", dependencies, autoPkgType, nil, err
100126
}
101127
done <- true
102128
fmt.Printf("\r")
@@ -106,11 +132,16 @@ func makeUpstreamSourceTarball(gopkg string, gitRevision string, pkgType string)
106132
log.Printf("WARNING: ignoring debian/ directory that came with the upstream sources\n")
107133
}
108134

109-
vendorpath := filepath.Join(tempdir, "src", gopkg, "vendor")
110-
if fi, err := os.Stat(vendorpath); err == nil && fi.IsDir() {
111-
log.Printf("Deleting upstream vendor/ directory, installing remaining dependencies")
112-
if err := os.RemoveAll(vendorpath); err != nil {
113-
return "", "", dependencies, autoPkgType, err
135+
vendorDirs, err := findVendorDirs(filepath.Join(tempdir, "src", gopkg))
136+
if err != nil {
137+
return "", "", dependencies, autoPkgType, nil, err
138+
}
139+
if len(vendorDirs) > 0 {
140+
log.Printf("Deleting upstream vendor/ directories, installing remaining dependencies")
141+
for _, dir := range vendorDirs {
142+
if err := os.RemoveAll(filepath.Join(tempdir, "src", gopkg, dir)); err != nil {
143+
return "", "", dependencies, autoPkgType, nil, err
144+
}
114145
}
115146
done := make(chan bool)
116147
go progressSize("go get", filepath.Join(tempdir, "src"), done)
@@ -122,7 +153,7 @@ func makeUpstreamSourceTarball(gopkg string, gitRevision string, pkgType string)
122153
cmd.Dir = filepath.Join(tempdir, "src", gopkg)
123154
if err := cmd.Run(); err != nil {
124155
done <- true
125-
return "", "", dependencies, autoPkgType, err
156+
return "", "", dependencies, autoPkgType, nil, err
126157
}
127158
done <- true
128159
fmt.Printf("\r")
@@ -144,18 +175,18 @@ func makeUpstreamSourceTarball(gopkg string, gitRevision string, pkgType string)
144175
cmd.Dir = filepath.Join(tempdir, "src", dir)
145176
cmd.Stderr = os.Stderr
146177
if err := cmd.Run(); err != nil {
147-
return "", "", dependencies, autoPkgType, err
178+
return "", "", dependencies, autoPkgType, nil, err
148179
}
149180

150181
if _, err := os.Stat(filepath.Join(tempdir, "src", gopkg, ".git")); os.IsNotExist(err) {
151-
return "", "", dependencies, autoPkgType, fmt.Errorf("Not a git repository, dh-make-golang currently only supports git")
182+
return "", "", dependencies, autoPkgType, nil, fmt.Errorf("Not a git repository, dh-make-golang currently only supports git")
152183
}
153184

154185
log.Printf("Determining upstream version number\n")
155186

156187
version, err := pkgVersionFromGit(filepath.Join(tempdir, "src", gopkg))
157188
if err != nil {
158-
return "", "", dependencies, autoPkgType, err
189+
return "", "", dependencies, autoPkgType, nil, err
159190
}
160191

161192
log.Printf("Package version is %q\n", version)
@@ -171,7 +202,7 @@ func makeUpstreamSourceTarball(gopkg string, gitRevision string, pkgType string)
171202

172203
out, err := cmd.Output()
173204
if err != nil {
174-
return "", "", dependencies, autoPkgType, err
205+
return "", "", dependencies, autoPkgType, nil, err
175206
}
176207
for _, line := range strings.Split(string(out), "\n") {
177208
if strings.TrimSpace(line) == "" {
@@ -203,7 +234,7 @@ func makeUpstreamSourceTarball(gopkg string, gitRevision string, pkgType string)
203234

204235
out, err = cmd.Output()
205236
if err != nil {
206-
return "", "", dependencies, autoPkgType, err
237+
return "", "", dependencies, autoPkgType, nil, err
207238
}
208239

209240
var godependencies []string
@@ -223,7 +254,7 @@ func makeUpstreamSourceTarball(gopkg string, gitRevision string, pkgType string)
223254
}
224255

225256
if len(godependencies) == 0 {
226-
return tempfile, version, dependencies, autoPkgType, nil
257+
return tempfile, version, dependencies, autoPkgType, vendorDirs, nil
227258
}
228259

229260
// Remove all packages which are in the standard lib.
@@ -239,7 +270,7 @@ func makeUpstreamSourceTarball(gopkg string, gitRevision string, pkgType string)
239270

240271
out, err = cmd.Output()
241272
if err != nil {
242-
return "", "", dependencies, autoPkgType, err
273+
return "", "", dependencies, autoPkgType, nil, err
243274
}
244275

245276
for _, p := range strings.Split(string(out), "\n") {
@@ -252,7 +283,7 @@ func makeUpstreamSourceTarball(gopkg string, gitRevision string, pkgType string)
252283
dependencies[rr.Root] = true
253284
}
254285
}
255-
return tempfile, version, dependencies, autoPkgType, nil
286+
return tempfile, version, dependencies, autoPkgType, vendorDirs, nil
256287
}
257288

258289
func runGitCommandIn(dir string, arg ...string) error {
@@ -433,7 +464,7 @@ func getDebianEmail() string {
433464
return "TODO"
434465
}
435466

436-
func writeTemplates(dir, gopkg, debsrc, debbin, debversion, pkgType string, dependencies []string) error {
467+
func writeTemplates(dir, gopkg, debsrc, debbin, debversion, pkgType string, dependencies []string, vendorDirs []string) error {
437468
if err := os.Mkdir(filepath.Join(dir, "debian"), 0755); err != nil {
438469
return err
439470
}
@@ -526,7 +557,11 @@ func writeTemplates(dir, gopkg, debsrc, debbin, debversion, pkgType string, depe
526557
fmt.Fprintf(f, "Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/\n")
527558
fmt.Fprintf(f, "Upstream-Name: %s\n", filepath.Base(gopkg))
528559
fmt.Fprintf(f, "Source: %s\n", getHomepageForGopkg(gopkg))
529-
fmt.Fprintf(f, "Files-Excluded: vendor Godeps/_workspace\n")
560+
fmt.Fprintf(f, "Files-Excluded:\n")
561+
for _, dir := range vendorDirs {
562+
fmt.Fprintf(f, " %s\n", dir)
563+
}
564+
fmt.Fprintf(f, " Godeps/_workspace\n")
530565
fmt.Fprintf(f, "\n")
531566
fmt.Fprintf(f, "Files: *\n")
532567
fmt.Fprintf(f, "Copyright: %s\n", copyright)
@@ -733,7 +768,7 @@ func execMake(args []string) {
733768
golangBinaries, err = getGolangBinaries()
734769
return err
735770
})
736-
tempfile, version, godependencies, autoPkgType, err := makeUpstreamSourceTarball(gopkg, gitRevision, pkgType)
771+
tempfile, version, godependencies, autoPkgType, vendorDirs, err := makeUpstreamSourceTarball(gopkg, gitRevision, pkgType)
737772
if err != nil {
738773
log.Fatalf("Could not create a tarball of the upstream source: %v\n", err)
739774
}
@@ -801,7 +836,7 @@ func execMake(args []string) {
801836
debdependencies = append(debdependencies, bin)
802837
}
803838

804-
if err := writeTemplates(dir, gopkg, debsrc, debbin, debversion, pkgType, debdependencies); err != nil {
839+
if err := writeTemplates(dir, gopkg, debsrc, debbin, debversion, pkgType, debdependencies, vendorDirs); err != nil {
805840
log.Fatalf("Could not create debian/ from templates: %v\n", err)
806841
}
807842

0 commit comments

Comments
 (0)