aboutsummaryrefslogtreecommitdiffstats
path: root/build/scripts/copy_to_dir.py
blob: ab71a3e1e2f86304d5a831ef1b810d2e619554bd (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import argparse
import errno
import sys
import os
import shutil
import tarfile


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--build-root', required=True)
    parser.add_argument('--dest-arch', default=None)
    parser.add_argument('--dest-dir', required=True)
    parser.add_argument('args', nargs='*')
    return parser.parse_args()


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


def hardlink_or_copy(src, dst):
    if os.name == 'nt':
        shutil.copy(src, dst)
    else:
        try:
            os.link(src, dst)
        except OSError as e:
            if e.errno == errno.EEXIST:
                return
            elif e.errno == errno.EXDEV:
                sys.stderr.write("Can't make cross-device hardlink - fallback to copy: {} -> {}\n".format(src, dst))
                shutil.copy(src, dst)
            else:
                raise


def main():
    opts = parse_args()

    dest_arch = None
    if opts.dest_arch:
        if opts.dest_arch.endswith('.tar'):
            dest_arch = tarfile.open(opts.dest_arch, 'w', dereference=True)
        elif opts.dest_arch.endswith('.tar.gz') or opts.dest_arch.endswith('.tgz'):
            dest_arch = tarfile.open(opts.dest_arch, 'w:gz', dereference=True)
        else:
            # TODO: move check to graph generation stage
            raise Exception(
                'Unsopported archive type for {}. Use one of: tar, tar.gz, tgz.'.format(
                    os.path.basename(opts.dest_arch)
                )
            )

    for arg in opts.args:
        dst = arg
        if dst.startswith(opts.build_root):
            dst = dst[len(opts.build_root) + 1 :]

        if dest_arch and not arg.endswith('.pkg.fake'):
            dest_arch.add(arg, arcname=dst)

        dst = os.path.join(opts.dest_dir, dst)
        ensure_dir_exists(os.path.dirname(dst))
        hardlink_or_copy(arg, dst)

    if dest_arch:
        dest_arch.close()


if __name__ == '__main__':
    sys.exit(main())