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