epm 10.9 KB
Newer Older
1
#!/bin/sh
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2
#
3 4
# Copyright (C) 2012-2014  Etersoft
# Copyright (C) 2012-2014  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
PROGDIR=$(dirname $0)
21 22
[ "$PROGDIR" = "." ] && PROGDIR=$(pwd)

23
# will replaced to /usr/share/eepm during install
24
SHAREDIR=$PROGDIR
25 26 27

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

34

35 36
load_helper epm-sh-functions

Vitaly Lipatov's avatar
Vitaly Lipatov committed
37 38
#PATH=$PATH:/sbin:/usr/sbin

39 40
set_pm_type

41
set_sudo
42

43
check_tty
Vitaly Lipatov's avatar
Vitaly Lipatov committed
44 45 46 47 48 49 50

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

phelp()
{
	echo "$Descr
$Usage
51
 Commands:
52
$(get_help HELPCMD)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
53

54
 Options:
55
$(get_help HELPOPT)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
56 57 58
"
}

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

67

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

71

72
verbose=
73
quiet=
74
nodeps=
75
force=
76
short=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
77
sort=
78
non_interactive=
79
skip_installed=
80
show_command_only=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
81 82
epm_cmd=
pkg_files=
83
pkg_dirs=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
84
pkg_names=
85
pkg_urls=
86
quoted_args=
87 88

progname="${0##*/}"
89

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

138 139 140 141 142
check_command()
{
    # do not override command
    [ -z "$epm_cmd" ] || return

Vitaly Lipatov's avatar
Vitaly Lipatov committed
143
# Base commands
144
    case $1 in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
145
    -i|install|add|i)         # HELPCMD: install package(s) from remote repositories or from local file
Vitaly Lipatov's avatar
Vitaly Lipatov committed
146 147
        epm_cmd=install
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
148
    -e|-P|remove|delete|uninstall|erase|e)  # HELPCMD: remove (delete) package(s) from the database and the system
Danil Mikhailov's avatar
Danil Mikhailov committed
149 150
        epm_cmd=remove
        ;;
151
    -s|search|s)                # HELPCMD: search in remote package repositories
152
        epm_cmd=search
153
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
154
    -qp|qp|query_package)     # HELPCMD: search in the list of installed packages
155 156
        epm_cmd=query_package
        ;;
157
    -qf|qf|-S|which|belongs)     # HELPCMD: query package(s) owning file
158
        epm_cmd=query_file
159
        ;;
160

Vitaly Lipatov's avatar
Vitaly Lipatov committed
161
# Useful commands
Vitaly Lipatov's avatar
Vitaly Lipatov committed
162
    reinstall)                # HELPCMD: reinstall package(s) from remote repositories or from local file
163
        epm_cmd=reinstall
Danil Mikhailov's avatar
Danil Mikhailov committed
164
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
165
    Install)                  # HELPCMD: perform update package repo info and install package(s) via install command
166 167
        epm_cmd=Install
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
168
    -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
169
        epm_cmd=query
Danil Mikhailov's avatar
Danil Mikhailov committed
170
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
171
    -sf|sf|filesearch)        # HELPCMD: search in which package a file is included
172 173
        epm_cmd=search_file
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
174
    -ql|ql|filelist)          # HELPCMD: print package file list
175
        epm_cmd=filelist
Danil Mikhailov's avatar
Danil Mikhailov committed
176
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
177
    check|fix|verify)         # HELPCMD: check local package base integrity and fix it
178 179
        epm_cmd=check
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
180
    changelog|cl|-cl)         # HELPCMD: show changelog for package
181 182
        epm_cmd=changelog
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
183
    -qi|qi|info|show)         # HELPCMD: print package detail info
184 185
        epm_cmd=info
        ;;
186
    requires|deplist|req)     # HELPCMD: print package requires
187 188
        epm_cmd=requires
        ;;
189
    provides|prov)            # HELPCMD: print package provides
190 191
        epm_cmd=provides
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
192
    whatdepends)              # HELPCMD: print packages dependences on that
193 194
        epm_cmd=whatdepends
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
195
    whatprovides)             # HELPCMD: print packages provides that target
