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

load_helper epm-sh-altlinux
load_helper epm-assure

23 24 25 26 27 28
# file prefix suffix
__epm_restore_print_comment()
{
    echo "#$2 generated by 'epm restore --dry-run' from $(basename $(dirname $(realpath "$1")))/$(basename "$1")$3"
}

29
# FIXME: python modules are packaged into python packages, but we have only module names and rpm package names instead of python packages
30 31
# enable python3dest(PEP-503 normalized name) provides
# https://bugzilla.altlinux.org/show_bug.cgi?id=39003
32 33
__epm_filter_pip_to_rpm()
{
34
    tr "A-Z" "a-z" | sed -e "s|_|-|g" -e "s|^python[-_]||" -e "s|python$||" \
35
        -e "s|bs4|beautifulsoup4|" \
36
        -e "s|pillow|Pillow|" \
37 38
        -e "s|sqlalchemy|SQLAlchemy|" \
        -e "s|flask-SQLAlchemy|flask_sqlalchemy|" \
39
        -e "s|redis|redis-py|" \
40
        -e "s|pyjwt|jwt|" \
41
        -e "s|pymonetdb|monetdb|" \
42
        -e "s|pyyaml|yaml|" \
43
        -e "s|flask-migrate|Flask-Migrate|" \
44 45
        -e "s|twisted|twisted-core|" \
        -e "s|pymacaroons|pymacaroons-pynacl|" \
46
        -e "s|pygments|Pygments|" \
47
        -e "s|memcached|memcache|" \
48
        -e "s|pyinstaller||" \
49 50 51
        -e "s|pyopenssl|OpenSSL|"
}

52 53 54 55
# TODO: remove me
fill_sign()
{
    local sign="$1"
56
    echo "$2" | grep -E -- "$sign[[:space:]]*[0-9.]+?" | sed -E -e "s|.*$sign[[:space:]]*([0-9.]+?).*|\1|"
57 58 59
}


60 61 62
# macro pkg caseline
__epm_pi_sign_to_rpm()
{
63 64 65 66
    local t="$1"
    local l="$2"
    local equal="$3"
    [ -n "$equal" ] || equal=">="
67 68 69

    local pi=''
    local sign ll
70
    for sign in "<=" "<" ">=" ">" "==" "!=" "~="; do
71 72 73
        ll=$(fill_sign "$sign" "$l")
        [ -n "$ll" ] || continue
        [ "$sign" = "==" ] && sign="$equal"
74
        [ "$sign" = "~=" ] && sign="$equal"
75 76 77
        [ "$sign" = "!=" ] && sign=">="
        [ -n "$pi" ] && pi="$pi
"
78
        pi="$pi$t $sign $ll"
79
    done
80
    [ -n "$pi" ] || pi="$t"
81 82 83
    echo "$pi"
}

84
__epm_get_array_name()
85
{
86 87
    echo "$*" | grep "=" | head -n1 | sed -e 's| *=.*||'
}
88

89 90 91 92 93
__epm_lineprint_python_array()
{
    local a="$*"
    [ -n "$a" ] || return
    local name="$(__epm_get_array_name "$a")"
94
    (echo "$a" | sed -E -e 's@(\]|\)).*@\1@' ; echo "print('\n'.join($name))" ) | ( a= python3 - || a= python - )
95 96 97 98 99 100 101 102
}

