epm-print 8.92 KB
Newer Older
1 2
#!/bin/sh
#
3 4
# Copyright (C) 2015, 2016, 2018, 2020  Etersoft
# Copyright (C) 2008, 2015, 2016, 2018, 2020  Vitaly Lipatov <lav@etersoft.ru>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#
# 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/>.
#


# Query variables from rpm package
# TODO: rpm only
query_package_field()
{
	local FORMAT="%{$1}\n"
	shift
27 28 29 30 31
	local INSTALLED=""
	# if a file, ad -p for get from rpm base
	if [ -f "$1" ] && [ "$(get_package_type "$1")" = "rpm" ] ; then
		INSTALLED="-p"
	fi
32
	a= rpmquery $INSTALLED --queryformat "$FORMAT" "$@"
33 34 35 36 37 38 39 40 41 42 43
}


# 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' "{}" \; \
Vitaly Lipatov's avatar
Vitaly Lipatov committed
44 45 46
		| grep "^$PKGNAME[[:space:]].*" \
		| cut -f2 \
		| xargs -n1 -I "{}" echo -n "$PKGDIR/{} "
47 48
}

49 50 51 52
# TODO: need try detect more strict
# TODO: package name mask for every system
#PKGNAMEMASK1="\(.*\)-\([^0-9].*[^0-9]\)-\(.*[0-9].*\)"
# mask to parse package name
53
PKGNAMEMASK="\(.*\)-\([0-9].*\)-\(.*[0-9].*\)\.\(.*\)\.\(.*\)"
54

55 56
print_name()
{
57
    echo "$@" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK|\1|g"
58 59 60 61
}

print_version()
{
62
    echo "$1" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK|\2|g"
63 64 65 66
}

print_release()
{
67
    echo "$1" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK|\3|g"
68 69
}

70 71 72 73 74
print_version_release()
{
    echo "$1" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK|\2-\3|g"
}

75 76
# get package name only by package filename
# TODO: see also __epm_get_hilevel_name()
77 78 79 80 81 82 83 84 85 86 87
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()
{
88
    print_name "$(print_srcpkgname "$@")"
89 90
}

91 92 93
print_specname()
{
    # CHECKME: it is possible to have two or more specs in one package?
94
    a= rpm -qlp "$@" | grep "\.spec\$"
95 96
}

97 98
print_srcpkgname()
{
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

    if [ -n "$FNFLAG" ] ; then
        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
122 123 124
    query_package_field sourcerpm "$@"
}

125 126 127
compare_version()
{
    which rpmevrcmp 2>/dev/null >/dev/null || fatal "rpmevrcmp exists in ALT Linux only"
128
    a= rpmevrcmp "$@"
129
}
130

131
# construct package file name.
132
# name version [arch] [pkgtype] [ds]
133 134 135 136 137 138
construct_name()
{
    local name="$1"
    local version="$2"
    local arch="$3"
    local pkgtype="$4"
139
    local ds="$5"
140
    local pds
141

142 143
    [ -n "$arch" ] || arch="$($DISTRVENDOR --distro-arch)"
    [ -n "$pkgtype" ] || pkgtype="$($DISTRVENDOR -p)"
144
    [ -n "$ds" ] || ds=$(get_pkg_name_delimiter $pkgtype)
145
    pds="$ds"
146
    [ "$pds" = "-" ] && pds="."
147
    [ -n "$version" ] && version="$ds$version"
148
    echo "${name}${version}${pds}$arch.$pkgtype"
149 150
}

151
epm_print()
152 153 154 155
{
    local WHAT="$1"
    shift
    local FNFLAG=
156
    local PKFLAG=
157 158 159 160 161 162 163 164
    [ "$1" = "from" ] && shift
    [ "$1" = "for" ] && shift
    [ "$1" = "in" ] && shift
    if [ "$1" = "filename" ] ; then
        FNFLAG="$1"
        shift
    fi

165 166 167 168 169
    if [ "$1" = "package" ] ; then
        PKFLAG="$1"
        shift
    fi

170 171
    case "$WHAT" in
        "")
