-
Notifications
You must be signed in to change notification settings - Fork 540
/
Copy pathutil.go
324 lines (290 loc) · 8.88 KB
/
util.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package util
import (
"encoding/xml"
"fmt"
"go/build"
"io"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/Masterminds/vcs"
)
// ResolveCurrent selects whether the package should only the dependencies for
// the current OS/ARCH instead of all possible permutations.
// This is not concurrently safe which is ok for the current application. If
// other needs arise it may need to be re-written.
var ResolveCurrent = false
func init() {
// Precompile the regular expressions used to check VCS locations.
for _, v := range vcsList {
v.regex = regexp.MustCompile(v.pattern)
}
}
// GetRootFromPackage retrives the top level package from a name.
//
// From a package name find the root repo. For example,
// the package github.com/Masterminds/cookoo/io has a root repo
// at github.com/Masterminds/cookoo
func GetRootFromPackage(pkg string) string {
pkg = filepath.ToSlash(pkg)
for _, v := range vcsList {
m := v.regex.FindStringSubmatch(pkg)
if m == nil {
continue
}
if m[1] != "" {
return m[1]
}
}
// There are cases where a package uses the special go get magic for
// redirects. If we've not discovered the location already try that.
pkg = getRootFromGoGet(pkg)
return pkg
}
// Pages like https://golang.org/x/net provide an html document with
// meta tags containing a location to work with. The go tool uses
// a meta tag with the name go-import which is what we use here.
// godoc.org also has one call go-source that we do not need to use.
// The value of go-import is in the form "prefix vcs repo". The prefix
// should match the vcsURL and the repo is a location that can be
// checked out. Note, to get the html document you you need to add
// ?go-get=1 to the url.
func getRootFromGoGet(pkg string) string {
p, found := checkRemotePackageCache(pkg)
if found {
return p
}
vcsURL := "https://" + pkg
u, err := url.Parse(vcsURL)
if err != nil {
return pkg
}
if u.RawQuery == "" {
u.RawQuery = "go-get=1"
} else {
u.RawQuery = u.RawQuery + "&go-get=1"
}
checkURL := u.String()
resp, err := http.Get(checkURL)
if err != nil {
addToRemotePackageCache(pkg, pkg)
return pkg
}
defer resp.Body.Close()
nu, err := parseImportFromBody(u, resp.Body)
if err != nil {
addToRemotePackageCache(pkg, pkg)
return pkg
} else if nu == "" {
addToRemotePackageCache(pkg, pkg)
return pkg
}
addToRemotePackageCache(pkg, nu)
return nu
}
// The caching is not concurrency safe but should be made to be that way.
// This implementation is far too much of a hack... rewrite needed.
var remotePackageCache = make(map[string]string)
func checkRemotePackageCache(pkg string) (string, bool) {
for k, v := range remotePackageCache {
if pkg == k {
return v, true
}
}
return pkg, false
}
func addToRemotePackageCache(pkg, v string) {
remotePackageCache[pkg] = v
}
func parseImportFromBody(ur *url.URL, r io.ReadCloser) (u string, err error) {
d := xml.NewDecoder(r)
d.CharsetReader = charsetReader
d.Strict = false
var t xml.Token
for {
t, err = d.Token()
if err != nil {
if err == io.EOF {
// If we hit the end of the markup and don't have anything
// we return an error.
err = vcs.ErrCannotDetectVCS
}
return
}
if e, ok := t.(xml.StartElement); ok && strings.EqualFold(e.Name.Local, "body") {
return
}
if e, ok := t.(xml.EndElement); ok && strings.EqualFold(e.Name.Local, "head") {
return
}
e, ok := t.(xml.StartElement)
if !ok || !strings.EqualFold(e.Name.Local, "meta") {
continue
}
if attrValue(e.Attr, "name") != "go-import" {
continue
}
if f := strings.Fields(attrValue(e.Attr, "content")); len(f) == 3 {
// If the prefix supplied by the remote system isn't a prefix to the
// url we're fetching return continue looking for more go-imports.
// This will work for exact matches and prefixes. For example,
// golang.org/x/net as a prefix will match for golang.org/x/net and
// golang.org/x/net/context.
vcsURL := ur.Host + ur.Path
if !strings.HasPrefix(vcsURL, f[0]) {
continue
} else {
u = f[0]
return
}
}
}
}
func charsetReader(charset string, input io.Reader) (io.Reader, error) {
switch strings.ToLower(charset) {
case "ascii":
return input, nil
default:
return nil, fmt.Errorf("can't decode XML document using charset %q", charset)
}
}
func attrValue(attrs []xml.Attr, name string) string {
for _, a := range attrs {
if strings.EqualFold(a.Name.Local, name) {
return a.Value
}
}
return ""
}
type vcsInfo struct {
host string
pattern string
regex *regexp.Regexp
}
var vcsList = []*vcsInfo{
{
host: "github.com",
pattern: `^(?P<rootpkg>github\.com/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)(/[A-Za-z0-9_.\-]+)*$`,
},
{
host: "bitbucket.org",
pattern: `^(?P<rootpkg>bitbucket\.org/([A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+))(/[A-Za-z0-9_.\-]+)*$`,
},
{
host: "launchpad.net",
pattern: `^(?P<rootpkg>launchpad\.net/(([A-Za-z0-9_.\-]+)(/[A-Za-z0-9_.\-]+)?|~[A-Za-z0-9_.\-]+/(\+junk|[A-Za-z0-9_.\-]+)/[A-Za-z0-9_.\-]+))(/[A-Za-z0-9_.\-]+)*$`,
},
{
host: "git.launchpad.net",
pattern: `^(?P<rootpkg>git\.launchpad\.net/(([A-Za-z0-9_.\-]+)|~[A-Za-z0-9_.\-]+/(\+git|[A-Za-z0-9_.\-]+)/[A-Za-z0-9_.\-]+))$`,
},
{
host: "hub.jazz.net",
pattern: `^(?P<rootpkg>hub\.jazz\.net/git/[a-z0-9]+/[A-Za-z0-9_.\-]+)(/[A-Za-z0-9_.\-]+)*$`,
},
{
host: "go.googlesource.com",
pattern: `^(?P<rootpkg>go\.googlesource\.com/[A-Za-z0-9_.\-]+/?)$`,
},
// TODO: Once Google Code becomes fully deprecated this can be removed.
{
host: "code.google.com",
pattern: `^(?P<rootpkg>code\.google\.com/[pr]/([a-z0-9\-]+)(\.([a-z0-9\-]+))?)(/[A-Za-z0-9_.\-]+)*$`,
},
// Alternative Google setup for SVN. This is the previous structure but it still works... until Google Code goes away.
{
pattern: `^(?P<rootpkg>[a-z0-9_\-.]+\.googlecode\.com/svn(/.*)?)$`,
},
// Alternative Google setup. This is the previous structure but it still works... until Google Code goes away.
{
pattern: `^(?P<rootpkg>[a-z0-9_\-.]+\.googlecode\.com/(git|hg))(/.*)?$`,
},
// If none of the previous detect the type they will fall to this looking for the type in a generic sense
// by the extension to the path.
{
pattern: `^(?P<rootpkg>(?P<repo>([a-z0-9.\-]+\.)+[a-z0-9.\-]+(:[0-9]+)?/[A-Za-z0-9_.\-/]*?)\.(bzr|git|hg|svn))(/[A-Za-z0-9_.\-]+)*$`,
},
}
// BuildCtxt is a convenience wrapper for not having to import go/build
// anywhere else
type BuildCtxt struct {
build.Context
}
// PackageName attempts to determine the name of the base package.
//
// If resolution fails, this will return "main".
func (b *BuildCtxt) PackageName(base string) string {
cwd, err := os.Getwd()
if err != nil {
return "main"
}
pkg, err := b.Import(base, cwd, 0)
if err != nil {
// There may not be any top level Go source files but the project may
// still be within the GOPATH.
if strings.HasPrefix(base, b.GOPATH) {
p := strings.TrimPrefix(base, filepath.Join(b.GOPATH, "src"))
return strings.Trim(p, string(os.PathSeparator))
}
}
return pkg.ImportPath
}
// GetBuildContext returns a build context from go/build. When the $GOROOT
// variable is not set in the users environment it sets the context's root
// path to the path returned by 'go env GOROOT'.
//
// TODO: This should be moved to the `dependency` package.
func GetBuildContext() (*BuildCtxt, error) {
buildContext := &BuildCtxt{build.Default}
// If we aren't resolving for the current system set to look at all
// build modes.
if !ResolveCurrent {
// This tells the context scanning to skip filtering on +build flags or
// file names.
buildContext.UseAllFiles = true
}
if goRoot := os.Getenv("GOROOT"); len(goRoot) == 0 {
goExecutable := os.Getenv("GLIDE_GO_EXECUTABLE")
if len(goExecutable) <= 0 {
goExecutable = "go"
}
out, err := exec.Command(goExecutable, "env", "GOROOT").Output()
if goRoot = strings.TrimSpace(string(out)); len(goRoot) == 0 || err != nil {
return nil, fmt.Errorf("Please set the $GOROOT environment " +
"variable to use this command\n")
}
buildContext.GOROOT = goRoot
}
return buildContext, nil
}
// NormalizeName takes a package name and normalizes it to the top level package.
//
// For example, golang.org/x/crypto/ssh becomes golang.org/x/crypto. 'ssh' is
// returned as extra data.
//
// FIXME: Is this deprecated?
func NormalizeName(name string) (string, string) {
// Fastpath check if a name in the GOROOT. There is an issue when a pkg
// is in the GOROOT and GetRootFromPackage tries to look it up because it
// expects remote names.
b, err := GetBuildContext()
if err == nil {
p := filepath.Join(b.GOROOT, "src", name)
if _, err := os.Stat(p); err == nil {
return filepath.ToSlash(name), ""
}
}
root := GetRootFromPackage(name)
extra := strings.TrimPrefix(name, root)
if len(extra) > 0 && extra != "/" {
extra = strings.TrimPrefix(extra, "/")
} else {
// If extra is / (which is what it would be here) we want to return ""
extra = ""
}
return root, extra
}