epm 12 KB
Newer Older
1
#!/bin/sh
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2
#
3 4
# Copyright (C) 2012-2016  Etersoft
# Copyright (C) 2012-2016  Vitaly Lipatov <lav@etersoft.ru>
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5
#
6 7 8
# 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
Vitaly Lipatov's avatar
Vitaly Lipatov committed
9 10 11 12 13
# (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
14
# GNU Affero General Public License for more details.
Vitaly Lipatov's avatar
Vitaly Lipatov committed
15
#
16 17
# 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/>.
Vitaly Lipatov's avatar
Vitaly Lipatov committed
18 19
#

20 21
PROGDIR=$(dirname "$0")
PROGNAME=$(basename "$0")
22
[ "$PROGDIR" = "." ] && PROGDIR=$(pwd)
23 24
if [ "$0" = "/dev/stdin" ] || [ "$0" = "sh" ] ; then
    PROGDIR=""
25
    PROGNAME=""
26
fi
27

28
# will replaced to /usr/share/eepm during install
29
SHAREDIR=$PROGDIR
30 31 32

load_helper()
{
33
    local CMD="$SHAREDIR/$1"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
34 35
    # do not use fatal() here, it can be initial state
    [ -r "$CMD" ] || { echo "FATAL: Have no $CMD helper file" ; exit 1; }
36
    . $CMD
37 38
}

39

40 41
load_helper epm-sh-functions

Vitaly Lipatov's avatar
Vitaly Lipatov committed
42 43
#PATH=$PATH:/sbin:/usr/sbin

44 45
set_pm_type

46
set_sudo
47

48
check_tty
Vitaly Lipatov's avatar
Vitaly Lipatov committed
49 50 51 52 53 54 55

#############################

phelp()
{
	echo "$Descr
$Usage
56
 Commands:
57
$(get_help HELPCMD)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
58

59
 Options:
60
$(get_help HELPOPT)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
61 62 63
"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
64 65 66
print_version()
{
        echo "EPM package manager version @VERSION@"
67
        echo "Running on $($DISTRVENDOR) ('$PMTYPE' package manager uses '$PKGFORMAT' package format)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
68
        echo "Copyright (c) Etersoft 2012-2017"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
69
        echo "This program may be freely redistributed under the terms of the GNU AGPLv3."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
70 71
}

72

Danil Mikhailov's avatar
Danil Mikhailov committed
73 74
Usage="Usage: epm [options] <command> [package name(s), package files]..."
Descr="epm - EPM package manager"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
75

76

77
verbose=
78
quiet=
79
nodeps=
80
noremove=
81
force=
82
short=
83
direct=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
84
sort=
85
non_interactive=
86
skip_installed=
87
show_command_only=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
88 89
epm_cmd=
pkg_files=
90
pkg_dirs=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
91
pkg_names=
92
pkg_urls=
93
quoted_args=
94

95
case $PROGNAME in
96 97 98
    epmi)
        epm_cmd=install
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
99 100 101
    epmI)
        epm_cmd=Install
        ;;
Danil Mikhailov's avatar
Danil Mikhailov committed
102 103 104
    epme)
        epm_cmd=remove
        ;;
105 106 107
    epmcl)
        epm_cmd=changelog
        ;;
Danil Mikhailov's avatar
Danil Mikhailov committed
108 109 110
    epms)
        epm_cmd=search
        ;;
111 112 113
    epmsf)
        epm_cmd=search_file
        ;;
114 115 116
    epmq)
        epm_cmd=query
        ;;
117 118 119
    epmqi)
        epm_cmd=info
        ;;
Danil Mikhailov's avatar
Danil Mikhailov committed
120 121 122
    epmqf)
        epm_cmd=query_file
        ;;
123 124 125 126 127 128
    epmqa)
        epm_cmd=packages
        ;;
    epmqp)
        epm_cmd=query_package
        ;;
129 130 131
    epmql)
        epm_cmd=filelist
        ;;
132 133 134
    epmrl)
        epm_cmd=repolist
        ;;
135 136 137
    epmu)
        epm_cmd=update
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
138
    epm|upm|eepm)
Danil Mikhailov's avatar
Danil Mikhailov committed
139
        ;;
