Type fix and checks for tools packages (#1403)

* Fix scripts/packages typings

* Prevent empty packages list when invalid distro
This commit is contained in:
Jonas L 2021-10-17 00:21:22 +02:00 committed by GitHub
parent 1efa0742c9
commit c7304badb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 8 deletions

View File

@ -2,18 +2,26 @@
from argparse import ArgumentParser from argparse import ArgumentParser
from configparser import ConfigParser from configparser import ConfigParser
from os import PathLike
from pathlib import Path from pathlib import Path
from typing import Iterator, Set from typing import Iterator, List, Set
DEFAULT_PACKAGES_FILENAME = "packages.ini" DEFAULT_PACKAGES_FILENAME = "packages.ini"
FORMATS = ("list", "line") FORMATS = ("list", "line")
SYSTEMS = ("buster", "bullseye", "bionic", "focal") DISTRIBUTIONS = ("buster", "bullseye", "bionic", "focal")
SETTINGS_SECTION = "=settings" SETTINGS_SECTION = "=settings"
DEVELOPMENT_SECTION = "=development" DEVELOPMENT_SECTION = "=development"
def load_packages(raw: str, distribution: str, development: bool = False) -> Set[str]: def load_packages(
raw: str,
distribution: str,
development: bool = False,
) -> Set[str]:
if distribution not in DISTRIBUTIONS:
raise ValueError(f"Invalid distribution '{distribution}'")
manager = ConfigParser(default_section=SETTINGS_SECTION) manager = ConfigParser(default_section=SETTINGS_SECTION)
manager.read_string(raw) manager.read_string(raw)
@ -29,9 +37,11 @@ def load_packages(raw: str, distribution: str, development: bool = False) -> Set
return packages return packages
def list_packages_files(paths: str) -> Iterator[Path]: def list_packages_files(
for path in paths: paths: List[PathLike],
path = Path(path) ) -> Iterator[Path]:
for path_like in paths:
path = Path(path_like)
if path.is_dir(): if path.is_dir():
path = path / DEFAULT_PACKAGES_FILENAME path = path / DEFAULT_PACKAGES_FILENAME
@ -42,7 +52,11 @@ def list_packages_files(paths: str) -> Iterator[Path]:
yield path yield path
def list_packages(paths: str, distribution: str, development: bool = False) -> Set[str]: def list_packages(
paths: List[PathLike],
distribution: str,
development: bool = False,
) -> Set[str]:
packages = set() packages = set()
for package_file in list_packages_files(paths): for package_file in list_packages_files(paths):
raw = package_file.read_text() raw = package_file.read_text()
@ -68,7 +82,7 @@ def run():
) )
parser.add_argument( parser.add_argument(
"distribution", "distribution",
choices=SYSTEMS, choices=DISTRIBUTIONS,
help="list packages for the given distribution.", help="list packages for the given distribution.",
) )
parser.add_argument( parser.add_argument(