NBash

Форк
0
466 строк · 13.6 Кб
1
#!/bin/sh
2
#
3
# Copyright (C) 2015, 2016, 2018, 2020, 2022  Etersoft
4
# Copyright (C) 2008, 2015, 2016, 2018, 2020, 2022  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
# TODO: improve file
21
is_pkgfile()
22
{
23
     [ -f "$1" ] || return
24
     echo "$1" | grep -q "\.rpm$" && return
25
     echo "$1" | grep -q "\.deb$" && return
26
     return 1
27
}
28

29
# Query variables from rpm package
30
# TODO: rpm only
31
rpm_query_package_format_field()
32
{
33
    local FORMAT="$1\n"
34
    shift
35
    local INSTALLED=""
36
    # if a file, add -p for get from rpm base
37
    if is_pkgfile "$1" ; then
38
        INSTALLED="-p"
39
    fi
40
    a= rpmquery $INSTALLED --queryformat "$FORMAT" "$@"
41
}
42

43
rpm_query_package_field()
44
{
45
    local FORMAT="%{$1}"
46
    shift
47
    rpm_query_package_format_field "$FORMAT" "$@"
48
}
49

50
dpkg_query_package_format_field()
51
{
52
        local field="$1"
53
        shift
54
        if is_pkgfile "$1" ; then
55
            a= dpkg-deb --show --showformat="$field\n" "$@"
56
        else
57
            #a= dpkg -s "$1" | grep "^$field: " | sed -e "s|^$field: ||"
58
            a= dpkg-query -W --showformat="$field\n" -- "$@"
59
        fi
60
}
61

62
dpkg_query_package_field()
63
{
64
        local field="$1"
65
        shift
66
        #if [ -f "$1" ] ; then
67
        #    a= dpkg -I "$@" | grep "^.*$field: " | sed -e "s|^.*$field: ||"
68
        #else
69
            dpkg_query_package_format_field "\${$field}" "$@"
70
        #fi
71
}
72

73
query_package_field()
74
{
75
    local field="$1"
76
    shift
77
    case $PMTYPE in
78
        *-dpkg)
79
            dpkg_query_package_field "$field" "$@"
80
            ;;
81
        *-rpm)
82
            rpm_query_package_field "$field" "$@"
83
            ;;
84
    esac
85
}
86

87

88
print_pkg_arch()
89
{
90
    case $PMTYPE in
91
        *-dpkg)
92
            dpkg_query_package_field "Arch" "$@" | sed -e "s|-.*||" -e "s|.*:||"
93
            ;;
94
        *-rpm)
95
            rpm_query_package_field "arch" "$@"
96
            ;;
97
    esac
98
}
99

100
print_pkg_version()
101
{
102
    case $PMTYPE in
103
        *-dpkg)
104
            dpkg_query_package_field "Version" "$@" | sed -e "s|-.*||" -e "s|.*:||"
105
            ;;
106
        *-rpm)
107
            rpm_query_package_field "version" "$@"
108
            ;;
109
    esac
110
}
111

112
print_pkg_release()
113
{
114
    case $PMTYPE in
115
        *-dpkg)
116
            dpkg_query_package_field "Version" "$@" | sed -e "s|.*-||"
117
            ;;
118
        *-rpm)
119
            rpm_query_package_field "release" "$@"
120
            ;;
121
    esac
122
}
123

124
print_pkg_version_release()
125
{
126
    case $PMTYPE in
127
        *-dpkg)
128
            dpkg_query_package_field "Version" "$@" | sed -e "s|.*:||"
129
            ;;
130
        *-rpm)
131
            rpm_query_package_format_field "%{version}-%{release}" "$@"
132
            ;;
133
    esac
134
}
135

136
print_pkg_name()
137
{
138
    case $PMTYPE in
139
        *-dpkg)
140
            dpkg_query_package_field "Package" "$@"
141
            ;;
142
        *-rpm)
143
            rpm_query_package_field "name" "$@"
144
            ;;
145
    esac
146
}
147

