1 # 2 # Copyright (C) 2016 The Android Open Source Project 3 # 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 # 16 """Utility functions for build rule generator.""" 17 18 import os 19 20 def HalNameDir(hal_name): 21 """Returns directory name corresponding to hal name.""" 22 return hal_name.replace('.', '/') 23 24 25 def HalVerDir(hal_version): 26 """Returns directory name corresponding to hal version.""" 27 return "V" + hal_version.replace('.', '_') 28 29 30 def WriteBuildRule(file_path, build_rule): 31 """Writes the build rule into specified file. 32 33 Opens file_path and writes build_rule into it. Creates intermediate 34 directories if necessary. 35 36 Args: 37 file_path: string, path to file to which to write. 38 build_rule: string, build rule to be written into file. 39 """ 40 print 'Updating %s' % file_path 41 dir_path = os.path.dirname(file_path) 42 43 if not os.path.exists(dir_path): 44 os.makedirs(dir_path) 45 46 with open(file_path, 'w') as bp_file: 47 bp_file.write(build_rule) 48 49 50 def OnlySubdirsBpRule(warning_header, subdirs): 51 """Returns a .bp rule containing only subdirs field. 52 53 For example, 'subdirs = ["*"]' bp rule tells soong to look in all 54 sub-directories for Android.bp files. 55 56 Args: 57 subdirs: list of sub-directories. 58 """ 59 result = warning_header 60 61 result += 'subdirs = [\n' 62 for subdir in subdirs: 63 result += ' "%s",\n' % subdir 64 result += ']\n' 65 return result 66 67 def RemoveFilesInDirIf(dir_path, condition): 68 """Removes all files under directory under given condition. 69 70 Args: 71 dir_path: string, path to directory 72 condition: boolean function takes absolute file path, 73 returns True iff file needs to be removed. 74 """ 75 for base, _, files in os.walk(dir_path): 76 for f in files: 77 abs_path = os.path.join(base, f) 78 if condition(abs_path): 79 os.remove(abs_path) 80