Skip to content

Commit 4e6b9f8

Browse files
authored
Fix version comparison in install-debs.py (#15376)
1 parent 2f07a67 commit 4e6b9f8

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

eng/common/cross/install-debs.py

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -162,23 +162,35 @@ def parse_package_index(content):
162162
packages = {}
163163
aliases = {}
164164
entries = re.split(r'\n\n+', content)
165+
165166
for entry in entries:
166167
fields = dict(re.findall(r'^(\S+): (.+)$', entry, re.MULTILINE))
167168
if "Package" in fields:
168169
package_name = fields["Package"]
169-
packages[package_name] = {
170-
"Version": fields.get("Version"),
171-
"Filename": fields.get("Filename"),
172-
"Depends": fields.get("Depends")
173-
}
174-
if "Provides" in fields:
175-
provides_list = [x.strip() for x in fields["Provides"].split(",")]
176-
for alias in provides_list:
177-
# strip version specifiers
178-
alias_name = re.sub(r'\s*\(=.*\)', '', alias)
179-
if alias_name not in aliases:
180-
aliases[alias_name] = []
181-
aliases[alias_name].append(package_name)
170+
version = fields.get("Version")
171+
filename = fields.get("Filename")
172+
depends = fields.get("Depends")
173+
provides = fields.get("Provides", None)
174+
175+
# Only update if package_name is not in packages or if the new version is higher
176+
if package_name not in packages or compare_debian_versions(version, packages[package_name]["Version"]) > 0:
177+
packages[package_name] = {
178+
"Version": version,
179+
"Filename": filename,
180+
"Depends": depends
181+
}
182+
183+
# Update aliases if package provides any alternatives
184+
if provides:
185+
provides_list = [x.strip() for x in provides.split(",")]
186+
for alias in provides_list:
187+
# Strip version specifiers
188+
alias_name = re.sub(r'\s*\(=.*\)', '', alias)
189+
if alias_name not in aliases:
190+
aliases[alias_name] = []
191+
if package_name not in aliases[alias_name]:
192+
aliases[alias_name].append(package_name)
193+
182194
return packages, aliases
183195

184196
def install_packages(mirror, packages_info, aliases, tmp_dir, extract_dir, ar_tool, desired_packages):
@@ -189,27 +201,13 @@ def install_packages(mirror, packages_info, aliases, tmp_dir, extract_dir, ar_to
189201
packages_to_download = {}
190202

191203
for pkg in resolved_packages:
192-
available_versions = [pkg]
204+
if pkg in packages_info:
205+
packages_to_download[pkg] = packages_info[pkg]
193206

194207
if pkg in aliases:
195-
available_versions.extend(aliases[pkg])
196-
197-
# Choose the package with the latest version
198-
if available_versions:
199-
best_package = max(
200-
(p for p in available_versions if p in packages_info),
201-
key=lambda p: (
202-
1 if p == pkg else 0,
203-
cmp_to_key(lambda p1, p2: compare_debian_versions(
204-
packages_info[p1]["Version"],
205-
packages_info[p2]["Version"]
206-
))(p)
207-
),
208-
default=None
209-
)
210-
211-
if best_package:
212-
packages_to_download[best_package] = packages_info[best_package]
208+
for alias in aliases[pkg]:
209+
if alias in packages_info:
210+
packages_to_download[alias] = packages_info[alias]
213211

214212
asyncio.run(download_deb_files_parallel(mirror, packages_to_download, tmp_dir))
215213

@@ -225,11 +223,11 @@ def install_packages(mirror, packages_info, aliases, tmp_dir, extract_dir, ar_to
225223
for pkg in reversed(resolved_packages):
226224
deb_file = package_to_deb_file_map.get(pkg)
227225
if deb_file and os.path.exists(deb_file):
228-
extract_deb_file_using_dpkg(deb_file, tmp_dir, extract_dir, ar_tool)
226+
extract_deb_file(deb_file, tmp_dir, extract_dir, ar_tool)
229227

230228
print("All done!")
231229

232-
def extract_deb_file_using_dpkg(deb_file, tmp_dir, extract_dir, ar_tool):
230+
def extract_deb_file(deb_file, tmp_dir, extract_dir, ar_tool):
233231
"""Extract .deb file contents"""
234232

235233
os.makedirs(extract_dir, exist_ok=True)
@@ -270,7 +268,7 @@ def extract_deb_file_using_dpkg(deb_file, tmp_dir, extract_dir, ar_tool):
270268
raise ValueError(f"Unsupported compression format: {file_extension}")
271269

272270
with tarfile.open(tar_file_path, mode) as tar:
273-
tar.extractall(path=extract_dir, filter='tar')
271+
tar.extractall(path=extract_dir, filter='fully_trusted')
274272

275273
def finalize_setup(rootfsdir):
276274
lib_dir = os.path.join(rootfsdir, 'lib')

0 commit comments

Comments
 (0)