Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/scripts/pkg_tree.sh
2065 views
1
#! /bin/sh
2
# Copyright (c) 2012 Bryan Drewery <[email protected]>
3
# All rights reserved.
4
#
5
# Redistribution and use in source and binary forms, with or without
6
# modification, are permitted provided that the following conditions
7
# are met:
8
# 1. Redistributions of source code must retain the above copyright
9
# notice, this list of conditions and the following disclaimer
10
# in this position and unchanged.
11
# 2. Redistributions in binary form must reproduce the above copyright
12
# notice, this list of conditions and the following disclaimer in the
13
# documentation and/or other materials provided with the distribution.
14
#
15
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18
# IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26
show_tree() {
27
local name=$1
28
local indentation=$2
29
local parent=$3
30
31
if [ $RECURSIVE -eq 0 -a $indentation -gt 1 ]; then
32
return
33
fi
34
35
if [ $indentation -eq 0 ]; then
36
parent="${name}"
37
else
38
parent="${parent}% ${name}"
39
fi
40
41
echo ${parent}
42
43
for depends in $(pkg ${PKG_QUERY} ${PKG_QUERY_DISPLAY_DEPENDS} ${name} | sort); do
44
test -z "${depends}" && return
45
show_tree $depends $((${indentation} + 1)) ${parent}
46
done
47
}
48
49
usage() {
50
echo "Usage: $0 [-nprR] [pkgname|origin] [...]"
51
echo "-n: Non-tree view, uses tabs instead"
52
echo "-o: Print package origin"
53
echo "-r: Recursively show required packages"
54
echo "-R: Use remote repository"
55
echo "-U: Show reverse depends"
56
echo "If no package/origin is specified, show all non-automatic packages at top-level."
57
exit 0
58
}
59
60
while getopts "hnorRU" opt; do
61
case "${opt}" in
62
n)
63
TREE_VIEW=0
64
;;
65
o)
66
DISPLAY_ORIGIN=1
67
;;
68
r)
69
RECURSIVE=1
70
;;
71
R)
72
USE_RQUERY=1
73
;;
74
U)
75
REVERSE_DEPENDS=1
76
;;
77
*)
78
usage
79
;;
80
esac
81
done
82
83
shift $(($OPTIND - 1))
84
85
: ${RECURSIVE:=0}
86
: ${REVERSE_DEPENDS:=0}
87
: ${TREE_VIEW:=1}
88
: ${USE_RQUERY:=0}
89
: ${DISPLAY_ORIGIN:=0}
90
91
if [ $REVERSE_DEPENDS -eq 0 ]; then
92
PKG_QUERY_DEPENDS="d"
93
else
94
PKG_QUERY_DEPENDS="r"
95
fi
96
97
if [ $DISPLAY_ORIGIN -eq 0 ]; then
98
PKG_QUERY_DISPLAY="%n-%v"
99
PKG_QUERY_DISPLAY_DEPENDS="%${PKG_QUERY_DEPENDS}n-%${PKG_QUERY_DEPENDS}v"
100
else
101
PKG_QUERY_DISPLAY="%o"
102
PKG_QUERY_DISPLAY_DEPENDS="%${PKG_QUERY_DEPENDS}o"
103
fi
104
105
if [ $USE_RQUERY -eq 0 ]; then
106
PKG_QUERY=query
107
else
108
PKG_QUERY=rquery
109
fi
110
111
main() {
112
if [ $# -eq 0 ]; then
113
if [ $USE_RQUERY -eq 0 ]; then
114
packages=$(pkg ${PKG_QUERY} -e '%a = 0' ${PKG_QUERY_DISPLAY} | sort)
115
else
116
packages=$(pkg ${PKG_QUERY} ${PKG_QUERY_DISPLAY} | sort)
117
fi
118
else
119
packages=$@
120
fi;
121
122
for name in ${packages}; do
123
show_tree ${name} 0
124
done
125
}
126
127
if [ $TREE_VIEW -eq 1 ]; then
128
main $@ | sed -e 's/[^%]*%/| /g' -e 's/| \([^|]\)/`--\1/g'
129
else
130
main $@ | sed -e 's/[^%]*%/ /g'
131
fi
132
133