feature(keyringctl): support importing multiple paths

Lets pass in a list of path's and reduce them to one set of path
iterables. This allows us to specify multiple source files/directories
This commit is contained in:
Levente Polyak 2021-10-20 20:15:42 +02:00
parent 1b1aa7f738
commit 74f317344f
No known key found for this signature in database
GPG Key ID: FC1B547C8D8172C8
2 changed files with 10 additions and 9 deletions

View File

@ -32,7 +32,7 @@ Updates to existing packager keys
```bash ```bash
# username is automatically derived from the fingerprint # username is automatically derived from the fingerprint
./keyringctl import <file_or_directory> ./keyringctl import <file_or_directory>...
``` ```
Import of a new main key Import of a new main key

View File

@ -5,6 +5,7 @@
from argparse import ArgumentParser from argparse import ArgumentParser
from collections import defaultdict from collections import defaultdict
from contextlib import contextmanager from contextlib import contextmanager
from itertools import chain
from logging import DEBUG from logging import DEBUG
from logging import basicConfig from logging import basicConfig
from logging import debug from logging import debug
@ -749,7 +750,7 @@ def derive_username_from_fingerprint(keyring_dir: Path, certificate_fingerprint:
def convert( def convert(
working_dir: Path, working_dir: Path,
source: Path, source: Iterable[Path],
target_dir: Path, target_dir: Path,
name_override: Optional[Username] = None, name_override: Optional[Username] = None,
fingerprint_filter: Optional[Set[Fingerprint]] = None, fingerprint_filter: Optional[Set[Fingerprint]] = None,
@ -762,7 +763,7 @@ def convert(
---------- ----------
working_dir: Path working_dir: Path
A directory to use for temporary files A directory to use for temporary files
source: Path source: Iterable[Path]
A path to a file or directory to decompose A path to a file or directory to decompose
target_dir: Path target_dir: Path
A directory path to write the new directory structure to A directory path to write the new directory structure to
@ -778,7 +779,7 @@ def convert(
""" """
directories: List[Path] = [] directories: List[Path] = []
keys: Iterable[Path] = source.iterdir() if source.is_dir() else [source] keys: Iterable[Path] = set(chain.from_iterable(map(lambda s: s.iterdir() if s.is_dir() else [s], source)))
for key in keys: for key in keys:
for cert in keyring_split(working_dir=working_dir, keyring=key): for cert in keyring_split(working_dir=working_dir, keyring=key):
@ -955,14 +956,14 @@ def export_revoked(certs: List[Path], main_keys: List[Fingerprint], output: Path
trusted_certs_file.write(f"{cert}\n") trusted_certs_file.write(f"{cert}\n")
def get_fingerprints_from_import_source(working_dir: Path, source: Path) -> List[Fingerprint]: def get_fingerprints_from_import_source(working_dir: Path, source: List[Path]) -> List[Fingerprint]:
"""Get all fingerprints of PGP public keys from import file(s) """Get all fingerprints of PGP public keys from import file(s)
Parameters Parameters
---------- ----------
working_dir: Path working_dir: Path
A directory to use for temporary files A directory to use for temporary files
source: Path source: List[Path]
The path to a source file or directory The path to a source file or directory
Returns Returns
@ -972,7 +973,7 @@ def get_fingerprints_from_import_source(working_dir: Path, source: Path) -> List
""" """
fingerprints: List[Fingerprint] = [] fingerprints: List[Fingerprint] = []
keys: List[Path] = list(source.iterdir()) if source.is_dir() else [source] keys: Iterable[Path] = set(chain.from_iterable(map(lambda s: s.iterdir() if s.is_dir() else [s], source)))
for key in keys: for key in keys:
for certificate in keyring_split(working_dir=working_dir, keyring=key): for certificate in keyring_split(working_dir=working_dir, keyring=key):
@ -1131,7 +1132,7 @@ if __name__ == "__main__":
"convert", "convert",
help="convert one or multiple PGP public keys to a decomposed directory structure", help="convert one or multiple PGP public keys to a decomposed directory structure",
) )
convert_parser.add_argument("source", type=absolute_path, help="File or directory to convert") convert_parser.add_argument("source", type=absolute_path, nargs="+", help="Files or directorie to convert")
convert_parser.add_argument("--target", type=absolute_path, help="Target directory instead of a random tmpdir") convert_parser.add_argument("--target", type=absolute_path, help="Target directory instead of a random tmpdir")
convert_parser.add_argument( convert_parser.add_argument(
"--name", "--name",
@ -1144,7 +1145,7 @@ if __name__ == "__main__":
"import", "import",
help="import one or several PGP keys to the keyring directory structure", help="import one or several PGP keys to the keyring directory structure",
) )
import_parser.add_argument("source", type=absolute_path, help="File or directory") import_parser.add_argument("source", type=absolute_path, nargs="+", help="Files or directories to import")
import_parser.add_argument( import_parser.add_argument(
"--name", "--name",
type=Username, type=Username,