Skip to content

Commit

Permalink
Convert mnt-excl into python.
Browse files Browse the repository at this point in the history
  • Loading branch information
WayneD committed May 22, 2023
1 parent 6f3c5ec commit 3476cae
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions support/mnt-excl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env perl
#!/usr/bin/env python3
# This script takes a command-line arg of a source directory
# that will be passed to rsync, and generates a set of excludes
# that will exclude all mount points from the list. This is
Expand Down Expand Up @@ -27,23 +27,33 @@
# awk '{print $2}' /proc/mounts | grep -v '^/$' | \
# rsync -avf 'merge,/- -' /dir host:/dest/

use strict;
use warnings;
use Cwd 'abs_path';
import os, argparse

my $file = '/proc/mounts';
my $dir = shift || '/';
my $trailing_slash = $dir =~ m{./$} ? '/' : '';
$dir = abs_path($dir) . $trailing_slash;
$dir =~ s{([^/]*)$}{};
my $trailing = $1;
$trailing = '' if $trailing eq '.' || !-d "$dir$trailing";
$trailing .= '/' if $trailing ne '';
MNT_FILE = '/proc/mounts';

open(IN, $file) or die "Unable to open $file: $!\n";
while (<IN>) {
$_ = (split)[1];
next unless s{^\Q$dir$trailing\E}{}o && $_ ne '';
print "- /$trailing$_\n";
}
close IN;
def main():
trailing_slash = '/' if args.path.endswith(('/', '/.')) and args.path != '/' else ''
args.path = os.path.realpath(args.path) + trailing_slash
parent_dir = os.path.dirname(args.path)
trailing = os.path.basename(args.path)
if not os.path.isdir(args.path):
trailing = ''
elif trailing != '':
trailing += '/'
want_path = os.path.join(parent_dir, trailing)
wp_len = len(want_path)

with open(MNT_FILE) as fh:
for line in fh:
mnt_path = line.split()[1]
if mnt_path.startswith(want_path) and mnt_path != want_path:
print(f"- /{trailing}{mnt_path[wp_len:]}")

if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Output mount points as rsync excludes.", add_help=False)
parser.add_argument("--help", "-h", action="help", help="Output this help message and exit.")
parser.add_argument('path', metavar='PATH', nargs='?', default='/', help="Limit output to those within the PATH hierarchy.")
args = parser.parse_args()
main()

# vim: sw=4 et

0 comments on commit 3476cae

Please sign in to comment.