epm-repofix 7.02 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 25 26 27

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

34 35 36 37 38 39 40 41 42 43
__repofix_check_vendor()
{
	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
}

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

59 60 61 62 63 64

__replace_alt_version_in_repo()
{
	local i
	assure_exists apt-repo
	#echo "Upgrading $DISTRNAME from $1 to $2 ..."
65
	a='' apt-repo list | sed -E -e "s|($1)|{\1}->{$2}|g" | grep -E --color -- "$1"
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	# 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
}

__alt_replace_sign_name()
{
	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"
	__replace_text_in_alt_repo "/^ *#/! s!\[p10\.?[0-9]?\]!$TO!g"
	__replace_text_in_alt_repo "/^ *#/! s!\[[tpc][6-9]\.?[0-9]?\]!$TO!g"
}

__alt_repofix()
{
	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
}

#__switch_repo_to()
epm_reposwitch()
{
	local TO="$1"
	[ -n "$TO" ] || fatal "run repo switch with arg (p9, p10, Sisyphus)"
	__replace_alt_version_in_repo "Sisyphus/" "$TO/branch/"
	__replace_alt_version_in_repo "[tpc][5-9]\.?[0-9]?/branch/" "$TO/branch/"
102 103 104 105 106 107
	if [ "$TO" != "p10" ] ; then
		__replace_alt_version_in_repo "p10\.?[0-9]?/branch/" "$TO/branch/"
	fi

	__alt_repofix "$TO"

108 109 110 111 112 113 114 115 116
	if [ "$TO" = "p10" ] ; then
		echo '%_priority_distbranch p10' >/etc/rpm/macros.d/p10
	else
		rm -fv /etc/rpm/macros.d/p10
	fi
	#epm repo list
}


117 118 119 120 121
# source-list vendor path
# example: /etc/apt/source.list p7 ALTLinux\/Sisyphus
__try_fix_apt_source_list()
{
	local list="$1"
122
	local br="$(__repofix_filter_vendor "$2")"
123 124 125 126 127
	local path="$3"
	if grep -q -e "^[^#].*$path" $list ; then
		if __repofix_check_vendor $br ; then
			regexp_subst "/$path/s/^rpm[[:space:]]*([fhr])/rpm [$br] \1/" $list
		else
Vitaly Lipatov's avatar
Vitaly Lipatov committed
128
			warning "Skip set $br vendor key (it is missed) for $list"
129 130 131 132 133
			regexp_subst "/$path/s/^rpm[[:space:]]*\[$br\][[:space:]]*([fhr])/rpm \1/" $list
		fi
	fi
}

134
__fix_alt_sources_list()
135
{
136
	# for beauty spaces
137 138
	local SUBST_ALT_RULE1='s!^(.*)[/ ](ALTLinux|LINUX\@Etersoft)[/ ]*(Sisyphus)[/ ](x86_64|i586|x86_64-i586|noarch|aarch64) !\1 \2/\3/\4 !gi'
	local SUBST_ALT_RULE2='s!^(.*)[/ ](ALTLinux|LINUX\@Etersoft)[/ ]*([tcp][6-9]\.?[0-9]?[/ ]branch|[tcp]1[012][/ ]branch)[/ ](x86_64|i586|x86_64-i586|noarch|aarch64) !\1 \2/\3/\4 !gi'
Vitaly Lipatov's avatar
Vitaly Lipatov committed
139
	local i
140

141 142
	for i in "$@" ; do
		[ -s "$i" ] || continue
143
		#perl -i.bak -pe "$SUBST_ALT_RULE" $i
Vitaly Lipatov's avatar
Vitaly Lipatov committed
144 145
		# TODO: only for uncommented strings
		#sed -i -r -e "$SUBST_ALT_RULE" $i
146
		regexp_subst "/^ *#/! s| pub|/pub|" $i
147 148
		regexp_subst "/^ *#/! $SUBST_ALT_RULE1" $i
		regexp_subst "/^ *#/! $SUBST_ALT_RULE2" $i
149

150
		# Sisyphus uses 'alt' vendor key
151
		__try_fix_apt_source_list $i alt "ALTLinux\/Sisyphus"
152
		__try_fix_apt_source_list $i etersoft "Etersoft\/Sisyphus"
153 154 155 156

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

157
		# add signs for branches
158 159
		__try_fix_apt_source_list $i $DISTRVERSION "ALTLinux\/$DISTRVERSION\/branch"
		__try_fix_apt_source_list $i etersoft "Etersoft\/$DISTRVERSION\/branch"
160 161 162
	done
}

