NBash

Форк
0
523 строки · 12.7 Кб
1
#!/bin/sh
2
#
3
# Copyright (C) 2015, 2017, 2019, 2020, 2022  Etersoft
4
# Copyright (C) 2015, 2017, 2019, 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

21
__check_installed_app()
22
{
23
    [ -s $epm_vardir/installed-app ] || return 1
24
    grep -q -- "^$1\$" $epm_vardir/installed-app
25
}
26

27
__save_installed_app()
28
{
29
    [ -d "$epm_vardir" ] || return 0
30
    __check_installed_app "$1" && return 0
31
    echo "$1" | sudorun tee -a $epm_vardir/installed-app >/dev/null
32
}
33

34
__remove_installed_app()
35
{
36
    [ -s $epm_vardir/installed-app ] || return 0
37
    local i
38
    for i in $* ; do
39
        sudorun sed -i "/^$i$/d" $epm_vardir/installed-app
40
    done
41
    return 0
42
}
43

44

45
__is_app_installed()
46
{
47
    __run_script "$1" --installed "$2"
48
    return
49
}
50

51
__run_script()
52
{
53
    local script="$psdir/$1.sh"
54
    [ -x "$script" ] || return
55
    [ -f "$script.rpmnew" ] && warning "There is .rpmnew file(s) in $psdir dir. The play script can be outdated."
56

57
    shift
58
    [ "$PROGDIR" = "/usr/bin" ] && SCPATH="$PATH" || SCPATH="$PROGDIR:$PATH"
59
    ( unset EPMCURDIR ; export PATH=$SCPATH ; $script "$@" )
60
    return
61
}
62

63
__get_app_package()
64
{
65
    __run_script "$1" --package-name "$2" "$3" 2>/dev/null
66
}
67

68

69

70
__list_all_app()
71
{
72
    cd $psdir || fatal
73
    for i in *.sh ; do
74
       local name=${i/.sh/}
75
       [ -n "$IGNOREi586" ] && startwith "$name" "i586-" && continue
76
       startwith "$name" "common" && continue
77
       echo "$name"
78
    done
79
    cd - >/dev/null
80
}
81

82
__list_all_packages()
83
{
84
    local name
85
    for name in $(__list_all_app) ; do
86
        __get_app_package $name
87
    done
88
}
89

90
# pkg app
91
__list_app_packages_table()
92
{
93
    local name
94
    for name in $(__list_all_app) ; do
95
        local pkg="$(__get_app_package $name)"
96
        [ -n "$pkg" ] || continue
97
        echo "$pkg $name"
98
    done
99
}
100

101
__filter_by_installed_packages()
102
{
103
    local i
104
    local tapt="$1"
105

106
    local pkglist
107
    pkglist="$(mktemp)" || fatal
108
    remove_on_exit $pkglist
109

110
    # get intersect between full package list and available packages table
111
    epm --short packages | LC_ALL=C sort -u >$pkglist
112
    LC_ALL=C join -11 -21 $tapt $pkglist | uniq
113
    rm -f $pkglist
114

115
    # rpm on Fedora/CentOS no more print missed packages to stderr
116
    # get supported packages list and print lines with it
117
    #for i in $(epm query --short $(cat $tapt | cut -f1 -d" ") 2>/dev/null) ; do
118
    #    grep "^$i " $tapt
119
    #done
120
}
121

122
__get_installed_table()
123
{
124
    local i
125
    local tapt
126
    tapt="$(mktemp)" || fatal
127
    remove_on_exit $tapt
128
    __list_app_packages_table | LC_ALL=C sort -u >$tapt
129
    __filter_by_installed_packages $tapt
130
    rm -f $tapt
131
}
132

133
__list_installed_app()
134
{
135
    # get all installed packages and convert it to a apps list
136
    __get_installed_table | cut -f2 -d" "
137
}
138

139
__list_installed_packages()
140
{
141
    # get all installed packages
142
    __get_installed_table | cut -f1 -d" "
143
}
144

145

