github.com/RobustRoundRobin/quorum@v20.10.0+incompatible/internal/build/util.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package build 18 19 import ( 20 "bytes" 21 "flag" 22 "fmt" 23 "io" 24 "io/ioutil" 25 "log" 26 "os" 27 "os/exec" 28 "path" 29 "path/filepath" 30 "runtime" 31 "strings" 32 "text/template" 33 ) 34 35 var DryRunFlag = flag.Bool("n", false, "dry run, don't execute commands") 36 37 // MustRun executes the given command and exits the host process for 38 // any error. 39 func MustRun(cmd *exec.Cmd) { 40 fmt.Println(">>>", strings.Join(cmd.Args, " ")) 41 if !*DryRunFlag { 42 cmd.Stderr = os.Stderr 43 cmd.Stdout = os.Stdout 44 if err := cmd.Run(); err != nil { 45 log.Fatal(err) 46 } 47 } 48 } 49 50 func MustRunCommand(cmd string, args ...string) { 51 MustRun(exec.Command(cmd, args...)) 52 } 53 54 // GOPATH returns the value that the GOPATH environment 55 // variable should be set to. 56 func GOPATH() string { 57 if os.Getenv("GOPATH") == "" { 58 log.Fatal("GOPATH is not set") 59 } 60 return os.Getenv("GOPATH") 61 } 62 63 var warnedAboutGit bool 64 65 // RunGit runs a git subcommand and returns its output. 66 // The command must complete successfully. 67 func RunGit(args ...string) string { 68 cmd := exec.Command("git", args...) 69 var stdout, stderr bytes.Buffer 70 cmd.Stdout, cmd.Stderr = &stdout, &stderr 71 if err := cmd.Run(); err != nil { 72 if e, ok := err.(*exec.Error); ok && e.Err == exec.ErrNotFound { 73 if !warnedAboutGit { 74 log.Println("Warning: can't find 'git' in PATH") 75 warnedAboutGit = true 76 } 77 return "" 78 } 79 log.Fatal(strings.Join(cmd.Args, " "), ": ", err, "\n", stderr.String()) 80 } 81 return strings.TrimSpace(stdout.String()) 82 } 83 84 // readGitFile returns content of file in .git directory. 85 func readGitFile(file string) string { 86 content, err := ioutil.ReadFile(path.Join(".git", file)) 87 if err != nil { 88 return "" 89 } 90 return strings.TrimSpace(string(content)) 91 } 92 93 // Render renders the given template file into outputFile. 94 func Render(templateFile, outputFile string, outputPerm os.FileMode, x interface{}) { 95 tpl := template.Must(template.ParseFiles(templateFile)) 96 render(tpl, outputFile, outputPerm, x) 97 } 98 99 // RenderString renders the given template string into outputFile. 100 func RenderString(templateContent, outputFile string, outputPerm os.FileMode, x interface{}) { 101 tpl := template.Must(template.New("").Parse(templateContent)) 102 render(tpl, outputFile, outputPerm, x) 103 } 104 105 func render(tpl *template.Template, outputFile string, outputPerm os.FileMode, x interface{}) { 106 if err := os.MkdirAll(filepath.Dir(outputFile), 0755); err != nil { 107 log.Fatal(err) 108 } 109 out, err := os.OpenFile(outputFile, os.O_CREATE|os.O_WRONLY|os.O_EXCL, outputPerm) 110 if err != nil { 111 log.Fatal(err) 112 } 113 if err := tpl.Execute(out, x); err != nil { 114 log.Fatal(err) 115 } 116 if err := out.Close(); err != nil { 117 log.Fatal(err) 118 } 119 } 120 121 // CopyFile copies a file. 122 func CopyFile(dst, src string, mode os.FileMode) { 123 if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil { 124 log.Fatal(err) 125 } 126 destFile, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, mode) 127 if err != nil { 128 log.Fatal(err) 129 } 130 defer destFile.Close() 131 132 srcFile, err := os.Open(src) 133 if err != nil { 134 log.Fatal(err) 135 } 136 defer srcFile.Close() 137 138 if _, err := io.Copy(destFile, srcFile); err != nil { 139 log.Fatal(err) 140 } 141 } 142 143 // GoTool returns the command that runs a go tool. This uses go from GOROOT instead of PATH 144 // so that go commands executed by build use the same version of Go as the 'host' that runs 145 // build code. e.g. 146 // 147 // /usr/lib/go-1.12.1/bin/go run build/ci.go ... 148 // 149 // runs using go 1.12.1 and invokes go 1.12.1 tools from the same GOROOT. This is also important 150 // because runtime.Version checks on the host should match the tools that are run. 151 func GoTool(tool string, args ...string) *exec.Cmd { 152 args = append([]string{tool}, args...) 153 return exec.Command(filepath.Join(runtime.GOROOT(), "bin", "go"), args...) 154 } 155 156 // UploadSFTP uploads files to a remote host using the sftp command line tool. 157 // The destination host may be specified either as [user@]host[: or as a URI in 158 // the form sftp://[user@]host[:port]. 159 func UploadSFTP(identityFile, host, dir string, files []string) error { 160 sftp := exec.Command("sftp") 161 sftp.Stdout = nil 162 sftp.Stderr = os.Stderr 163 if identityFile != "" { 164 sftp.Args = append(sftp.Args, "-i", identityFile) 165 } 166 sftp.Args = append(sftp.Args, host) 167 fmt.Println(">>>", strings.Join(sftp.Args, " ")) 168 if *DryRunFlag { 169 return nil 170 } 171 172 stdin, err := sftp.StdinPipe() 173 if err != nil { 174 return fmt.Errorf("can't create stdin pipe for sftp: %v", err) 175 } 176 if err := sftp.Start(); err != nil { 177 return err 178 } 179 in := io.MultiWriter(stdin, os.Stdout) 180 for _, f := range files { 181 fmt.Fprintln(in, "put", f, path.Join(dir, filepath.Base(f))) 182 } 183 stdin.Close() 184 return sftp.Wait() 185 } 186 187 // ExpandPackagesNoVendor expands a cmd/go import path pattern, skipping 188 // vendored packages. 189 func ExpandPackagesNoVendor(patterns []string) []string { 190 expand := false 191 for _, pkg := range patterns { 192 if strings.Contains(pkg, "...") { 193 expand = true 194 } 195 } 196 if expand { 197 cmd := GoTool("list", patterns...) 198 out, err := cmd.CombinedOutput() 199 if err != nil { 200 log.Fatalf("package listing failed: %v\n%s", err, string(out)) 201 } 202 var packages []string 203 for _, line := range strings.Split(string(out), "\n") { 204 if !strings.Contains(line, "/vendor/") { 205 packages = append(packages, strings.TrimSpace(line)) 206 } 207 } 208 return packages 209 } 210 return patterns 211 } 212 213 // Read QUORUM_IGNORE_TEST_PACKAGES env and remove from packages 214 func IgnorePackages(packages []string) []string { 215 ignore := os.Getenv("QUORUM_IGNORE_TEST_PACKAGES") 216 if ignore == "" { 217 return packages 218 } 219 ret := make([]string, 0, len(packages)) 220 ignorePackages := strings.Split(ignore, ",") 221 222 for _, p := range packages { 223 mustInclude := true 224 for _, ig := range ignorePackages { 225 if strings.Index(p, strings.TrimSpace(ig)) == 0 { 226 mustInclude = false 227 break 228 } 229 } 230 if mustInclude { 231 ret = append(ret, p) 232 } 233 } 234 return ret 235 }