NBash

Форк
0
360 строк · 9.8 Кб
1
#!/bin/sh
2
#
3
# Copyright (C) 2012, 2013, 2015-2017  Etersoft
4
# Copyright (C) 2012, 2013, 2015-2017  Vitaly Lipatov <lav@etersoft.ru>
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU Affero General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU Affero General Public License for more details.
15
#
16
# You should have received a copy of the GNU Affero General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18
#
19

20
load_helper epm-packages
21
load_helper epm-print
22

23
# pkg suffix
24
__print_with_arch_suffix()
25
{
26
    local pkg="$1"
27
    local suffix="$2"
28
    [ -n "$pkg" ] || return 1
29
    # do not change if some suffix already exists
30
    echo "$pkg" | grep -q "(x86-32)$" && echo "$pkg" | sed -e "s|(x86-32)$|.i686|" && return 1
31
    echo "$pkg" | grep "\.x86_64$" && return 1
32
    echo "$pkg" | grep "\.noarch$" && return 1
33
    echo "$pkg" | grep "\.i[56]86$" && return 1
34
    echo "$pkg$suffix"
35
}
36

37
# add suffix .x86_64 if we have 64 arch
38
exp_with_arch_suffix()
39
{
40
    local suffix
41

42
    [ "$DISTRARCH" = "x86_64" ] || { cat ; return ; }
43
    [ "$DISTRNAME" = "ROSA" ] &&  { cat ; return ; }
44

45
    # TODO: it is ok for ALT rpm to remove with this suffix
46
    # TODO: separate install and remove?
47
    case $PMTYPE in
48
        yum-rpm|dnf-rpm)
49
            suffix=".x86_64"
50
            ;;
51
        *)
52
            cat
53
            return
54
            ;;
55
    esac
56

57
    # TODO: use estrlist or some function to do it
58
    local pkg
59
    for pkg in $(cat) ; do
60
        local p
61
        # check only packages without arch
62
        p="$(__print_with_arch_suffix "$pkg" .i686)" || { echo "$pkg" ; continue ; }
63
        # add arch suffix only if arch package already installed (otherwise we don't know package arch)
64
        is_installed "$p" || { echo "$pkg" ; continue ; }
65
        echo "$pkg.x86_64"
66
    done
67
}
68

69

70
_get_grep_exp()
71
{
72
    local def="^$1$"
73
    [ "$PMTYPE" != "emerge" ] && echo "$def" && return
74
    # Gentoo hack: support for short package form
75
    echo "$1" | grep -q "/" && echo "$def" && return
76
    echo "/$1$"
77
}
78

79
# TODO: combine with -qa (the difference only in return status now)
80
_shortquery_via_packages_list()
81
{
82
    local res=1
83
    local grepexp
84
    local firstpkg=$1
85
    shift
86

87
    grepexp=$(_get_grep_exp $firstpkg)
88

89
    # TODO: we miss status due grep
90
    # Note: double call due stderr redirect
91
    # Note: we use short=1 here due grep by ^name$
92
    # separate first line for print out command
93
    (short=1 epm_packages $firstpkg | grep -- "$grepexp") && res=0 || res=1
94

95
    local pkg
96
    for pkg in "$@" ; do
97
        grepexp=$(_get_grep_exp $pkg)
98
        (short=1 epm_packages $pkg 2>/dev/null) | grep -- "$grepexp" || res=1
99
    done
100

101
    # TODO: print in query (for user): 'warning: package $pkg is not installed'
102
    return $res
103
}
104

105
# Note: modified from _shortquery_via_packages_list
106
# TODO: non optimal double epm packages call
107
_query_via_packages_list()
108
{
109
    local res=1
110
    local grepexp
111
    local firstpkg=$1
112
    shift
113

114
    grepexp=$(_get_grep_exp $firstpkg)
115

116
    # TODO: we miss status due grep
117
    # TODO: grep correctly
118
    # Note: double call due stderr redirect
119
    # Note: we use short=1 here due grep by ^name$
120
    # separate first line for print out command
121
    (short=1 epm_packages $firstpkg) | grep -q -- "$grepexp" && (quiet=1 epm_packages $firstpkg) && res=0 || res=1
122

123
    local pkg
124
    for pkg in "$@" ; do
125
        grepexp=$(_get_grep_exp $pkg)
126
        (short=1 epm_packages $pkg 2>/dev/null) | grep -q -- "$grepexp" && (quiet=1 epm_packages $pkg) || res=1
127
    done
128

129
    return $res
130
}
131

