github.com/tcnksm/go@v0.0.0-20141208075154-439b32936367/src/cmd/api/run.go (about) 1 // Copyright 2013 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build ignore 6 7 // The run program is invoked via "go run" from src/run.bash or 8 // src/run.bat conditionally builds and runs the cmd/api tool. 9 // 10 // TODO(bradfitz): the "conditional" condition is always true. 11 // We should only do this if the user has the hg codereview extension 12 // enabled and verifies that the go.tools subrepo is checked out with 13 // a suitably recently version. In prep for the cmd/api rewrite. 14 package main 15 16 import ( 17 "fmt" 18 "log" 19 "net/http" 20 "os" 21 "os/exec" 22 "os/user" 23 "path/filepath" 24 "strings" 25 ) 26 27 // goToolsVersion is the hg revision of the go.tools subrepo we need 28 // to build cmd/api. This only needs to be updated whenever a go/types 29 // bug fix is needed by the cmd/api tool. 30 const goToolsVersion = "6698ca2900e2" 31 32 var goroot string 33 34 func main() { 35 log.SetFlags(0) 36 goroot = os.Getenv("GOROOT") // should be set by run.{bash,bat} 37 if goroot == "" { 38 log.Fatal("No $GOROOT set.") 39 } 40 _, err := exec.LookPath("hg") 41 if err != nil { 42 fmt.Println("Skipping cmd/api checks; hg not available") 43 return 44 } 45 46 gopath := prepGoPath() 47 48 cmd := exec.Command("go", "install", "--tags=api_tool", "cmd/api") 49 cmd.Env = append(filterOut(os.Environ(), "GOARCH", "GOPATH"), "GOPATH="+gopath) 50 out, err := cmd.CombinedOutput() 51 if err != nil { 52 log.Fatalf("Error installing cmd/api: %v\n%s", err, out) 53 } 54 55 out, err = exec.Command("go", "tool", "api", 56 "-c", file("go1", "go1.1", "go1.2", "go1.3"), 57 "-next", file("next"), 58 "-except", file("except")).CombinedOutput() 59 if err != nil { 60 log.Fatalf("Error running API checker: %v\n%s", err, out) 61 } 62 fmt.Print(string(out)) 63 } 64 65 // filterOut returns a copy of the src environment without environment 66 // variables from remove. 67 // TODO: delete when issue 6201 is fixed. 68 func filterOut(src []string, remove ...string) (out []string) { 69 S: 70 for _, s := range src { 71 for _, r := range remove { 72 if strings.HasPrefix(s, r) && strings.HasPrefix(s, r+"=") { 73 continue S 74 } 75 } 76 out = append(out, s) 77 } 78 return 79 } 80 81 // file expands s to $GOROOT/api/s.txt. 82 // If there are more than 1, they're comma-separated. 83 func file(s ...string) string { 84 if len(s) > 1 { 85 return file(s[0]) + "," + file(s[1:]...) 86 } 87 return filepath.Join(goroot, "api", s[0]+".txt") 88 } 89 90 // prepGoPath returns a GOPATH for the "go" tool to compile the API tool with. 91 // It tries to re-use a go.tools checkout from a previous run if possible, 92 // else it hg clones it. 93 func prepGoPath() string { 94 const tempBase = "go.tools.TMP" 95 96 username := "" 97 u, err := user.Current() 98 if err == nil { 99 username = u.Username 100 } else { 101 username = os.Getenv("USER") 102 if username == "" { 103 username = "nobody" 104 } 105 } 106 107 // The GOPATH we'll return 108 gopath := filepath.Join(os.TempDir(), "gopath-api-"+cleanUsername(username), goToolsVersion) 109 110 // cloneDir is where we run "hg clone". 111 cloneDir := filepath.Join(gopath, "src", "code.google.com", "p") 112 113 // The dir we clone into. We only atomically rename it to finalDir on 114 // clone success. 115 tmpDir := filepath.Join(cloneDir, tempBase) 116 117 // finalDir is where the checkout will live once it's complete. 118 finalDir := filepath.Join(cloneDir, "go.tools") 119 120 if goToolsCheckoutGood(finalDir) { 121 return gopath 122 } 123 os.RemoveAll(finalDir) // in case it's there but corrupt 124 os.RemoveAll(tmpDir) // in case of aborted hg clone before 125 126 if err := os.MkdirAll(cloneDir, 0700); err != nil { 127 log.Fatal(err) 128 } 129 cmd := exec.Command("hg", 130 "clone", "--rev="+goToolsVersion, 131 "https://code.google.com/p/go.tools", 132 tempBase) 133 cmd.Dir = cloneDir 134 out, err := cmd.CombinedOutput() 135 if err != nil { 136 if _, err := http.Head("http://ip.appspot.com/"); err != nil { 137 log.Printf("# Skipping API check; network appears to be unavailable") 138 os.Exit(0) 139 } 140 log.Fatalf("Error running hg clone on go.tools: %v\n%s", err, out) 141 } 142 if err := os.Rename(tmpDir, finalDir); err != nil { 143 log.Fatal(err) 144 } 145 return gopath 146 } 147 148 func cleanUsername(n string) string { 149 b := make([]rune, len(n)) 150 for i, r := range n { 151 if r == '\\' || r == '/' || r == ':' { 152 b[i] = '_' 153 } else { 154 b[i] = r 155 } 156 } 157 return string(b) 158 } 159 160 func goToolsCheckoutGood(dir string) bool { 161 if _, err := os.Stat(dir); err != nil { 162 return false 163 } 164 165 cmd := exec.Command("hg", "id", "--id") 166 cmd.Dir = dir 167 out, err := cmd.Output() 168 if err != nil { 169 return false 170 } 171 id := strings.TrimSpace(string(out)) 172 if id != goToolsVersion { 173 return false 174 } 175 176 cmd = exec.Command("hg", "status") 177 cmd.Dir = dir 178 out, err = cmd.Output() 179 if err != nil || len(out) > 0 { 180 return false 181 } 182 183 return true 184 }