forked from unixpickle/gobfuscate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gopath_copy.go
144 lines (128 loc) · 2.81 KB
/
gopath_copy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"fmt"
"go/build"
"io"
"os"
"path/filepath"
"strings"
"golang.org/x/tools/refactor/importgraph"
)
// CopyGopath creates a new Gopath with a copy of a package
// and all of its dependencies.
func CopyGopath(packageName, newGopath string, keepTests bool) error {
ctx := build.Default
rootPkg, err := ctx.Import(packageName, "", 0)
if err != nil {
return err
}
allDeps, err := findDeps(packageName, &ctx)
if err != nil {
return err
}
for dep := range allDeps {
pkg, err := build.Default.Import(dep, rootPkg.Dir, 0)
if err != nil {
return err
}
if pkg.Goroot {
continue
}
if err := copyDep(pkg, newGopath, keepTests); err != nil {
return err
}
}
if !keepTests {
ctx.GOPATH = newGopath
allDeps, err = findDeps(packageName, &ctx)
if err != nil {
return err
}
}
if err := removeUnusedPkgs(newGopath, allDeps); err != nil {
return err
}
return nil
}
func findDeps(packageName string, ctx *build.Context) (map[string]bool, error) {
forward, _, errs := importgraph.Build(ctx)
if _, ok := forward[packageName]; !ok {
if err, ok := errs[packageName]; ok {
return nil, err
}
return nil, fmt.Errorf("package %s not found", packageName)
}
return forward.Search(packageName), nil
}
func copyDep(pkg *build.Package, newGopath string, keepTests bool) error {
newPath := filepath.Join(newGopath, "src", pkg.ImportPath)
err := os.MkdirAll(newPath, 0755)
if err != nil {
return err
}
srcFiles := [][]string{
pkg.GoFiles,
pkg.CgoFiles,
pkg.CFiles,
pkg.CXXFiles,
pkg.MFiles,
pkg.HFiles,
pkg.FFiles,
pkg.SFiles,
pkg.SwigFiles,
pkg.SwigCXXFiles,
pkg.SysoFiles,
}
if keepTests {
srcFiles = append(srcFiles, pkg.TestGoFiles, pkg.XTestGoFiles)
}
for _, list := range srcFiles {
for _, file := range list {
src := filepath.Join(pkg.Dir, file)
dst := filepath.Join(newPath, file)
if err := copyFile(src, dst); err != nil {
return err
}
}
}
return nil
}
func removeUnusedPkgs(gopath string, deps map[string]bool) error {
srcDir := filepath.Join(gopath, "src")
return filepath.Walk(srcDir, func(sub string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
return nil
}
if !containsDep(gopath, sub, deps) {
os.RemoveAll(sub)
return filepath.SkipDir
}
return nil
})
}
func containsDep(gopath, dir string, deps map[string]bool) bool {
for dep := range deps {
depDir := filepath.Clean(filepath.Join(gopath, "src", dep))
if strings.HasPrefix(depDir, filepath.Clean(dir)) {
return true
}
}
return false
}
func copyFile(src, dest string) error {
newFile, err := os.Create(dest)
if err != nil {
return err
}
defer newFile.Close()
oldFile, err := os.Open(src)
if err != nil {
return err
}
defer oldFile.Close()
_, err = io.Copy(newFile, oldFile)
return err
}