132
# internal use only, for installed package
133
__epm_get_hilevel_nameform()
134
{
135
    [ -n "$*" ] || return
136

137
    case $PMTYPE in
138
        apt-rpm)
139
            # use # as delimeter for apt
140
            local pkg
141
            pkg=$(rpm -q --queryformat "%{NAME}=%{SERIAL}:%{VERSION}-%{RELEASE}\n" -- $1)
142
            # for case if serial is missed
143
            echo $pkg | grep -q "(none)" && pkg=$(rpm -q --queryformat "%{NAME}#%{VERSION}-%{RELEASE}\n" -- $1)
144
            # HACK: can use only for multiple install packages like kernel
145
            echo $pkg | grep -q kernel || return 1
146
            echo $pkg
147
            return
148
            ;;
149
        yum-rpm|dnf-rpm)
150
            # just use strict version with Epoch and Serial
151
            local pkg
152
            #pkg=$(rpm -q --queryformat "%{EPOCH}:%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n" -- $1)
153
            #echo $pkg | grep -q "(none)" && pkg=$(rpm -q --queryformat "%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n" -- $1)
154
            pkg=$(rpm -q --queryformat "%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n" -- $1)
155
            echo $pkg
156
            return
157
            ;;
158
        *)
159
            return 1
160
            ;;
161
    esac
162
}
163

164
# for local installed packages only
165
# used from epm remove
166
__epm_get_hilevel_name()
167
{
168
    local i
169
    for i in $@ ; do
170
        local pkg
171
        # get short form in pkg
172
        # FIXME: where we use it? continue or pkg=$i?
173
        quiet=1 pkg=$(__epm_query_shortname "$i") || pkg="$i" #continue # drop not installed packages
174
        # if already short form, skipped
175
        [ "$pkg" = "$i" ] && echo "$i" && continue
176
        # try get long form or use short form
177
        __epm_get_hilevel_nameform "$i" || echo $pkg
178
    done
179
}
180

181
__epm_query_file()
182
{
183
    local CMD
184

185
    [ -z "$*" ] && return
186

187
    case $PMTYPE in
188
        *-rpm)
189
            CMD="rpm -qp"
190
            [ -n "$short" ] && CMD="rpm -qp --queryformat %{name}\n"
191
            ;;
192
        *-dpkg)
193
            CMD="dpkg-deb --show --showformat=\${Package}-\${Version}\n"
194
            [ -n "$short" ] && CMD="dpkg-query --show --showformat=\${Package}\n"
195
            ;;
196
        *)
197
            fatal "Do not know command for query file package"
198
            ;;
199
    esac
200

201
    docmd $CMD -- $@
202
}
203

204
# hack: dpkg -W will print names for removed packages too (until newest Ubuntu)
205
__epm_query_dpkg_check()
206
{
207
    local i
208
    for i in $@ ; do
209
        a='' dpkg -s $i >/dev/null 2>/dev/null || return
210
    done
211
    return 0
212
}
213

214
__epm_query_name()
215
{
216
    local CMD
217

218
    [ -z "$*" ] && return
219

220
    case $PMTYPE in
221
        *-rpm)
222
            CMD="rpm -q"
223
            ;;
224
        *-dpkg)
225
            #docmd dpkg -l $@ | grep "^ii"
226
            #CMD="dpkg-query -W --showformat=\${Package}-\${Version}\n"
227
            docmd dpkg-query -W "--showformat=\${Package}-\${Version}\n" -- $@ || return
228
            __epm_query_dpkg_check $@ || return
229
            return
230
            ;;
231
        npackd)
