1#!/usr/bin/env python3
2#
3# Copyright 2019 The Hafnium Authors.
4#
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.
8
9"""Copies all files inside one folder to another, preserving subfolders."""
10
11import argparse
12import os
13import shutil
14import sys
15
16def 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
42if __name__ == "__main__":
43    sys.exit(main())
44