# translate pip requirement lines to rpm notation
# (compare signs and package names)
__epm_restore_convert_to_rpm_notation()
{
    local equal="$1"
    local l
103
    while read l ; do
104 105 106 107 108 109 110 111 112 113 114 115
        if echo "$l" | grep -q 'platform_python_implementation != "PyPy"' ; then
            [ -n "$verbose" ] && warning "    $t is not PyPi requirement, skipped"
            continue
        fi
        if echo "$l" | grep -q 'sys_platform == "darwin"' ; then
            [ -n "$verbose" ] && warning "    $t is darwin only requirement, skipped"
            continue
        fi
        if echo "$l" | grep -q 'sys_platform == "win32"' ; then
            [ -n "$verbose" ] && warning "    $t is win32 only requirement, skipped"
            continue
        fi
116
        if echo "$l" | grep -q "; *python_version *< *['\"]3" ; then
117 118 119
            [ -n "$verbose" ] && warning "    $t is python2 only requirement, skipped"
            continue
        fi
120 121 122 123
        if echo "$l" | grep -q "; *python_version *<= *['\"]2\." ; then
            [ -n "$verbose" ] && warning "    $t is python2 only requirement, skipped"
            continue
        fi
124 125
        # drop various "python_version > '3.5'"
        l="$(echo "$l" | sed -e "s| *;.*||")"
126
        if echo "$l" | grep -qE "^ *#" || [ -z "$l" ] ; then
127 128
            continue
        fi
129
        local t="$(echo "$l" | sed -E -e "s|[[:space:]]*[<>!=~]+.*||" -e "s| *#.*||" | __epm_filter_pip_to_rpm)"
130
        [ -n "$t" ] || continue
131 132 133 134 135
        # until new section
        if echo "$l" | grep -qE "^\[" ; then
            break
        fi
        # if dependency_links URLs, use egg name
136 137 138 139
        if echo "$l" | grep -qE "://" ; then
            if echo "$l" | grep -q "#egg=" ; then
                t="$(echo "$l" | sed -e "s|.*#egg=||" -e "s|\[.*||" | __epm_filter_pip_to_rpm)"
            else
140
                warning "    skipping URL $l ..."
141 142 143
                continue
            fi
        fi
144

145 146 147 148 149 150 151 152 153
        __epm_pi_sign_to_rpm "$t" "$l" "$equal"
    done
}

__epm_restore_pip()
{
    local req_file="$1"
    local reqmacro
    local ilist
154

155
    if [ -n "$dryrun" ] ; then
156
        reqmacro="%py3_use"
157
        basename "$req_file" | egrep -q "(dev|test|coverage)" && reqmacro="%py3_buildrequires"
158
        echo
159
        __epm_restore_print_comment "$req_file"
160
        cat $req_file | __epm_restore_convert_to_rpm_notation | sed -e "s|^|$reqmacro |"
161
        return
162 163 164
    else
        info "Install requirements from $req_file ..."
        ilist="$(cat $req_file | __epm_restore_convert_to_rpm_notation | cut -d' ' -f 1 | sed -e "s|^|python3-module-|")"
165 166
    fi

167
    ilist="$(estrlist list $ilist)"
168 169 170
    docmd epm install $ilist
}

171 172
__eresection()
{
173
    rhas "$1" "[[:space:]]*$2[[:space:]]*=[[:space:]]*[\[(]"
174 175
}