146
# args: script, host arch
147
__get_app_description()
148
{
149
    __run_script "$1" --description "$2" 2>/dev/null
150
}
151

152
__check_play_script()
153
{
154
    local script="$psdir/$1.sh"
155
    shift
156

157
    [ -x "$script" ]
158
}
159

160

161
__epm_play_run()
162
{
163
    local script="$psdir/$1.sh"
164
    shift
165

166
    local addopt
167
    addopt="$dryrun $non_interactive"
168

169
    local bashopt=''
170
    [ -n "$debug" ] && bashopt='-x'
171
    #info "Running $($script --description 2>/dev/null) ..."
172
    [ "$PROGDIR" = "/usr/bin" ] && SCPATH="$PATH" || SCPATH="$PROGDIR:$PATH"
173
    ( export EPM_OPTIONS="$EPM_OPTIONS $addopt" export PATH=$SCPATH ; docmd $CMDSHELL $bashopt $script "$@" )
174
}
175

176
__epm_play_list_installed()
177
{
178
    local i
179
    if [ -n "$short" ] ; then
180
        for i in $(__list_installed_app) ; do
181
            # skip hidden apps
182
            local desc="$(__get_app_description $i)"
183
            [ -n "$desc" ] || continue
184
            echo "$i"
185
        done
186
        exit
187
    fi
188
    [ -n "$quiet" ] || echo "Installed applications:"
189
    for i in $(__list_installed_app) ; do
190
        # skip hidden apps
191
        local desc="$(__get_app_description $i)"
192
        [ -n "$desc" ] || continue
193
        [ -n "$quiet" ] || echo -n "  "
194
        printf "%-20s - %s\n" "$i" "$desc"
195
    done
196
}
197

198

199
__epm_play_list()
200
{
201
    local psdir="$1"
202
    local extra="$2"
203
    local i
204
    local IGNOREi586
205
    local arch="$SYSTEMARCH"
206
    [ "$arch" = "x86_64" ] && IGNOREi586='' || IGNOREi586=1
207

208
    if [ -n "$short" ] ; then
209
        for i in $(__list_all_app) ; do
210
            local desc="$(__get_app_description $i $arch)"
211
            [ -n "$desc" ] || continue
212
            echo "$i"
213
            if [ -n "$extra" ] ; then
214
                for j in $(__run_script "$i" "--product-alternatives") ; do
215
                    echo "  $i=$j"
216
                done
217
            fi
218
        done
219
        exit
220
    fi
221

222
    for i in $(__list_all_app) ; do
223
        local desc="$(__get_app_description $i $arch)"
224
        [ -n "$desc" ] || continue
225
        [ -n "$quiet" ] || echo -n "  "
226
        printf "%-20s - %s\n" "$i" "$desc"
227
        if [ -n "$extra" ] ; then
228
            for j in $(__run_script "$i" "--product-alternatives") ; do
229
                printf "  %-20s - %s\n" "$i=$j" "$desc"
230
            done
231
        fi
232
    done
233
}
234

235

236
epm_play_help()
237
{
238
    cat <<EOF
239
Usage: epm play [options] [<app>]
240
Options:
241
    <app>                 - install <app>
242
    --remove <app>        - uninstall <app>
243
    --update [<app>|all]  - update <app> (or all installed apps) if there is new version
244
    --list                - list all installed apps
245
    --list-all            - list all available apps
246
    --list-scripts        - list all available scripts
247
    --short (with --list) - list names only
248
    --installed <app>     - check if the app is installed
249
    --product-alternatives- list alternatives (use like epm play app=beta)
250

251
Examples:
252
    epm play --remove opera
253
    epm play yandex-browser = beta
254
    epm play telegram = beta
255
    epm play telegram = 4.7.1
256
    epm play --update all
257
EOF
258
}
259

260

