Skip to content

Commit 07f3cbb

Browse files
committed
Improve support for deb822 sources
1 parent c9eb925 commit 07f3cbb

File tree

1 file changed

+93
-19
lines changed

1 file changed

+93
-19
lines changed

scripts/install.sh

Lines changed: 93 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,26 +76,69 @@ update_lists() {
7676
if [ ! -e /tmp/setup_php ] || [[ -n $ppa && -n $ppa_search ]]; then
7777
if [[ -n "$ppa" && -n "$ppa_search" ]]; then
7878
list="$list_dir"/"$(basename "$(grep -lr "$ppa_search" "$list_dir")")"
79-
elif [ -e "$list_file" ] && grep -Eq '^deb |^Types deb' "$list_file"; then
79+
elif [ -e "$list_file" ] && grep -Eq '^deb |^Types: *deb' "$list_file"; then
8080
list="$list_file"
8181
fi
8282
update_lists_helper "$list"
8383
[[ -n $ppa && -n $ppa_search ]] || echo '' | tee /tmp/setup_php >/dev/null 2>&1
8484
fi
8585
}
8686

87+
get_sources_format() {
88+
if [ -n "$sources_format" ]; then
89+
echo "$sources_format"
90+
return
91+
fi
92+
sources_format=deb
93+
if [ -e "$list_dir"/ubuntu.sources ] || [ -e "$list_dir"/debian.sources ]; then
94+
sources_format="deb822"
95+
else
96+
find "$list_dir" -type f -name '*.sources' | grep -q . && sources_format="deb822"
97+
fi
98+
echo "$sources_format"
99+
}
100+
101+
get_repo_patterns() {
102+
local list_format=$1
103+
local ppa_url=$2
104+
local package_dist=$3
105+
local branches=$4
106+
local deb_pattern="^deb .*$ppa_url $package_dist .*$branches$"
107+
local deb822_pattern="^URIs: $ppa_url$"
108+
if [ "$list_format" = "deb822" ]; then
109+
printf '%s|%s\n' "$deb822_pattern" "$deb_pattern"
110+
else
111+
printf '%s|%s\n' "$deb_pattern" "$deb822_pattern"
112+
fi
113+
}
114+
87115
check_lists() {
88116
local ppa=$1
89-
local ppa_search=$2
90-
if grep -Eqr "$ppa_search" "$list_dir"; then
117+
local primary=${2:-}
118+
local secondary=${3:-}
119+
local match_pattern=
120+
local match_file=
121+
check_lists_file=
122+
check_lists_pattern=
123+
if [ -n "$primary" ]; then
124+
match_file=$(grep -Elr "$primary" "$list_dir" 2>/dev/null | head -n 1)
125+
[ -n "$match_file" ] && match_pattern="$primary"
126+
fi
127+
if [ -z "$match_file" ] && [ -n "$secondary" ]; then
128+
match_file=$(grep -Elr "$secondary" "$list_dir" 2>/dev/null | head -n 1)
129+
[ -n "$match_file" ] && match_pattern="$secondary"
130+
fi
131+
if [ -n "$match_file" ]; then
132+
local list_count
91133
list_count="$(sudo find /var/lib/apt/lists -type f -name "*${ppa/\//_}*" | wc -l)"
92134
if [ "$list_count" = "0" ]; then
93-
update_lists "$ppa" "$ppa_search"
135+
update_lists "$ppa" "$match_pattern"
94136
fi
95-
return 0;
96-
else
97-
return 1;
137+
check_lists_file=$match_file
138+
check_lists_pattern=$match_pattern
139+
return 0
98140
fi
141+
return 1
99142
}
100143

101144
ubuntu_fingerprint() {
@@ -140,18 +183,39 @@ add_list() {
140183
local key_source=${3:-"$ppa_url"}
141184
local package_dist=${4:-"$VERSION_CODENAME"}
142185
local branches=${5:-main}
143-
local ppa_search="deb .*$ppa_url $package_dist .*$branches"
144-
if check_lists "$ppa" "$ppa_search"; then
145-
echo "Repository $ppa already exists";
146-
return 1;
186+
local list_format
187+
list_format="$(get_sources_format)"
188+
IFS='|' read -r primary_pattern secondary_pattern <<< "$(get_repo_patterns "$list_format" "$ppa_url" "$package_dist" "$branches")"
189+
if check_lists "$ppa" "$primary_pattern" "$secondary_pattern"; then
190+
if [ "$list_format" = "deb822" ] && [ -n "$check_lists_file" ] && [[ "$check_lists_file" = *.list ]]; then
191+
sudo rm -f "$check_lists_file"
192+
else
193+
echo "Repository $ppa already exists";
194+
return 1;
195+
fi
196+
fi
197+
arch=$(dpkg --print-architecture)
198+
[ -e "$key_source" ] && key_file=$key_source || key_file="$key_dir"/"${ppa/\//-}"-keyring.gpg
199+
add_key "$ppa" "$ppa_url" "$package_dist" "$key_source" "$key_file"
200+
local list_basename="${ppa%%/*}"-"$ID"-"${ppa#*/}"-"$package_dist"
201+
sudo rm -f "$list_dir"/"${ppa/\//-}".list "$list_dir"/"${ppa/\//-}".sources "$list_dir"/"$list_basename".list "$list_dir"/"$list_basename".sources || true
202+
local list_path
203+
if [ "$list_format" = "deb822" ]; then
204+
list_path="$list_dir"/"$list_basename".sources
205+
cat <<EOF | sudo tee "$list_path" >/dev/null
206+
Types: deb
207+
URIs: $ppa_url
208+
Suites: $package_dist
209+
Components: $branches
210+
Architectures: $arch
211+
Signed-By: $key_file
212+
EOF
147213
else
148-
arch=$(dpkg --print-architecture)
149-
[ -e "$key_source" ] && key_file=$key_source || key_file="$key_dir"/"${ppa/\//-}"-keyring.gpg
150-
add_key "$ppa" "$ppa_url" "$package_dist" "$key_source" "$key_file"
151-
echo "deb [arch=$arch signed-by=$key_file] $ppa_url $package_dist $branches" | sudo tee -a "$list_dir"/"${ppa%%/*}"-"$ID"-"${ppa#*/}"-"$package_dist".list >/dev/null 2>&1
152-
update_lists "$ppa" "$ppa_search"
153-
. /etc/os-release
214+
list_path="$list_dir"/"$list_basename".list
215+
echo "deb [arch=$arch signed-by=$key_file] $ppa_url $package_dist $branches" | sudo tee "$list_path" >/dev/null 2>&1
154216
fi
217+
update_lists "$ppa" "$primary_pattern"
218+
. /etc/os-release
155219
return 0;
156220
}
157221

@@ -178,8 +242,15 @@ update_ppa() {
178242
local ppa_url=$lp_ppa/$ppa/ubuntu
179243
local package_dist=$VERSION_CODENAME
180244
local branches=main
181-
local ppa_search="deb .*$ppa_url $package_dist .*$branches"
182-
update_lists "$ppa" "$ppa_search"
245+
local list_format
246+
list_format="$(get_sources_format)"
247+
IFS='|' read -r primary_pattern secondary_pattern <<< "$(get_repo_patterns "$list_format" "$ppa_url" "$package_dist" "$branches")"
248+
if ! grep -Elr "$primary_pattern" "$list_dir" >/dev/null 2>&1 && [ -n "$secondary_pattern" ]; then
249+
if grep -Elr "$secondary_pattern" "$list_dir" >/dev/null 2>&1; then
250+
primary_pattern=$secondary_pattern
251+
fi
252+
fi
253+
update_lists "$ppa" "$primary_pattern"
183254
. /etc/os-release
184255
}
185256

@@ -424,6 +495,9 @@ pecl_file="/etc/php/$version/mods-available/pecl.ini"
424495
list_dir='/etc/apt/sources.list.d'
425496
list_file="$list_dir/ubuntu.sources"
426497
[ -e "$list_file" ] || list_file='/etc/apt/sources.list'
498+
sources_format=
499+
check_lists_file=
500+
check_lists_pattern=
427501
debconf_fix='DEBIAN_FRONTEND=noninteractive'
428502
upstream_lsb='/etc/upstream-release/lsb-release'
429503
lp_api=(

0 commit comments

Comments
 (0)