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

20
load_helper epm-query
21

22 23 24

__replace_text_in_alt_repo()
{
25 26 27 28 29 30 31
    local i
    for i in /etc/apt/sources.list /etc/apt/sources.list.d/*.list ; do
        [ -s "$i" ] || continue
        # TODO: don't change file if untouched
        #grep -q -- "$1" "$i" || continue
        regexp_subst "$1" "$i"
    done
32 33
}

34 35
__repofix_check_vendor()
{
36 37 38 39 40 41
    local i
    for i in /etc/apt/vendors.list.d/*.list; do
        [ -e "$i" ] || continue
        grep -q "^simple-key \"$1\"" $i && return
    done
    return 1
42 43
}

44 45
__repofix_filter_vendor()
{
46 47 48 49 50 51 52
    local br="$1"
    br="$(echo "$br" | sed -e "s|\..*||")"
    case $br in
        c8*)
            br="cert8"
            ;;
        c9*)
53 54 55 56
            br="cert8"
            ;;
        c10*)
            br="cert8"
57 58 59 60 61 62
            ;;
        Sisyphus)
            br="alt"
            ;;
    esac
    echo "$br"
63 64
}

65 66 67

__replace_alt_version_in_repo()
{
68 69 70 71 72 73 74 75 76
    local i
    assure_exists apt-repo
    #echo "Upgrading $DISTRNAME from $1 to $2 ..."
    a='' apt-repo list | sed -E -e "s|($1)|{\1}->{$2}|g" | grep -E --color -- "$1"
    # ask and replace only we will have changes
    if a='' apt-repo list | grep -E -q -- "$1" ; then
        __replace_text_in_alt_repo "/^ *#/! s!$1!$2!g"
    fi
    #docmd apt-repo list
77 78 79 80
}

__alt_replace_sign_name()
{
81 82 83 84 85
    local TO="$1"
    __replace_text_in_alt_repo "/^ *#/! s!\[alt\]!$TO!g"
    __replace_text_in_alt_repo "/^ *#/! s!\[sisyphus\]!$TO!g"
    __replace_text_in_alt_repo "/^ *#/! s!\[updates\]!$TO!g"
    __replace_text_in_alt_repo "/^ *#/! s!\[cert[789]\]!$TO!g"
86
    __replace_text_in_alt_repo "/^ *#/! s!\[[tcp][1-3]?[0-9][.f]?[0-9]?\]!$TO!g"
87 88 89 90
}

__alt_repofix()
{
91 92 93 94 95 96 97
    local TO="$1"
    epm --quiet repo fix >/dev/null
    if [ -n "$TO" ] ; then
        # TODO: switch it in repo code
        TO="$(__repofix_filter_vendor "$TO")"
        __alt_replace_sign_name "[$TO]"
    fi
98 99
}

100
__alt_branch_reg='[tcp][1-3]?[0-9][.f]?[0-9]?'
101

102 103 104
#__switch_repo_to()
epm_reposwitch()
{
105 106 107 108
    local TO="$1"
    [ -n "$TO" ] || fatal "run repo switch with arg (p9, p10, Sisyphus)"
    [ "$TO" = "sisyphus" ] && TO="Sisyphus"
    if [ "$TO" = "Sisyphus" ] ; then
109
        __replace_alt_version_in_repo "$__alt_branch_reg/branch/" "$TO/"
110 111
    else
        __replace_alt_version_in_repo "Sisyphus/" "$TO/branch/"
112
        __replace_alt_version_in_repo "$__alt_branch_reg/branch/" "$TO/branch/"
113
    fi
114

115
    __alt_repofix "$TO"
116

117
    # TODO: improve for c10f1?
118 119 120 121 122 123 124 125 126 127 128 129
    case $TO in
        "p10"|"p11"|"Sisyphus")
            rm -fv /etc/rpm/macros.d/{p10,p11} 

            [ "$TO" = "Sisyphus" ] && TO="sisyphus"
            
            echo "%_priority_distbranch $TO" >/etc/rpm/macros.d/priority_distbranch
            ;;
        *)
            rm -fv /etc/rpm/macros.d/{p10,p11,priority_distbranch}
            ;;
    esac
130
    #epm repo list
131 132 133
}


134 135 136 137
# source-list vendor path
# example: /etc/apt/source.list p7 ALTLinux\/Sisyphus
__try_fix_apt_source_list()
{
138 139 140
    local list="$1"
    local br="$(__repofix_filter_vendor "$2")"
    local path="$3"
141 142
    # FIXME: masked grep: предупреждение: stray \ before /
    if grep -q -e "^[^#].*$path" $list 2>/dev/null ; then
143 144 145 146 147 148 149
        if __repofix_check_vendor $br ; then
            regexp_subst "/$path/s/^rpm[[:space:]]*([fhr])/rpm [$br] \1/" $list
        else
            warning "Skip set $br vendor key (it is missed) for $list"
            regexp_subst "/$path/s/^rpm[[:space:]]*\[$br\][[:space:]]*([fhr])/rpm \1/" $list
        fi
    fi
150 151
}

152
__fix_alt_sources_list()
153
{
154 155
    # for beauty spaces
    local SUBST_ALT_RULE1='s!^(.*)[/ ](ALTLinux|LINUX\@Etersoft)[/ ]*(Sisyphus)[/ ](x86_64|i586|x86_64-i586|noarch|aarch64) !\1 \2/\3/\4 !gi'
156
    local SUBST_ALT_RULE2='s!^(.*)[/ ](ALTLinux|LINUX\@Etersoft)[/ ]*('$__alt_branch_reg'[/ ]branch)[/ ](x86_64|i586|x86_64-i586|noarch|aarch64) !\1 \2/\3/\4 !gi'
157
    local i
158

159 160 161 162 163 164
    for i in "$@" ; do
        [ -s "$i" ] || continue
        #perl -i.bak -pe "$SUBST_ALT_RULE" $i
        # TODO: only for uncommented strings
        #sed -i -r -e "$SUBST_ALT_RULE" $i
        regexp_subst "/^ *#/! s| pub|/pub|" $i
165
        regexp_subst "/^ *#/! s| distributions|/distributions|" $i
166 167
        regexp_subst "/^ *#/! $SUBST_ALT_RULE1" $i
        regexp_subst "/^ *#/! $SUBST_ALT_RULE2" $i
168

169 170 171
        # Sisyphus uses 'alt' vendor key
        __try_fix_apt_source_list $i alt "ALTLinux\/Sisyphus"
        __try_fix_apt_source_list $i etersoft "Etersoft\/Sisyphus"
172

173 174
        # skip branch replacement for ALT Linux Sisyphus
        [ "$DISTRVERSION" = "Sisyphus" ] && continue
175

176 177 178 179
        # add signs for branches
        __try_fix_apt_source_list $i $DISTRVERSION "ALTLinux\/$DISTRVERSION\/branch"
        __try_fix_apt_source_list $i etersoft "Etersoft\/$DISTRVERSION\/branch"
    done
180 181
}

182

183
__subst_with_repo_url()
184
{
185
    local NURL="$2"
186
    echo "$1" | sed \
187 188 189 190 191 192 193
        -e "s|//mirror.yandex.ru/* altlinux|$NURL|" \
        -e "s|//ftp.altlinux.org/pub/distributions/* ALTLinux|$NURL|" \
        -e "s|//ftp.basealt.ru/pub/distributions/* ALTLinux|$NURL|" \
        -e "s|//update.altsp.su/pub/distributions/* ALTLinux|$NURL|" \
        -e "s|//ftp.etersoft.ru/pub/* ALTLinux|$NURL|" \
        -e "s|//download.etersoft.ru/pub/* ALTLinux|$NURL|" \
        -e "s|//mirror.eterfund.org/download.etersoft.ru/pub/* ALTLinux|$NURL|"
194 195
}

196
__change_repo()
197
{
198 199
    local SHORT="$1"
    local REPLTO="$2"
200
    local NN
201 202
    a="" apt-repo list | grep -v $SHORT | grep -v "file:/" | while read nn ; do
        NN="$(__subst_with_repo_url "$nn" "$REPLTO")"
203
        [ "$NN" = "$nn" ] && continue
204
        epm addrepo "$NN" && epm removerepo "$nn" || return 1
205
    done
206 207
}

208

209
__epm_repochange_alt()
210
{
211
    case "$1" in
212
        "etersoft")
213
            __change_repo etersoft "//download.etersoft.ru/pub ALTLinux"
214 215
            ;;
        "eterfund.org")
216
            __change_repo eterfund.org "//mirror.eterfund.org/download.etersoft.ru/pub ALTLinux"
217 218
            ;;
        "yandex")
219
            __change_repo mirror.yandex "//mirror.yandex.ru altlinux"
220 221
            ;;
        "basealt")
222
            __change_repo ftp.basealt "//ftp.basealt.ru/pub/distributions ALTLinux"
223 224
            ;;
        "altlinux.org")
225 226
            __change_repo ftp.altlinux "//ftp.altlinux.org/pub/distributions ALTLinux"
            ;;
227
        *)
228
            fatal 'Unsupported change key $1'
229 230 231 232 233 234 235 236
            ;;
    esac
}


epm_repochange()
{

237
    epm_repofix
238 239 240 241 242
    case $BASEDISTRNAME in
        "alt")
            __epm_repochange_alt "$1"
            ;;
         *)
243
            fatal 'Repo change Unsupported for $BASEDISTRNAME'
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
            ;;
    esac
}


epm_repofix()
{

case $BASEDISTRNAME in
    "alt")
        assure_exists apt-repo
        [ -n "$quiet" ] || docmd apt-repo list
        assure_root

        __fix_alt_sources_list /etc/apt/sources.list
        __fix_alt_sources_list /etc/apt/sources.list.d/*.list

        [ -n "$quiet" ] || docmd apt-repo list
262 263
        return
        ;;
264 265 266
esac

case $PMTYPE in
267 268 269 270 271
#    apt-rpm)
#        ;;
#    yum-rpm|dnf-rpm)
#        ;;
    *)
272
        fatal 'Have no suitable command for $PMTYPE'
273
        ;;
274 275 276
esac

}