196 197
        epm_cmd=whatprovides
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
198
    conflicts)                # HELPCMD: print package conflicts
199 200
        epm_cmd=conflicts
        ;;
201
    -qa|list|packages|-l|qa)  # HELPCMD: print list of installed package(s)
202 203
        epm_cmd=packages
        ;;
204
    programs)                 # HELPCMD: print list of installed GUI program(s)
205 206
        epm_cmd=programs
        ;;
207
    assure)                   # HELPCMD: <command> [package]: install package if command does not exists
208 209
        epm_cmd=assure
        ;;
210 211 212
    policy)                   # HELPCMD: print detailed information about the priority selection of package
        epm_cmd=policy
        ;;
213

Vitaly Lipatov's avatar
Vitaly Lipatov committed
214
# Repository control
Vitaly Lipatov's avatar
Vitaly Lipatov committed
215
    update)                   # HELPCMD: update remote package repository databases
216
        epm_cmd=update
217
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
218
    addrepo|ar)               # HELPCMD: add package repo
219 220
        epm_cmd=addrepo
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
221
    repolist|sl|rl|listrepo)  # HELPCMD: print repo list
Vitaly Lipatov's avatar
Vitaly Lipatov committed
222 223
        epm_cmd=repolist
        ;;
224 225 226
    repofix)                  # HELPCMD: fix paths in sources lists (ALT Linux only)
        epm_cmd=repofix
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
227
    removerepo|rr)            # HELPCMD: remove package repo
Vitaly Lipatov's avatar
Vitaly Lipatov committed
228 229
        epm_cmd=removerepo
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
230
    release-upgrade)          # HELPCMD: update whole system to the next release
231 232
        epm_cmd=release_upgrade
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
233
    kernel-update|kernel-upgrade|update-kernel|upgrade-kernel)      # HELPCMD: update system kernel to the last repo version
234 235
        epm_cmd=kernel_update
        ;;
236

Vitaly Lipatov's avatar
Vitaly Lipatov committed
237
# Other commands
Vitaly Lipatov's avatar
Vitaly Lipatov committed
238
    clean)                    # HELPCMD: clean local package cache
239 240
        epm_cmd=clean
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
241
    autoremove)               # HELPCMD: auto remove unneeded package(s)
242 243
        epm_cmd=autoremove
        ;;
244 245 246
    autoorphans|--orphans)    # HELPCMD: remove all packages not from the repository
        epm_cmd=autoorphans
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
247
    upgrade|dist-upgrade)     # HELPCMD: performs upgrades of package software distributions
248 249
        epm_cmd=upgrade
        ;;
250
    Upgrade)                  # HELPCMD: force update package base, then run upgrade
251 252
        epm_cmd=Upgrade
        ;;
253 254 255
    downgrade)                # HELPCMD: downgrade [all] packages to the repo state
        epm_cmd=downgrade
        ;;
256
    download)                 # HELPCMD: download package(s) file to the current dir
257 258
        epm_cmd=download
        ;;
259
    simulate)                 # HELPCMD: simulate install with check requires
260 261
        epm_cmd=simulate
        ;;
262 263 264
    audit)                    # HELPCMD: audits installed packages against known vulnerabilities
        epm_cmd=audit
        ;;
265 266 267
    #checksystem)              # HELPCMD: check system for known errors (package management related)
    #    epm_cmd=checksystem
    #    ;;
268 269 270
    site|url)                 # HELPCMD: open package's site in a browser (use -p for open packages.altlinux.org site)
        epm_cmd=site
        ;;
271 272 273
    print)                    # HELPCMD: print various info, run epm print help for details
        epm_cmd=print
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
274
    -V|checkpkg|integrity)    # HELPCMD: check package file integrity (checksum)
275 276
        epm_cmd=checkpkg
        ;;
277 278 279 280 281
    -h|--help|help)           # HELPOPT: print this help
        help=1
        phelp
        exit 0
        ;;
282 283
    *)
        return 1
284
        ;;
285 286 287 288
    esac
    return 0
}

289 290 291
check_option()
{
    case $1 in
292
    -v|--version)         # HELPOPT: print version