140 141
    epm.sh)
        ;;
142
    *)
143 144
        # epm by default
        # fatal "Unknown command: $progname"
145 146 147
        ;;
esac

148 149 150
# was called with alias name
[ -n "$epm_cmd" ] && PROGNAME="epm"

151 152 153 154 155
check_command()
{
    # do not override command
    [ -z "$epm_cmd" ] || return

Vitaly Lipatov's avatar
Vitaly Lipatov committed
156
# Base commands
157
    case $1 in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
158
    -i|install|add|i)         # HELPCMD: install package(s) from remote repositories or from local file
Vitaly Lipatov's avatar
Vitaly Lipatov committed
159 160
        epm_cmd=install
        ;;
161
    -e|-P|rm|del|remove|delete|uninstall|erase|e)  # HELPCMD: remove (delete) package(s) from the database and the system
Danil Mikhailov's avatar
Danil Mikhailov committed
162 163
        epm_cmd=remove
        ;;
164
    -s|search|s)                # HELPCMD: search in remote package repositories
165
        epm_cmd=search
166
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
167
    -qp|qp|query_package)     # HELPCMD: search in the list of installed packages
168 169
        epm_cmd=query_package
        ;;
170
    -qf|qf|-S|which|belongs)     # HELPCMD: query package(s) owning file
171
        epm_cmd=query_file
172
        ;;
173

Vitaly Lipatov's avatar
Vitaly Lipatov committed
174
# Useful commands
Vitaly Lipatov's avatar
Vitaly Lipatov committed
175
    reinstall)                # HELPCMD: reinstall package(s) from remote repositories or from local file
176
        epm_cmd=reinstall
Danil Mikhailov's avatar
Danil Mikhailov committed
177
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
178
    Install)                  # HELPCMD: perform update package repo info and install package(s) via install command
179 180
        epm_cmd=Install
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
181
    -q|q|installed|query)     # HELPCMD: check presence of package(s) and print this name (also --short is supported)
Danil Mikhailov's avatar
Danil Mikhailov committed
182
        epm_cmd=query
Danil Mikhailov's avatar
Danil Mikhailov committed
183
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
184
    -sf|sf|filesearch)        # HELPCMD: search in which package a file is included
185 186
        epm_cmd=search_file
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
187
    -ql|ql|filelist)          # HELPCMD: print package file list
188
        epm_cmd=filelist
Danil Mikhailov's avatar
Danil Mikhailov committed
189
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
190
    check|fix|verify)         # HELPCMD: check local package base integrity and fix it
191 192
        epm_cmd=check
        ;;
193
    -cl|cl|changelog)         # HELPCMD: show changelog for package
194 195
        epm_cmd=changelog
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
196
    -qi|qi|info|show)         # HELPCMD: print package detail info
197 198
        epm_cmd=info
        ;;
199
    requires|deplist|req)     # HELPCMD: print package requires
200 201
        epm_cmd=requires
        ;;
202
    provides|prov)            # HELPCMD: print package provides
203 204
        epm_cmd=provides
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
205
    whatdepends)              # HELPCMD: print packages dependences on that
206 207
        epm_cmd=whatdepends
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
208
    whatprovides)             # HELPCMD: print packages provides that target
209 210
        epm_cmd=whatprovides
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
211
    conflicts)                # HELPCMD: print package conflicts
212 213
        epm_cmd=conflicts
        ;;
214
    -qa|qa|-l|list|packages)  # HELPCMD: print list of installed package(s)
215 216
        epm_cmd=packages
        ;;
217
    programs)                 # HELPCMD: print list of installed GUI program(s) (they have .desktop files)
218 219
        epm_cmd=programs
        ;;
220
    assure)                   # HELPCMD: <command> [package]: install package if command does not exist
221 222
        epm_cmd=assure
        ;;
223 224 225
    policy)                   # HELPCMD: print detailed information about the priority selection of package
        epm_cmd=policy
        ;;
226

Vitaly Lipatov's avatar
Vitaly Lipatov committed
227
# Repository control
Vitaly Lipatov's avatar
Vitaly Lipatov committed
228
    update)                   # HELPCMD: update remote package repository databases
229
        epm_cmd=update
