Skip to content

Commit b683b1b

Browse files
[ros2-setup] make universe check compatible with .sources (#474)
2 parents 8f85fec + ea89f41 commit b683b1b

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

ros2-setup/install.bash

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#! /usr/bin/env bash
22

3+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
4+
35
if [[ -f /etc/apt/sources.list.d/ros2.list ]]
46
then
57
tue-install-echo "Removing the old ROS2 apt sources"
@@ -8,10 +10,30 @@ else
810
tue-install-debug "Old ROS2 apt sources do not exist"
911
fi
1012

13+
# Check whether universe is enabled
1114
ubuntu_name=$(lsb_release -cs)
15+
ubuntu_version=$(lsb_release -rs)
16+
needs_enabling_universe=true
17+
# Check whether on ubuntu >= 24.04
18+
if dpkg --compare-versions "${ubuntu_version}" ge "24.04"
19+
then
20+
if ! "${SCRIPT_DIR}"/read_sources_files.py /etc/apt/sources.list.d/ubuntu.sources | jq -e 'all(.[]; .Components | index("universe") != null)' &>/dev/null
21+
then
22+
tue-install-debug "Not all sources have the universe repository enabled yet, going to enabled it"
23+
else
24+
tue-install-debug "All sources have the universe repository enabled"
25+
needs_enabling_universe=false
26+
fi
27+
else
28+
if ! grep -h ^deb /etc/apt/sources.list 2>/dev/null | grep "${ubuntu_name} (?:[a-z ]*(?:[a-z]+(?: [a-z]+)*)) universe" -q
29+
then
30+
tue-install-debug "No universe found in the sources.list, going to enable the universe repository"
31+
else
32+
tue-install-debug "Universe found in the sources.list, no need to enable it"
33+
fi
34+
fi
1235

13-
# Check whether universe is enabled
14-
if ! grep -h ^deb /etc/apt/sources.list 2>/dev/null | grep "${ubuntu_name} universe" -q
36+
if [[ ${needs_enabling_universe} == true ]]
1537
then
1638
tue-install-echo "Enabling universe repository"
1739
tue-install-pipe sudo add-apt-repository universe || tue-install-error "Failed to enable universe repository"

ros2-setup/read_sources_files.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#! /usr/bin/env python
2+
3+
import json
4+
import re
5+
from argparse import ArgumentParser
6+
from pathlib import Path
7+
8+
9+
def extract_all_sources_entries(text):
10+
entry_pattern = re.compile(
11+
r"""
12+
Types:\s*(?P<Types>[^\n]+)\s+
13+
URIs:\s*(?P<URIs>[^\n]+)\s+
14+
Suites:\s*(?P<Suites>[^\n]+)\s+
15+
Components:\s*(?P<Components>[^\n]+)
16+
(?:\s+Signed-By:\s*(?P<SignedBy>[^\n]+))?
17+
(?:\s+Architectures:\s*(?P<Architectures>[^\n]+))?
18+
""",
19+
re.VERBOSE,
20+
)
21+
22+
entries = []
23+
for match in entry_pattern.finditer(text):
24+
entries.append(
25+
{
26+
"Types": match.group("Types").strip().split(" "),
27+
"URIs": match.group("URIs").strip().split(" "),
28+
"Suites": match.group("Suites").strip().split(" "),
29+
"Components": match.group("Components").strip().split(" "),
30+
"Architectures": match.group("Architectures").strip().split(",")
31+
if match.group("Architectures")
32+
else None,
33+
"SignedBy": match.group("SignedBy").strip() if match.group("SignedBy") else None,
34+
}
35+
)
36+
37+
return entries
38+
39+
40+
if __name__ == "__main__":
41+
parser = ArgumentParser()
42+
parser.add_argument("file", type=str, help="Path to the sources files", nargs="+")
43+
args = parser.parse_args()
44+
entries = []
45+
for file in args.file:
46+
with Path(file).open("r") as f:
47+
content = f.read()
48+
49+
entries.extend(extract_all_sources_entries(content))
50+
print(json.dumps(entries, indent=2))
51+

0 commit comments

Comments
 (0)