keyringctl: Copy files instead of moving them

keyringctl:
Change `__main__` to create the `target_dir` before calling any further
function that relies on it.
Change `convert()` to require the `target_dir` to be not None and to
create all username based target directories before using
`shutil.copytree()` to copy all sources to their respective target
directories when iterating over the paths to persist. This has the
upside, that updates to a target directory structure can be done on the
fly (overwriting existing data), which is not possible with
`shutil.move()`.
This commit is contained in:
David Runge 2021-10-04 19:25:24 +02:00 committed by Levente Polyak
parent a5be572136
commit cb8e827112
No known key found for this signature in database
GPG Key ID: FC1B547C8D8172C8

View File

@ -8,7 +8,7 @@ from os import getcwd
from pathlib import Path from pathlib import Path
from shutil import move from shutil import copytree
from re import escape from re import escape
from re import split from re import split
@ -457,7 +457,7 @@ def simplify_user_id(user_id: str) -> str:
def convert( def convert(
working_dir: Path, working_dir: Path,
source: Path, source: Path,
target_dir: Optional[Path] = None, target_dir: Path,
name_override: Optional[str] = None, name_override: Optional[str] = None,
) -> Path: ) -> Path:
directories: List[Path] = [] directories: List[Path] = []
@ -471,19 +471,9 @@ def convert(
convert_certificate(working_dir=working_dir, certificate=source, name_override=name_override) convert_certificate(working_dir=working_dir, certificate=source, name_override=name_override)
) )
if target_dir:
target_dir.mkdir(parents=True, exist_ok=True)
else:
# persistent target directory
target_dir = Path(mkdtemp()).absolute()
for path in directories: for path in directories:
target_dir.mkdir(exist_ok=True) (target_dir / path.name).mkdir(exist_ok=True)
if (target_dir / path.name).exists(): copytree(src=path, dst=(target_dir / path.name), dirs_exist_ok=True)
for key_dir in path.iterdir():
move(key_dir, target_dir / path.name)
else:
move(path, target_dir)
return target_dir return target_dir
@ -525,6 +515,13 @@ if __name__ == '__main__':
if args.verbose: if args.verbose:
basicConfig(level=DEBUG) basicConfig(level=DEBUG)
if args.target:
args.target.mkdir(parents=True, exist_ok=True)
target_dir = args.target
else:
# persistent target directory
target_dir = Path(mkdtemp()).absolute()
# temporary working directory that gets auto cleaned # temporary working directory that gets auto cleaned
with TemporaryDirectory() as tempdir: with TemporaryDirectory() as tempdir:
working_dir = Path(tempdir) working_dir = Path(tempdir)
@ -533,9 +530,9 @@ if __name__ == '__main__':
debug(f'Working directory: {working_dir}') debug(f'Working directory: {working_dir}')
if 'convert' == args.subcommand: if 'convert' == args.subcommand:
print(convert(working_dir, args.source, args.target)) print(convert(working_dir, args.source, target_dir))
elif 'import' == args.subcommand: elif 'import' == args.subcommand:
keyring_import(working_dir, args.source, args.target) keyring_import(working_dir, args.source, target_dir)
if args.wait: if args.wait:
print('Press [ENTER] to continue') print('Press [ENTER] to continue')