230
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
231
    addrepo|ar)               # HELPCMD: add package repo
232 233
        epm_cmd=addrepo
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
234
    repolist|sl|rl|listrepo)  # HELPCMD: print repo list
Vitaly Lipatov's avatar
Vitaly Lipatov committed
235 236
        epm_cmd=repolist
        ;;
237 238 239
    repofix)                  # HELPCMD: fix paths in sources lists (ALT Linux only)
        epm_cmd=repofix
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
240
    removerepo|rr)            # HELPCMD: remove package repo
Vitaly Lipatov's avatar
Vitaly Lipatov committed
241 242
        epm_cmd=removerepo
        ;;
243
    release-upgrade|upgrade-release)          # HELPCMD: update whole system to the next release
244 245
        epm_cmd=release_upgrade
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
246
    kernel-update|kernel-upgrade|update-kernel|upgrade-kernel)      # HELPCMD: update system kernel to the last repo version
247
        epm_cmd=kernel_update
Vitaly Lipatov's avatar
Vitaly Lipatov committed
248
        ;;
249 250
    remove-old-kernels|remove-old-kernel)      # HELPCMD: remove old system kernels (exclude current or last two kernels)
        epm_cmd=remove_old_kernels
251
        ;;
252

Vitaly Lipatov's avatar
Vitaly Lipatov committed
253
# Other commands
Vitaly Lipatov's avatar
Vitaly Lipatov committed
254
    clean)                    # HELPCMD: clean local package cache
255 256
        epm_cmd=clean
        ;;
257
    autoremove|package-cleanup)   # HELPCMD: auto remove unneeded package(s)
258 259
        epm_cmd=autoremove
        ;;
260 261 262
    autoorphans|--orphans)    # HELPCMD: remove all packages not from the repository
        epm_cmd=autoorphans
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
263
    upgrade|dist-upgrade)     # HELPCMD: performs upgrades of package software distributions
264 265
        epm_cmd=upgrade
        ;;
266
    Upgrade)                  # HELPCMD: force update package base, then run upgrade
267 268
        epm_cmd=Upgrade
        ;;
269 270 271
    downgrade)                # HELPCMD: downgrade [all] packages to the repo state
        epm_cmd=downgrade
        ;;
272
    download)                 # HELPCMD: download package(s) file to the current dir
273 274
        epm_cmd=download
        ;;
275
    simulate)                 # HELPCMD: simulate install with check requires
276 277
        epm_cmd=simulate
        ;;
278 279 280
    audit)                    # HELPCMD: audits installed packages against known vulnerabilities
        epm_cmd=audit
        ;;
281 282 283
    #checksystem)              # HELPCMD: check system for known errors (package management related)
    #    epm_cmd=checksystem
    #    ;;
284 285 286
    site|url)                 # HELPCMD: open package's site in a browser (use -p for open packages.altlinux.org site)
        epm_cmd=site
        ;;
287
    ei|epminstall|epm-install|selfinstall) # HELPCMD: install or update eepm package from all in one script
288 289
        epm_cmd=epm_install
        ;;
290 291 292
    print)                    # HELPCMD: print various info, run epm print help for details
        epm_cmd=print
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
293
    -V|checkpkg|integrity)    # HELPCMD: check package file integrity (checksum)
294 295
        epm_cmd=checkpkg
        ;;
296 297 298 299 300
    -h|--help|help)           # HELPOPT: print this help
        help=1
        phelp
        exit 0
        ;;
301 302
    *)
        return 1
303
        ;;
304 305 306 307
    esac
    return 0
}

308 309 310
check_option()
{
    case $1 in
311
    -v|--version)         # HELPOPT: print version
Vitaly Lipatov's avatar
Vitaly Lipatov committed
312 313
        print_version
        exit 0
314 315 316 317
        ;;
    --verbose)            # HELPOPT: verbose mode
        verbose=1
        ;;
318
    --skip-installed)     # HELPOPT: skip already installed packages during install
319 320
        skip_installed=1
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
321
    --show-command-only)  # HELPOPT: show command only, do not any action (supports install and remove ONLY)
322 323
        show_command_only=1
        ;;
