1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright (c) 2025 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import argparse 17import os 18import shutil 19 20 21def copy_tree(src, dst): 22 """Recursive Copy Directory""" 23 copied_files = [] 24 25 for item in os.listdir(src): 26 src_path = os.path.join(src, item) 27 dst_path = os.path.join(dst, item) 28 29 if os.path.isdir(src_path): 30 os.makedirs(dst_path, exist_ok=True) 31 # Recursive call and expand result list 32 copied_files.extend(copy_tree(src_path, dst_path)) 33 else: 34 try: 35 if os.path.exists(dst_path): 36 os.remove(dst_path) 37 shutil.copy2(src_path, dst_path) 38 copied_files.append(dst_path) 39 print(f"Copied: {src_path} -> {dst_path}") 40 except Exception as e: 41 print(f"Error copying {src} to {dst}: {str(e)}") 42 raise 43 44 return copied_files 45 46 47def main(): 48 parser = argparse.ArgumentParser() 49 parser.add_argument("--src-dir", required=True) 50 parser.add_argument("--out-dir", required=True) 51 parser.add_argument("--stamp-file", required=True) 52 args = parser.parse_args() 53 54 # Ensure that the target root directory exists 55 os.makedirs(args.out_dir, exist_ok=True) 56 57 # Recursive copying and recording of results 58 copied_files = [path for path in copy_tree(args.src_dir, args.out_dir) if path] 59 60 # If no files are copied, still create the stamp file (ensure build continues) 61 with os.fdopen(os.open(args.stamp_file, os.O_WRONLY | os.O_CREAT, mode=0o640), 'w') as f: 62 f.write("DONE\n") 63 f.write("\n".join(copied_files)) # Record copied files 64 65 66if __name__ == "__main__": 67 main()