148
# build binary package list (1st - repo dir, 2st - pkgname)
149
# algorithm: list all files in PKGDIR, print packages with our source pkg name
150
print_binpkgfilelist()
151
{
152
    local PKGDIR=$1
153
    local PKGNAME=$(basename $2)
154
    find "$PKGDIR" ! -name '*\.src\.rpm' -name '*\.rpm' -execdir \
155
        rpmquery -p --qf='%{sourcerpm}\t%{name}-%{version}-%{release}.%{arch}.rpm\n' "{}" \; \
156
        | grep "^$PKGNAME[[:space:]].*" \
157
        | cut -f2 \
158
        | xargs -n1 -I "{}" echo -n "$PKGDIR/{} "
159
}
160

161
# TODO: need try detect more strict
162
# TODO: package name mask for every system
163
# TODO: broken for all systems
164
#PKGNAMEMASK1="\(.*\)-\([^0-9].*[^0-9]\)-\(.*[0-9].*\)"
165
# mask to parse package name
166

167
# TODO: с хвоста, сначала релиз, потом версию, остаётся пакет.
168
# всё равно остаются неоднозначности: libname-1.0 где 1.0 то ли версия, то ли часть названия.
169
# возможно, нужно ориентироваться только на выделение в полном имени/
170

171
# libpq5.2-9.0eter-9.0.4-alt14
172
# nx-libs-3.5.99.26.1-eter2astra:amd64
173
PKGNAMEMASK4="6\(.*\)[_-]\([^_-]*\)[_-]\(.*[0-9].*\):\(.*\)$"
174
# nx-libs-3.5.99.26.1-eter2astra
175
PKGNAMEMASK3="^\(.*\)[_-]\([^_-]*\)[_-]\(.*[0-9].*\)$"
176
# nx-libs-3.5.99.26.1
177
#PKGNAMEMASK2="^\(.*\)[_-]\([0-9].*\)$"
178

179
PKGNAMEMASK="\(.*\)-\([0-9].*\)-\(.*[0-9].*\)\.\(.*\)\.\(.*\)"
180

181
print_name()
182
{
183
    # FIXME:
184
    # don't change name (false cases)
185
    #echo "$@" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK4|\1-\2-\3|" -e "s|$PKGNAMEMASK3|\1|"
186
    echo "$@" | xargs -n1 echo
187
}
188

189
# as hack for print_name
190
print_shortname()
191
{
192
    #if [ "$
193
    #echo "$@" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK4|\1-\2-\3|" -e "s|$PKGNAMEMASK3|\1|"
194
    echo "$@" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK3|\1|"
195
}
196

197
print_version()
198
{
199
    echo "$1" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK4|\1-\2-\3|" -e "s|$PKGNAMEMASK3|\2|"
200
}
201

202
print_release()
203
{
204
    echo "$1" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK4|\1-\2-\3|" -e "s|$PKGNAMEMASK3|\3|"
205
}
206

207
print_version_release()
208
{
209
    echo "$1" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK4|\1-\2-\3|" -e "s|$PKGNAMEMASK3|\2-\3|"
210
}
211

212
# get package name only by package filename
213
# TODO: see also __epm_get_hilevel_name()
214
print_pkgname()
215
{
216
    local i
217
    for i in $@ ; do
218
        # TODO: deb and other, arch string
219
        echo "$(basename "$i") " | sed -e "s|\.[a-z_0-9]*\.rpm||g" -e "s|\(.*\)_\(.*\)_[a-z_0-9]*\.deb|\1-\2|g"
220
    done
221
}
222

223
print_srcname()
224
{
225
    print_name "$(print_srcpkgname "$@")"
226
}
227

228
print_specname()
229
{
230
    # CHECKME: it is possible to have two or more specs in one package?
231
    a= rpm -qlp "$@" | grep "\.spec\$"
232
}
233

234
print_srcpkgname()
235
{
236

237
    if [ -n "$FNFLAG" ] ; then
238
        rpm_query_package_field "sourcerpm" "$@"
239
        return
240
    fi
241

242
    # if PKFLAG
243
    case $PMTYPE in
244
        apt-dpkg)
245
            fatal "Unknown command for get source package name via dpkg"
246
            ;;
247
        urpm-rpm)
248
            docmd urpmq --sourcerpm "$@"
249
            return
250
            ;;
251
        dnf-rpm)
252
            showcmd dnf repoquery --qf '%{SOURCERPM}' "$@"