324 325 326
    --quiet)              # HELPOPT: quiet mode (do not print commands before exec)
        quiet=1
        ;;
327 328 329
    --nodeps)             # HELPOPT: skip dependency check (during install/simulate and so on)
        nodeps="--nodeps"
        ;;
330 331 332
    --force)              # HELPOPT: force install/remove package (f.i., override)
        force="--force"
        ;;
333 334 335
    --noremove|--no-remove)  # HELPOPT: exit if any packages are to be removed during upgrade
        noremove="--no-remove"
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
336
    --short)              # HELPOPT: short output (just 'package' instead 'package-version-release')
337 338
        short="--short"
        ;;
339 340 341
    --direct)              # HELPOPT: direct install package file from ftp (not via hilevel repository manager)
        direct="--direct"
        ;;
342
    --sort)               # HELPOPT: sort output, f.i. --sort=size (supported only for packages command)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
343
        # TODO: how to read arg?
344 345
        sort="$1"
        ;;
346 347 348
    --auto)               # HELPOPT: non interactive mode
        non_interactive=1
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
349
    *)
350
        return 1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
351 352
        ;;
    esac
353 354 355
    return 0
}

356 357
check_filenames()
{
358
    local opt
359
    for opt in "$@" ; do
360 361 362
        # files can be with full path or have extension via .
        if [ -f "$opt" ] && echo "$opt" | grep -q "[/\.]" ; then
            pkg_files="$pkg_files $opt"
363 364
        elif [ -d "$opt" ] ; then
            pkg_dirs="$pkg_dirs $opt"
365
        elif echo "$opt" | grep -q "://" ; then
366
            pkg_urls="$pkg_urls $opt"
367 368 369 370 371
        else
            pkg_names="$pkg_names $opt"
        fi
        quoted_args="$quoted_args \"$opt\""
    done
372 373
}

374
FLAGENDOPTS=
375
for opt in "$@" ; do
376 377 378 379 380
    [ "$opt" = "--" ] && FLAGENDOPTS=1 && continue
    if [ -z "$FLAGENDOPTS" ] ; then
        check_command $opt && continue
        check_option $opt && continue
    fi
381
    # Note: will parse all params separately (no package names with spaces!)
382
    check_filenames $opt
Vitaly Lipatov's avatar
Vitaly Lipatov committed
383 384
done

385 386
# if input is not console and run script from file, get pkgs from stdin too
if ! inputisatty && [ -n "$PROGDIR" ] ; then
387
    for opt in $(withtimeout 1 cat) ; do
388 389 390 391
        check_filenames $opt
    done
fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
392
pkg_files=$(strip_spaces "$pkg_files")
393
pkg_dirs=$(strip_spaces "$pkg_dirs")
394 395
# in common case dirs equals to names only suddenly
pkg_names=$(strip_spaces "$pkg_names $pkg_dirs")
396
pkg_urls=$(strip_spaces "$pkg_urls")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
397

398
pkg_filenames=$(strip_spaces "$pkg_files $pkg_names")
399

400 401 402 403
# Just debug
#echover "command: $epm_cmd"
#echover "pkg_files=$pkg_files"
#echover "pkg_names=$pkg_names"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
404

Vitaly Lipatov's avatar
Vitaly Lipatov committed
405
# Just printout help if run without args
Vitaly Lipatov's avatar
Vitaly Lipatov committed
406 407 408
if [ -z "$epm_cmd" ] ; then
    print_version
    echo
409
    fatstr="Unknown command in $* arg(s)"
410 411
    [ -n "$*" ] || fatstr="That program needs be running with some command"
    fatal "$fatstr. Run $ $PROGNAME --help to get help."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
412 413
fi

414 415
# Use eatmydata for write specific operations
case $epm_cmd in
416
    update|upgrade|Upgrade|install|reinstall|Install|remove|autoremove|kernel_update|release_upgrade|check)
417
        set_eatmydata
418
        ;;
419 420
esac

Vitaly Lipatov's avatar
Vitaly Lipatov committed
421
# Run helper for command
422
load_helper epm-$epm_cmd
423
epm_$epm_cmd
Vitaly Lipatov's avatar
Vitaly Lipatov committed
424
# return last error code (from subroutine)