-
Notifications
You must be signed in to change notification settings - Fork 0
/
getpkgs
executable file
·176 lines (148 loc) · 4.61 KB
/
getpkgs
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
#
## Copyright (c) 2017 Timothy Savannah All Rights Reserved
# Licensed under terms of Apache 2.0 License
# getpkgs -
# If no arguments, prints all package names in current directory
# (like pacman -U `getpkgs`)
#
# For multiple args:
# If argument is a directory,
# Prints all package names in that directory
# If argument is a file,
# Prints if filename matches layout of a package
# So if a glob expression, will process all directories and files
# at that level and print the package names, for ease of package
# management, especially when building multiple packages locally.
#
#
# DEVELOPERS:
# You may set env __API_ONLY=1
# prior to sourcing this file to just import the API
# functions.
#
# i.e. __API_ONLY=1 source `which getpkgs`
#
# Otherwise (like on direct execution), it will execute
# "getpkgs" function with the commandline arguments
#
# _strip_trailing_slash - Strip trailing slash on FIRST argument
#
# Arg1 - (Be sure to quote) - Remove any trailing /,
# e.x. /abc/def/ -> /abc/def
# /abc/def/////// -> /abc/def
# /abc/def -> /abc/dev
# / -> /
# "" (empty str) -> "" (empty str)
#
#
# Arg2..N - IGNORED
#
# Return - None
_strip_trailing_slash() {
_STS_VAL="${1}"
# Differentiate between empty string and / , so after
# strip below we can assume empty string to be "//" or similar
if [ -z "${_STS_VAL}" -o "${_STS_VAL}" = "/" -o "${_STS_VAL}" ];
then
echo "${_STS_VAL}"
return
fi
# Strip the trailing slashes
_STS_RES="$(echo "${_STS_VAL}" | sed -e 's|[/][\/]*$||g')"
# If empty string, was only slashes, e.x. // /////
[ -z "${_STS_RES}" ] && _STS_RES='/'
echo "${_STS_RES}"
}
#
# getpkgs - Get package files from a directory
#
# No args - Gets package files from current directory
# If args:
# For each arg:
# If arg is directory - Prints all packages in
# that directory
#
# If arg is file - Prints filename if is a package
#
# So glob expressions, like "/usr/src/arch/*" would show
# Any packages in the direct subdirs (built packages),
# And any packages that may happen to be at /usr/src/arch level
#
# Output:
# Echos all matching packages, or nothing (even if glob provided)
# if no matches
#
# Return -
# 0 (true) - At least one package was found
# 1 (false - No packages found in any arg
getpkgs() {
# Capture current value of nullglob
if ( shopt nullglob | grep -q 'on$' );
then
HAD_NULLGLOB=1
else
HAD_NULLGLOB=0
shopt -s nullglob
fi
RET=1
if [ $# -eq 0 ] || [ $# -eq 1 -a -z "$1" ];
then
pkgvals="."
else
pkgvals="$@"
fi
# echo "pkgvals: ${pkgvals}"
for pkgval in ${pkgvals};
do
# echo "pkgval: ${pkgval}"
[ -z "${pkgval}" ] && continue;
if [ -d "${pkgval}" ];
then
STRIPPED_VAL="$(_strip_trailing_slash "${pkgval}")"
PKG_FILES="$(echo ${STRIPPED_VAL}/*.pkg.tar.xz | tr ' ' '\n')"
else
if ( echo "${pkgval}" | grep -q '.*[\.]pkg[\.]tar[\.]xz$' );
then
PKG_FILES="${pkgval}"
else
PKG_FILES=
fi
fi
if [ ! -z "${PKG_FILES}" ];
then
printf "%s\n" "${PKG_FILES}"
RET=0;
fi
done
if [[ "${HAD_NULLGLOB}" -eq 0 ]];
then
shopt -u nullglob;
fi
return ${RET}
}
if [ "${__API_ONLY}" != "1" ];
then
if [ "$#" -eq 1 -a "$1" = "--help" ];
then
cat -- <<EOT
Usage: getpkgs ([values])
Prints a list of packages found in a given directory or pattern
If no arguments, prints all packages in current directory,
i.e.
pacman -U \`getpkgs\`
If arguments:
If a glob expression (quoted) e.x. "/usr/src/arch/*",
will handle the null-unroll (i.e. if unrolls to nothing, no value)
with each entry same as an argument
If a directory: Prints all the packages in that directory (no subdirs)
If a file: Echos the filename, if it matches package filename pattern
Will print matched packages, one per line. This makes it apt to work with pipes to grep, etc,
as well as for substitution into arguments for other commandline programs
EOT
else
getpkgs "$@"
exit $?
fi
fi
# vim: set ts=4 sw=4 expandtab :