util.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. package build
  17. import (
  18. "bytes"
  19. "flag"
  20. "fmt"
  21. "go/parser"
  22. "go/token"
  23. "io"
  24. "io/ioutil"
  25. "log"
  26. "os"
  27. "os/exec"
  28. "path"
  29. "path/filepath"
  30. "strings"
  31. "text/template"
  32. )
  33. var DryRunFlag = flag.Bool("n", false, "dry run, don't execute commands")
  34. // MustRun executes the given command and exits the host process for
  35. // any error.
  36. func MustRun(cmd *exec.Cmd) {
  37. fmt.Println(">>>", strings.Join(cmd.Args, " "))
  38. if !*DryRunFlag {
  39. cmd.Stderr = os.Stderr
  40. cmd.Stdout = os.Stdout
  41. if err := cmd.Run(); err != nil {
  42. log.Fatal(err)
  43. }
  44. }
  45. }
  46. func MustRunCommand(cmd string, args ...string) {
  47. MustRun(exec.Command(cmd, args...))
  48. }
  49. var warnedAboutGit bool
  50. // RunGit runs a git subcommand and returns its output.
  51. // The command must complete successfully.
  52. func RunGit(args ...string) string {
  53. cmd := exec.Command("git", args...)
  54. var stdout, stderr bytes.Buffer
  55. cmd.Stdout, cmd.Stderr = &stdout, &stderr
  56. if err := cmd.Run(); err != nil {
  57. if e, ok := err.(*exec.Error); ok && e.Err == exec.ErrNotFound {
  58. if !warnedAboutGit {
  59. log.Println("Warning: can't find 'git' in PATH")
  60. warnedAboutGit = true
  61. }
  62. return ""
  63. }
  64. log.Fatal(strings.Join(cmd.Args, " "), ": ", err, "\n", stderr.String())
  65. }
  66. return strings.TrimSpace(stdout.String())
  67. }
  68. // readGitFile returns content of file in .git directory.
  69. func readGitFile(file string) string {
  70. content, err := ioutil.ReadFile(path.Join(".git", file))
  71. if err != nil {
  72. return ""
  73. }
  74. return strings.TrimSpace(string(content))
  75. }
  76. // Render renders the given template file into outputFile.
  77. func Render(templateFile, outputFile string, outputPerm os.FileMode, x interface{}) {
  78. tpl := template.Must(template.ParseFiles(templateFile))
  79. render(tpl, outputFile, outputPerm, x)
  80. }
  81. // RenderString renders the given template string into outputFile.
  82. func RenderString(templateContent, outputFile string, outputPerm os.FileMode, x interface{}) {
  83. tpl := template.Must(template.New("").Parse(templateContent))
  84. render(tpl, outputFile, outputPerm, x)
  85. }
  86. func render(tpl *template.Template, outputFile string, outputPerm os.FileMode, x interface{}) {
  87. if err := os.MkdirAll(filepath.Dir(outputFile), 0755); err != nil {
  88. log.Fatal(err)
  89. }
  90. out, err := os.OpenFile(outputFile, os.O_CREATE|os.O_WRONLY|os.O_EXCL, outputPerm)
  91. if err != nil {
  92. log.Fatal(err)
  93. }
  94. if err := tpl.Execute(out, x); err != nil {
  95. log.Fatal(err)
  96. }
  97. if err := out.Close(); err != nil {
  98. log.Fatal(err)
  99. }
  100. }
  101. // UploadSFTP uploads files to a remote host using the sftp command line tool.
  102. // The destination host may be specified either as [user@]host[: or as a URI in
  103. // the form sftp://[user@]host[:port].
  104. func UploadSFTP(identityFile, host, dir string, files []string) error {
  105. sftp := exec.Command("sftp")
  106. sftp.Stdout = nil
  107. sftp.Stderr = os.Stderr
  108. if identityFile != "" {
  109. sftp.Args = append(sftp.Args, "-i", identityFile)
  110. }
  111. sftp.Args = append(sftp.Args, host)
  112. fmt.Println(">>>", strings.Join(sftp.Args, " "))
  113. if *DryRunFlag {
  114. return nil
  115. }
  116. stdin, err := sftp.StdinPipe()
  117. if err != nil {
  118. return fmt.Errorf("can't create stdin pipe for sftp: %v", err)
  119. }
  120. if err := sftp.Start(); err != nil {
  121. return err
  122. }
  123. in := io.MultiWriter(stdin, os.Stdout)
  124. for _, f := range files {
  125. fmt.Fprintln(in, "put", f, path.Join(dir, filepath.Base(f)))
  126. }
  127. stdin.Close()
  128. return sftp.Wait()
  129. }
  130. // FindMainPackages finds all 'main' packages in the given directory and returns their
  131. // package paths.
  132. func FindMainPackages(dir string) []string {
  133. var commands []string
  134. cmds, err := ioutil.ReadDir(dir)
  135. if err != nil {
  136. log.Fatal(err)
  137. }
  138. for _, cmd := range cmds {
  139. pkgdir := filepath.Join(dir, cmd.Name())
  140. pkgs, err := parser.ParseDir(token.NewFileSet(), pkgdir, nil, parser.PackageClauseOnly)
  141. if err != nil {
  142. log.Fatal(err)
  143. }
  144. for name := range pkgs {
  145. if name == "main" {
  146. path := "./" + filepath.ToSlash(pkgdir)
  147. commands = append(commands, path)
  148. break
  149. }
  150. }
  151. }
  152. return commands
  153. }
  154. // ExpandPackagesNoVendor expands a cmd/go import path pattern, skipping
  155. // vendored packages.
  156. func ExpandPackagesNoVendor(tc *GoToolchain, patterns []string) []string {
  157. expand := false
  158. for _, pkg := range patterns {
  159. if strings.Contains(pkg, "...") {
  160. expand = true
  161. }
  162. }
  163. if expand {
  164. cmd := tc.goTool("list", patterns...)
  165. out, err := cmd.CombinedOutput()
  166. if err != nil {
  167. log.Fatalf("package listing failed: %v\n%s", err, string(out))
  168. }
  169. var packages []string
  170. for _, line := range strings.Split(string(out), "\n") {
  171. if !strings.Contains(line, "/vendor/") && !strings.Contains(line, "go: downloading") { // Quorum
  172. packages = append(packages, strings.TrimSpace(line))
  173. }
  174. }
  175. return packages
  176. }
  177. return patterns
  178. }
  179. // Read QUORUM_IGNORE_TEST_PACKAGES env and remove from packages
  180. func IgnorePackages(packages []string) []string {
  181. ignore := os.Getenv("QUORUM_IGNORE_TEST_PACKAGES")
  182. if ignore == "" {
  183. return packages
  184. }
  185. ret := make([]string, 0, len(packages))
  186. ignorePackages := strings.Split(ignore, ",")
  187. for _, p := range packages {
  188. mustInclude := true
  189. for _, ig := range ignorePackages {
  190. if strings.Index(p, strings.TrimSpace(ig)) == 0 {
  191. mustInclude = false
  192. break
  193. }
  194. }
  195. if mustInclude {
  196. ret = append(ret, p)
  197. }
  198. }
  199. return ret
  200. }