172
            fatal "Use epm print help to get help."
173 174 175
            ;;
        "-h"|"--help"|"help")
cat <<EOF
176
  Examples:
177
    epm print info [args]                    print system and distro info (via distro_info command)
178 179 180
    epm print name [from filename|for package] NN        print only name of package name or package file
    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
181
    epm print version-release [from filename|for package] NN     print only release-release of package name or package file
182
    epm print field FF for package NN        print field of the package
183 184
    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
185
    epm print srcpkgname from [filename|package] NN    print source package name for the binary package file
186
    epm print specname from filename NN      print spec filename for the source package file
187
    epm print binpkgfilelist in DIR for NN   list binary package(s) filename(s) from DIR for the source package file
188
    epm print compare [package] version N1 N2          compare (package) versions and print -1, 0, 1
189
    epm print constructname <name> <version> [arch] [ pkgtype]  print distro dependend package filename from args name version arch pkgtype
190 191 192 193 194
EOF
            ;;
        "name")
            [ -n "$1" ] || fatal "Arg is missed"
            if [ -n "$FNFLAG" ] ; then
195
                print_name "$(print_pkgname "$@")"
196 197
            elif [ -n "$PKFLAG" ] ; then
                query_package_field "name" "$@"
198 199 200 201 202 203 204
            else
                print_name "$@"
            fi
            ;;
        "version")
            [ -n "$1" ] || fatal "Arg is missed"
            if [ -n "$FNFLAG" ] ; then
205
                print_version "$(print_pkgname "$@")"
206 207
            elif [ -n "$PKFLAG" ] ; then
                query_package_field "version" "$@"
208 209 210 211 212 213 214
            else
                print_version "$@"
            fi
            ;;
        "release")
            [ -n "$1" ] || fatal "Arg is missed"
            if [ -n "$FNFLAG" ] ; then
215
                print_release "$(print_pkgname "$@")"
216 217
            elif [ -n "$PKFLAG" ] ; then
                query_package_field "release" "$@"
218 219 220 221
            else
                print_release "$@"
            fi
            ;;
222 223 224 225 226 227 228 229 230 231
        "version-release")
            [ -n "$1" ] || fatal "Arg is missed"
            if [ -n "$FNFLAG" ] ; then
                print_version_release "$(print_pkgname "$@")"
            elif [ -n "$PKFLAG" ] ; then
                echo "$(query_package_field "version" "$@")-$(query_package_field "release" "$@")"
            else
                print_version_release "$@"
            fi
            ;;
232 233 234 235 236
        "field")
            [ -n "$1" ] || fatal "Arg is missed"
            local FIELD="$1"
            shift
            [ "$1" = "for" ] && shift
237
            query_package_field "$FIELD" "$@"
238 239 240 241 242 243 244 245 246 247 248 249 250
            ;;
        "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")
251
            [ -n "$FNFLAG" ] || [ -n "$PKFLAG" ] || fatal "print $WHAT works only for filename(s)"
252 253 254
            [ -n "$1" ] || fatal "Arg is missed"
            print_srcpkgname "$@"
            ;;
255 256 257 258 259
        "specname")
            [ -n "$FNFLAG" ] || [ -n "$PKFLAG" ] || fatal "print $WHAT works only for filename(s)"
            [ -n "$1" ] || fatal "Arg is missed"
            print_specname "$@"
            ;;
260 261 262 263 264 265 266 267 268 269
        "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"
            ;;
270 271 272 273 274 275 276 277 278
        "compare")
            [ "$1" = "version" ] && shift
            [ -n "$1" ] || fatal "Arg is missed"
            #if [ -n "$PKFLAG" ] ; then
            #    query_package_field "name" "$@"
            #else
                 compare_version "$1" "$2"
            #fi
            ;;
279 280 281
        "constructname")
            construct_name "$@"
            ;;
282 283 284
        "info")
            $DISTRVENDOR "$@"
            ;;
285 286 287 288 289
        *)
            fatal "Unknown command $ epm print $WHAT. Use epm print help for get help."
            ;;
    esac
}