bpfix.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2017 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // This file provides a bpfix command-line library
  15. // TODO(jeffrygaston) should this file be consolidated with bpfmt.go?
  16. package cmd_lib
  17. import (
  18. "bytes"
  19. "flag"
  20. "fmt"
  21. "io"
  22. "io/ioutil"
  23. "os"
  24. "os/exec"
  25. "path/filepath"
  26. "github.com/google/blueprint/parser"
  27. "android/soong/bpfix/bpfix"
  28. )
  29. var (
  30. // main operation modes
  31. list = flag.Bool("l", false, "list files whose formatting differs from bpfmt's")
  32. write = flag.Bool("w", false, "write result to (source) file instead of stdout")
  33. doDiff = flag.Bool("d", false, "display diffs instead of rewriting files")
  34. )
  35. var (
  36. exitCode = 0
  37. )
  38. func report(err error) {
  39. fmt.Fprintln(os.Stderr, err)
  40. exitCode = 2
  41. }
  42. func openAndProcess(filename string, out io.Writer, fixRequest bpfix.FixRequest) error {
  43. f, err := os.Open(filename)
  44. if err != nil {
  45. return err
  46. }
  47. defer f.Close()
  48. return processFile(filename, f, out, fixRequest)
  49. }
  50. // If in == nil, the source is the contents of the file with the given filename.
  51. func processFile(filename string, in io.Reader, out io.Writer, fixRequest bpfix.FixRequest) error {
  52. // load the input file
  53. src, err := ioutil.ReadAll(in)
  54. if err != nil {
  55. return err
  56. }
  57. r := bytes.NewBuffer(append([]byte(nil), src...))
  58. file, errs := parser.Parse(filename, r, parser.NewScope(nil))
  59. if len(errs) > 0 {
  60. for _, err := range errs {
  61. fmt.Fprintln(os.Stderr, err)
  62. }
  63. return fmt.Errorf("%d parsing errors", len(errs))
  64. }
  65. // compute and apply any requested fixes
  66. fixer := bpfix.NewFixer(file)
  67. file, err = fixer.Fix(fixRequest)
  68. if err != nil {
  69. return err
  70. }
  71. // output the results
  72. res, err := parser.Print(file)
  73. if err != nil {
  74. return err
  75. }
  76. if !bytes.Equal(src, res) {
  77. // contents have changed
  78. if *list {
  79. fmt.Fprintln(out, filename)
  80. }
  81. if *write {
  82. err = ioutil.WriteFile(filename, res, 0644)
  83. if err != nil {
  84. return err
  85. }
  86. }
  87. if *doDiff {
  88. data, err := diff(src, res)
  89. if err != nil {
  90. return fmt.Errorf("computing diff: %s", err)
  91. }
  92. fmt.Printf("diff %s bpfix/%s\n", filename, filename)
  93. out.Write(data)
  94. }
  95. }
  96. if !*list && !*write && !*doDiff {
  97. _, err = out.Write(res)
  98. }
  99. return err
  100. }
  101. func makeFileVisitor(fixRequest bpfix.FixRequest) func(string, os.FileInfo, error) error {
  102. return func(path string, f os.FileInfo, err error) error {
  103. if err == nil && f.Name() == "Android.bp" {
  104. err = openAndProcess(path, os.Stdout, fixRequest)
  105. }
  106. if err != nil {
  107. report(err)
  108. }
  109. return nil
  110. }
  111. }
  112. func walkDir(path string, fixRequest bpfix.FixRequest) {
  113. filepath.Walk(path, makeFileVisitor(fixRequest))
  114. }
  115. func Run() {
  116. flag.Parse()
  117. fixRequest := bpfix.NewFixRequest().AddAll()
  118. if flag.NArg() == 0 {
  119. if *write {
  120. fmt.Fprintln(os.Stderr, "error: cannot use -w with standard input")
  121. exitCode = 2
  122. return
  123. }
  124. if err := processFile("<standard input>", os.Stdin, os.Stdout, fixRequest); err != nil {
  125. report(err)
  126. }
  127. return
  128. }
  129. for i := 0; i < flag.NArg(); i++ {
  130. path := flag.Arg(i)
  131. switch dir, err := os.Stat(path); {
  132. case err != nil:
  133. report(err)
  134. case dir.IsDir():
  135. walkDir(path, fixRequest)
  136. default:
  137. if err := openAndProcess(path, os.Stdout, fixRequest); err != nil {
  138. report(err)
  139. }
  140. }
  141. }
  142. }
  143. func diff(b1, b2 []byte) (data []byte, err error) {
  144. f1, err := ioutil.TempFile("", "bpfix")
  145. if err != nil {
  146. return
  147. }
  148. defer os.Remove(f1.Name())
  149. defer f1.Close()
  150. f2, err := ioutil.TempFile("", "bpfix")
  151. if err != nil {
  152. return
  153. }
  154. defer os.Remove(f2.Name())
  155. defer f2.Close()
  156. f1.Write(b1)
  157. f2.Write(b2)
  158. data, err = exec.Command("diff", "-u", f1.Name(), f2.Name()).CombinedOutput()
  159. if len(data) > 0 {
  160. // diff exits with a non-zero status when the files don't match.
  161. // Ignore that failure as long as we get output.
  162. err = nil
  163. }
  164. return
  165. }