163 164 165 166
__subst_with_etersoft_url()
{
	local NURL="http://download.etersoft.ru/pub ALTLinux"
	echo "$1" | sed \
167 168 169 170 171 172 173 174 175 176 177
		-e "s|h\?f\?t\?tp://ftp.altlinux.org/pub/distributions/* ALTLinux|$NURL|" \
		-e "s|h\?f\?t\?tp://ftp.basealt.ru/pub/distributions/* ALTLinux|$NURL|" \
		-e "s|h\?f\?t\?tp://mirror.yandex.ru/* altlinux|$NURL|"
}

__subst_with_yandex_url()
{
	local NURL="http://mirror.yandex.ru altlinux"
	echo "$1" | sed \
		-e "s|h\?f\?t\?tp://ftp.altlinux.org/pub/distributions/* ALTLinux|$NURL|" \
		-e "s|h\?f\?t\?tp://ftp.basealt.ru/pub/distributions/* ALTLinux|$NURL|" \
178
		-e "s|h\?f\?t\?tp://ftp.etersoft.ru/pub/* ALTLinux|$NURL|" \
179 180 181 182 183 184 185 186
		-e "s|h\?f\?t\?tp://download.etersoft.ru/pub/* ALTLinux|$NURL|"
}

__subst_with_basealt_url()
{
	local NURL="http://ftp.basealt.ru/pub/distributions ALTLinux"
	echo "$1" | sed \
		-e "s|h\?f\?t\?tp://mirror.yandex.ru/* altlinux|$NURL|" \
187
		-e "s|h\?f\?t\?tp://ftp.etersoft.ru/pub/* ALTLinux|$NURL|" \
188
		-e "s|h\?f\?t\?tp://download.etersoft.ru/pub/* ALTLinux|$NURL|"
189 190 191 192 193
}

__fix_repo_to_etersoft()
{
	local NN
194
	a="" apt-repo list | grep -v debuginfo | grep -v etersoft | grep -v "file:/" | while read nn ; do
195
		NN="$(__subst_with_etersoft_url "$nn")"
196 197
		[ "$NN" = "$nn" ] && continue
		epm removerepo "$nn"
198
		epm addrepo "$NN"
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
	done
}

__fix_repo_to_yandex()
{
	local NN
	a="" apt-repo list | grep -v debuginfo | grep -v mirror\.yandex | grep -v "file:/" | while read nn ; do
		NN="$(__subst_with_yandex_url "$nn")"
		[ "$NN" = "$nn" ] && continue
		epm removerepo "$nn"
		epm addrepo "$NN"
	done
}

__fix_repo_to_basealt()
{
	local NN
	a="" apt-repo list | grep -v debuginfo | grep -v ftp.basealt | grep -v "file:/" | while read nn ; do
		NN="$(__subst_with_basealt_url "$nn")"
		[ "$NN" = "$nn" ] && continue
219
		epm removerepo "$nn"
220
		epm addrepo "$NN"
221 222 223
	done
}

224

Vitaly Lipatov's avatar
Vitaly Lipatov committed
225
epm_repofix()
226
{
227

228
case $DISTRNAME in
229
	ALTLinux|ALTServer)
230
		assure_exists apt-repo
231
		[ -n "$quiet" ] || docmd apt-repo list
232
		assure_root
233 234
		__fix_alt_sources_list /etc/apt/sources.list
		__fix_alt_sources_list /etc/apt/sources.list.d/*.list
235
        # TODO: move to repo change
236
		if [ "$1" = "etersoft" ] ; then
237 238 239
			__fix_repo_to_etersoft /etc/apt/sources.list
			__fix_repo_to_etersoft /etc/apt/sources.list.d/*.list
		fi
240
		if [ "$1" = "yandex" ] ; then
241 242 243
			__fix_repo_to_yandex /etc/apt/sources.list
			__fix_repo_to_yandex /etc/apt/sources.list.d/*.list
		fi
244
		if [ "$1" = "basealt" ] ; then
245 246 247
			__fix_repo_to_basealt /etc/apt/sources.list
			__fix_repo_to_basealt /etc/apt/sources.list.d/*.list
		fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
248
		docmd apt-repo list
249
		return
250
		;;
251 252 253 254 255 256 257
esac

case $PMTYPE in
#	apt-rpm)
#		;;
#	yum-rpm|dnf-rpm)
#		;;
258 259 260 261 262 263
	*)
		fatal "Have no suitable command for $PMTYPE"
		;;
esac

}