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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#!/bin/sh
#
# Copyright (C) 2015, 2016, 2018, 2020, 2022 Etersoft
# Copyright (C) 2008, 2015, 2016, 2018, 2020, 2022 Vitaly Lipatov <lav@etersoft.ru>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# TODO: improve file
is_pkgfile()
{
[ -f "$1" ] || return
echo "$1" | grep -q "\.rpm$" && return
echo "$1" | grep -q "\.deb$" && return
return 1
}
# Query variables from rpm package
# TODO: rpm only
rpm_query_package_format_field()
{
local FORMAT="$1\n"
shift
local INSTALLED=""
# if a file, add -p for get from rpm base
if is_pkgfile "$1" ; then
INSTALLED="-p"
fi
a= rpmquery $INSTALLED --queryformat "$FORMAT" "$@"
}
rpm_query_package_field()
{
local FORMAT="%{$1}"
shift
rpm_query_package_format_field "$FORMAT" "$@"
}
dpkg_query_package_format_field()
{
local field="$1"
shift
if is_pkgfile "$1" ; then
a= dpkg-deb --show --showformat="$field\n" "$@"
else
#a= dpkg -s "$1" | grep "^$field: " | sed -e "s|^$field: ||"
a= dpkg-query -W --showformat="$field\n" -- "$@"
fi
}
dpkg_query_package_field()
{
local field="$1"
shift
#if [ -f "$1" ] ; then
# a= dpkg -I "$@" | grep "^.*$field: " | sed -e "s|^.*$field: ||"
#else
dpkg_query_package_format_field "\${$field}" "$@"
#fi
}
query_package_field()
{
local field="$1"
shift
case $PMTYPE in
*-dpkg)
dpkg_query_package_field "$field" "$@"
;;
*-rpm)
rpm_query_package_field "$field" "$@"
;;
esac
}
print_pkg_arch()
{
case $PMTYPE in
*-dpkg)
dpkg_query_package_field "Arch" "$@" | sed -e "s|-.*||" -e "s|.*:||"
;;
*-rpm)
rpm_query_package_field "arch" "$@"
;;
esac
}
print_pkg_version()
{
case $PMTYPE in
*-dpkg)
dpkg_query_package_field "Version" "$@" | sed -e "s|-.*||" -e "s|.*:||"
;;
*-rpm)
rpm_query_package_field "version" "$@"
;;
esac
}
print_pkg_release()
{
case $PMTYPE in
*-dpkg)
dpkg_query_package_field "Version" "$@" | sed -e "s|.*-||"
;;
*-rpm)
rpm_query_package_field "release" "$@"
;;
esac
}
print_pkg_version_release()
{
case $PMTYPE in
*-dpkg)
dpkg_query_package_field "Version" "$@" | sed -e "s|.*:||"
;;
*-rpm)
rpm_query_package_format_field "%{version}-%{release}" "$@"
;;
esac
}
print_pkg_name()
{
case $PMTYPE in
*-dpkg)
dpkg_query_package_field "Package" "$@"
;;
*-rpm)
rpm_query_package_field "name" "$@"
;;
esac
}
# build binary package list (1st - repo dir, 2st - pkgname)
# algorithm: list all files in PKGDIR, print packages with our source pkg name
print_binpkgfilelist()
{
local PKGDIR=$1
local PKGNAME=$(basename $2)
find "$PKGDIR" ! -name '*\.src\.rpm' -name '*\.rpm' -execdir \
rpmquery -p --qf='%{sourcerpm}\t%{name}-%{version}-%{release}.%{arch}.rpm\n' "{}" \; \
| grep "^$PKGNAME[[:space:]].*" \
| cut -f2 \
| xargs -n1 -I "{}" echo -n "$PKGDIR/{} "
}
# TODO: need try detect more strict
# TODO: package name mask for every system
# TODO: broken for all systems
#PKGNAMEMASK1="\(.*\)-\([^0-9].*[^0-9]\)-\(.*[0-9].*\)"
# mask to parse package name
# TODO: с хвоста, сначала релиз, потом версию, остаётся пакет.
# всё равно остаются неоднозначности: libname-1.0 где 1.0 то ли версия, то ли часть названия.
# возможно, нужно ориентироваться только на выделение в полном имени/
# libpq5.2-9.0eter-9.0.4-alt14
# nx-libs-3.5.99.26.1-eter2astra:amd64
PKGNAMEMASK4="6\(.*\)[_-]\([^_-]*\)[_-]\(.*[0-9].*\):\(.*\)$"
# nx-libs-3.5.99.26.1-eter2astra
PKGNAMEMASK3="^\(.*\)[_-]\([^_-]*\)[_-]\(.*[0-9].*\)$"
# nx-libs-3.5.99.26.1
#PKGNAMEMASK2="^\(.*\)[_-]\([0-9].*\)$"
PKGNAMEMASK="\(.*\)-\([0-9].*\)-\(.*[0-9].*\)\.\(.*\)\.\(.*\)"
print_name()
{
# FIXME:
# don't change name (false cases)
#echo "$@" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK4|\1-\2-\3|" -e "s|$PKGNAMEMASK3|\1|"
echo "$@" | xargs -n1 echo
}
# as hack for print_name
print_shortname()
{
#if [ "$
#echo "$@" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK4|\1-\2-\3|" -e "s|$PKGNAMEMASK3|\1|"
echo "$@" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK3|\1|"
}
print_version()
{
echo "$1" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK4|\1-\2-\3|" -e "s|$PKGNAMEMASK3|\2|"
}
print_release()
{
echo "$1" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK4|\1-\2-\3|" -e "s|$PKGNAMEMASK3|\3|"
}
print_version_release()
{
echo "$1" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK4|\1-\2-\3|" -e "s|$PKGNAMEMASK3|\2-\3|"
}
# get package name only by package filename
# TODO: see also __epm_get_hilevel_name()
print_pkgname()
{
local i
for i in $@ ; do
# TODO: deb and other, arch string
echo "$(basename "$i") " | sed -e "s|\.[a-z_0-9]*\.rpm||g" -e "s|\(.*\)_\(.*\)_[a-z_0-9]*\.deb|\1-\2|g"
done
}
print_srcname()
{
print_name "$(print_srcpkgname "$@")"
}
print_specname()
{
# CHECKME: it is possible to have two or more specs in one package?
a= rpm -qlp "$@" | grep "\.spec\$"
}
print_srcpkgname()
{
if [ -n "$FNFLAG" ] ; then
rpm_query_package_field "sourcerpm" "$@"
return
fi
# if PKFLAG
case $PMTYPE in
apt-dpkg)
fatal "Unknown command for get source package name via dpkg"
;;
urpm-rpm)
docmd urpmq --sourcerpm "$@"
return
;;
dnf-rpm)
showcmd dnf repoquery --qf '%{SOURCERPM}' "$@"
a= dnf repoquery --qf '%{SOURCERPM}' "$@"
return
;;
esac
# FIXME: only for installed rpm packages
rpm_query_package_field "sourcerpm" "$@"
}
compare_version()
{
case $PMTYPE in
*-rpm)
is_command rpmevrcmp || fatal "rpmevrcmp exists in ALT Linux only"
a= rpmevrcmp "$@"
;;
*-dpkg)
a= dpkg --compare-versions "$1" lt "$2" && echo "-1" && return
a= dpkg --compare-versions "$1" eq "$2" && echo "0" && return
echo "1"
;;
*)
fatal "Not implemented for $PMTYPE"
;;
esac
}
# construct package file name.
# name version [arch] [pkgtype] [ds] [pds]
construct_name()
{
local name="$1"
local version="$2"
local arch="$3"
local pkgtype="$4"
local ds="$5"
local pds="$6"
[ -n "$arch" ] || arch="$($DISTRVENDOR --distro-arch)"
[ -n "$pkgtype" ] || pkgtype="$PKGFORMAT"
[ -n "$ds" ] || ds=$(get_pkg_name_delimiter $pkgtype)
[ -z "$pds" ] && pds="$ds" && [ "$pds" = "-" ] && pds="."
[ -n "$version" ] && version="$ds$version"
echo "${name}${version}${pds}$arch.$pkgtype"
}
epm_print_help()
{
cat <<EOF
Examples:
epm print info [args] print system and distro info (via distro_info command)
epm print name [from filename|for package] NN print only name of package name or package file
epm print shortname [for package] NN print only short name of package name
epm print version [from filename|for package] NN print only version of package name or package file
epm print release [from filename|for package] NN print only release of package name or package file
epm print version-release [from filename|for package] NN print only release-release of package name or package file
epm print arch [from filename|for package] NN print arch of package name or package file
epm print field FF for package NN print field of the package
epm print pkgname from filename NN print package name for the package file
epm print srcname from filename NN print source name for the package file
epm print srcpkgname from [filename|package] NN print source package name for the binary package file
epm print specname from filename NN print spec filename for the source package file
epm print binpkgfilelist in DIR for NN list binary package(s) filename(s) from DIR for the source package file
epm print compare [package] version N1 N2 compare (package) versions and print -1 (N1 < N2), 0 (N1 == N2), 1 (N1 > N2)
epm print constructname <name> <version> [arch] [pkgtype] [delimiter1] [delimiter2] print distro dependend package filename from args name version arch pkgtype
EOF
}
epm_print()
{
local WHAT="$1"
shift
local FNFLAG=
local PKFLAG=
[ "$1" = "from" ] && shift
[ "$1" = "for" ] && shift
[ "$1" = "of" ] && shift
[ "$1" = "in" ] && shift
if [ "$1" = "filename" ] ; then
FNFLAG="$1"
shift
fi
if [ "$1" = "package" ] ; then
PKFLAG="$1"
shift
fi
case "$WHAT" in
"")
fatal "Use epm print --help to get help."
;;
"-h"|"--help"|"help")
epm_print_help
;;
"name")
[ -n "$1" ] || fatal "Arg is missed"
if [ -n "$FNFLAG" ] ; then
print_name "$(print_pkgname "$@")"
elif [ -n "$PKFLAG" ] ; then
print_pkg_name "$@"
else
print_name "$@"
fi
;;
"arch")
[ -n "$1" ] || fatal "Arg is missed"
if [ -n "$FNFLAG" ] ; then
print_pkg_arch "$@"
elif [ -n "$PKFLAG" ] ; then
print_pkg_arch "$@"
else
print_pkg_arch "$@"
fi
;;
"version")
[ -n "$1" ] || fatal "Arg is missed"
if [ -n "$FNFLAG" ] ; then
print_version "$(print_pkgname "$@")"
elif [ -n "$PKFLAG" ] ; then
print_pkg_version "$@"
else
print_version "$@"
fi
;;
"release")
[ -n "$1" ] || fatal "Arg is missed"
if [ -n "$FNFLAG" ] ; then
print_release "$(print_pkgname "$@")"
elif [ -n "$PKFLAG" ] ; then
print_pkg_release "$@"
else
print_release "$@"
fi
;;
"shortname")
[ -n "$1" ] || exit 0 #fatal "Arg is missed"
print_shortname "$@"
;;
"version-release")
[ -n "$1" ] || fatal "Arg is missed"
if [ -n "$FNFLAG" ] ; then
print_version_release "$(print_pkgname "$@")"
elif [ -n "$PKFLAG" ] ; then
print_pkg_version_release "$@"
else
print_version_release "$@"
fi
;;
"field")
[ -n "$1" ] || fatal "Arg is missed"
local FIELD="$1"
shift
[ "$1" = "for" ] && shift
[ "$1" = "package" ] && shift
query_package_field "$FIELD" "$@"
;;
"pkgname")
[ -n "$FNFLAG" ] || fatal "print $WHAT works only for filename(s)"
[ -n "$1" ] || fatal "Arg is missed"
# TODO: drop_pkg_extensions
print_pkgname "$@"
;;
"srcname")
[ -n "$FNFLAG" ] || fatal "print $WHAT works only for filename(s)"
[ -n "$1" ] || fatal "Arg is missed"
print_srcname "$@"
;;
"srcpkgname")
[ -n "$FNFLAG" ] || [ -n "$PKFLAG" ] || fatal "print $WHAT works only for filename(s)"
[ -n "$1" ] || fatal "Arg is missed"
print_srcpkgname "$@"
;;
"specname")
[ -n "$FNFLAG" ] || [ -n "$PKFLAG" ] || fatal "print $WHAT works only for filename(s)"
[ -n "$1" ] || fatal "Arg is missed"
print_specname "$@"
;;
"binpkgfilelist")
# TODO: rpm only
# TODO: replace get_binpkg_list
local DIR="$1"
shift
[ "$1" = "for" ] && shift
[ -n "$DIR" ] || fatal "DIR arg is missed"
[ -n "$1" ] || fatal "source package filename is missed"
print_binpkgfilelist "$DIR" "$1"
;;
"compare")
[ "$1" = "version" ] && shift
[ -n "$1" ] || fatal "Arg is missed"
#if [ -n "$PKFLAG" ] ; then
# query_package_field "name" "$@"
#else
compare_version "$1" "$2"
#fi
;;
"constructname")
construct_name "$@"
;;
"info")
$DISTRVENDOR "$@"
;;
*)
fatal "Unknown command $ epm print $WHAT. Use epm print help for get help."
;;
esac
}