261
__epm_play_remove()
262
{
263
    local prescription
264
    for prescription in $* ; do
265
        if __check_play_script "$prescription" ; then
266
            __epm_play_run $prescription --remove
267
            __remove_installed_app "$prescription"
268
        else
269
            psdir=$prsdir
270
            __check_play_script "$prescription" || fatal "We have no idea how to remove $prescription (checked in $psdir and $prsdir)"
271
            __epm_play_run "$prescription" --remove || fatal "There was some error during run the script."
272
        fi
273
    done
274
}
275

276

277
__epm_play_update()
278
{
279
    local i RES
280
    local CMDUPDATE="$1"
281
    shift
282
    RES=0
283
    for i in $* ; do
284
        echo
285
        echo "$i"
286
            if ! __is_app_installed "$i" ; then
287
                warning "$i is not installed"
288
                continue
289
            fi
290
        prescription="$i"
291
        if ! __check_play_script $prescription ; then
292
            warning "Can't find executable play script for $prescription. Try epm play --remove $prescription if you don't need it anymore."
293
            RES=1
294
            continue
295
        fi
296
        __epm_play_run $prescription $CMDUPDATE || RES=$?
297
    done
298
    return $RES
299
}
300

301

302
# name argument
303
__epm_play_install_one()
304
{
305
    local prescription="$1"
306
    shift
307

308
    if __check_play_script "$prescription" ; then
309
        #__is_app_installed "$prescription" && info "$$prescription is already installed (use --remove to remove)" && exit 1
310
        __epm_play_run "$prescription" --run "$@" && __save_installed_app "$prescription" || fatal "There was some error during install the application."
311
    else
312
        opsdir=$psdir
313
        psdir=$prsdir
314
        __check_play_script "$prescription" || fatal "We have no idea how to play $prescription (checked in $opsdir and $prsdir)"
315
        __epm_play_run "$prescription" --run "$@" || fatal "There was some error during run $prescription script."
316
    fi
317
}
318

319

320
__epm_play_install()
321
{
322
   local i RES
323
   RES=0
324

325
   load_helper epm-check_updated_repo
326

327
   update_repo_if_needed
328

329
   # get all options
330
   options=''
331
   for i in  $* ; do
332
       case "$i" in
333
           --*)
334
               options="$options $i"
335
               ;;
336
       esac
337
   done
338

339
   while [ -n "$1" ] ; do
340
       case "$1" in
341
           --*)
342
               shift
343
               continue
344
               ;;
345
       esac
346
       local p="$1"
347
       local v=''
348
       # drop spaces
349
       n="$(echo $2)"
350
       if [ "$n" = "=" ] ; then
351
           v="$3"
352
           shift 3
353
       else
354
           shift
355
       fi
356
       __epm_play_install_one "$p" "$v" $options || RES=1
357
   done
358

359
   return $RES
360
}
361

362
__epm_play_download_epm_file()
363
{
364
    local target="$1"
365
    local file="$2"
366
    # use short version (3.4.5)
367
    local epmver="$(epm --short --version)"
368
    local URL
369
    for URL in "https://eepm.ru/releases/$epmver/app-versions" "https://eepm.ru/app-versions" ; do
370
        info "Updating local IPFS DB in $eget_ipfs_db file from $URL/eget-ipfs-db.txt"
371
        docmd eget -q -O "$target" "$URL/$file" && return
372
    done
373
}
374

375

376
__epm_play_initialize_ipfs()
377
{
378
    if [ ! -d "$(dirname "$eget_ipfs_db")" ] ; then
379
        warning "ipfs db dir $eget_ipfs_db does not exist, skipping IPFS mode"
380
        return 1
381
    fi
382

383
    if [ ! -r "$eget_ipfs_db" ] ; then
384
        sudorun touch "$eget_ipfs_db" >&2
385
        sudorun chmod -v a+rw "$eget_ipfs_db" >&2
386
    fi
387

388
    # download and merge with local db
389
    local t
390
    t=$(mktemp) || fatal
391
    remove_on_exit $t
392
    __epm_play_download_epm_file "$t" "eget-ipfs-db.txt" || warning "Can't update IPFS DB"
393
    if [ -s "$t" ] && [ -z "$EPM_IPFS_DB_UPDATE_SKIPPING" ] ; then
394
        echo >>$t
395
        cat $eget_ipfs_db >>$t
396
        sort -u < $t | grep -v "^$" > $eget_ipfs_db
397
    fi
398

399
    # the only one thing is needed to enable IPFS in eget
400
    export EGET_IPFS_DB="$eget_ipfs_db"
401
}
402

