| 1 |
|
| 2 |
"""Update debian/control build dependencies from debcargo output.""" |
| 3 |
|
| 4 |
import argparse |
| 5 |
import json |
| 6 |
import logging |
| 7 |
import subprocess |
| 8 |
import sys |
| 9 |
from pathlib import Path |
| 10 |
from typing import List, Set, Tuple |
| 11 |
|
| 12 |
from debian.deb822 import PkgRelation |
| 13 |
|
| 14 |
from debmutate.control import ControlEditor, PkgRelationFieldEditor, ensure_relation |
| 15 |
from debmutate.debcargo import debcargo_binary_name |
| 16 |
|
| 17 |
|
| 18 |
def discover_workspace_crates() -> Tuple[List[Path], Set[str]]: |
| 19 |
"""Discover all Cargo.toml files for workspace crates. |
| 20 |
|
| 21 |
Uses `cargo metadata` to find all workspace member crates. |
| 22 |
|
| 23 |
Returns: |
| 24 |
Tuple of: |
| 25 |
- List of paths to Cargo.toml files for workspace crates |
| 26 |
- Set of crate names in the workspace |
| 27 |
|
| 28 |
Raises: |
| 29 |
subprocess.CalledProcessError: If cargo metadata command fails |
| 30 |
""" |
| 31 |
result = subprocess.run( |
| 32 |
["cargo", "metadata", "--no-deps", "--format-version", "1"], |
| 33 |
capture_output=True, |
| 34 |
text=True, |
| 35 |
check=True, |
| 36 |
) |
| 37 |
|
| 38 |
metadata = json.loads(result.stdout) |
| 39 |
|
| 40 |
|
| 41 |
workspace_member_ids = set(metadata["workspace_members"]) |
| 42 |
|
| 43 |
|
| 44 |
manifest_paths = [] |
| 45 |
crate_names = set() |
| 46 |
for package in metadata["packages"]: |
| 47 |
if package["id"] in workspace_member_ids: |
| 48 |
manifest_path = Path(package["manifest_path"]) |
| 49 |
manifest_paths.append(manifest_path) |
| 50 |
crate_names.add(package["name"]) |
| 51 |
|
| 52 |
return manifest_paths, crate_names |
| 53 |
|
| 54 |
|
| 55 |
def get_debcargo_dependencies( |
| 56 |
cargo_toml_path: Path, |
| 57 |
features: List[str] | None = None, |
| 58 |
all_features: bool = False, |
| 59 |
no_default_features: bool = False, |
| 60 |
allow_prerelease_deps: bool = False, |
| 61 |
include_dev_dependencies: bool = False, |
| 62 |
) -> List[str]: |
| 63 |
"""Run debcargo deb-dependencies and parse the output. |
| 64 |
|
| 65 |
Args: |
| 66 |
cargo_toml_path: Path to the Cargo.toml file |
| 67 |
features: Features to include in dependencies |
| 68 |
all_features: Include all features in dependencies |
| 69 |
no_default_features: Do not include default feature in dependencies |
| 70 |
allow_prerelease_deps: Allow prerelease versions of dependencies |
| 71 |
include_dev_dependencies: Include dev dependencies |
| 72 |
|
| 73 |
Returns: |
| 74 |
List of dependency strings |
| 75 |
|
| 76 |
Raises: |
| 77 |
subprocess.CalledProcessError: If debcargo command fails |
| 78 |
""" |
| 79 |
cmd = ["debcargo", "deb-dependencies"] |
| 80 |
|
| 81 |
if features: |
| 82 |
for feature in features: |
| 83 |
cmd.extend(["--features", feature]) |
| 84 |
if all_features: |
| 85 |
cmd.append("--all-features") |
| 86 |
if no_default_features: |
| 87 |
cmd.append("--no-default-features") |
| 88 |
if allow_prerelease_deps: |
| 89 |
cmd.append("--allow-prerelease-deps") |
| 90 |
if include_dev_dependencies: |
| 91 |
cmd.append("--include-dev-dependencies") |
| 92 |
|
| 93 |
cmd.append(str(cargo_toml_path)) |
| 94 |
|
| 95 |
result = subprocess.run( |
| 96 |
cmd, |
| 97 |
capture_output=True, |
| 98 |
text=True, |
| 99 |
check=True, |
| 100 |
) |
| 101 |
|
| 102 |
|
| 103 |
deps = [dep.strip() for dep in result.stdout.strip().split(",")] |
| 104 |
return [dep for dep in deps if dep] |
| 105 |
|
| 106 |
|
| 107 |
def filter_local_crate_dependencies( |
| 108 |
deps: List[str], local_crate_names: Set[str] |
| 109 |
) -> List[str]: |
| 110 |
"""Filter out dependencies that correspond to local workspace crates. |
| 111 |
|
| 112 |
Args: |
| 113 |
deps: List of dependency strings |
| 114 |
local_crate_names: Set of crate names that are workspace members |
| 115 |
|
| 116 |
Returns: |
| 117 |
Filtered list of dependencies with local crates excluded |
| 118 |
""" |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
local_package_prefixes = { |
| 123 |
debcargo_binary_name(name)[:-4] |
| 124 |
for name in local_crate_names |
| 125 |
} |
| 126 |
|
| 127 |
filtered_deps = [] |
| 128 |
for dep in deps: |
| 129 |
|
| 130 |
parsed = PkgRelation.parse_relations(dep) |
| 131 |
|
| 132 |
is_local = False |
| 133 |
for alternatives in parsed: |
| 134 |
for rel in alternatives: |
| 135 |
pkg_name = rel["name"] |
| 136 |
|
| 137 |
|
| 138 |
if pkg_name.endswith("-dev"): |
| 139 |
pkg_prefix = pkg_name[:-4] |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
for local_prefix in local_package_prefixes: |
| 144 |
if ( |
| 145 |
pkg_prefix == local_prefix |
| 146 |
or pkg_prefix.startswith(local_prefix + "+") |
| 147 |
or pkg_prefix.startswith(local_prefix + "-") |
| 148 |
): |
| 149 |
is_local = True |
| 150 |
logging.info(f" Excluding local crate: {pkg_name}") |
| 151 |
break |
| 152 |
if is_local: |
| 153 |
break |
| 154 |
if is_local: |
| 155 |
break |
| 156 |
|
| 157 |
if not is_local: |
| 158 |
filtered_deps.append(dep) |
| 159 |
|
| 160 |
return filtered_deps |
| 161 |
|
| 162 |
|
| 163 |
def update_build_dependencies( |
| 164 |
control_path: str, new_deps: List[str], drop_unreferenced: bool = False |
| 165 |
) -> None: |
| 166 |
"""Update Build-Depends field in debian/control. |
| 167 |
|
| 168 |
Args: |
| 169 |
control_path: Path to the debian/control file |
| 170 |
new_deps: List of new dependency strings to add/update |
| 171 |
drop_unreferenced: If True, remove librust-*-dev dependencies not in new_deps |
| 172 |
""" |
| 173 |
with ControlEditor(control_path) as editor: |
| 174 |
source = editor.source |
| 175 |
|
| 176 |
|
| 177 |
new_pkg_names = set() |
| 178 |
for dep in new_deps: |
| 179 |
parsed = PkgRelation.parse_relations(dep) |
| 180 |
for alternatives in parsed: |
| 181 |
for rel in alternatives: |
| 182 |
new_pkg_names.add(rel["name"]) |
| 183 |
|
| 184 |
|
| 185 |
dropped_count = 0 |
| 186 |
if drop_unreferenced: |
| 187 |
|
| 188 |
existing_deps = source.get("Build-Depends", "") |
| 189 |
parsed_existing = ( |
| 190 |
PkgRelation.parse_relations(existing_deps) if existing_deps else [] |
| 191 |
) |
| 192 |
|
| 193 |
|
| 194 |
packages_to_drop = [] |
| 195 |
for alternatives in parsed_existing: |
| 196 |
for rel in alternatives: |
| 197 |
pkg_name = rel["name"] |
| 198 |
|
| 199 |
if pkg_name.startswith("librust-") and pkg_name.endswith("-dev"): |
| 200 |
if pkg_name not in new_pkg_names: |
| 201 |
packages_to_drop.append(pkg_name) |
| 202 |
|
| 203 |
|
| 204 |
if packages_to_drop: |
| 205 |
with PkgRelationFieldEditor(source, "Build-Depends") as build_deps: |
| 206 |
for pkg_name in packages_to_drop: |
| 207 |
build_deps.drop_relation(pkg_name) |
| 208 |
dropped_count += 1 |
| 209 |
logging.info(f" Dropping: {pkg_name}") |
| 210 |
|
| 211 |
|
| 212 |
existing_deps = source.get("Build-Depends", "") |
| 213 |
updated_deps = existing_deps |
| 214 |
|
| 215 |
|
| 216 |
added_count = 0 |
| 217 |
for dep in new_deps: |
| 218 |
|
| 219 |
before = updated_deps |
| 220 |
updated_deps = ensure_relation(updated_deps, dep) |
| 221 |
|
| 222 |
|
| 223 |
if updated_deps != before: |
| 224 |
added_count += 1 |
| 225 |
logging.info(f" Adding: {dep}") |
| 226 |
|
| 227 |
|
| 228 |
source["Build-Depends"] = updated_deps |
| 229 |
|
| 230 |
if dropped_count > 0: |
| 231 |
logging.info( |
| 232 |
f"\nDropped {dropped_count} unreferenced librust-*-dev dependencies" |
| 233 |
) |
| 234 |
logging.info(f"Added {added_count} new build dependencies") |
| 235 |
|
| 236 |
|
| 237 |
def main() -> int: |
| 238 |
"""Main entry point.""" |
| 239 |
parser = argparse.ArgumentParser( |
| 240 |
description="Update debian/control build dependencies from debcargo output." |
| 241 |
) |
| 242 |
parser.add_argument( |
| 243 |
"--drop-unreferenced", |
| 244 |
action="store_true", |
| 245 |
help="Drop librust-*-dev dependencies that are not referenced by any workspace crate", |
| 246 |
) |
| 247 |
parser.add_argument( |
| 248 |
"--exclude-local-crates", |
| 249 |
action="store_true", |
| 250 |
default=True, |
| 251 |
help="Exclude dependencies for crates that are present as workspace members (default)", |
| 252 |
) |
| 253 |
parser.add_argument( |
| 254 |
"--include-local-crates", |
| 255 |
action="store_false", |
| 256 |
dest="exclude_local_crates", |
| 257 |
help="Include dependencies for crates that are present as workspace members", |
| 258 |
) |
| 259 |
parser.add_argument( |
| 260 |
"--features", |
| 261 |
action="append", |
| 262 |
help="Features to include in dependencies (can be specified multiple times)", |
| 263 |
) |
| 264 |
parser.add_argument( |
| 265 |
"--all-features", |
| 266 |
action="store_true", |
| 267 |
help="Include all features in dependencies", |
| 268 |
) |
| 269 |
parser.add_argument( |
| 270 |
"--no-default-features", |
| 271 |
action="store_true", |
| 272 |
help="Do not include default feature in dependencies", |
| 273 |
) |
| 274 |
parser.add_argument( |
| 275 |
"--allow-prerelease-deps", |
| 276 |
action="store_true", |
| 277 |
help="Allow prerelease versions of dependencies", |
| 278 |
) |
| 279 |
parser.add_argument( |
| 280 |
"--include-dev-dependencies", |
| 281 |
action="store_true", |
| 282 |
help="Include dev dependencies", |
| 283 |
) |
| 284 |
args = parser.parse_args() |
| 285 |
|
| 286 |
logging.basicConfig(level=logging.INFO, format="%(message)s") |
| 287 |
|
| 288 |
logging.warning( |
| 289 |
"update-rust-deps is deprecated. " |
| 290 |
'Please use "debcargo update-dependencies" instead.' |
| 291 |
) |
| 292 |
|
| 293 |
control_path = "debian/control" |
| 294 |
|
| 295 |
|
| 296 |
logging.info("Discovering workspace crates...") |
| 297 |
crate_manifests, crate_names = discover_workspace_crates() |
| 298 |
logging.info(f"Found {len(crate_manifests)} workspace crates:") |
| 299 |
for manifest in crate_manifests: |
| 300 |
logging.info(f" - {manifest}") |
| 301 |
|
| 302 |
|
| 303 |
all_deps: Set[str] = set() |
| 304 |
for manifest in crate_manifests: |
| 305 |
logging.info( |
| 306 |
f"\nRunning debcargo deb-dependencies on {manifest.relative_to(Path.cwd())}..." |
| 307 |
) |
| 308 |
deps = get_debcargo_dependencies( |
| 309 |
manifest, |
| 310 |
features=args.features, |
| 311 |
all_features=args.all_features, |
| 312 |
no_default_features=args.no_default_features, |
| 313 |
allow_prerelease_deps=args.allow_prerelease_deps, |
| 314 |
include_dev_dependencies=args.include_dev_dependencies, |
| 315 |
) |
| 316 |
logging.info(f" Found {len(deps)} dependencies") |
| 317 |
all_deps.update(deps) |
| 318 |
|
| 319 |
logging.info(f"\nTotal unique dependencies across all crates: {len(all_deps)}") |
| 320 |
|
| 321 |
|
| 322 |
deps_to_add = sorted(all_deps) |
| 323 |
if args.exclude_local_crates: |
| 324 |
logging.info("\nFiltering out local workspace crates...") |
| 325 |
deps_to_add = filter_local_crate_dependencies(deps_to_add, crate_names) |
| 326 |
logging.info(f"Remaining dependencies after filtering: {len(deps_to_add)}") |
| 327 |
|
| 328 |
|
| 329 |
logging.info(f"\nUpdating {control_path}...") |
| 330 |
update_build_dependencies( |
| 331 |
control_path, deps_to_add, drop_unreferenced=args.drop_unreferenced |
| 332 |
) |
| 333 |
|
| 334 |
logging.info("\nDone!") |
| 335 |
return 0 |
| 336 |
|
| 337 |
|
| 338 |
if __name__ == "__main__": |
| 339 |
sys.exit(main()) |