module main import alpm import v.profiler // DepTracer walks the dependency graph straight from libalpm: the local db // answers for installed packages and the sync dbs for everything else, so a // package that is not installed can be traced too. the sync dbs are the // lookup's: set up, and asked about their freshness, by the first target or // dependency the installed packages do not answer, so a tree that stays // among them never touches the sync dbs struct DepTracer { mut: handle &alpm.Handle ui &TransUI lookup &InfoLookup installed &alpm.List sync_lost bool reverse bool optional bool max_depth int seen map[string]bool } // TreeNode is one node of the tree: a resolved package, or a dependency that // nothing provides (pkg is nil then, and dep is how it was asked for). tag // marks an optional dependency struct TreeNode { pkg &alpm.Package = unsafe { nil } dep string tag string } // TreeBranch is a node with the branches below it, as the tracer collects // them. a package already expanded higher up is seen, and is not expanded // again, which also stops cycles struct TreeBranch { node TreeNode seen bool children []TreeBranch } // tree_label is the name and version, then the size: what the package takes // on disk when installed, or what it downloads when it is only in the repos fn tree_label(pkg &alpm.Package) string { label := '${name_style(pkg.get_name())} ${version_style(pkg.get_version())}' if pkg.get_origin() == .syncdb { return '${label} (${profiler.format_bytes(u64(pkg.download_size()))} to download)' } return '${label} (${profiler.format_bytes(u64(pkg.get_isize()))})' } // node_label is the line a node draws: the package's label, or the // dependency nothing provides fn node_label(node TreeNode) string { if node.pkg == unsafe { nil } { return old_style('${node.dep} (not found)') + node.tag } return tree_label(node.pkg) + node.tag } fn (mut t DepTracer) dep_node(depstring string, tag string) TreeNode { if pkg := t.installed.find_satisfier(depstring) { return TreeNode{ pkg: pkg tag: tag } } // sync dbs that cannot be set up are said once; the tree goes on with // what is installed, and the dependency draws as not found if !t.sync_lost { t.lookup.sync_dbs(mut t.ui, mut t.handle) or { warn('the sync databases are left out of the tree: ${err.msg()}') t.sync_lost = true } } if dbs := t.handle.get_syncdbs() { if pkg := t.handle.find_dbs_satisfier(dbs, depstring) { return TreeNode{ pkg: pkg tag: tag } } } return TreeNode{ dep: depstring tag: tag } } // children are what the package depends on, or with -r what requires it fn (mut t DepTracer) children(pkg &alpm.Package) []TreeNode { mut nodes := []TreeNode{} if t.reverse { for name in pkg.compute_required_by() { if p := t.installed.pkg_find(name) { nodes << TreeNode{ pkg: p } } } return nodes } for dep in pkg.get_depends().depends() { nodes << t.dep_node(dep.compute_string(), '') } if t.optional { for dep in pkg.get_opt_depends().depends() { nodes << t.dep_node(dep.compute_string(), ' (optional)') } } return nodes } // branches collects the tree below nodes, depth first: a package is // expanded the first time it appears, down to the max depth, and later // appearances are marked seen fn (mut t DepTracer) branches(nodes []TreeNode, depth int) []TreeBranch { mut out := []TreeBranch{} for node in nodes { if node.pkg == unsafe { nil } { out << TreeBranch{ node: node } continue } name := node.pkg.get_name() if t.seen[name] { out << TreeBranch{ node: node seen: true } continue } t.seen[name] = true out << TreeBranch{ node: node children: if t.max_depth == 0 || depth < t.max_depth { t.branches(t.children(node.pkg), depth + 1) } else { []TreeBranch{} } } } return out } // tree_item is a branch as a frontend gets it, plain fn tree_item(b TreeBranch) TreeItem { optional := b.node.tag != '' if b.node.pkg == unsafe { nil } { return TreeItem{ name: b.node.dep optional: optional } } return TreeItem{ name: b.node.pkg.get_name() version: b.node.pkg.get_version() found: true optional: optional seen: b.seen children: b.children.map(tree_item(it)) } } // print_branches draws the branch for each node, then the ones below it fn print_branches(branches []TreeBranch, prefix string) { for i, b in branches { last := i == branches.len - 1 branch := branch_style(if last { '└── ' } else { '├── ' }) println(prefix + branch + node_label(b.node) + if b.seen { ' (seen)' } else { '' }) print_branches(b.children, prefix + branch_style(if last { ' ' } else { '│ ' })) } } fn tree(config ConfigTree) ! { need_query(config.query, 'package')! mut ui := new_trans_ui(noconfirm: config.noconfirm) mut handle := init_handle(ui)! defer { handle.release() or { went_on('release the alpm handle', err) } } defer { ui.stop() } // the roots are found as info finds its targets: installed first, then // the sync dbs, a repo/ prefix scoping the search. the lookup sets the // sync dbs up when a target or a dependency first needs them mut lookup := InfoLookup{ installed: local_pkgcache(handle)! } // a dependency several sync packages provide is drawn with the first, as // pactree draws it: libalpm's provider question is answered silently // there. the flag rides the whole walk, and lands false again on the way // out, the scope the way walk_upgrade_deps keeps its own ui.walking = true defer { ui.walking = false } mut installed := local_pkgcache(handle)! for i, target in config.query { // one target that resolves to nothing does not take the rest of // them down with it root := lookup.find(mut ui, mut handle, target) or { target_error(err.msg()) continue } // the blank line between trees is a terminal's frame; under ipc each // tree leaves as its own event, and a stray line on stdout is not // something a structured reader asked for if i > 0 && !ipc_on() { println('') } mut t := DepTracer{ handle: handle ui: ui lookup: &lookup installed: installed reverse: config.reverse optional: config.optional max_depth: config.depth } t.seen[root.get_name()] = true branches := t.branches(t.children(root), 1) if ipc_on() { emit(TreeEvent{ reverse: config.reverse root: TreeItem{ name: root.get_name() version: root.get_version() found: true children: branches.map(tree_item(it)) } }) continue } println(tree_label(root)) if branches.len == 0 { if config.reverse { println('nothing requires it') } else { println('no dependencies') } continue } print_branches(branches, '') } }