From: Wayne Davison Date: Tue, 21 Dec 2021 00:08:34 +0000 (-0800) Subject: Convert munge-symlinks to python. X-Git-Tag: v3.2.4pre1~19 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=39c3ae0ea3a4257c3be9e967549a953a95304b73;p=thirdparty%2Frsync.git Convert munge-symlinks to python. --- diff --git a/support/munge-symlinks b/support/munge-symlinks index 3e5f3ad2..e7a54741 100755 --- a/support/munge-symlinks +++ b/support/munge-symlinks @@ -1,60 +1,71 @@ -#!/usr/bin/env perl +#!/usr/bin/env python3 # This script will either prefix all symlink values with the string # "/rsyncd-munged/" or remove that prefix. -use strict; -use Getopt::Long; - -my $SYMLINK_PREFIX = '/rsyncd-munged/'; - -my $munge_opt; - -&GetOptions( - 'munge' => sub { $munge_opt = 1 }, - 'unmunge' => sub { $munge_opt = 0 }, - 'all' => \( my $all_opt ), - 'help|h' => \( my $help_opt ), -) or &usage; - -&usage if $help_opt || !defined $munge_opt; - -my $munged_re = $all_opt ? qr/^($SYMLINK_PREFIX)+(?=.)/ : qr/^$SYMLINK_PREFIX(?=.)/; - -push(@ARGV, '.') unless @ARGV; - -open(PIPE, '-|', 'find', @ARGV, '-type', 'l') or die $!; - -while () { - chomp; - my $lnk = readlink($_) or next; - if ($munge_opt) { - next if !$all_opt && $lnk =~ /$munged_re/; - $lnk =~ s/^/$SYMLINK_PREFIX/; - } else { - next unless $lnk =~ s/$munged_re//; - } - if (!unlink($_)) { - warn "Unable to unlink symlink: $_ ($!)\n"; - } elsif (!symlink($lnk, $_)) { - warn "Unable to recreate symlink: $_ -> $lnk ($!)\n"; - } else { - print "$_ -> $lnk\n"; - } -} - -close PIPE; -exit; - -sub usage -{ - die <', lnk + ':', str(e), file=sys.stderr) + return + print(fn, '->', lnk) + + +if __name__ == '__main__': + our_desc = """\ +Adds or removes the %s prefix to/from the start of each symlink's value. +When given the name of a directory, affects all the symlinks in that directory hierarchy. +""" % SYMLINK_PREFIX + epilog = 'See the "munge symlinks" option in the rsyncd.conf manpage for more details.' + parser = argparse.ArgumentParser(description=our_desc, epilog=epilog, add_help=False) + uniq_group = parser.add_mutually_exclusive_group() + uniq_group.add_argument('--munge', action='store_true', help="Add the prefix to symlinks (the default).") + uniq_group.add_argument('--unmunge', action='store_true', help="Remove the prefix from symlinks.") + parser.add_argument('--all', action='store_true', help="Always adds the prefix when munging (even if already munged) or removes multiple instances of the prefix when unmunging.") + parser.add_argument('--help', '-h', action='help', help="Output this help message and exit.") + parser.add_argument('names', metavar='NAME', nargs='+', help="One or more directories and/or symlinks to process.") + args = parser.parse_args() + main() + +# vim: sw=4 et