tarfilter: add --strip-components option

This commit is contained in:
Johannes Schauer Marin Rodrigues 2022-02-11 22:56:38 +01:00
parent 632a918780
commit e3a7b7d013
Signed by: josch
GPG key ID: F2CBA5C78FBD83E1

View file

@ -64,6 +64,10 @@ Both types of options use Unix shell-style wildcards:
? matches any single character ? matches any single character
[seq] matches any character in seq [seq] matches any character in seq
[!seq] matches any character not in seq [!seq] matches any character not in seq
Thirdly, strip leading directory components off of tar members. Just as with
GNU tar --strip-components, tar members that have less or equal components in
their path are not passed through.
""" """
) )
parser.add_argument( parser.add_argument(
@ -90,8 +94,18 @@ Both types of options use Unix shell-style wildcards:
action=PaxFilterAction, action=PaxFilterAction,
help="Re-include a pax header after a previous exclusion.", help="Re-include a pax header after a previous exclusion.",
) )
parser.add_argument(
"--strip-components",
metavar="number",
type=int,
help="Strip NUMBER leading components from file names",
)
args = parser.parse_args() args = parser.parse_args()
if not hasattr(args, "pathfilter") and not hasattr(args, "paxfilter"): if (
not hasattr(args, "pathfilter")
and not hasattr(args, "paxfilter")
and not hasattr(args, "strip_components")
):
from shutil import copyfileobj from shutil import copyfileobj
copyfileobj(sys.stdin.buffer, sys.stdout.buffer) copyfileobj(sys.stdin.buffer, sys.stdout.buffer)
@ -141,6 +155,11 @@ Both types of options use Unix shell-style wildcards:
for member in in_tar: for member in in_tar:
if path_filter_should_skip(member): if path_filter_should_skip(member):
continue continue
if args.strip_components:
comps = member.name.split("/")
if len(comps) <= args.strip_components:
continue
member.name = "/".join(comps[args.strip_components :])
member.pax_headers = { member.pax_headers = {
k: v k: v
for k, v in member.pax_headers.items() for k, v in member.pax_headers.items()