176 177 178 179 180 181 182
__epm_restore_setup_py()
{
    local req_file="$1"
    if [ -z "$dryrun" ] ; then
        info "Install requirements from $req_file ..."
    fi

183
    local ar=''
184 185
    local ilist=''
    local reqmacro
186
    local section=''
187 188 189 190
    while read l ; do
        if rhas "$l" "^ *#" ; then
            continue
        fi
191 192 193 194
        # start of section
        if __eresection "$l" "REQUIREMENTS" ; then
            reqmacro="%py3_use"
            section="$l"
195
        fi
196
        if __eresection "$l" "install_requires" ; then
197
            reqmacro="%py3_use"
198
            section="$l"
199
        fi
200
        if __eresection "$l" "setup_requires" ; then
201
            reqmacro="%py3_buildrequires"
202 203 204 205 206 207 208 209 210 211 212 213
            section="$l"
        fi
        if __eresection "$l" "tests_require" ; then
            reqmacro="%py3_buildrequires"
            section="$l"
        fi
        if [ -n "$section" ] ; then
            ar="$ar
$l"
        fi

        # not end of section
214
        if [ -z "$section" ] || ! rhas "$l" "(\]|\)),*" ; then
215 216
            continue
        fi
217 218 219

        if [ -n "$dryrun" ] ; then
            echo
220
            __epm_restore_print_comment "$req_file" "" " $(__epm_get_array_name "$section")"
221 222 223 224 225 226
            __epm_lineprint_python_array "$ar" | __epm_restore_convert_to_rpm_notation ">=" | sed -e "s|^|$reqmacro |"
        else
            ilist="$ilist $(__epm_lineprint_python_array "$ar" | __epm_restore_convert_to_rpm_notation ">=" | cut -d' ' -f 1 | sed -e "s|^|python3-module-|")"
        fi
        section=''
        ar=''
227 228 229 230 231
    done < $req_file

    if [ -n "$dryrun" ] ; then
        return
    fi
232

233
    ilist="$(estrlist list $ilist)"
234
    docmd epm install $ilist
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
__epm_print_npm_list()
{
    local reqmacro="$1"
    local req_file="$2"
    local l
    while read l ; do
        # "tap": "^14.10.7"
        echo "$l" | grep -q '"\(.*\)": "\(.*\)"' || continue
        local name="$(echo "$l" | sed -e 's|.*"\(.*\)": ".*|\1|')"
        [ -z "$name" ] && continue
        local ver="$(echo "$l" | sed -e 's|.*"\(.*\)": "\(.*\)".*|\2|')" #'
        [ -z "$name" ] && continue

        if [ -n "$dryrun" ] ; then
            local pi=''
            local sign
            if echo "$ver" | grep -q "^\^" ; then
                sign=">="
            else
                sign="="
            fi
            ll=$(echo "$ver" | sed -e 's|^[^~]||')
            pi="$pi$reqmacro node-$name $sign $ll"
#         [ -n "$pi" ] && pi="$pi
#"
#           [ -n "$pi" ] || pi="$pi$reqmacro: node-$t"
            echo "$pi"
            continue
        else
            local pi="node-$name"
            #echo "    $l -> $name -> $pi"
        fi
        [ -n "$name" ] || continue
        ilist="$ilist $pi"
    done < $req_file

    [ -n "$dryrun" ] || echo "$ilist"
}

276

277
__epm_print_perl_list()
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
{
    local reqmacro="$1"
    local req_file="$2"
    local l
    for l in $(cat) ; do
        # perl(Class::ErrorHandler)>=0
        echo "$l" | grep -q '^perl(' || continue
        local name="$(echo "$l" | sed -e 's|>=.*||' -e 's|::|/|g' -e 's|)|.pm)|')"
        [ "$name" = "perl(perl.pm)" ] && continue
        [ -z "$name" ] && continue
        local ver="$(echo "$l" | sed -e 's|.*>=||')"
        [ -z "$name" ] && continue

        if [ -n "$dryrun" ] ; then
            local pi=''
            local sign=''
            [ "$ver" = "0" ] || sign=" >= $ver"
            pi="$pi$reqmacro $name$sign"
            echo "$pi"
            continue
        else
            local pi="$name"
            #echo "    $l -> $name -> $pi"
        fi
        [ -n "$name" ] || continue
        ilist="$ilist $pi"
    done < $req_file

    [ -n "$dryrun" ] || echo "$ilist"
}

__epm_print_perl_list_shyaml()
310 311 312 313 314 315 316
{
    local reqmacro="$1"
    local req_file="$2"
    local l
    while read l ; do
        # Convert::ASN1: 0.10
        echo "$l" | grep -q '^ *\(.*\): \(.*\)' || continue
317 318
        local name="$(echo "$l" | sed -e 's| *\(.*\): \(.*\)|\1|' -e 's|::|/|g')".pm
        [ "$name" = "perl.pm" ] && continue
319 320 321 322 323 324 325 326
        [ -z "$name" ] && continue
        local ver="$(echo "$l" | sed -e 's| *\(.*\): \(.*\)|\2|')"
        [ -z "$name" ] && continue

        if [ -n "$dryrun" ] ; then
            local pi=''
            local sign=''
            [ "$ver" = "0" ] || sign=" >= $ver"
327
            pi="$pi$reqmacro perl($name)$sign"
328 329 330
            echo "$pi"
            continue
        else
331
            local pi="perl($name)"
332 333 334 335 336 337 338 339 340
            #echo "    $l -> $name -> $pi"
        fi
        [ -n "$name" ] || continue
        ilist="$ilist $pi"
    done < $req_file

    [ -n "$dryrun" ] || echo "$ilist"
}

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

__epm_print_nupkg_list()
{
    a= dotnet list $1 package | grep "^   > " | while read n name req other; do
        if [ -n "$dryrun" ] ; then
            echo "BuildRequires: nupkg($name) >= $req"
        else
            echo "nupkg($name)"
        fi
    done
}

__epm_restore_nupkg()
{
    local req_file="$1"
    if [ -n "$dryrun" ] ; then
        echo "# generated via dotnet list $(basename $(dirname $(realpath "$req_file")))/$(basename "$req_file") package"
        __epm_print_nupkg_list $req_file
        return
    fi

    info "Install requirements from $req_file ..."
    ilist=$(__epm_print_nupkg_list $req_file)
    ilist="$(estrlist list $ilist)"
    docmd epm install $ilist
}

368 369 370 371
__epm_restore_npm()
{
    local req_file="$1"

372
    assure_exists jq || fatal
373 374 375 376 377

    if [ -n "$dryrun" ] ; then
        local lt=$(mktemp)
        a= jq .dependencies <$req_file >$lt
        echo
378
        __epm_restore_print_comment "$req_file"
379 380 381
        __epm_print_npm_list "Requires:" $lt

        echo
382
        __epm_restore_print_comment "$req_file" " devDependencies"
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
        a= jq .devDependencies <$req_file >$lt
        __epm_print_npm_list "BuildRequires:" $lt
        rm -f $lt
        return
    fi

    info "Install requirements from $req_file ..."
    local lt=$(mktemp)
    a= jq .dependencies <$req_file >$lt
    ilist="$(__epm_print_npm_list "" $lt)"
    a= jq .devDependencies <$req_file >$lt
    ilist="$ilist $(__epm_print_npm_list "" $lt)"
    rm -f $lt
    docmd epm install $ilist
}

399
# TODO: check 
400 401 402 403
__epm_restore_perl()
{
    local req_file="$1"

404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
    if [ -n "$dryrun" ] ; then
        local lt=$(mktemp)
        a= /usr/bin/perl $req_file PRINT_PREREQ=1 >$lt
        # all requirements will autodetected during packing, put it to the buildreq
        echo
        __epm_restore_print_comment "$req_file"
        __epm_print_perl_list "BuildRequires:" $lt
        rm -f $lt
        return
    fi

    info "Install requirements from $req_file ..."
    local lt=$(mktemp)
    a= /usr/bin/perl $req_file PRINT_PREREQ=1 >$lt
    ilist="$(__epm_print_perl_list "" $lt)"
    rm -f $lt
    docmd epm install $ilist
}

# disabled
__epm_restore_perl_shyaml()
{
    local req_file="$1"

428
    assure_exists shyaml || fatal
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

    if [ -n "$dryrun" ] ; then
        local lt=$(mktemp)
        a= shyaml get-value requires <$req_file >$lt
        # all requirements will autodetected during packing, put it to the buildreq
        echo
        __epm_restore_print_comment "$req_file"
        __epm_print_perl_list "BuildRequires:" $lt

        echo
        __epm_restore_print_comment "$req_file" " build_requires"
        a= shyaml get-value build_requires <$req_file >$lt
        __epm_print_perl_list "BuildRequires:" $lt
        rm -f $lt
        return
    fi

    info "Install requirements from $req_file ..."
    local lt=$(mktemp)
    a= shyaml get-value requires <$req_file >$lt
    ilist="$(__epm_print_perl_list "" $lt)"
    a= shyaml get-value build_requires <$req_file >$lt
    ilist="$ilist $(__epm_print_perl_list "" $lt)"
    rm -f $lt
    docmd epm install $ilist
}

456 457 458 459
__epm_restore_by()
{
    local req_file="$1"
    [ -s "$req_file" ] || return
460 461 462 463 464

    if file $req_file | grep -q "ELF [3264]*-bit LSB executable" ; then
        assure_exists ldd-requires
        showcmd ldd-requires $req_file
        local TOINSTALL="$(a= ldd-requires $req_file | grep "^apt-get install" | sed -e "s|^apt-get install ||")"
465 466 467 468
        if [ -n "$dryrun" ] ; then
            estrlist list $TOINSTALL
            return
        fi
469
        [ -n "$TOINSTALL" ] || { info "There are no missed packages is found for $req_file binary." ; return ; }
470
        docmd epm install $TOINSTALL
471 472 473
        return
    fi

474
    case $req_file in
475
        requirements/default.txt|requirements/dev.txt|requirements/test.txt|requirements/coverage.txt)
476 477 478 479
            [ -s "$req_file" ] && __epm_restore_pip "$req_file" && return
            ;;
    esac

480
    case $(basename $req_file) in
481
        requirements.txt|dev-requirements.txt|requirements-dev.txt|requirements_dev.txt|requirements_test.txt|requirements-test.txt|test-requirements.txt|requires.txt)
