1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# Copyright (c) 2025 Huawei Device Co., Ltd. 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# http://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 17import argparse 18import logging 19import os 20import sys 21 22 23def find_files_with_extensions(directory=".", extensions=None): 24 if extensions is None: 25 extensions = [".abc"] 26 files_list = sorted([ 27 os.path.abspath(os.path.join(root, file)) 28 for root, _, files in os.walk(directory) 29 for file in files 30 if any(file.endswith(ext) for ext in extensions) 31 ]) 32 return files_list 33 34 35def write_abc_files_to_filesinfo(abc_files, output_file): 36 try: 37 fd = os.open(output_file, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o644) 38 with os.fdopen(fd, "w", encoding="utf-8") as f: 39 for file_path in abc_files: 40 f.write(file_path + '\n') 41 except Exception as e: 42 raise Exception(f"failed to write to {output_file}") from e 43 44 45def find_abc_files(directory=".", output_file="filesinfo.txt"): 46 try: 47 abc_files = find_files_with_extensions(directory, [".abc"]) 48 write_abc_files_to_filesinfo(abc_files, output_file) 49 except Exception as e: 50 logging.exception("generate filesinfo failed") 51 sys.exit(1) 52 53 54if __name__ == "__main__": 55 logging.basicConfig(level=logging.INFO, format='%(message)s') 56 parser = argparse.ArgumentParser(description='generate filesinfo') 57 parser.add_argument( 58 '--dir', 59 type=str, 60 default=".", 61 help='working directory (default: .)', 62 required=False 63 ) 64 parser.add_argument( 65 '--output', 66 type=str, 67 default="filesinfo.txt", 68 help='output path (default: filesinfo.txt)', 69 required=False 70 ) 71 72 args = parser.parse_args() 73 74 logging.info('working directory: %s', args.dir) 75 logging.info('output path: %s', args.output) 76 77 find_abc_files(args.dir, args.output) 78