253
            a= dnf repoquery --qf '%{SOURCERPM}' "$@"
254
            return
255
            ;;
256
    esac
257

258
    # FIXME: only for installed rpm packages
259
    rpm_query_package_field "sourcerpm" "$@"
260
}
261

262
# https://blog.jasonantman.com/2014/07/how-yum-and-rpm-compare-versions/
263
compare_version()
264
{
265
    case $PMTYPE in
266
        *-rpm)
267
            # rpmevrcmp exists in ALT Linux only
268
            if is_command rpmevrcmp ; then
269
                a= rpmevrcmp "$@"
270
            else
271
                a= rpm --eval "%{lua:print(rpm.vercmp('$1', '$2'))}"
272
            fi
273
            ;;
274
        *-dpkg)
275
            a= dpkg --compare-versions "$1" lt "$2" && echo "-1" && return
276
            a= dpkg --compare-versions "$1" eq "$2" && echo "0" && return
277
            echo "1"
278
            ;;
279
        *)
280
            fatal "Not implemented for $PMTYPE"
281
            ;;
282
    esac
283
}
284

285
# construct package file name.
286
# name version [arch] [pkgtype] [ds] [pds]
287
construct_name()
288
{
289
    local name="$1"
290
    local version="$2"
291
    local arch="$3"
292
    local pkgtype="$4"
293
    local ds="$5"
294
    local pds="$6"
295

296
    [ -n "$arch" ] || arch="$DISTRARCH"
297
    [ -n "$pkgtype" ] || pkgtype="$PKGFORMAT"
298
    [ -n "$ds" ] || ds=$(get_pkg_name_delimiter $pkgtype)
299
    [ -z "$pds" ] && pds="$ds" && [ "$pds" = "-" ] && pds="."
300
    [ -n "$version" ] && version="$ds$version"
301
    echo "${name}${version}${pds}$arch.$pkgtype"
302
}
303

304
epm_print_help()
305
{
306
cat <<EOF
307
  Examples:
308
    epm print info [args]                    print system and distro info (via distro_info command)
309
    epm print name [from filename|for package] NN        print only name of package name or package file
310
    epm print shortname [for package] NN        print only short name of package name
311
    epm print version [from filename|for package] NN     print only version of package name or package file
312
    epm print release [from filename|for package] NN     print only release of package name or package file
313
    epm print version-release [from filename|for package] NN     print only release-release of package name or package file
314
    epm print arch [from filename|for package] NN     print arch  of package name or package file
315
    epm print field FF for package NN        print field of the package
316
    epm print pkgname from filename NN       print package name for the package file
317
    epm print srcname from filename NN       print source name for the package file
318
    epm print srcpkgname from [filename|package] NN    print source package name for the binary package file
319
    epm print specname from filename NN      print spec filename for the source package file
320
    epm print binpkgfilelist in DIR for NN   list binary package(s) filename(s) from DIR for the source package file
321
    epm print compare [package] version N1 N2          compare (package) versions and print -1 (N1 < N2), 0 (N1 == N2), 1 (N1 > N2)
322
    epm print constructname <name> <version> [arch] [pkgtype] [delimiter1] [delimiter2]  print distro dependend package filename from args name version arch pkgtype
323
EOF
324
}
325

326
epm_print()
327
{
328
    local WHAT="$1"
329
    shift
330
    local FNFLAG=
331
    local PKFLAG=
332
    [ "$1" = "from" ] && shift
333
    [ "$1" = "for" ] && shift
334
    [ "$1" = "of" ] && shift
335
    [ "$1" = "in" ] && shift
336
    if [ "$1" = "filename" ] ; then
337
        FNFLAG="$1"
338
        shift
339
    fi
340

341
    if [ "$1" = "package" ] ; then
342
        PKFLAG="$1"
343
        shift
344
    fi
345

346
    case "$WHAT" in
347
        "")
348
            fatal "Use epm print --help to get help."
349
            ;;
350
        "-h"|"--help"|"help")
351
            epm_print_help
352
            ;;
353
        "name")
354
            [ -n "$1" ] || fatal "Arg is missed"
355
            if [ -n "$FNFLAG" ] ; then
356
                print_name "$(print_pkgname "$@")"
357
            elif [ -n "$PKFLAG" ] ; then