482 483
            [ -s "$req_file" ] && __epm_restore_pip "$req_file"
            ;;
484
        setup.py|python_dependencies.py)
485 486
            [ -s "$req_file" ] && __epm_restore_setup_py "$req_file"
            ;;
487 488 489
        package.json)
            [ -s "$req_file" ] && __epm_restore_npm "$req_file"
            ;;
490 491 492 493
#        META.yml)
#            [ -s "$req_file" ] && __epm_restore_perl "$req_file"
#            ;;
        Makefile.PL)
494 495
            [ -s "$req_file" ] && __epm_restore_perl "$req_file"
            ;;
496 497 498 499
        *.sln|*.csproj)
            local PROJ="$(echo $req_file)"
            [ -s "$PROJ" ] && __epm_restore_nupkg "$PROJ"
            ;;
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
        Gemfile|package.json)
            info "$req_file support is not implemented yet"
            ;;
    esac
}

epm_restore()
{
    req_file="$pkg_filenames"
    if [ -n "$pkg_urls" ] && echo "$pkg_urls" | grep -qE "^https?://" ; then
        req_file="$(basename "$pkg_urls")"
        #assure eget
        [ -r "$req_file" ] && fatal "File $req_file is already exists in $(pwd)"
        info "Downloading '$req_file' from '$pkg_urls' ..."
        eget "$pkg_urls"
        [ -s "$req_file" ] || fatal "Can't download $req_file from '$pkg_urls'"
    fi

    if [ -n "$req_file" ] ; then
        __epm_restore_by $req_file
        return
    fi

523 524
# TODO: nowhere works: python3 setup.py --requires

525
    # if run with empty args
526
    for i in requirements.txt requirements/default.txt requirements_dev.txt requirements-dev.txt requirements/dev.txt dev-requirements.txt \
527
             requirements-test.txt requirements_test.txt requirements/test.txt test-requirements.txt requirements/coverage.txt \
528
             Gemfile requires.txt package.json setup.py python_dependencies.py Makefile.PL \
529
             *.sln *.csproj ; do
530 531 532 533
        __epm_restore_by $i
    done

}