-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdependency.go
132 lines (119 loc) · 2.91 KB
/
dependency.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
package main
import (
"encoding/json"
"os"
"os/exec"
"strings"
)
// getDependencyTreeFrom returns the dependency tree of the current directory.
func getDependencyTreeFrom(_ string) ([]PipPackage, error) {
if checkPackage("pipdeptree", "") == NotExist {
if err := installPackage("pipdeptree", ""); err != nil {
return nil, err
}
}
data, err := exec.Command(vpython, "-m", "pipdeptree", "--json-tree").Output()
if err != nil {
return nil, err
}
p := new([]PipPackage)
*p = []PipPackage{}
if err := json.Unmarshal(data, p); err != nil {
return nil, err
}
return *p, nil
}
// getDependencies returns the dependencies of the current directory.
func getDependencies(path string, name string) ([]string, error) {
spec, err := readSpecFromPath(path)
if err != nil {
return nil, err
}
c := map[string]bool{
name: true,
}
r := map[string]bool{}
al := make([]PipPackage, len(spec.Dependencies))
copy(al, spec.Dependencies)
for len(al) > 0 {
p := al[0]
al = al[1:]
if c[p.PackageName] {
for _, d := range p.Dependencies {
if !c[d.PackageName] {
c[d.PackageName] = true
if strings.Contains(d.PackageName, "_") {
c[strings.ReplaceAll(d.PackageName, "_", "-")] = true
}
if strings.Contains(d.PackageName, "-") {
c[strings.ReplaceAll(d.PackageName, "-", "_")] = true
}
}
}
} else {
for _, d := range p.Dependencies {
r[d.PackageName] = true
if strings.Contains(d.PackageName, "_") {
r[strings.ReplaceAll(d.PackageName, "_", "-")] = true
}
if strings.Contains(d.PackageName, "-") {
r[strings.ReplaceAll(d.PackageName, "-", "_")] = true
}
}
}
al = append(al, p.Dependencies...)
}
for k := range r {
delete(c, k)
}
deps := make([]string, 0, len(c))
for k := range c {
deps = append(deps, k)
}
return deps, nil
}
// addDependencyToSpec adds dependencies to the current directory's spec.yaml.
func appendDependencyToSpec(spec *Spec, name, version string) error {
path, err := os.Getwd()
if err != nil {
return err
}
if e := checkPackage(name, version); e != NotExist {
if err := removePackage(name, true); err != nil {
return err
}
}
if err := installPackage(name, version); err != nil {
return err
}
packages, err := getDependencyTreeFrom(path)
if err != nil {
return err
}
spec.Dependencies = packages
return nil
}
// subductDependencyFromSpec removes dependencies from the current directory's spec.yaml.
func subductDependencyFromSpec(spec *Spec, name string, yes bool, dep bool) error {
target := []string{name}
if dep {
deps, err := getDependencies(".", name)
if err != nil {
return err
}
target = append(target, deps...)
}
for _, t := range target {
if e := checkPackage(t, ""); e != NotExist {
if err := removePackage(t, yes); err != nil {
return err
}
}
}
packages, err := getDependencyTreeFrom(".")
if err != nil {
return err
}
spec.Dependencies = packages
return nil
}