aboutsummaryrefslogtreecommitdiffstats
path: root/build/scripts/copy_files_to_dir.py
blob: a597eb987df6e9407754e4b8e910b9defe14645b (plain) (blame)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import argparse
import errno
import os
import sys

# Explicitly enable local imports
# Don't forget to add imported scripts to inputs of the calling command!
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
import process_command_files as pcf
import shutil


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--dest-dir', required=True)
    parser.add_argument('--existing', choices=('skip', 'overwrite'), default='overwrite')
    parser.add_argument('--flat', action='store_true')
    parser.add_argument('--skip-prefix', dest='skip_prefixes', action='append', default=[])
    parser.add_argument('files', nargs='*')
    return parser.parse_args(pcf.get_args(sys.argv[1:]))


def makedirs(dirname):
    try:
        os.makedirs(dirname)
    except OSError as e:
        if e.errno == errno.EEXIST and os.path.isdir(dirname):
            pass
        else:
            raise


def main():
    args = parse_args()

    dest_dir = os.path.normpath(args.dest_dir) + os.pathsep
    makedirs(dest_dir)

    prefixes = ['{}{}'.format(os.path.normpath(p), os.path.sep) for p in args.skip_prefixes]

    for src in args.files:
        src = os.path.normpath(src)
        assert os.path.isfile(src)
        if args.flat:
            rel_dst = os.path.basename(src)
        else:
            rel_dst = src
            for prefix in prefixes:
                if src.startswith(prefix):
                    rel_dst = src[len(prefix) :]
                    break
        assert not os.path.isabs(rel_dst)
        dst = os.path.join(args.dest_dir, rel_dst)
        if os.path.isfile(dst) and args.existing == 'skip':
            break

        makedirs(os.path.dirname(dst))

        shutil.copyfile(src, dst)


if __name__ == '__main__':
    main()