David Brazdil | e7a5a1f | 2019-08-09 18:48:38 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2019 The Hafnium Authors. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | #!/usr/bin/env python |
| 18 | """Copies all files inside one folder to another, preserving subfolders.""" |
| 19 | |
| 20 | import argparse |
| 21 | import os |
| 22 | import shutil |
| 23 | import sys |
| 24 | |
| 25 | def main(): |
| 26 | parser = argparse.ArgumentParser() |
| 27 | parser.add_argument("source_folder", |
| 28 | help="directory to be copied from") |
| 29 | parser.add_argument("destination_folder", |
| 30 | help="directory to be copied into") |
| 31 | parser.add_argument("stamp_file", |
| 32 | help="stamp file to be touched") |
| 33 | args = parser.parse_args() |
| 34 | |
| 35 | # Walk the subfolders of the source directory and copy individual files. |
| 36 | # Not using shutil.copytree() because it never overwrites files. |
| 37 | for root, _, files in os.walk(args.source_folder): |
| 38 | for f in files: |
| 39 | abs_src_path = os.path.join(root, f) |
| 40 | rel_path = os.path.relpath(abs_src_path, args.source_folder) |
| 41 | abs_dst_path = os.path.join(args.destination_folder, rel_path) |
| 42 | abs_dst_folder = os.path.dirname(abs_dst_path) |
| 43 | if not os.path.isdir(abs_dst_folder): |
| 44 | os.makedirs(abs_dst_folder) |
| 45 | shutil.copyfile(abs_src_path, abs_dst_path) |
| 46 | |
| 47 | # Touch `stamp_file`. |
| 48 | with open(args.stamp_file, "w"): |
| 49 | pass |
| 50 | |
| 51 | if __name__ == "__main__": |
| 52 | sys.exit(main()) |