gopkg.in/tools/godep.v46@v46.0.0-20160104045137-2daafc448812/godepfile.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io" 7 "log" 8 "os" 9 "path/filepath" 10 ) 11 12 var ( 13 godepsFile = filepath.Join("Godeps", "Godeps.json") 14 oldGodepsFile = filepath.Join("Godeps") 15 ) 16 17 // Godeps describes what a package needs to be rebuilt reproducibly. 18 // It's the same information stored in file Godeps. 19 type Godeps struct { 20 ImportPath string 21 GoVersion string 22 Packages []string `json:",omitempty"` // Arguments to save, if any. 23 Deps []Dependency 24 isOldFile bool 25 } 26 27 func createGodepsFile() (*os.File, error) { 28 return os.Create(godepsFile) 29 } 30 31 func loadGodepsFile(path string) (Godeps, error) { 32 var g Godeps 33 f, err := os.Open(path) 34 if err != nil { 35 return g, err 36 } 37 defer f.Close() 38 err = json.NewDecoder(f).Decode(&g) 39 if err != nil { 40 err = fmt.Errorf("Unable to parse %s: %s", path, err.Error()) 41 } 42 return g, err 43 } 44 45 func loadDefaultGodepsFile() (Godeps, error) { 46 var g Godeps 47 var err error 48 g, err = loadGodepsFile(godepsFile) 49 if err != nil { 50 if os.IsNotExist(err) { 51 var err1 error 52 g, err1 = loadGodepsFile(oldGodepsFile) 53 if err1 != nil { 54 if os.IsNotExist(err1) { 55 return g, err 56 } 57 return g, err1 58 } 59 g.isOldFile = true 60 return g, nil 61 } 62 } 63 return g, err 64 } 65 66 // pkgs is the list of packages to read dependencies for 67 func (g *Godeps) fill(pkgs []*Package, destImportPath string) error { 68 debugln("fill", destImportPath) 69 ppln(pkgs) 70 var err1 error 71 var path, testImports []string 72 for _, p := range pkgs { 73 if p.Standard { 74 log.Println("ignoring stdlib package:", p.ImportPath) 75 continue 76 } 77 if p.Error.Err != "" { 78 log.Println(p.Error.Err) 79 err1 = errorLoadingPackages 80 continue 81 } 82 path = append(path, p.ImportPath) 83 path = append(path, p.Deps...) 84 testImports = append(testImports, p.TestImports...) 85 testImports = append(testImports, p.XTestImports...) 86 } 87 ps, err := LoadPackages(testImports...) 88 if err != nil { 89 return err 90 } 91 for _, p := range ps { 92 if p.Standard { 93 continue 94 } 95 if p.Error.Err != "" { 96 log.Println(p.Error.Err) 97 err1 = errorLoadingPackages 98 continue 99 } 100 path = append(path, p.ImportPath) 101 path = append(path, p.Deps...) 102 } 103 debugln("path", path) 104 for i, p := range path { 105 path[i] = unqualify(p) 106 } 107 path = uniq(path) 108 debugln("uniq, unqualify'd path", path) 109 ps, err = LoadPackages(path...) 110 if err != nil { 111 return err 112 } 113 seen := []string{destImportPath} 114 for _, pkg := range ps { 115 if pkg.Error.Err != "" { 116 log.Println(pkg.Error.Err) 117 err1 = errorLoadingDeps 118 continue 119 } 120 if pkg.Standard || containsPathPrefix(seen, pkg.ImportPath) { 121 continue 122 } 123 seen = append(seen, pkg.ImportPath) 124 vcs, reporoot, err := VCSFromDir(pkg.Dir, filepath.Join(pkg.Root, "src")) 125 if err != nil { 126 log.Println(err) 127 err1 = errorLoadingDeps 128 continue 129 } 130 id, err := vcs.identify(pkg.Dir) 131 if err != nil { 132 log.Println(err) 133 err1 = errorLoadingDeps 134 continue 135 } 136 if vcs.isDirty(pkg.Dir, id) { 137 log.Println("dirty working tree (please commit changes):", pkg.Dir) 138 err1 = errorLoadingDeps 139 continue 140 } 141 comment := vcs.describe(pkg.Dir, id) 142 g.Deps = append(g.Deps, Dependency{ 143 ImportPath: pkg.ImportPath, 144 Rev: id, 145 Comment: comment, 146 dir: pkg.Dir, 147 ws: pkg.Root, 148 root: filepath.ToSlash(reporoot), 149 vcs: vcs, 150 }) 151 } 152 return err1 153 } 154 155 func (g *Godeps) copy() *Godeps { 156 h := *g 157 h.Deps = make([]Dependency, len(g.Deps)) 158 copy(h.Deps, g.Deps) 159 return &h 160 } 161 162 func (g *Godeps) file() string { 163 if g.isOldFile { 164 return oldGodepsFile 165 } 166 return godepsFile 167 } 168 169 func (g *Godeps) save() (int64, error) { 170 f, err := os.Create(g.file()) 171 if err != nil { 172 return 0, err 173 } 174 defer f.Close() 175 return g.writeTo(f) 176 } 177 178 func (g *Godeps) writeTo(w io.Writer) (int64, error) { 179 b, err := json.MarshalIndent(g, "", "\t") 180 if err != nil { 181 return 0, err 182 } 183 n, err := w.Write(append(b, '\n')) 184 return int64(n), err 185 }