1#!/usr/bin/env python
2
3"""Symlinks, or on Windows copies, an existing file to a second location.
4
5Overwrites the target location if it exists.
6Updates the mtime on a stamp file when done."""
7
8import argparse
9import errno
10import os
11import sys
12
13
14def main():
15    parser = argparse.ArgumentParser(description=__doc__)
16    parser.add_argument('--stamp', required=True,
17                        help='name of a file whose mtime is updated on run')
18    parser.add_argument('source')
19    parser.add_argument('output')
20    args = parser.parse_args()
21
22    # FIXME: This should not check the host platform but the target platform
23    # (which needs to be passed in as an arg), for cross builds.
24    if sys.platform != 'win32':
25        try:
26            os.makedirs(os.path.dirname(args.output))
27        except OSError as e:
28            if e.errno != errno.EEXIST:
29                raise
30        try:
31            os.symlink(args.source, args.output)
32        except OSError as e:
33            if e.errno == errno.EEXIST:
34                os.remove(args.output)
35                os.symlink(args.source, args.output)
36            else:
37                raise
38    else:
39        import shutil
40        output = args.output + ".exe"
41        source = args.source + ".exe"
42        shutil.copyfile(os.path.join(os.path.dirname(output), source), output)
43
44    open(args.stamp, 'w') # Update mtime on stamp file.
45
46
47if __name__ == '__main__':
48    sys.exit(main())
49