summaryrefslogtreecommitdiffstats
path: root/support/files-to-excludes
blob: a47d7f8a269bb53f03ee547ac1e9285f11ffce1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python3
# This script takes an input of filenames and outputs a set of include/exclude
# directives that can be used by rsync to copy just the indicated files using
# an --exclude-from=FILE or -f'. FILE' option. To be able to delete files on
# the receiving side, either use --delete-excluded or change the exclude (-)
# rules to hide filter rules (H) that only affect the sending side.

import os, fileinput, argparse

def main():
    paths = set()
    for line in fileinput.input(args.files):
        dirs = line.strip().lstrip('/').split('/')
        if not dirs:
            continue
        for j in range(1, len(dirs)):
            if dirs[j] == '':
                continue
            path = '/' + '/'.join(dirs[:j]) + '/'
            if path not in paths:
                print('+', path)
                paths.add(path)
        print('+', '/' + '/'.join(dirs))

    for path in sorted(paths):
        print('-', path + '*')
    print('-', '/*')


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="Transform a list of files into a set of include/exclude rules.", add_help=False)
    parser.add_argument("--help", "-h", action="help", help="Output this help message and exit.")
    parser.add_argument("files", metavar="FILE", default='-', nargs='*', help="The file(s) that hold the pathnames to translate. Defaults to stdin.")
    args = parser.parse_args()
    main()

# vim: sw=4 et