Skip to content

Instantly share code, notes, and snippets.

@Tantalus13A98B5F
Last active January 8, 2020 07:59
Show Gist options
  • Save Tantalus13A98B5F/f4de5e55b999658c0d8dd74c48129887 to your computer and use it in GitHub Desktop.
Save Tantalus13A98B5F/f4de5e55b999658c0d8dd74c48129887 to your computer and use it in GitHub Desktop.
My `rsync` wrapper, filtering output and filling exclude list for NTFS drives.
#!/usr/bin/env python3
import subprocess as subp
import sys
import os
exclude_list = [
'System Volume Information',
'$RECYCLE.BIN',
]
realargs = sum((['--exclude', it] for it in exclude_list), [])
realargs = ['rsync'] + realargs + sys.argv[1:]
def grab_output(args):
p = subp.Popen(args, stdout=subp.PIPE, encoding='utf-8')
yield from p.stdout
def is_dry_run(args):
return '-n' in args or '--dry-run' in args
class DeleteFileBase:
def add_item(self, ln):
print(ln)
def dump_and_clear(self):
pass
class DeleteFilesSet(DeleteFileBase):
def __init__(self):
super().__init__()
self.lines = set()
def add_item(self, ln):
if ln.endswith('/'):
self.lines = {
it for it in self.lines
if not it.startswith(ln)
}
self.lines.add(ln)
def dump_and_clear(self):
if self.lines:
for item in self.lines:
print(item)
self.lines.clear()
def main():
deletefiles = DeleteFilesSet() \
if is_dry_run(realargs) else DeleteFileBase()
for ln in grab_output(realargs):
ln = ln.rstrip()
if ln.startswith('deleting '):
deletefiles.add_item(ln)
else:
deletefiles.dump_and_clear()
if not ln.endswith('/'):
print(ln)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment