David Brazdil | 6c63a26 | 2019-12-23 13:23:46 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
David Brazdil | e7a5a1f | 2019-08-09 18:48:38 +0100 | [diff] [blame] | 2 | # |
| 3 | # Copyright 2019 The Hafnium Authors. |
| 4 | # |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 5 | # Use of this source code is governed by a BSD-style |
| 6 | # license that can be found in the LICENSE file or at |
| 7 | # https://opensource.org/licenses/BSD-3-Clause. |
David Brazdil | e7a5a1f | 2019-08-09 18:48:38 +0100 | [diff] [blame] | 8 | |
David Brazdil | e7a5a1f | 2019-08-09 18:48:38 +0100 | [diff] [blame] | 9 | """Copies all files inside one folder to another, preserving subfolders.""" |
| 10 | |
| 11 | import argparse |
| 12 | import os |
| 13 | import shutil |
| 14 | import sys |
| 15 | |
| 16 | def main(): |
| 17 | parser = argparse.ArgumentParser() |
| 18 | parser.add_argument("source_folder", |
| 19 | help="directory to be copied from") |
| 20 | parser.add_argument("destination_folder", |
| 21 | help="directory to be copied into") |
| 22 | parser.add_argument("stamp_file", |
| 23 | help="stamp file to be touched") |
| 24 | args = parser.parse_args() |
| 25 | |
| 26 | # Walk the subfolders of the source directory and copy individual files. |
| 27 | # Not using shutil.copytree() because it never overwrites files. |
| 28 | for root, _, files in os.walk(args.source_folder): |
| 29 | for f in files: |
| 30 | abs_src_path = os.path.join(root, f) |
| 31 | rel_path = os.path.relpath(abs_src_path, args.source_folder) |
| 32 | abs_dst_path = os.path.join(args.destination_folder, rel_path) |
| 33 | abs_dst_folder = os.path.dirname(abs_dst_path) |
| 34 | if not os.path.isdir(abs_dst_folder): |
| 35 | os.makedirs(abs_dst_folder) |
| 36 | shutil.copyfile(abs_src_path, abs_dst_path) |
| 37 | |
| 38 | # Touch `stamp_file`. |
| 39 | with open(args.stamp_file, "w"): |
| 40 | pass |
| 41 | |
| 42 | if __name__ == "__main__": |
| 43 | sys.exit(main()) |