forked from centreon/centreon-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deps.go
117 lines (106 loc) · 2.67 KB
/
deps.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
// MaxDepth Max depth in search tree
const MaxDepth = 3
func findIncludes(file string, treated *[]string, edges *[]string, depth int) {
var myList []string
file1 := file
f, err := os.Open(file1)
if err != nil {
file1 = strings.TrimPrefix(file, "inc/")
for _, pref := range []string{
"/usr/local/include/",
"inc/",
"modules/external_commands/inc/" } {
f, err = os.Open(pref + file1)
if err == nil {
file1 = pref + file1
*treated = append(*treated, file1)
break
}
}
} else {
*treated = append(*treated, file1)
}
defer f.Close()
depth++
if depth > MaxDepth {
return
}
scanner := bufio.NewScanner(f)
r, _ := regexp.Compile("^#\\s*include\\s*([<\"])(.*)[>\"]")
for scanner.Scan() {
line := scanner.Text()
match := r.FindStringSubmatch(line)
if len(match) > 0 {
/* match[0] is the global match, match[1] is '<' or '"' and match[2] is the file to include */
if match[1] == "\"" {
*edges = append(*edges, fmt.Sprintf(" \"%s\" -> \"%s\";\n", file, match[2]))
myList = append(myList, match[2])
} else {
*edges = append(*edges, fmt.Sprintf(" \"%s\" -> \"%s\";\n", file, match[2]))
}
}
}
if err := scanner.Err(); err != nil {
log.Print(file, " --- ", err)
}
for _, file2 := range myList {
found := false
for _, ff := range *treated {
if ff == file2 {
found = true
break
}
}
if !found {
findIncludes(file2, treated, edges, depth)
}
}
}
func unique(edges []string) []string {
keys := make(map[string]bool)
list := []string{}
for _, entry := range edges {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
func main() {
args := os.Args[1:]
var fileList []string
var edges []string
if len(args) == 0 {
for _, searchDir := range []string{"src", "inc"} {
filepath.Walk(searchDir, func(path string, f os.FileInfo, err error) error {
if strings.HasSuffix(path, ".cc") || strings.HasSuffix(path, ".hh") {
fileList = append(fileList, path)
}
return nil
})
}
} else {
fileList = append(fileList, args[0])
}
fmt.Println("digraph deps {")
var treated []string
for _, file := range fileList {
findIncludes(file, &treated, &edges, 0)
}
edges = unique(edges)
for _, l := range edges {
fmt.Println(l)
}
fmt.Println("}")
}