jw-pkg/src/python/jw/pkg/cmds/posix/CmdCopy.py
Jan Lindemann e5656d7d18
All checks were successful
CI / Packaging - Kali Linux (pull_request) Successful in 4m54s
CI / Packaging - OpenSUSE Tumbleweed (pull_request) Successful in 5m0s
CI / Packaging test (pull_request) Successful in 0s
CI / Packaging - Kali Linux (push) Successful in 4m50s
CI / Packaging - OpenSUSE Tumbleweed (push) Successful in 4m42s
CI / Packaging test (push) Successful in 0s
cmds.posix.CmdCopy: Add --parents
copy() can now land a single object at its source path, but the copy
CLI command exposes no way to ask for it.

Add a --parents flag forwarded to copy()'s parents argument.

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.1.0
2026-10-17 11:47:06 +02:00

70 lines
2.2 KiB
Python

from __future__ import annotations
from typing import TYPE_CHECKING, override
from ...lib.util import copy
from .Cmd import Cmd, Parent
if TYPE_CHECKING:
from argparse import ArgumentParser, Namespace
class CmdCopy(Cmd): # export
def __init__(self, parent: Parent) -> None:
super().__init__(parent, 'copy', help = 'Copy files')
@override
def add_arguments(self, parser: ArgumentParser) -> None:
super().add_arguments(parser)
parser.add_argument('src', help = 'Source file URI')
parser.add_argument('dst', help = 'Destination file URI')
parser.add_argument(
'-o', '--owner', default = None, help = 'Destination file owner'
)
parser.add_argument(
'-g', '--group', default = None, help = 'Destination file group'
)
parser.add_argument(
'-m', '--mode', default = None, help = 'Destination file mode'
)
parser.add_argument(
'-r',
'--recursive',
action = 'store_true',
help = 'Copy directories recursively',
)
parser.add_argument(
'--parents',
action = 'store_true',
help = 'Land a single object at its source path below the destination root',
)
parser.add_argument(
'-F',
'--fixed-strings',
action = 'store_true',
help = "Don't expand macros in <src> and <dst>",
)
@override
async def _run(self, args: Namespace) -> None:
def __expand(url: str) -> str:
if args.fixed_strings:
return url
ret = self.app.distro.expand_macros(url)
if not isinstance(ret, str):
raise Exception(
f'Expanding macros in "{url}" returned unexpected ret "{ret}"'
)
return ret
await copy(
__expand(args.src),
__expand(args.dst),
owner = args.owner,
group = args.group,
# -- A mode is given as a string, if at all
mode = int(args.mode, 0) if args.mode is not None else None,
recursive = args.recursive,
parents = args.parents,
)