vpac / src / deptree.v
246 lines · 231 sloc · 6.7 KB · 3609dfbd82867dc1034a443f8f9ced529ddbbedd
Raw
1module main
2
3import alpm
4import v.profiler
5
6// DepTracer walks the dependency graph straight from libalpm: the local db
7// answers for installed packages and the sync dbs for everything else, so a
8// package that is not installed can be traced too. the sync dbs are the
9// lookup's: set up, and asked about their freshness, by the first target or
10// dependency the installed packages do not answer, so a tree that stays
11// among them never touches the sync dbs
12struct DepTracer {
13mut:
14 handle &alpm.Handle
15 ui &TransUI
16 lookup &InfoLookup
17 installed &alpm.List
18 sync_lost bool
19 reverse bool
20 optional bool
21 max_depth int
22 seen map[string]bool
23}
24
25// TreeNode is one node of the tree: a resolved package, or a dependency that
26// nothing provides (pkg is nil then, and dep is how it was asked for). tag
27// marks an optional dependency
28struct TreeNode {
29 pkg &alpm.Package = unsafe { nil }
30 dep string
31 tag string
32}
33
34// TreeBranch is a node with the branches below it, as the tracer collects
35// them. a package already expanded higher up is seen, and is not expanded
36// again, which also stops cycles
37struct TreeBranch {
38 node TreeNode
39 seen bool
40 children []TreeBranch
41}
42
43// tree_label is the name and version, then the size: what the package takes
44// on disk when installed, or what it downloads when it is only in the repos
45fn tree_label(pkg &alpm.Package) string {
46 label := '${name_style(pkg.get_name())} ${version_style(pkg.get_version())}'
47 if pkg.get_origin() == .syncdb {
48 return '${label} (${profiler.format_bytes(u64(pkg.download_size()))} to download)'
49 }
50 return '${label} (${profiler.format_bytes(u64(pkg.get_isize()))})'
51}
52
53// node_label is the line a node draws: the package's label, or the
54// dependency nothing provides
55fn node_label(node TreeNode) string {
56 if node.pkg == unsafe { nil } {
57 return old_style('${node.dep} (not found)') + node.tag
58 }
59 return tree_label(node.pkg) + node.tag
60}
61
62fn (mut t DepTracer) dep_node(depstring string, tag string) TreeNode {
63 if pkg := t.installed.find_satisfier(depstring) {
64 return TreeNode{
65 pkg: pkg
66 tag: tag
67 }
68 }
69 // sync dbs that cannot be set up are said once; the tree goes on with
70 // what is installed, and the dependency draws as not found
71 if !t.sync_lost {
72 t.lookup.sync_dbs(mut t.ui, mut t.handle) or {
73 warn('the sync databases are left out of the tree: ${err.msg()}')
74 t.sync_lost = true
75 }
76 }
77 if dbs := t.handle.get_syncdbs() {
78 if pkg := t.handle.find_dbs_satisfier(dbs, depstring) {
79 return TreeNode{
80 pkg: pkg
81 tag: tag
82 }
83 }
84 }
85 return TreeNode{
86 dep: depstring
87 tag: tag
88 }
89}
90
91// children are what the package depends on, or with -r what requires it
92fn (mut t DepTracer) children(pkg &alpm.Package) []TreeNode {
93 mut nodes := []TreeNode{}
94 if t.reverse {
95 for name in pkg.compute_required_by() {
96 if p := t.installed.pkg_find(name) {
97 nodes << TreeNode{
98 pkg: p
99 }
100 }
101 }
102 return nodes
103 }
104 for dep in pkg.get_depends().depends() {
105 nodes << t.dep_node(dep.compute_string(), '')
106 }
107 if t.optional {
108 for dep in pkg.get_opt_depends().depends() {
109 nodes << t.dep_node(dep.compute_string(), ' (optional)')
110 }
111 }
112 return nodes
113}
114
115// branches collects the tree below nodes, depth first: a package is
116// expanded the first time it appears, down to the max depth, and later
117// appearances are marked seen
118fn (mut t DepTracer) branches(nodes []TreeNode, depth int) []TreeBranch {
119 mut out := []TreeBranch{}
120 for node in nodes {
121 if node.pkg == unsafe { nil } {
122 out << TreeBranch{
123 node: node
124 }
125 continue
126 }
127 name := node.pkg.get_name()
128 if t.seen[name] {
129 out << TreeBranch{
130 node: node
131 seen: true
132 }
133 continue
134 }
135 t.seen[name] = true
136 out << TreeBranch{
137 node: node
138 children: if t.max_depth == 0 || depth < t.max_depth {
139 t.branches(t.children(node.pkg), depth + 1)
140 } else {
141 []TreeBranch{}
142 }
143 }
144 }
145 return out
146}
147
148// tree_item is a branch as a frontend gets it, plain
149fn tree_item(b TreeBranch) TreeItem {
150 optional := b.node.tag != ''
151 if b.node.pkg == unsafe { nil } {
152 return TreeItem{
153 name: b.node.dep
154 optional: optional
155 }
156 }
157 return TreeItem{
158 name: b.node.pkg.get_name()
159 version: b.node.pkg.get_version()
160 found: true
161 optional: optional
162 seen: b.seen
163 children: b.children.map(tree_item(it))
164 }
165}
166
167// print_branches draws the branch for each node, then the ones below it
168fn print_branches(branches []TreeBranch, prefix string) {
169 for i, b in branches {
170 last := i == branches.len - 1
171 branch := branch_style(if last { '└── ' } else { '├── ' })
172 println(prefix + branch + node_label(b.node) + if b.seen { ' (seen)' } else { '' })
173 print_branches(b.children, prefix + branch_style(if last { ' ' } else { '│ ' }))
174 }
175}
176
177fn tree(config ConfigTree) ! {
178 need_query(config.query, 'package')!
179 mut ui := new_trans_ui(noconfirm: config.noconfirm)
180 mut handle := init_handle(ui)!
181 defer { handle.release() or { went_on('release the alpm handle', err) } }
182 defer { ui.stop() }
183
184 // the roots are found as info finds its targets: installed first, then
185 // the sync dbs, a repo/ prefix scoping the search. the lookup sets the
186 // sync dbs up when a target or a dependency first needs them
187 mut lookup := InfoLookup{
188 installed: local_pkgcache(handle)!
189 }
190 // a dependency several sync packages provide is drawn with the first, as
191 // pactree draws it: libalpm's provider question is answered silently
192 // there. the flag rides the whole walk, and lands false again on the way
193 // out, the scope the way walk_upgrade_deps keeps its own
194 ui.walking = true
195 defer { ui.walking = false }
196 mut installed := local_pkgcache(handle)!
197
198 for i, target in config.query {
199 // one target that resolves to nothing does not take the rest of
200 // them down with it
201 root := lookup.find(mut ui, mut handle, target) or {
202 target_error(err.msg())
203 continue
204 }
205
206 // the blank line between trees is a terminal's frame; under ipc each
207 // tree leaves as its own event, and a stray line on stdout is not
208 // something a structured reader asked for
209 if i > 0 && !ipc_on() {
210 println('')
211 }
212 mut t := DepTracer{
213 handle: handle
214 ui: ui
215 lookup: &lookup
216 installed: installed
217 reverse: config.reverse
218 optional: config.optional
219 max_depth: config.depth
220 }
221 t.seen[root.get_name()] = true
222 branches := t.branches(t.children(root), 1)
223 if ipc_on() {
224 emit(TreeEvent{
225 reverse: config.reverse
226 root: TreeItem{
227 name: root.get_name()
228 version: root.get_version()
229 found: true
230 children: branches.map(tree_item(it))
231 }
232 })
233 continue
234 }
235 println(tree_label(root))
236 if branches.len == 0 {
237 if config.reverse {
238 println('nothing requires it')
239 } else {
240 println('no dependencies')
241 }
242 continue
243 }
244 print_branches(branches, '')
245 }
246}
247