-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathclean.go
More file actions
114 lines (95 loc) · 1.78 KB
/
Copy pathclean.go
File metadata and controls
114 lines (95 loc) · 1.78 KB
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
package main
import (
"errors"
"gopkg.in/cheggaaa/pb.v1"
"os"
"path/filepath"
)
var deletableExtensions = []string{
".aux",
".4ct",
".4tc",
".oc",
".md5",
".dpth",
".out",
".jax",
".idv",
".lg",
".tmp",
".xref",
".log",
".auxlock",
".dvi",
".pdf",
".html",
".dpth",
".ids",
".scmd",
".sout",
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func isDeletable(path string) bool {
return stringInSlice(filepath.Ext(path), deletableExtensions)
}
func RemoveBuiltFiles(pathname string) error {
if pathname == "" {
pathname = repository
} else {
pathname, err := filepath.Abs(pathname)
if err != nil {
return err
}
// This is unfortunately a deprecated function, but I don't
// know what the alternative is
if !(filepath.HasPrefix(pathname, repository)) {
return errors.New("The path to clean is not under the current repository.")
}
}
filenames, err := TexFilesInRepository(repository)
if err != nil {
return err
}
included := make(map[string]bool)
for _, filename := range filenames {
images, err := IncludedImages(filename)
if err == nil {
for _, image := range images {
included[image] = true
}
}
}
var toDelete []string
var visit = func(path string, f os.FileInfo, err error) error {
if isDeletable(path) {
if !included[path] {
toDelete = append(toDelete, path)
}
}
return nil
}
err = filepath.Walk(pathname, visit)
if err != nil {
return err
}
var bar *pb.ProgressBar
bar = pb.StartNew(len(toDelete))
bar.ShowTimeLeft = true
bar.Start()
for _, filename := range toDelete {
err := os.Remove(filename)
if err != nil {
return err
}
bar.Increment()
}
bar.FinishPrint("Cleaned " + pathname)
return nil
}