232
            docmd npackdcl path --package=$1
233
            return
234
            ;;
235
        conary)
236
            CMD="conary query"
237
            ;;
238
        eopkg)
239
            showcmd eopkg blame $1
240
            local str
241
            str="$(a= eopkg blame $1 | grep "^Name")"
242
            [ -n "$str" ] || return 1
243
            echo "$str" | sed -e "s|Name[[:space:]]*: \(.*\), version: \(.*\), release: \(.*\)|\1-\2-\3|"
244
            return
245
            ;;
246
        #homebrew)
247
        #    showcmd "brew info $1"
248
        #    local HBRESULT
249
        #    HBRESULT="$(brew info "$1" 2>/dev/null)" || return
250
        #    echo "$HBRESULT" | grep -q "Not installed" && return 1
251
        #    echo "$1"
252
        #    return 0
253
        #    ;;
254
        pacman)
255
            docmd pacman -Q $@
256
            return
257
            ;;
258
        # TODO: need to print name if exists
259
        #pkgng)
260
        #    CMD="pkg info -e"
261
        #    ;;
262
        # Note: slackpkg info pkgname
263
        *)
264
            # default slow workaround
265
            _query_via_packages_list $@
266
            return
267
            ;;
268
    esac
269

270
    docmd $CMD $@
271
}
272

273
__epm_query_shortname()
274
{
275
    local CMD
276

277
    [ -z "$*" ] && return
278

279
    case $PMTYPE in
280
        *-rpm)
281
            showcmd rpm -q --queryformat '%{name}\n' -- $@
282
            a='' rpm -q --queryformat '%{name}\n' -- $@
283
            return
284
            ;;
285
        *-dpkg)
286
            #CMD="dpkg-query -W --showformat=\${Package}\n"
287
            docmd dpkg-query -W "--showformat=\${Package}\n" -- $@ || return
288
            __epm_query_dpkg_check $@ || return
289
            return
290
            ;;
291
        npackd)
292
            docmd npackdcl path --package=$1
293
            return
294
            ;;
295
        conary)
296
            CMD="conary query"
297
            ;;
298
        eopkg)
299
            showcmd eopkg blame $1
300
            local str
301
            str="$(a= eopkg blame $1 | grep "^Name")"
302
            [ -n "$str" ] || return 1
303
            echo "$str" | sed -e "s|Name[[:space:]]*: \(.*\), version: \(.*\), release: \(.*\)|\1|"
304
            return
305
            ;;
306
        homebrew)
307
            docmd brew info "$1" >/dev/null 2>/dev/null && echo "$1" && return
308
            return 1
309
            ;;
310
        # TODO: check status
311
        #pacman)
312
        #    docmd pacman -Q $@ | sed -e "s| .*||g"
313
        #    return
314
        #    ;;
315

316
        # TODO: need to print name if exists
317
        #pkgng)
318
        #    CMD="pkg info -e"
319
        #    ;;
320
        # Note: slackpkg info pkgname
321
        *)
322
            # default slow workaround
323
            _shortquery_via_packages_list $@
324
            return
325
            ;;
326
    esac
327

328
    docmd $CMD $@
329
}
330

331

332
# keep here, all modules and epm-query use is_installed from epm-query
333
# see epm-installed
334

335
# check if pkg is installed
336
is_installed()
337
{
338
    (quiet=1 __epm_query_name "$@") >/dev/null 2>/dev/null
339
}
340

341
filter_pkgnames_to_short()
342
{
343
    local names="$(cat)"
344
    __epm_query_shortname $names
345
}
346

347
epm_query()
348
{
349
    [ -n "$pkg_filenames" ] || fatal "Query: package name is missed"
350

351
    __epm_query_file $pkg_files || return
352

353
    if [ -n "$short" ] ; then
354
        # shellcheck disable=SC2046
355
        __epm_query_shortname $(print_name $pkg_names) || return
356
    else
357
        # shellcheck disable=SC2046
358
        __epm_query_name $(print_name $pkg_names) || return
359
    fi
360
}
361

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.