403
epm_play()
404
{
405
[ "$EPMMODE" = "package" -o "$EPMMODE" = "git" ] || fatal "epm play is not supported in single file mode"
406
local psdir="$(realpath $CONFIGDIR/play.d)"
407
local prsdir="$(realpath $CONFIGDIR/prescription.d)"
408

409
if [ -z "$1" ] ; then
410
    [ -n "$short" ] || [ -n "$quiet" ] || echo "Available applications (for current arch $DISTRARCH):"
411
    __epm_play_list $psdir
412
    exit
413
fi
414

415

416
while [ -n "$1" ] ; do
417
case "$1" in
418
    -h|--help)
419
        epm_play_help
420
        exit
421
        ;;
422

423
    --ipfs)
424
        shift
425
        __epm_play_initialize_ipfs
426
        ;;
427

428
    --remove)
429
        shift
430
        if [ -z "$1" ] ; then
431
            fatal "run --remove with 'all' or a project name"
432
        fi
433

434
        local list
435
        if [ "$1" = "all" ] ; then
436
            shift
437
            info "Retrieving list of installed apps ..."
438
            list="$(__list_installed_app)"
439
        else
440
            list="$*"
441
        fi
442

443
        __epm_play_remove $list
444
        exit
445
        ;;
446

447
    --update)
448
        shift
449
        local CMDUPDATE="--update"
450
        # check --force on common.sh side
451
        #[ -n "$force" ] && CMDUPDATE="--run"
452

453
        if [ -z "$1" ] ; then
454
            fatal "run --update with 'all' or a project name"
455
        fi
456

457
        local list
458
        if [ "$1" = "all" ] ; then
459
            shift
460
            info "Retrieving list of installed apps ..."
461
            list="$(__list_installed_app)"
462
        else
463
            list="$*"
464
        fi
465

466
        __epm_play_update $CMDUPDATE $list
467
        exit
468
        ;;
469

470
    --installed)
471
        shift
472
        __is_app_installed "$1" "$2"
473
        #[ -n "$quiet" ] && exit
474
        exit
475
        ;;
476

477
    # internal options
478
    --installed-version|--package-name|--product-alternatives|--info)
479
        __run_script "$2" "$1" "$3"
480
        exit
481
        ;;
482
    --list-installed-packages)
483
        __list_installed_packages
484
        exit
485
        ;;
486
    --list|--list-installed)
487
        __epm_play_list_installed
488
        exit
489
        ;;
490

491
    --full-list-all)
492
        [ -n "$short" ] || [ -n "$quiet" ] || echo "Available applications (for current arch $DISTRARCH):"
493
        __epm_play_list $psdir extra
494
        exit
495
        ;;
496

497
    --list-all)
498
        [ -n "$short" ] || [ -n "$quiet" ] || echo "Available applications (for current arch $DISTRARCH):"
499
        __epm_play_list $psdir
500
        [ -n "$quiet" ] || [ -n "$*" ] && exit
501
        echo
502
        #echo "Run epm play --help for help"
503
        epm_play_help
504
        exit
505
        ;;
506

507
    --list-scripts)
508
        [ -n "$short" ] || [ -n "$quiet" ] || echo "Run with a name of a play script to run:"
509
        __epm_play_list $prsdir
510
        exit
511
        ;;
512
    -*)
513
        fatal "Unknown option $1"
514
        ;;
515
     *)
516
        break
517
        ;;
518
esac
519

520
done
521

522
__epm_play_install $(echo "$*" | sed -e 's|=| = |g')
523
}
524

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

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

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

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