1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# Copyright (c) 2024 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 17from __future__ import absolute_import 18 19from io import open 20from glob import iglob 21 22import os 23import datetime 24import fnmatch 25import shutil 26import sys 27import time 28 29# ------------------------------------------------------------------------------------------------------# 30 31def get_year(): 32 return str(datetime.datetime.now().year) 33 34# ------------------------------------------------------------------------------------------------------# 35 36def read_file(name, normalize=True): 37 with open(name, 'r', encoding='utf-8') as f: 38 data = f.read() 39 if normalize: 40 data = data.replace("\r\n", "\n") 41 return data 42 43# ------------------------------------------------------------------------------------------------------# 44 45def write_file(name, data): 46 with open(name, 'w', encoding='utf-8') as f: 47 if sys.version_info.major == 2: 48 f.write(data.decode('utf-8')) 49 else: 50 f.write(data) 51 52# ------------------------------------------------------------------------------------------------------# 53def make_dirs(name, quiet=True): 54 if not path_exists(name): 55 if not quiet: 56 sys.stdout.write('creating ' + name + ' directory.\n') 57 os.makedirs(name) 58 59# ------------------------------------------------------------------------------------------------------# 60def path_exists(name): 61 return os.path.exists(name) 62 63# ------------------------------------------------------------------------------------------------------# 64 65def backup_file(name, quiet=True): 66 bak_name = name + '.' + time.strftime('%Y-%m-%d-%H-%M-%S') 67 shutil.move(name, bak_name) 68 if not quiet: 69 sys.stdout.write('moving ' + name + ' file.\n') 70 71# ------------------------------------------------------------------------------------------------------# 72 73def get_files(search_glob): 74 recursive_glob = '**' + os.path.sep 75 if recursive_glob in search_glob: 76 if sys.version_info >= (3, 5): 77 result = iglob(search_glob, recursive=True) 78 else: 79 result = get_files_recursive(*search_glob.split(recursive_glob)) 80 else: 81 result = iglob(search_glob) 82 83 return sorted(result) 84 85# ------------------------------------------------------------------------------------------------------# 86 87def get_files_recursive(directory, pattern): 88 for root, dirs, files in os.walk(directory): 89 for basename in files: 90 if fnmatch.fnmatch(basename, pattern): 91 filename = os.path.join(root, basename) 92 yield filename 93 94