|
1 | 1 | #! /usr/bin/env python3 |
2 | 2 |
|
3 | | -from typing import List, Mapping, Optional |
| 3 | +from typing import Any, List, Mapping, Optional |
| 4 | +from functools import partial |
4 | 5 | from os import environ |
5 | 6 | import sys |
6 | 7 | import yaml |
@@ -254,5 +255,143 @@ def get_distro_item(item: Mapping, key: str, release_version: str, release_type: |
254 | 255 | return {"system_packages": system_packages, "commands": commands} |
255 | 256 |
|
256 | 257 |
|
| 258 | +def installyaml_parser2(installer: Any, path: str, now: bool = False) -> Mapping: |
| 259 | + with open(path) as f: |
| 260 | + try: |
| 261 | + install_items = yaml.load(f, yaml.CSafeLoader) |
| 262 | + except AttributeError: |
| 263 | + install_items = yaml.load(f, yaml.SafeLoader) |
| 264 | + except (yaml.parser.ParserError, yaml.scanner.ScannerError) as e: |
| 265 | + raise ValueError(f"Invalid yaml syntax: {e}") |
| 266 | + |
| 267 | + if not isinstance(install_items, list): |
| 268 | + raise ValueError("Root of install.yaml file should be a YAML sequence") |
| 269 | + |
| 270 | + system_packages = [] |
| 271 | + |
| 272 | + commands = [] |
| 273 | + |
| 274 | + def get_distro_item(item: Mapping, key: str, release_version: str, release_type: str) -> Optional[str]: |
| 275 | + if key in item: |
| 276 | + value = item[key] |
| 277 | + if value is None: |
| 278 | + raise ValueError(f"'{key}' is defined, but has no value") |
| 279 | + elif len(item) < 3 or "default" not in item: |
| 280 | + raise ValueError(f"At least one distro and 'default' should be specified or none in install.yaml") |
| 281 | + else: |
| 282 | + for version in [release_version, "default"]: |
| 283 | + if version in item: |
| 284 | + value = item[version][key] |
| 285 | + break |
| 286 | + |
| 287 | + return value |
| 288 | + |
| 289 | + # Combine now calls |
| 290 | + now_cache = { |
| 291 | + "system-now": [], |
| 292 | + "pip-now": [], |
| 293 | + "pip3-now": [], |
| 294 | + "ppa-now": [], |
| 295 | + "snap-now": [], |
| 296 | + "gem-now": [], |
| 297 | + } |
| 298 | + |
| 299 | + for install_item in install_items: |
| 300 | + command = None |
| 301 | + |
| 302 | + try: |
| 303 | + install_type = install_item["type"] # type: str |
| 304 | + |
| 305 | + if install_type == "empty": |
| 306 | + return {"system_packages": system_packages, "commands": commands} |
| 307 | + |
| 308 | + elif install_type == "ros": |
| 309 | + try: |
| 310 | + source = get_distro_item(install_item, "source", ros_release, "ROS") |
| 311 | + except ValueError as e: |
| 312 | + raise ValueError(f"[{install_type}]: {e.args[0]}") |
| 313 | + |
| 314 | + # Both release and default are allowed to be None |
| 315 | + if source is None: |
| 316 | + continue |
| 317 | + |
| 318 | + source_type = source["type"] |
| 319 | + if source_type == "git": |
| 320 | + command = partial(getattr(installer, "tue_install_ros", "git", catkin_git(source))) |
| 321 | + elif source_type == "system": |
| 322 | + system_packages.append(f"ros-{ros_release}-{source['name']}") |
| 323 | + command = partial(getattr(installer, "tue_install_ros", "system", catkin_git(source))) |
| 324 | + else: |
| 325 | + raise ValueError(f"Unknown ROS install type: '{source_type}'") |
| 326 | + |
| 327 | + # Non ros targets that are packaged to be built with catkin |
| 328 | + elif install_type == "catkin": |
| 329 | + source = install_item["source"] |
| 330 | + |
| 331 | + if not source["type"] == "git": |
| 332 | + raise ValueError(f"Unknown catkin install type: '{source['type']}'") |
| 333 | + command = partial(getattr(installer, "tue_install_ros", "git", catkin_git(source))) |
| 334 | + |
| 335 | + elif install_type == "git": |
| 336 | + command = partial(getattr(installer, "tue_install_git", "git", type_git(install_item))) |
| 337 | + |
| 338 | + elif install_type in [ |
| 339 | + "target", |
| 340 | + "system", |
| 341 | + "pip", |
| 342 | + "pip3", |
| 343 | + "ppa", |
| 344 | + "snap", |
| 345 | + "gem", |
| 346 | + "dpkg", |
| 347 | + "target-now", |
| 348 | + "system-now", |
| 349 | + "pip-now", |
| 350 | + "pip3-now", |
| 351 | + "ppa-now", |
| 352 | + "snap-now", |
| 353 | + "gem-now", |
| 354 | + ]: |
| 355 | + install_type = install_type.replace("-", "_") |
| 356 | + if now and "now" not in install_type: |
| 357 | + install_type += "_now" |
| 358 | + |
| 359 | + try: |
| 360 | + pkg_name = get_distro_item(install_item, "name", ubuntu_release, "Ubuntu") |
| 361 | + except ValueError as e: |
| 362 | + raise ValueError(f"[{install_type}]: {e.args[0]}") |
| 363 | + |
| 364 | + # Both release and default are allowed to be None |
| 365 | + if pkg_name is None: |
| 366 | + continue |
| 367 | + |
| 368 | + if "system" in install_type: |
| 369 | + system_packages.append(pkg_name) |
| 370 | + |
| 371 | + # Cache install types which accept multiple pkgs at once |
| 372 | + if install_type in now_cache: |
| 373 | + now_cache[install_type].append(pkg_name) |
| 374 | + continue |
| 375 | + command = partial(getattr(installer, f"tue_install_{install_type}", pkg_name)) |
| 376 | + |
| 377 | + else: |
| 378 | + raise ValueError(f"Unknown install type: '{install_type}'") |
| 379 | + |
| 380 | + except KeyError as e: |
| 381 | + raise KeyError(f"Invalid install.yaml file: Key '{e}' could not be found.") |
| 382 | + |
| 383 | + if not command: |
| 384 | + raise ValueError("Invalid install.yaml file") |
| 385 | + |
| 386 | + commands.append(command) |
| 387 | + |
| 388 | + for install_type, pkg_list in now_cache.items(): |
| 389 | + if pkg_list: |
| 390 | + command = partial(getattr(installer, f"tue_install_{install_type}", pkg_list)) |
| 391 | + commands.append(command) |
| 392 | + |
| 393 | + return {"system_packages": system_packages, "commands": commands} |
| 394 | + |
| 395 | + |
257 | 396 | if __name__ == "__main__": |
258 | 397 | sys.exit(main()) |
0 commit comments