Vitaly Lipatov's avatar
Vitaly Lipatov committed
293 294
        print_version
        exit 0
295 296 297 298
        ;;
    --verbose)            # HELPOPT: verbose mode
        verbose=1
        ;;
299
    --skip-installed)     # HELPOPT: skip already installed packages during install
300 301
        skip_installed=1
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
302
    --show-command-only)  # HELPOPT: show command only, do not any action (supports install and remove ONLY)
303 304
        show_command_only=1
        ;;
305 306 307
    --quiet)              # HELPOPT: quiet mode (do not print commands before exec)
        quiet=1
        ;;
308 309 310
    --nodeps)             # HELPOPT: skip dependency check (during install/simulate and so on)
        nodeps="--nodeps"
        ;;
311 312 313
    --force)              # HELPOPT: force install/remove package (f.i., override)
        force="--force"
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
314
    --short)              # HELPOPT: short output (just 'package' instead 'package-version-release')
315 316
        short="--short"
        ;;
317
    --sort)               # HELPOPT: sort output, f.i. --sort=size (supported only for packages command)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
318
        # TODO: how to read arg?
319 320
        sort="$1"
        ;;
321 322 323
    --auto)               # HELPOPT: non interactive mode
        non_interactive=1
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
324
    *)
325
        return 1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
326 327
        ;;
    esac
328 329 330
    return 0
}

331 332
check_filenames()
{
333 334 335 336 337
    local opt
    for opt in $* ; do
        # files can be with full path or have extension via .
        if [ -f "$opt" ] && echo "$opt" | grep -q "[/\.]" ; then
            pkg_files="$pkg_files $opt"
338 339
        elif [ -d "$opt" ] ; then
            pkg_dirs="$pkg_dirs $opt"
340
        elif echo "$opt" | grep -q "://" ; then
341
            pkg_urls="$pkg_urls $opt"
342 343 344 345 346
        else
            pkg_names="$pkg_names $opt"
        fi
        quoted_args="$quoted_args \"$opt\""
    done
347 348
}

349
FLAGENDOPTS=
350
for opt in "$@" ; do
351 352 353 354 355
    [ "$opt" = "--" ] && FLAGENDOPTS=1 && continue
    if [ -z "$FLAGENDOPTS" ] ; then
        check_command $opt && continue
        check_option $opt && continue
    fi
356
    # Note: will parse all params separately (no package names with spaces!)
357
    check_filenames $opt
Vitaly Lipatov's avatar
Vitaly Lipatov committed
358 359
done

360
# if input is not console, get pkg from it too
361
if ! inputisatty ; then
362
    for opt in $(withtimeout 1 cat) ; do
363 364 365 366
        check_filenames $opt
    done
fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
367
pkg_files=$(strip_spaces "$pkg_files")
368
pkg_dirs=$(strip_spaces "$pkg_dirs")
369 370
# in common case dirs equals to names only suddenly
pkg_names=$(strip_spaces "$pkg_names $pkg_dirs")
371
pkg_urls=$(strip_spaces "$pkg_urls")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
372

373
pkg_filenames=$(strip_spaces "$pkg_files $pkg_names")
374

375 376 377 378
# Just debug
#echover "command: $epm_cmd"
#echover "pkg_files=$pkg_files"
#echover "pkg_names=$pkg_names"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
379

Vitaly Lipatov's avatar
Vitaly Lipatov committed
380
# Just printout help if run without args
Vitaly Lipatov's avatar
Vitaly Lipatov committed
381 382 383
if [ -z "$epm_cmd" ] ; then
    print_version
    echo
384
    fatal "Run $ $progname --help for get help"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
385 386
fi

387 388
# Use eatmydata for write specific operations
case $epm_cmd in
389
    update|upgrade|Upgrade|install|reinstall|Install|remove|autoremove|kernel_update|release_upgrade|check)
390
        set_eatmydata
391
        ;;
392 393
esac

Vitaly Lipatov's avatar
Vitaly Lipatov committed
394
# Run helper for command
395
load_helper epm-$epm_cmd
396
epm_$epm_cmd
Vitaly Lipatov's avatar
Vitaly Lipatov committed
397
# return last error code (from subroutine)