Codebase list dh-make-golang / 0b564d6
New upstream version 0.0~git20170925.0.fd02c4a Dr. Tobias Quathamer 6 years ago
4 changed file(s) with 107 addition(s) and 65 deletion(s). Raw diff Collapse all Expand all
436436 return "TODO"
437437 }
438438
439 func websiteForGopkg(gopkg string) string {
440 if strings.HasPrefix(gopkg, "github.com/") {
441 return "https://" + gopkg
442 }
443 return "TODO"
444 }
445
446439 func writeTemplates(dir, gopkg, debsrc, debbin, debversion string, dependencies []string) error {
447440 if err := os.Mkdir(filepath.Join(dir, "debian"), 0755); err != nil {
448441 return err
481474 fmt.Fprintf(f, "Source: %s\n", debsrc)
482475 // TODO: change this once we have a “golang” section.
483476 fmt.Fprintf(f, "Section: devel\n")
484 fmt.Fprintf(f, "Priority: extra\n")
477 fmt.Fprintf(f, "Priority: optional\n")
485478 fmt.Fprintf(f, "Maintainer: Debian Go Packaging Team <pkg-go-maintainers@lists.alioth.debian.org>\n")
486479 fmt.Fprintf(f, "Uploaders: %s <%s>\n", getDebianName(), getDebianEmail())
487480 sort.Strings(dependencies)
488481 builddeps := append([]string{"debhelper (>= 10)", "dh-golang", "golang-any"}, dependencies...)
489482 fmt.Fprintf(f, "Build-Depends: %s\n", strings.Join(builddeps, ",\n "))
490 fmt.Fprintf(f, "Standards-Version: 4.0.0\n")
491 fmt.Fprintf(f, "Homepage: %s\n", websiteForGopkg(gopkg))
483 fmt.Fprintf(f, "Standards-Version: 4.1.0\n")
484 fmt.Fprintf(f, "Homepage: %s\n", getHomepageForGopkg(gopkg))
492485 fmt.Fprintf(f, "Vcs-Browser: https://anonscm.debian.org/cgit/pkg-go/packages/%s.git\n", debsrc)
493486 fmt.Fprintf(f, "Vcs-Git: https://anonscm.debian.org/git/pkg-go/packages/%s.git\n", debsrc)
494487 fmt.Fprintf(f, "XS-Go-Import-Path: %s\n", gopkg)
535528 }
536529 fmt.Fprintf(f, "Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/\n")
537530 fmt.Fprintf(f, "Upstream-Name: %s\n", filepath.Base(gopkg))
538 fmt.Fprintf(f, "Source: %s\n", websiteForGopkg(gopkg))
531 fmt.Fprintf(f, "Source: %s\n", getHomepageForGopkg(gopkg))
532 fmt.Fprintf(f, "Files-Excluded: vendor Godeps/_workspace\n")
539533 fmt.Fprintf(f, "\n")
540534 fmt.Fprintf(f, "Files: *\n")
541535 fmt.Fprintf(f, "Copyright: %s\n", copyright)
634628 fmt.Fprintf(f, "* Package name : %s\n", debsrc)
635629 fmt.Fprintf(f, " Version : %s\n", debversion)
636630 fmt.Fprintf(f, " Upstream Author : %s\n", author)
637 fmt.Fprintf(f, "* URL : %s\n", websiteForGopkg(gopkg))
631 fmt.Fprintf(f, "* URL : %s\n", getHomepageForGopkg(gopkg))
638632 fmt.Fprintf(f, "* License : %s\n", license)
639633 fmt.Fprintf(f, " Programming Lang: Go\n")
640634 fmt.Fprintf(f, " Description : %s\n", description)
33 "encoding/json"
44 "fmt"
55 "net/http"
6 "regexp"
67 "strings"
78 "time"
9
10 "golang.org/x/net/html"
811 )
912
1013 type licensesReply struct {
5053 "lgpl-2.1": "LGPL-2.1",
5154 "lgpl-3.0": "LGPL-3.0",
5255 //"mit" - expat?
53 //"mpl-2.0" (only 1.1 is in debian)
56 "mpl-2.0": "MPL-2.0", // include in base-files >= 9.9
5457 //"unlicense" (not in debian)
5558 }
5659
7073 On Debian systems, the complete text of the Apache version 2.0 license
7174 can be found in "/usr/share/common-licenses/Apache-2.0".
7275 `,
76 "MPL-2.0": ` This Source Code Form is subject to the terms of the Mozilla Public
77 License, v. 2.0. If a copy of the MPL was not distributed with this
78 file, You can obtain one at http://mozilla.org/MPL/2.0/.
79 .
80 On Debian systems, the complete text of the MPL-2.0 license can be found
81 in "/usr/share/common-licenses/MPL-2.0".
82 `,
83 }
84
85 var githubRegexp = regexp.MustCompile(`github\.com/([^/]+/[^/]+)`)
86
87 func findGitHubRepo(gopkg string) (string, error) {
88 if strings.HasPrefix(gopkg, "github.com/") {
89 return strings.TrimPrefix(gopkg, "github.com/"), nil
90 }
91 resp, err := http.Get("https://" + gopkg + "?go-get=1")
92 if err != nil {
93 return "", err
94 }
95 defer resp.Body.Close()
96 z := html.NewTokenizer(resp.Body)
97 for {
98 tt := z.Next()
99 if tt == html.ErrorToken {
100 return "", fmt.Errorf("%q is not on GitHub", gopkg)
101 }
102 token := z.Token()
103 if token.Data != "meta" {
104 continue
105 }
106 var meta struct {
107 name, content string
108 }
109 for _, attr := range token.Attr {
110 if attr.Key == "name" {
111 meta.name = attr.Val
112 }
113 if attr.Key == "content" {
114 meta.content = attr.Val
115 }
116 }
117
118 match := func(name string, length int) string {
119 if f := strings.Fields(meta.content); meta.name == name && len(f) == length {
120 if f[0] != gopkg {
121 return ""
122 }
123 if repoMatch := githubRegexp.FindStringSubmatch(f[2]); repoMatch != nil {
124 return repoMatch[1]
125 }
126 }
127 return ""
128 }
129 if repo := match("go-import", 3); repo != "" {
130 return repo, nil
131 }
132 if repo := match("go-source", 4); repo != "" {
133 return repo, nil
134 }
135 }
73136 }
74137
75138 func getLicenseForGopkg(gopkg string) (string, string, error) {
76 if !strings.HasPrefix(gopkg, "github.com/") {
77 return "", "", fmt.Errorf("%q is not on GitHub", gopkg)
139 repo, err := findGitHubRepo(gopkg)
140 if err != nil {
141 return "", "", err
78142 }
79143 // TODO: cache this reply
80 req, err := http.NewRequest("GET", "https://api.github.com/repos/"+gopkg[len("github.com/"):], nil)
144 req, err := http.NewRequest("GET", "https://api.github.com/repos/"+repo, nil)
81145 if err != nil {
82146 return "", "", err
83147 }
103167 }
104168
105169 func getAuthorAndCopyrightForGopkg(gopkg string) (string, string, error) {
106 if !strings.HasPrefix(gopkg, "github.com/") {
107 return "", "", fmt.Errorf("%q is not on GitHub", gopkg)
108 }
109 resp, err := http.Get("https://api.github.com/repos/" + gopkg[len("github.com/"):])
170 repo, err := findGitHubRepo(gopkg)
171 if err != nil {
172 return "", "", err
173 }
174 resp, err := http.Get("https://api.github.com/repos/" + repo)
110175 if err != nil {
111176 return "", "", err
112177 }
137202 return "", "", err
138203 }
139204
140 return ur.Name, creation.Format("2006") + " " + ur.Name, nil
205 copyright := creation.Format("2006") + " " + ur.Name
206 if strings.HasPrefix(repo, "google/") {
207 // As per https://opensource.google.com/docs/creating/, Google retains
208 // the copyright for repositories underneath github.com/google/.
209 copyright = creation.Format("2006") + " Google Inc."
210 }
211
212 return ur.Name, copyright, nil
141213 }
142214
143215 func getDescriptionForGopkg(gopkg string) (string, error) {
144 if !strings.HasPrefix(gopkg, "github.com/") {
145 return "", fmt.Errorf("%q is not on GitHub", gopkg)
146 }
147 resp, err := http.Get("https://api.github.com/repos/" + gopkg[len("github.com/"):])
216 repo, err := findGitHubRepo(gopkg)
217 if err != nil {
218 return "", err
219 }
220 resp, err := http.Get("https://api.github.com/repos/" + repo)
148221 if err != nil {
149222 return "", err
150223 }
157230
158231 return strings.TrimSpace(rr.Description), nil
159232 }
233
234 func getHomepageForGopkg(gopkg string) string {
235 repo, err := findGitHubRepo(gopkg)
236 if err != nil {
237 return "TODO"
238 }
239 return "https://github.com/" + repo
240 }
11
22 import (
33 "fmt"
4 "log"
54 "os"
65 "os/exec"
76 "regexp"
1211
1312 var (
1413 // describeRegexp parses the count and revision part of the “git describe --long” output.
15 describeRegexp = regexp.MustCompile(`-(\d+)-g([0-9a-f]+)\s*$`)
14 describeRegexp = regexp.MustCompile(`-\d+-g([0-9a-f]+)\s*$`)
1615 )
1716
1817 // TODO: also support other VCS
1918 func pkgVersionFromGit(gitdir string) (string, error) {
20 cmd := exec.Command("git", "describe", "--exact-match")
19 cmd := exec.Command("git", "describe", "--exact-match", "--tags")
2120 cmd.Dir = gitdir
2221 if tag, err := cmd.Output(); err == nil {
2322 version := strings.TrimSpace(string(tag))
4544 // https://www.debian.org/doc/manuals/maint-guide/first.en.html#namever
4645 lastTag := "0.0~"
4746 if lastTagBytes, err := cmd.Output(); err == nil {
48 lastTag = strings.TrimSpace(string(lastTagBytes))
49
50 // Compare with the most recent annotated tag
51 foundLightweightTag := false
52 cmd = exec.Command("git", "describe", "--abbrev=0")
53 cmd.Dir = gitdir
54 if lastAnnotatedTagBytes, err := cmd.Output(); err == nil {
55 lastAnnotatedTag := strings.TrimSpace(string(lastAnnotatedTagBytes))
56 if lastTag != lastAnnotatedTag {
57 log.Printf("WARNING: Lightweight tag %q found, but the most recent annotated tag is %q\n", lastTag, lastAnnotatedTag)
58 foundLightweightTag = true
59 }
60 } else {
61 log.Printf("WARNING: Annotated tag not found, using lightweight tag %q\n", lastTag)
62 foundLightweightTag = true
63 }
64
65 if foundLightweightTag {
66 log.Printf(" Lightweight tags (created by e.g. \"git tag %s\"", lastTag)
67 log.Printf(" with no flag) are problematic because, among other\n")
68 log.Printf(" things, they are ignored by \"git describe\" by default.\n")
69 log.Printf(" Please suggest that the upstream replaces the\n")
70 log.Printf(" lightweight tags with annotated ones. See\n")
71 log.Printf(" https://github.com/russross/blackfriday/issues/196\n")
72 log.Printf(" for details.\n")
73 log.Printf("\n")
74 }
75
76 lastTag += "+"
77 if strings.HasPrefix(lastTag, "v") {
78 lastTag = lastTag[1:]
79 }
47 lastTag = strings.TrimPrefix(strings.TrimSpace(string(lastTagBytes)), "v") + "+"
8048 }
8149
8250 // This results in an output like 4.10.2-232-g9f107c8
9967 if submatches == nil {
10068 return "", fmt.Errorf("git describe output %q does not match expected format", string(describeBytes))
10169 }
102 version := fmt.Sprintf("%sgit%s.%s.%s",
70 version := fmt.Sprintf("%sgit%s.%s",
10371 lastTag,
10472 time.Unix(lastCommitUnix, 0).UTC().Format("20060102"),
105 string(submatches[1]),
106 string(submatches[2]))
73 string(submatches[1]))
10774 return version, nil
10875 }
4545 if err != nil {
4646 t.Fatalf("Determining package version from git failed: %v", err)
4747 }
48 if want := "0.0~git20150420.0."; !strings.HasPrefix(got, want) {
49 t.Logf("got %q, want %q", got, want)
48 if want := "0.0~git20150420."; !strings.HasPrefix(got, want) {
49 t.Errorf("got %q, want %q", got, want)
5050 }
5151
5252 gitCmdOrFatal(t, tempdir, "tag", "-a", "v1", "-m", "release v1")