This commit is contained in:
Даниил Грабарь
2026-05-22 14:27:52 +10:00
parent e154a41d33
commit 2bdfb139bf
+28 -20
View File
@@ -1,7 +1,6 @@
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import glob
import json import json
import os import os
import shutil import shutil
@@ -216,9 +215,7 @@ def _add_ros2_repo(
_prog(30) _prog(30)
# Fetch the latest ros2-apt-source release version from the GitHub API. # Fetch the latest ros2-apt-source release version from the GitHub API.
# The User-Agent header is required by the GitHub API.
# Получаем последнюю версию ros2-apt-source через GitHub API. # Получаем последнюю версию ros2-apt-source через GitHub API.
# Заголовок User-Agent обязателен для GitHub API.
write("[cyan][*][/cyan] Fetching ros2-apt-source package...") write("[cyan][*][/cyan] Fetching ros2-apt-source package...")
req = urllib.request.Request( req = urllib.request.Request(
_ROS_APT_SOURCE_API, _ROS_APT_SOURCE_API,
@@ -243,35 +240,46 @@ def _add_ros2_repo(
Path(tmp_path).write_bytes(resp.read()) Path(tmp_path).write_bytes(resp.read())
_prog(55) _prog(55)
# Remember which files exist before dpkg so we can find the new sources file it creates. # apt install resolves dependencies automatically unlike dpkg -i.
# Запоминаем файлы до dpkg, чтобы найти новый файл источников, который он создаст. # The "./" prefix tells apt this is a local file, not a package name from a repo.
before = set(glob.glob("/etc/apt/sources.list.d/*")) # apt install автоматически разрешает зависимости в отличие от dpkg -i.
_run_quiet(["sudo", "dpkg", "-i", tmp_path], write) # Префикс "./" говорит apt что это локальный файл, а не имя пакета из репозитория.
after = set(glob.glob("/etc/apt/sources.list.d/*")) _run_quiet(["sudo", "apt-get", "install", "-y", f"./{tmp_path}"], write, _APT_ENV)
finally: finally:
os.unlink(tmp_path) os.unlink(tmp_path)
write("[green][ok][/green] ROS2 apt source installed") write("[green][ok][/green] ROS2 apt source installed")
_prog(60) _prog(60)
# Update ONLY the newly added ROS2 sources file, not all Ubuntu repos. # Ask dpkg which files the package installed and find the apt sources entry.
# This avoids re-downloading hundreds of MB of ubuntu package lists that are already cached. # This is more reliable than comparing directory snapshots: dpkg knows exactly what it placed.
# Обновляем ТОЛЬКО новый файл источников ROS2, а не все репозитории Ubuntu. # Спрашиваем dpkg какие файлы установил пакет и находим файл источников apt.
# Это позволяет не перекачивать сотни МБ списков пакетов Ubuntu, которые уже закешированы. # Это надёжнее сравнения снимков директории: dpkg точно знает что он положил.
write("[cyan][*][/cyan] Updating ROS2 package list...") dpkg_files = subprocess.run(
new_files = [f for f in (after - before) if f.endswith((".list", ".sources"))] ["dpkg", "-L", "ros2-apt-source"],
if new_files: capture_output=True, text=True,
).stdout.splitlines()
sources_file = next(
(f for f in dpkg_files
if f.startswith("/etc/apt/") and f.endswith((".list", ".sources"))),
None,
)
if sources_file:
write(f"[dim]Sources file: {sources_file}[/dim]")
update_cmd = [ update_cmd = [
"sudo", "apt-get", "update", "-q", "sudo", "apt-get", "update",
"-o", f"Dir::Etc::sourcelist={new_files[0]}", "-o", f"Dir::Etc::sourcelist={sources_file}",
"-o", "Dir::Etc::sourceparts=-", "-o", "Dir::Etc::sourceparts=-",
"-o", "APT::Get::List-Cleanup=0", "-o", "APT::Get::List-Cleanup=0",
] + _APT_TIMEOUTS ] + _APT_TIMEOUTS
else: else:
# Fallback when the deb updates an existing file rather than creating a new one. # Fall back to full update if dpkg -L does not reveal the sources file.
# Запасной вариант когда deb обновляет существующий файл, а не создаёт новый. # Запасной вариант: полный update если dpkg -L не раскрыл файл источников.
update_cmd = ["sudo", "apt-get", "update", "-q"] + _APT_TIMEOUTS write("[dim]Sources file not found via dpkg -L, running full apt-get update[/dim]")
update_cmd = ["sudo", "apt-get", "update"] + _APT_TIMEOUTS
write("[cyan][*][/cyan] Updating ROS2 package list...")
_run_apt_with_progress( _run_apt_with_progress(
update_cmd, write, update_cmd, write,
lambda p: _prog(60 + p * 0.40), lambda p: _prog(60 + p * 0.40),