forked from lhcb-org/lbpkr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_dep_graph.go
146 lines (128 loc) · 3.32 KB
/
cmd_dep_graph.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"fmt"
"io/ioutil"
"strconv"
graph "code.google.com/p/gographviz"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
"github.com/lhcb-org/lbpkr/yum"
)
func lbpkr_make_cmd_dep_graph() *commander.Command {
cmd := &commander.Command{
Run: lbpkr_run_cmd_dep_graph,
UsageLine: "dep-graph [options] [<name-pattern> [<version-pattern> [<release-pattern>]]]",
Short: "dump the DOT graph of installed RPM packages [<name-pattern> [<version-pattern> [<release-pattern>]]]",
Long: `
dep-graph dumps the DAG of the installed RPM package(s) satisfying [<name-pattern> [<version-pattern> [<release-pattern>]]].
ex:
$ lbpkr dep-graph -o graph.dot
$ lbpkr dep-graph GAUDI
`,
Flag: *flag.NewFlagSet("lbpkr-dep-graph", flag.ExitOnError),
}
add_default_options(cmd)
cmd.Flag.String("o", "graph.dot", "generate a DOT file holding the dependency graph")
cmd.Flag.Int("rec-lvl", 1, "recursive-level (-1 to display all the graph)")
return cmd
}
func lbpkr_run_cmd_dep_graph(cmd *commander.Command, args []string) error {
var err error
siteroot := cmd.Flag.Lookup("siteroot").Value.Get().(string)
cfgtype := cmd.Flag.Lookup("type").Value.Get().(string)
debug := cmd.Flag.Lookup("v").Value.Get().(bool)
dotfname := cmd.Flag.Lookup("o").Value.Get().(string)
reclvl := cmd.Flag.Lookup("rec-lvl").Value.Get().(int)
name := ""
vers := ""
release := ""
switch len(args) {
case 0:
name = ""
case 1:
name = args[0]
case 2:
name = args[0]
vers = args[1]
case 3:
name = args[0]
vers = args[1]
release = args[2]
default:
cmd.Usage()
return fmt.Errorf("lbpkr: invalid number of arguments. expected n=0|1|2|3. got=%d (%v)",
len(args),
args,
)
}
cfg := NewConfig(cfgtype, siteroot)
ctx, err := New(cfg, debug)
if err != nil {
return err
}
defer ctx.Close()
g := graph.NewGraph()
g.SetName("rpms")
g.SetDir(true)
decorate := func(pkg yum.RPM) map[string]string {
return map[string]string{
"name": strconv.Quote(pkg.Name()),
"version": strconv.Quote(pkg.Version()),
"release": strconv.Quote(pkg.Release()),
"epoch": strconv.Quote(pkg.Epoch()),
}
}
pkgs, err := ctx.ListInstalledPackages(name, vers, release)
if err != nil {
return err
}
str_in_slice := func(str string, slice []string) bool {
for _, v := range slice {
if str == v {
return true
}
}
return false
}
var process func(pkg *yum.Package, lvl int) error
process = func(pkg *yum.Package, lvl int) error {
var err error
root := strconv.Quote(pkg.ID())
g.AddNode("rpms", root, decorate(pkg))
reqs := pkg.Requires()
for _, req := range reqs {
if str_in_slice(req.Name(), yum.IGNORED_PACKAGES) {
continue
}
dep, err := ctx.Client().FindLatestMatchingRequire(req)
if err != nil {
ctx.msg.Infof("no package providing name=%q version=%q release=%q\n",
req.Name(),
req.Version(),
req.Release(),
)
continue
}
g.AddNode("rpms", strconv.Quote(dep.ID()), decorate(dep))
g.AddEdge(root, strconv.Quote(dep.ID()), true, nil)
if lvl < reclvl || reclvl < 0 {
err = process(dep, lvl+1)
if err != nil {
return err
}
}
}
return err
}
for _, pkg := range pkgs {
err = process(pkg, 1)
if err != nil {
return err
}
}
err = ioutil.WriteFile(dotfname, []byte(g.String()), 0644)
if err != nil {
return err
}
return err
}