-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspaghetti.go
More file actions
191 lines (155 loc) · 3.75 KB
/
spaghetti.go
File metadata and controls
191 lines (155 loc) · 3.75 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"bufio"
"bytes"
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
)
type result struct {
path string
snippets string
err error
}
// Regexp pattern requires formatting with user input
var target_pattern = ".*%v\\(.*\\)|.*%v\\.delay|.*%v\\.apply_async"
// Compiled Regexps
var python_source_re = regexp.MustCompile(`[a-z]*\.py$`)
var decorator_re = regexp.MustCompile(`^@`)
var function_def_re = regexp.MustCompile(`^\s*def\s.+:$`)
// Declaring command-line flags
var exclude_patterns string
func init() {
flag.StringVar(&exclude_patterns, "exclude_patterns", "", "A comma-separated list of files to be exlcuded")
}
func walkFiles(done <-chan struct{}, root string, exclude_patterns string) (<-chan string, <-chan error) {
paths := make(chan string)
errc := make(chan error, 1)
go func() {
defer close(paths)
errc <- filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Checking to see if the file ends in .py
is_python_source := python_source_re.MatchString(path)
if err != nil {
return err
}
if !is_python_source {
return nil
}
if len(exclude_patterns) > 0 {
for _, pattern := range strings.Split(exclude_patterns, ",") {
if strings.Contains(path, pattern) {
return nil
}
}
}
select {
case paths <- path:
case <-done:
return errors.New("Walk canceled")
}
return nil
})
}()
return paths, errc
}
func searcher(done <-chan struct{}, target_re *regexp.Regexp, paths <-chan string, results chan<- result) {
for path := range paths {
f, err := os.Open(path)
var snippets_buffer bytes.Buffer
var buffer bytes.Buffer
scanner := bufio.NewScanner(f)
is_target_stub := false
for scanner.Scan() {
line := scanner.Text()
is_decorated := decorator_re.MatchString(line)
is_def := function_def_re.MatchString(line)
if is_def || is_decorated {
if is_target_stub {
is_target_stub = false
snippets_buffer.WriteString(buffer.String())
}
buffer.Reset()
}
is_target_call := target_re.MatchString(line)
// When true, we'll add this stub to snippets when we hit the next def
if is_target_call {
is_target_stub = true
}
buffer.WriteString(line)
buffer.WriteString("\n")
}
if is_target_stub {
snippets_buffer.WriteString(buffer.String())
buffer.Reset()
}
snippets := snippets_buffer.String()
if len(snippets) == 0 {
continue
}
result := result{
path,
snippets,
err,
}
select {
case results <- result:
case <-done:
return
}
}
}
func main() {
runtime.GOMAXPROCS(2)
flag.Parse()
args := flag.Args()
// TODO: This arg is required, validate that shit
var target string
if len(args) > 0 {
target = args[0]
}
formatted_target_pattern := fmt.Sprintf(target_pattern, target)
target_re := regexp.MustCompile(formatted_target_pattern)
pwd, _ := os.Getwd()
// Set up channel to alert searchers we're done
done := make(chan struct{})
defer close(done)
paths, errc := walkFiles(done, pwd, exclude_patterns)
results := make(chan result)
var wait_group sync.WaitGroup
// It's possible that spreading out this work across goroutines inherently isn't performant, but it's also possible that I'm doing this wrong
// So 1 for now!
const numSearchers = 8
wait_group.Add(numSearchers)
for i := 0; i < numSearchers; i++ {
go func() {
searcher(done, target_re, paths, results)
wait_group.Done()
}()
}
go func() {
wait_group.Wait()
close(results)
}()
for result := range results {
if result.err != nil {
fmt.Println(result.err)
return
}
fmt.Println(result.path)
fmt.Println(result.snippets)
}
if err := <-errc; err != nil {
fmt.Println(err)
return
}
}