Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(76)

Side by Side Diff: src/cmd/api/run.go

Issue 13652043: code review 13652043: cmd/api/run: make api check directory per-user (Closed)
Patch Set: diff -r 381aeb04fa0d https://code.google.com/p/go/ Created 11 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Go Authors. All rights reserved. 1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 // +build ignore 5 // +build ignore
6 6
7 // The run program is invoked via "go run" from src/run.bash or 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. 8 // src/run.bat conditionally builds and runs the cmd/api tool.
9 // 9 //
10 // TODO(bradfitz): the "conditional" condition is always true. 10 // TODO(bradfitz): the "conditional" condition is always true.
11 // We should only do this if the user has the hg codereview extension 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 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. 13 // a suitably recently version. In prep for the cmd/api rewrite.
14 package main 14 package main
15 15
16 import ( 16 import (
17 "fmt" 17 "fmt"
18 "log" 18 "log"
19 "net/http" 19 "net/http"
20 "os" 20 "os"
21 "os/exec" 21 "os/exec"
22 "os/user"
22 "path/filepath" 23 "path/filepath"
23 "strconv" 24 "strconv"
24 "strings" 25 "strings"
25 ) 26 )
26 27
27 // goToolsVersion is the hg revision of the go.tools subrepo we need 28 // 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 // 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 // bug fix is needed by the cmd/api tool.
30 const goToolsVersion = "6698ca2900e2" 31 const goToolsVersion = "6698ca2900e2"
31 32
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 v, _ := strconv.ParseBool(os.Getenv("GO_FORCE_API_CHECK")) 93 v, _ := strconv.ParseBool(os.Getenv("GO_FORCE_API_CHECK"))
93 return v 94 return v
94 } 95 }
95 96
96 // prepGoPath returns a GOPATH for the "go" tool to compile the API tool with. 97 // prepGoPath returns a GOPATH for the "go" tool to compile the API tool with.
97 // It tries to re-use a go.tools checkout from a previous run if possible, 98 // It tries to re-use a go.tools checkout from a previous run if possible,
98 // else it hg clones it. 99 // else it hg clones it.
99 func prepGoPath() string { 100 func prepGoPath() string {
100 const tempBase = "go.tools.TMP" 101 const tempBase = "go.tools.TMP"
101 102
103 u, err := user.Current()
104 if err != nil {
105 log.Fatalf("Error getting current user: %v", err)
106 }
107
102 // The GOPATH we'll return 108 // The GOPATH we'll return
103 » gopath := filepath.Join(os.TempDir(), "gopath-api", goToolsVersion) 109 » gopath := filepath.Join(os.TempDir(), "gopath-api-"+cleanUsername(u.User name), goToolsVersion)
104 110
105 // cloneDir is where we run "hg clone". 111 // cloneDir is where we run "hg clone".
106 cloneDir := filepath.Join(gopath, "src", "code.google.com", "p") 112 cloneDir := filepath.Join(gopath, "src", "code.google.com", "p")
107 113
108 // The dir we clone into. We only atomically rename it to finalDir on 114 // The dir we clone into. We only atomically rename it to finalDir on
109 // clone success. 115 // clone success.
110 tmpDir := filepath.Join(cloneDir, tempBase) 116 tmpDir := filepath.Join(cloneDir, tempBase)
111 117
112 // finalDir is where the checkout will live once it's complete. 118 // finalDir is where the checkout will live once it's complete.
113 finalDir := filepath.Join(cloneDir, "go.tools") 119 finalDir := filepath.Join(cloneDir, "go.tools")
(...skipping 19 matching lines...) Expand all
133 os.Exit(0) 139 os.Exit(0)
134 } 140 }
135 log.Fatalf("Error running hg clone on go.tools: %v\n%s", err, ou t) 141 log.Fatalf("Error running hg clone on go.tools: %v\n%s", err, ou t)
136 } 142 }
137 if err := os.Rename(tmpDir, finalDir); err != nil { 143 if err := os.Rename(tmpDir, finalDir); err != nil {
138 log.Fatal(err) 144 log.Fatal(err)
139 } 145 }
140 return gopath 146 return gopath
141 } 147 }
142 148
149 func cleanUsername(n string) string {
brainman 2013/09/11 00:24:24 I think you code will read simpler if you just do
150 b := make([]rune, len(n))
151 for i, r := range n {
152 if r == '\\' || r == '/' {
153 b[i] = '_'
154 } else {
155 b[i] = r
156 }
157 }
158 return string(b)
159 }
160
143 func goToolsCheckoutGood(dir string) bool { 161 func goToolsCheckoutGood(dir string) bool {
144 if _, err := os.Stat(dir); err != nil { 162 if _, err := os.Stat(dir); err != nil {
145 return false 163 return false
146 } 164 }
147 165
148 cmd := exec.Command("hg", "id", "--id") 166 cmd := exec.Command("hg", "id", "--id")
149 cmd.Dir = dir 167 cmd.Dir = dir
150 out, err := cmd.Output() 168 out, err := cmd.Output()
151 if err != nil { 169 if err != nil {
152 return false 170 return false
153 } 171 }
154 id := strings.TrimSpace(string(out)) 172 id := strings.TrimSpace(string(out))
155 if id != goToolsVersion { 173 if id != goToolsVersion {
156 return false 174 return false
157 } 175 }
158 176
159 cmd = exec.Command("hg", "status") 177 cmd = exec.Command("hg", "status")
160 cmd.Dir = dir 178 cmd.Dir = dir
161 out, err = cmd.Output() 179 out, err = cmd.Output()
162 if err != nil || len(out) > 0 { 180 if err != nil || len(out) > 0 {
163 return false 181 return false
164 } 182 }
165 183
166 return true 184 return true
167 } 185 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b