-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtidy.go
executable file
·63 lines (60 loc) · 1.21 KB
/
tidy.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
package main
import (
"log"
"github.com/snowmerak/prew/pypi"
)
// tidyUpProject removes unused packages from the project.
func tidyUpProject(path string, yes bool) error {
used, unused, expected, err := getUnusedPackages(path)
if err != nil {
return err
}
used["pipdeptree"] = true
used["mypy"] = true
dep := map[string]bool{}
for k := range used {
ls, err := getDependencies(path, k)
if err != nil {
return err
}
for _, l := range ls {
dep[l] = true
}
}
for k := range unused {
if dep[k] {
delete(unused, k)
}
}
log.Println("Remove unused package")
for k := range unused {
if k == "pip" || k == "pipdeptree" || k == "mypy" {
continue
}
log.Println("remove: ", k)
if err := removePackage(k, yes); err != nil {
return err
}
}
log.Println("Install expected package")
for k := range expected {
_, err := pypi.GetPackageInfo(k, "")
if err != nil {
continue
}
log.Println("install: ", k)
if err := installPackage(k, ""); err != nil {
return err
}
}
spec, err := readSpecFromPath(path)
if err != nil {
return err
}
packages, err := getDependencyTreeFrom(path)
if err != nil {
return err
}
spec.Dependencies = packages
return writeSpecToPath(path, spec)
}