358
                print_pkg_name "$@"
359
            else
360
                print_name "$@"
361
            fi
362
            ;;
363
        "arch")
364
            [ -n "$1" ] || fatal "Arg is missed"
365
            if [ -n "$FNFLAG" ] ; then
366
                print_pkg_arch "$@"
367
            elif [ -n "$PKFLAG" ] ; then
368
                print_pkg_arch "$@"
369
            else
370
                print_pkg_arch "$@"
371
            fi
372
            ;;
373
        "version")
374
            [ -n "$1" ] || fatal "Arg is missed"
375
            if [ -n "$FNFLAG" ] ; then
376
                print_version "$(print_pkgname "$@")"
377
            elif [ -n "$PKFLAG" ] ; then
378
                print_pkg_version "$@"
379
            else
380
                print_version "$@"
381
            fi
382
            ;;
383
        "release")
384
            [ -n "$1" ] || fatal "Arg is missed"
385
            if [ -n "$FNFLAG" ] ; then
386
                print_release "$(print_pkgname "$@")"
387
            elif [ -n "$PKFLAG" ] ; then
388
                print_pkg_release "$@"
389
            else
390
                print_release "$@"
391
            fi
392
            ;;
393
        "shortname")
394
            [ -n "$1" ] || exit 0 #fatal "Arg is missed"
395
            print_shortname "$@"
396
            ;;
397
        "version-release")
398
            [ -n "$1" ] || fatal "Arg is missed"
399
            if [ -n "$FNFLAG" ] ; then
400
                print_version_release "$(print_pkgname "$@")"
401
            elif [ -n "$PKFLAG" ] ; then
402
                print_pkg_version_release "$@"
403
            else
404
                print_version_release "$@"
405
            fi
406
            ;;
407
        "field")
408
            [ -n "$1" ] || fatal "Arg is missed"
409
            local FIELD="$1"
410
            shift
411
            [ "$1" = "for" ] && shift
412
            [ "$1" = "package" ] && shift
413
            query_package_field "$FIELD" "$@"
414
            ;;
415
        "pkgname")
416
            [ -n "$FNFLAG" ] || fatal "print $WHAT works only for filename(s)"
417
            [ -n "$1" ] || fatal "Arg is missed"
418
            # TODO: drop_pkg_extensions
419
            print_pkgname "$@"
420
            ;;
421
        "srcname")
422
            [ -n "$FNFLAG" ] || fatal "print $WHAT works only for filename(s)"
423
            [ -n "$1" ] || fatal "Arg is missed"
424
            print_srcname "$@"
425
            ;;
426
        "srcpkgname")
427
            [ -n "$FNFLAG" ] || [ -n "$PKFLAG" ] || fatal "print $WHAT works only for filename(s)"
428
            [ -n "$1" ] || fatal "Arg is missed"
429
            print_srcpkgname "$@"
430
            ;;
431
        "specname")
432
            [ -n "$FNFLAG" ] || [ -n "$PKFLAG" ] || fatal "print $WHAT works only for filename(s)"
433
            [ -n "$1" ] || fatal "Arg is missed"
434
            print_specname "$@"
435
            ;;
436
        "binpkgfilelist")
437
            # TODO: rpm only
438
            # TODO: replace get_binpkg_list
439
            local DIR="$1"
440
            shift
441
            [ "$1" = "for" ] && shift
442
            [ -n "$DIR" ] || fatal "DIR arg is missed"
443
            [ -n "$1" ] || fatal "source package filename is missed"
444
            print_binpkgfilelist "$DIR" "$1"
445
            ;;
446
        "compare")
447
            [ "$1" = "version" ] && shift
448
            [ -n "$1" ] || fatal "Arg is missed"
449
            #if [ -n "$PKFLAG" ] ; then
450
            #    query_package_field "name" "$@"
451
            #else
452
                 compare_version "$1" "$2"
453
            #fi
454
            ;;
455
        "constructname")
456
            construct_name "$@"
457
            ;;
458
        "info")
459
            export EPMVERSION
460
            $DISTRVENDOR "$@"
461
            ;;
462
        *)
463
            fatal "Unknown command $ epm print $WHAT. Use epm print help for get help."
464
            ;;
465
    esac
466
}
467

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

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

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

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